forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
1000 lines (856 loc) · 27.4 KB
/
site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2019 Martin Tournoij – This file is part of GoatCounter and
// published under the terms of a slightly modified EUPL v1.2 license, which can
// be found in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"context"
"database/sql"
"fmt"
"path"
"strconv"
"strings"
"time"
"zgo.at/errors"
"zgo.at/guru"
"zgo.at/zdb"
"zgo.at/zstd/zcrypto"
"zgo.at/zstd/zint"
"zgo.at/zstd/znet"
"zgo.at/zstd/zstring"
"zgo.at/zstd/ztime"
)
// Plan column values.
const (
PlanTrial = "trial"
PlanFree = "free"
PlanPersonal = "personal"
PlanStarter = "starter"
PlanBusiness = "business"
PlanBusinessPlus = "businessplus"
PlanChild = "child"
)
var PlanCodes = []string{PlanTrial, PlanFree, PlanPersonal, PlanStarter, PlanBusiness, PlanBusinessPlus}
var reserved = []string{
"www", "mail", "smtp", "imap", "static",
"admin", "ns1", "ns2", "m", "mobile", "api",
"dev", "test", "beta", "new", "staging", "debug", "pprof",
"chat", "example", "yoursite", "test", "sql", "license",
}
var statTables = []string{"hit_stats", "system_stats", "browser_stats",
"location_stats", "language_stats", "size_stats"}
type Site struct {
ID int64 `db:"site_id" json:"id,readonly"`
Parent *int64 `db:"parent" json:"parent,readonly"`
// Custom domain, e.g. "stats.example.com".
//
// When self-hosting this is the domain/vhost your site is accessible at.
Cname *string `db:"cname" json:"cname"`
// When the CNAME was verified.
CnameSetupAt *time.Time `db:"cname_setup_at" json:"cname_setup_at,readonly"`
// Domain code (e.g. "arp242", which makes arp242.goatcounter.com). Only
// used for goatcounter.com and not when self-hosting.
Code string `db:"code" json:"code"`
// Site domain for linking (www.arp242.net).
LinkDomain string `db:"link_domain" json:"link_domain"`
// Plan currently subscribed to.
Plan string `db:"plan" json:"plan,readonly"`
// Plan this site tried to subscribe to, but payment hasn't been verified
// yet.
PlanPending *string `db:"plan_pending" json:"plan_pending,readonly"`
// Stripe customer ID.
Stripe *string `db:"stripe" json:"stripe,readonly"`
// When this plan is scheduled to be cancelled.
PlanCancelAt *time.Time `db:"plan_cancel_at" json:"plan_cancel_at,readonly"`
// Amount is being paid for the plan.
BillingAmount *string `db:"billing_amount" json:"billing_amount,readonly"`
// Maximum number of extra pageviews to charge for.
ExtraPageviews *int `db:"extra_pageviews" json:"extra_pageviews,readonly"`
// subscription_item in Stripe for extra pageviews (si_ ...)
ExtraPageviewsSub *string `db:"extra_pageviews_sub" json:"-"`
// "Anchor" for the billing period.
//
// This is the time someone subscribed to a plan; and their billing period
// will start on this day.
BillingAnchor *time.Time `db:"billing_anchor" json:"billing_anchor,readonly"`
Settings SiteSettings `db:"settings" json:"setttings"`
UserDefaults UserSettings `db:"user_defaults" json:"user_defaults"`
// Whether this site has received any data; will be true after the first
// pageview.
ReceivedData bool `db:"received_data" json:"received_data"`
// {omitdoc}
Notes string `db:"notes" json:"-"`
State string `db:"state" json:"state"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt *time.Time `db:"updated_at" json:"updated_at"`
FirstHitAt time.Time `db:"first_hit_at" json:"first_hit_at"`
}
// ClearCache clears the cache for this site.
func (s Site) ClearCache(ctx context.Context, full bool) {
cacheSites(ctx).Delete(strconv.FormatInt(s.ID, 10))
// TODO: be more selective about this.
if full {
cachePaths(ctx).Flush()
cacheChangedTitles(ctx).Flush()
}
}
// Defaults sets fields to default values, unless they're already set.
func (s *Site) Defaults(ctx context.Context) {
if s.State == "" {
s.State = StateActive
}
n := ztime.Now()
if !Config(ctx).GoatcounterCom {
s.Code = "serve-" + zcrypto.Secret64()
s.CnameSetupAt = &n
}
s.Code = strings.ToLower(s.Code)
if s.CreatedAt.IsZero() {
s.CreatedAt = n
} else {
s.UpdatedAt = &n
}
if s.FirstHitAt.IsZero() {
s.FirstHitAt = n
}
s.Settings.Defaults(ctx)
s.UserDefaults.Defaults(ctx)
}
var noUnderscore = time.Date(2020, 03, 20, 0, 0, 0, 0, time.UTC)
// Validate the object.
func (s *Site) Validate(ctx context.Context) error {
v := NewValidate(ctx)
if Config(ctx).GoatcounterCom {
v.Required("plan", s.Plan)
v.Required("code", s.Code)
v.Len("code", s.Code, 2, 50)
v.Exclude("code", s.Code, reserved)
} else {
v.Required("cname", s.Cname)
}
v.Required("state", s.State)
v.Include("state", s.State, States)
if s.Parent == nil {
v.Include("plan", s.Plan, PlanCodes)
} else {
v.Include("plan", s.Plan, []string{PlanChild})
}
if s.PlanPending != nil {
v.Include("plan_pending", *s.PlanPending, PlanCodes)
if s.Parent != nil {
v.Append("plan_pending", "can't be set if there's a parent")
}
}
v.URL("link_domain", s.LinkDomain)
v.Sub("settings", "", s.Settings.Validate(ctx))
v.Sub("user_defaults", "", s.UserDefaults.Validate(ctx))
// TODO: compat with older requirements, otherwise various update functions
// will error out.
if !s.CreatedAt.IsZero() && s.CreatedAt.Before(noUnderscore) {
for _, c := range s.Code {
if !(c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
v.Append("code", fmt.Sprintf("%q not allowed; characters are limited to '_', '-', a to z, and numbers", c))
break
}
}
if len(s.Code) > 0 && (s.Code[0] == '_' || s.Code[0] == '-') { // Special domains, like _acme-challenge.
v.Append("code", "cannot start with underscore or dash (_, -)")
}
} else {
labels := v.Hostname("code", s.Code)
if len(labels) > 1 {
v.Append("code", "cannot contain '.'")
}
}
if s.Stripe != nil && !strings.HasPrefix(*s.Stripe, "cus_") {
v.Append("stripe", "not a valid Stripe customer ID")
}
if s.Cname != nil {
v.Len("cname", *s.Cname, 4, 255)
v.Domain("cname", *s.Cname)
if Config(ctx).GoatcounterCom && strings.HasSuffix(*s.Cname, Config(ctx).Domain) {
v.Append("cname", "cannot end with %q", Config(ctx).Domain)
}
}
if !v.HasErrors() {
exists, err := s.Exists(ctx)
if err != nil {
return err
}
if exists > 0 {
field := "code"
if s.Cname != nil {
field = "cname"
}
v.Append(field, "already exists")
}
}
return v.ErrorOrNil()
}
// Insert a new row.
func (s *Site) Insert(ctx context.Context) error {
if s.ID > 0 {
return errors.New("ID > 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
s.ID, err = zdb.InsertID(ctx, "site_id", `insert into sites (
parent, code, cname, link_domain, settings, user_defaults, plan, created_at, first_hit_at, cname_setup_at) values (?)`,
zdb.L{s.Parent, s.Code, s.Cname, s.LinkDomain, s.Settings, s.UserDefaults, s.Plan, s.CreatedAt, s.CreatedAt, s.CnameSetupAt})
if err != nil && zdb.ErrUnique(err) {
return guru.New(400, "this site already exists: code or domain must be unique")
}
return errors.Wrap(err, "Site.Insert")
}
// Update existing site. Sets settings, cname, link_domain.
func (s *Site) Update(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx,
`update sites set settings=?, user_defaults=?, cname=?, link_domain=?, updated_at=? where site_id=?`,
s.Settings, s.UserDefaults, s.Cname, s.LinkDomain, s.UpdatedAt, s.ID)
if err != nil {
return errors.Wrap(err, "Site.Update")
}
s.ClearCache(ctx, false)
return nil
}
// UpdateStripe sets the billing info.
func (s *Site) UpdateStripe(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx, `update sites
set stripe=?, plan=?, plan_pending=?, billing_amount=?, extra_pageviews=?, extra_pageviews_sub=?, billing_anchor=?, plan_cancel_at=?, updated_at=?
where site_id=?`,
s.Stripe, s.Plan, s.PlanPending, s.BillingAmount, s.ExtraPageviews, s.ExtraPageviewsSub, s.BillingAnchor, s.PlanCancelAt, s.UpdatedAt, s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateStripe")
}
s.ClearCache(ctx, false)
return nil
}
func (s *Site) UpdateParent(ctx context.Context, newParent *int64) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Parent = newParent
if newParent != nil {
s.Plan = PlanChild
}
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.TX(ctx, func(ctx context.Context) error {
var users Users
err := users.BySite(ctx, s.ID)
if err != nil {
return err
}
for _, u := range users {
u.Site = *newParent
err := u.UpdateSite(ctx)
if err != nil {
return err
}
}
return zdb.Exec(ctx,
`update sites set parent=?, plan=?, updated_at=? where site_id=?`,
s.Parent, s.Plan, s.UpdatedAt, s.ID)
})
if err != nil {
return errors.Wrap(err, "Site.UpdateParent")
}
s.ClearCache(ctx, false)
return nil
}
// UpdateCode changes the site's domain code (e.g. "test" in
// "test.goatcounter.com").
func (s *Site) UpdateCode(ctx context.Context, code string) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
s.Code = code
s.Defaults(ctx)
err := s.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx,
`update sites set code=$1, updated_at=$2 where site_id=$3`,
s.Code, s.UpdatedAt, s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateCode")
}
cacheSites(ctx).Delete(strconv.FormatInt(s.ID, 10))
cacheSitesHost(ctx).Flush()
return nil
}
func (s *Site) UpdateReceivedData(ctx context.Context) error {
err := zdb.Exec(ctx, `update sites set received_data=1 where site_id=$1`, s.ID)
s.ClearCache(ctx, false)
return errors.Wrap(err, "Site.UpdateReceivedData")
}
func (s *Site) UpdateFirstHitAt(ctx context.Context, f time.Time) error {
f = f.UTC().Add(-12 * time.Hour)
s.FirstHitAt = f
err := zdb.Exec(ctx,
`update sites set first_hit_at=$1 where site_id=$2`,
s.FirstHitAt, s.ID)
s.ClearCache(ctx, false)
return errors.Wrap(err, "Site.UpdateFirstHitAt")
}
// UpdateCnameSetupAt confirms the custom domain was setup correct.
func (s *Site) UpdateCnameSetupAt(ctx context.Context) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
n := ztime.Now()
s.CnameSetupAt = &n
err := zdb.Exec(ctx,
`update sites set cname_setup_at=$1 where site_id=$2`,
s.CnameSetupAt, s.ID)
if err != nil {
return errors.Wrap(err, "Site.UpdateCnameSetupAt")
}
s.ClearCache(ctx, false)
return nil
}
// Delete a site and all child sites.
func (s *Site) Delete(ctx context.Context, deleteChildren bool) error {
if s.ID == 0 {
return errors.New("ID == 0")
}
if !deleteChildren {
var n int
err := zdb.Get(ctx, &n, `select count(*) from sites where parent = ?`, s.ID)
if err != nil {
return errors.Wrap(err, "Site.Delete")
}
if n > 0 {
return fmt.Errorf("Site.Delete: site %d has %d linked sites", s.ID, n)
}
}
t := ztime.Now()
err := zdb.Exec(ctx,
`update sites set state=$1, updated_at=$2 where site_id=$3 or parent=$3`,
StateDeleted, t, s.ID)
if err != nil {
return errors.Wrap(err, "Site.Delete")
}
s.ClearCache(ctx, true)
s.ID = 0
s.UpdatedAt = &t
s.State = StateDeleted
return nil
}
func (s Site) Undelete(ctx context.Context, id int64) error {
s.State = StateActive
s.ID = id
err := zdb.Exec(ctx, `update sites set state = ? where site_id = ?`, StateActive, id)
if err != nil {
return fmt.Errorf("Site.Undelete %d: %w", id, err)
}
s.ClearCache(ctx, false)
return errors.Wrap(s.ByID(ctx, id), "Site.Undelete")
}
// Exists checks if this site already exists, based on either the Cname or Code
// field.
func (s Site) Exists(ctx context.Context) (int64, error) {
var (
id int64
query = `select site_id from sites where lower(code) = lower($1) and site_id != $2 limit 1`
params = zdb.L{s.Code, s.ID}
)
if s.Cname != nil {
query = `select site_id from sites where lower(cname) = lower($1) and site_id != $2 limit 1`
params = zdb.L{s.Cname, s.ID}
}
err := zdb.Get(ctx, &id, query, params...)
if err != nil && err != sql.ErrNoRows {
return 0, fmt.Errorf("Site.Exists: %w", err)
}
return id, nil
}
// ByID gets a site by ID.
func (s *Site) ByID(ctx context.Context, id int64) error {
err := s.ByIDState(ctx, id, StateActive)
if err != nil {
return fmt.Errorf("Site.ByID: %w", errors.Unwrap(err))
}
return nil
}
// ByIDState gets a site by ID and state. This may return deleted sites.
func (s *Site) ByIDState(ctx context.Context, id int64, state string) error {
k := strconv.FormatInt(id, 10)
ss, ok := cacheSites(ctx).Get(k)
if ok {
*s = *ss.(*Site)
return nil
}
err := zdb.Get(ctx, s,
`/* Site.ByID */ select * from sites where site_id=$1 and state=$2`,
id, state)
if err != nil {
return errors.Wrapf(err, "Site.ByIDState %d", id)
}
cacheSites(ctx).SetDefault(k, s)
return nil
}
// ByStripe gets a site by the Stripe customer ID.
func (s *Site) ByStripe(ctx context.Context, stripe string) error {
err := zdb.Get(ctx, s, `select * from sites where stripe=$1`, stripe)
return errors.Wrapf(err, "Site.ByStripe %s", stripe)
}
// ByCode gets a site by code.
func (s *Site) ByCode(ctx context.Context, code string) error {
return errors.Wrapf(zdb.Get(ctx, s,
`/* Site.ByCode */ select * from sites where code=$1 and state=$2`,
code, StateActive), "Site.ByCode %s", code)
}
// ByHost gets a site by host name.
func (s *Site) ByHost(ctx context.Context, host string) error {
ss, ok := cacheSitesHost(ctx).Get(host)
if ok {
*s = *ss.(*Site)
return nil
}
// Custom domain or serve.
if !Config(ctx).GoatcounterCom || !strings.HasSuffix(host, Config(ctx).Domain) {
err := zdb.Get(ctx, s,
`/* Site.ByHost */ select * from sites where lower(cname)=lower($1) and state=$2`,
znet.RemovePort(host), StateActive)
if err != nil {
return errors.Wrap(err, "site.ByHost: from custom domain")
}
cacheSitesHost(ctx).Set(strconv.FormatInt(s.ID, 10), host, s)
return nil
}
// Get from code (e.g. "arp242" in "arp242.goatcounter.com").
p := strings.Index(host, ".")
if p == -1 {
return errors.Errorf("Site.ByHost: no subdomain in host %q", host)
}
err := zdb.Get(ctx, s,
`/* Site.ByHost */ select * from sites where lower(code)=lower($1) and state=$2`,
host[:p], StateActive)
if err != nil {
return errors.Wrap(err, "site.ByHost: from code")
}
cacheSitesHost(ctx).Set(strconv.FormatInt(s.ID, 10), host, s)
return nil
}
// Find a site: by ID if ident is a number, or by host if it's not.
func (s *Site) Find(ctx context.Context, ident string) error {
id, err := strconv.ParseInt(ident, 10, 64)
if err == nil {
return errors.Wrap(s.ByID(ctx, id), "Site.Find")
}
return errors.Wrap(s.ByHost(ctx, ident), "Site.Find")
}
// ListSubs lists all subsites, including the current site and parent.
func (s *Site) ListSubs(ctx context.Context) ([]string, error) {
col := "cname"
if Config(ctx).GoatcounterCom {
col = "code"
}
var codes []string
err := zdb.Select(ctx, &codes, `/* Site.ListSubs */
select `+col+` from sites
where state=$1 and (parent=$2 or site_id=$2) or (
parent = (select parent from sites where site_id=$2) or
site_id = (select parent from sites where site_id=$2)
) and state=$1
order by code
`, StateActive, s.ID)
return codes, errors.Wrap(err, "Site.ListSubs")
}
// Domain gets the global default domain, or this site's configured custom
// domain.
func (s Site) Domain(ctx context.Context) string {
if s.Cname != nil && s.CnameSetupAt != nil {
return *s.Cname
}
return Config(ctx).Domain
}
// Display format: just the domain (cname or code+domain).
func (s Site) Display(ctx context.Context) string {
if s.Cname != nil && s.CnameSetupAt != nil {
return *s.Cname
}
return fmt.Sprintf("%s.%s", s.Code, znet.RemovePort(Config(ctx).Domain))
}
// URL to this site.
func (s Site) URL(ctx context.Context) string {
if s.Cname != nil && s.CnameSetupAt != nil {
return fmt.Sprintf("http%s://%s%s",
map[bool]string{true: "", false: "s"}[Config(ctx).Dev],
*s.Cname, Config(ctx).Port)
}
return fmt.Sprintf("http%s://%s.%s%s",
map[bool]string{true: "", false: "s"}[Config(ctx).Dev],
s.Code, Config(ctx).Domain, Config(ctx).Port)
}
// LinkDomainURL creates a valid url to the configured LinkDomain.
func (s Site) LinkDomainURL(withProto bool, paths ...string) string {
if s.LinkDomain == "" {
return ""
}
if withProto && !zstring.HasPrefixes(s.LinkDomain, "http://", "https://") {
s.LinkDomain = "http://" + s.LinkDomain
} else if !withProto {
s.LinkDomain = zstring.TrimPrefixes(s.LinkDomain, "http://", "https://")
}
return strings.TrimRight(s.LinkDomain, "/") + path.Join(paths...)
}
// PlanCustomDomain reports if this site's plan allows custom domains.
func (s Site) PlanCustomDomain(ctx context.Context) bool {
account := MustGetAccount(ctx)
return account.Plan == PlanStarter || account.Plan == PlanBusiness || account.Plan == PlanBusinessPlus
}
// IDOrParent gets this site's ID or the parent ID if that's set.
func (s Site) IDOrParent() int64 {
if s.Parent != nil {
return *s.Parent
}
return s.ID
}
//lint:ignore U1001 used in template (via ShowPayBanner)
var trialPeriod = time.Hour * 24 * 14
// ShowPayBanner determines if we should show a "please pay" banner for the
// customer.
//
//lint:ignore U1001 used in template.
func (s Site) ShowPayBanner(ctx context.Context) bool {
account := MustGetAccount(ctx)
return account.Plan == PlanTrial && -ztime.Now().Sub(account.CreatedAt.Add(trialPeriod)) < 0
}
// StripeCustomer reports if this Stripe column refers to a Stripe customer.
func (s Site) StripeCustomer() bool {
return s.Stripe != nil && !zstring.HasPrefixes(*s.Stripe, "cus_github", "cus_patreon_")
}
// Subscribed reports if this customer is currently a paying customer subscribed
// to a plan. This may be payed outside of GoatCounter (e.g. GitHub).
func (s Site) Subscribed() bool {
return s.BillingAmount != nil
}
// PayExternal gets the external payment name, or an empty string if there is
// none.
func (s Site) PayExternal() string {
if s.Stripe == nil {
return ""
}
if strings.HasPrefix(*s.Stripe, "cus_github_") {
return "GitHub Sponsors"
}
if strings.HasPrefix(*s.Stripe, "cus_patreon_") {
return "Patreon"
}
return ""
}
// DeleteAll deletes all pageviews for this site, keeping the site itself and
// user intact.
func (s Site) DeleteAll(ctx context.Context) error {
return zdb.TX(ctx, func(ctx context.Context) error {
for _, t := range append(statTables, "hit_counts", "ref_counts", "hits", "paths") {
err := zdb.Exec(ctx, `delete from `+t+` where site_id=:id`, zdb.P{"id": s.ID})
if err != nil {
return errors.Wrap(err, "Site.DeleteAll: delete "+t)
}
}
s.ClearCache(ctx, true)
return nil
})
}
func (s Site) DeleteOlderThan(ctx context.Context, days int) error {
if days < 14 {
return errors.Errorf("days must be at least 14: %d", days)
}
return zdb.TX(ctx, func(ctx context.Context) error {
ival := interval(ctx, days)
var pathIDs []int64
err := zdb.Select(ctx, &pathIDs, `/* Site.DeleteOlderThan */
select path_id from hit_counts where site_id=$1 and hour < `+ival+` group by path_id`, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: get paths")
}
for _, t := range statTables {
err := zdb.Exec(ctx, `delete from `+t+` where site_id=$1 and day < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete "+t)
}
}
err = zdb.Exec(ctx, `delete from hit_counts where site_id=$1 and hour < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete hit_counts")
}
err = zdb.Exec(ctx, `delete from ref_counts where site_id=$1 and hour < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete ref_counts")
}
err = zdb.Exec(ctx, `delete from hits where site_id=$1 and created_at < `+ival, s.ID)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan: delete hits")
}
if len(pathIDs) > 0 {
var remainPath []int64
err := zdb.Select(ctx, &remainPath, `/* Site.DeleteOlderThan */
select path_id from hit_counts where site_id=? and path_id in (?)`,
s.ID, pathIDs)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
diff := zint.Difference(pathIDs, remainPath)
if len(diff) > 0 {
err = zdb.Exec(ctx, `delete from paths where site_id=? and path_id in (?)`, s.ID, diff)
if err != nil {
return errors.Wrap(err, "Site.DeleteOlderThan")
}
}
}
return nil
})
}
func (s Site) BillingAnchorDay() int {
if s.BillingAnchor == nil {
return 1
}
return s.BillingAnchor.Day()
}
func (s Site) NextInvoice() time.Time {
if s.BillingAnchor == nil {
return ztime.Time{ztime.Now()}.Add(1, ztime.Month).StartOf(ztime.Month).Time
}
diff := ztime.NewRange(*s.BillingAnchor).To(ztime.Now()).Diff(ztime.Month)
if ztime.Now().Day() > s.BillingAnchor.Day() {
diff.Months++
}
n := ztime.Add(*s.BillingAnchor, diff.Months, ztime.Month)
return ztime.StartOf(n, ztime.Day)
}
func (s Site) ThisBillingPeriod() ztime.Range {
return ztime.NewRange(ztime.Add(s.NextInvoice(), -1, ztime.Month)).To(ztime.Now())
}
func (s Site) PreviousBillingPeriod() ztime.Range {
c := s.ThisBillingPeriod()
return ztime.NewRange(ztime.Add(c.Start, -1, ztime.Month)).To(ztime.EndOf(c.Start.AddDate(0, 0, -1), ztime.Day))
}
// Sites is a list of sites.
type Sites []Site
// UnscopedList lists all sites, not scoped to the current user.
func (s *Sites) UnscopedList(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s,
`/* Sites.List */ select * from sites where state=$1`,
StateActive), "Sites.List")
}
// UnscopedListCnames all sites that have CNAME set, not scoped to the current
// user.
func (s *Sites) UnscopedListCnames(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, `/* Sites.ListCnames */
select * from sites where state=$1 and cname is not null`,
StateActive), "Sites.List")
}
// ListSubs lists all subsites for the current site.
func (s *Sites) ListSubs(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, `/* Sites.ListSubs */
select * from sites where parent=$1 and state=$2 order by code`,
MustGetSite(ctx).ID, StateActive), "Sites.ListSubs")
}
// ForThisAccount gets all sites associated with this account.
func (s *Sites) ForThisAccount(ctx context.Context, excludeCurrent bool) error {
site := MustGetSite(ctx)
err := zdb.Select(ctx, s, `/* Sites.ForThisAccount */
select * from sites
where state=$1 and (parent=$2 or site_id=$2) or (
parent = (select parent from sites where site_id=$2) or
site_id = (select parent from sites where site_id=$2)
) and state=$1
order by code
`, StateActive, site.ID)
if err != nil {
return errors.Wrap(err, "Sites.ForThisAccount")
}
if excludeCurrent {
ss := *s
for i := range ss {
if ss[i].ID == site.ID {
*s = append(ss[:i], ss[i+1:]...)
break
}
}
}
return nil
}
// ContainsCNAME reports if there is a site with this CNAME set.
func (s *Sites) ContainsCNAME(ctx context.Context, cname string) (bool, error) {
var ok bool
err := zdb.Get(ctx, &ok, `/* Sites.ContainsCNAME */
select 1 from sites where lower(cname)=lower($1) limit 1`, cname)
return ok, errors.Wrapf(err, "Sites.ContainsCNAME for %q", cname)
}
// OldSoftDeleted finds all sites which have been soft-deleted more than a week
// ago.
func (s *Sites) OldSoftDeleted(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, s, fmt.Sprintf(`/* Sites.OldSoftDeleted */
select * from sites where state=$1 and updated_at < %s`, interval(ctx, 7)),
StateDeleted), "Sites.OldSoftDeleted")
}
// ExpiredPlans finds all sites which have a plan that's expired.
func (s *Sites) ExpiredPlans(ctx context.Context) error {
err := zdb.Select(ctx, s, `/* Sites.ExpiredPlans */
select * from sites where ? > plan_cancel_at`, ztime.Now())
return errors.Wrap(err, "Sites.ExpiredPlans")
}
// Find sites: by ID if ident is a number, or by host if it's not.
func (s *Sites) Find(ctx context.Context, ident []string) error {
ids, strs := splitIntStr(ident)
err := zdb.Select(ctx, s, `select * from sites where
{{:ids site_id in (:ids) or}}
{{:strs! 0=1}}
{{:strs cname in (:strs)}}`,
zdb.P{"ids": ids, "strs": strs})
return errors.Wrap(err, "Sites.Find")
}
// IDs gets a list of all IDs for these sites.
func (s *Sites) IDs() []int64 {
ids := make([]int64, 0, len(*s))
for _, ss := range *s {
ids = append(ids, ss.ID)
}
return ids
}
// Delete all sites in this selection.
func (s *Sites) Delete(ctx context.Context, deleteChildren bool) error {
err := zdb.TX(ctx, func(ctx context.Context) error {
for _, ss := range *s {
err := ss.Delete(ctx, deleteChildren)
if err != nil {
return err
}
}
return nil
})
return errors.Wrap(err, "Sites.Delete")
}
// Plan represents a plan people can subscribe to.
type Plan struct {
Code string
Name string
Price int
MaxHits int
MonthlyHits int
}
type Plans []Plan
func (p Plans) Find(s Site) Plan {
for _, pp := range p {
if pp.Code == s.Plan {
return pp
}
}
return Plan{}
}
var planDetails = Plans{
{
Code: PlanTrial,
Name: "Free",
MaxHits: 2_400_000,
MonthlyHits: 100_000,
}, {
Code: PlanFree,
Name: "Free",
MaxHits: 2_400_000,
MonthlyHits: 100_000,
}, {
Code: PlanPersonal,
Name: "Personal",
MaxHits: 2_400_000,
MonthlyHits: 100_000,
}, {
Code: PlanStarter,
Name: "Starter",
MaxHits: 4_800_000,
MonthlyHits: 100_000,
Price: 500,
}, {
Code: PlanBusiness,
Name: "Business",
MaxHits: 24_000_000,
MonthlyHits: 500_000,
Price: 1500,
}, {
Code: PlanBusinessPlus,
Name: "Business plus",
MaxHits: 0,
MonthlyHits: 1_000_000,
Price: 3000,
}, {
Code: PlanChild,
Name: "Child",
},
}
type AccountUsage struct {
Plan Plan
ThisStart, PrevStart, PrevEnd time.Time
Stats []AccountUsageStats
Total AccountUsageStats
}
type AccountUsageStats struct {
Code string `db:"code"`
Total int `db:"total"`
ThisPeriod int `db:"this_period"`
PrevPeriod int `db:"prev_period"`
}
func (a *AccountUsage) Get(ctx context.Context) error {
account, err := GetAccount(ctx)
if err != nil {
return fmt.Errorf("AccountUsage: %w", err)
}
a.Plan = planDetails.Find(*account)
var sites Sites
err = sites.ForThisAccount(WithSite(ctx, account), false)
if err != nil {
return errors.Wrap(err, "AccountUsage.Get")
}
a.ThisStart = ztime.Add(account.NextInvoice(), -1, ztime.Month)
a.PrevStart = ztime.Add(a.ThisStart, -1, ztime.Month)
a.PrevEnd = ztime.EndOf(a.ThisStart.AddDate(0, 0, -1), ztime.Day)
var query []string
for _, s := range sites {
query = append(query, fmt.Sprintf(`select
(select code from sites where site_id=%[1]d) as code,
(select coalesce(sum(total), 0) from hit_counts where site_id=%[1]d) as total,
(select coalesce(sum(total), 0) from hit_counts where site_id=%[1]d and hour>='%[2]s') as this_period,
(select coalesce(sum(total), 0) from hit_counts where site_id=%[1]d and hour>='%[3]s' and hour<'%[4]s') as prev_period`,
s.ID,
a.ThisStart.Format("2006-01-02 15:04:05"),
a.PrevStart.Format("2006-01-02 15:04:05"),
a.PrevEnd.Format("2006-01-02 15:04:05"),
))
}
err = zdb.Select(ctx, &a.Stats,
"/* AccountUsage.Get */\n"+strings.Join(query, "\nunion ")+"\norder by code asc")
if err != nil {
return errors.Wrap(err, "AccountUsage.Get")
}
for _, s := range a.Stats {
a.Total.PrevPeriod += s.PrevPeriod
a.Total.ThisPeriod += s.ThisPeriod
a.Total.Total += s.Total
}
return nil
}