Skip to content

Commit 6bc0162

Browse files
committed
Don't store stats if count=0
Now that only visitors are stored, we don't need to store rows in the stats tables if there are only pageviews but no visits.
1 parent d556673 commit 6bc0162

10 files changed

+74
-14
lines changed

cron/browser_stat.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ func updateBrowserStats(ctx context.Context, hits []goatcounter.Hit) error {
6363
}
6464

6565
for _, v := range grouped {
66-
ins.Values(siteID, v.day, v.pathID, v.browserID, v.count)
66+
if v.count > 0 {
67+
ins.Values(siteID, v.day, v.pathID, v.browserID, v.count)
68+
}
6769
}
6870
return ins.Finish()
6971
}), "cron.updateBrowserStats")

cron/browser_stat_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestBrowserStats(t *testing.T) {
3333
t.Fatal(err)
3434
}
3535

36-
want := `{false [{ Firefox 1 <nil>} { Chrome 0 <nil>}]}`
36+
want := `{false [{ Firefox 1 <nil>}]}`
3737
out := fmt.Sprintf("%v", stats)
3838
if want != out {
3939
t.Errorf("\nwant: %s\nout: %s", want, out)
@@ -45,6 +45,7 @@ func TestBrowserStats(t *testing.T) {
4545
{Site: site.ID, CreatedAt: now, UserAgentHeader: "Firefox/69.0"},
4646
{Site: site.ID, CreatedAt: now, UserAgentHeader: "Firefox/70.0"},
4747
{Site: site.ID, CreatedAt: now, UserAgentHeader: "Firefox/70.0"},
48+
{Site: site.ID, CreatedAt: now, UserAgentHeader: "Chrome/77.0.123.666", FirstVisit: true},
4849
}...)
4950

5051
stats = goatcounter.HitStats{}
@@ -53,7 +54,7 @@ func TestBrowserStats(t *testing.T) {
5354
t.Fatal(err)
5455
}
5556

56-
want = `{false [{ Firefox 2 <nil>} { Chrome 0 <nil>}]}`
57+
want = `{false [{ Firefox 2 <nil>} { Chrome 1 <nil>}]}`
5758
out = fmt.Sprintf("%v", stats)
5859
if want != out {
5960
t.Errorf("\nwant: %s\nout: %s", want, out)
@@ -66,7 +67,7 @@ func TestBrowserStats(t *testing.T) {
6667
t.Fatal(err)
6768
}
6869

69-
want = `{false [{ Firefox 68 1 <nil>} { Firefox 69 1 <nil>} { Firefox 70 0 <nil>}]}`
70+
want = `{false [{ Firefox 68 1 <nil>} { Firefox 69 1 <nil>}]}`
7071
out = fmt.Sprintf("%v", stats)
7172
if want != out {
7273
t.Errorf("\nwant: %s\nout: %s", want, out)

cron/campaign_stat.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func updateCampaignStats(ctx context.Context, hits []goatcounter.Hit) error {
5656
}
5757

5858
for _, v := range grouped {
59-
ins.Values(siteID, v.day, v.pathID, v.campaignID, v.ref, v.count)
59+
if v.count > 0 {
60+
ins.Values(siteID, v.day, v.pathID, v.campaignID, v.ref, v.count)
61+
}
6062
}
6163
return ins.Finish()
6264
}), "cron.updateCampaignStats")

cron/campaign_stat_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cron_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"zgo.at/goatcounter/v2"
8+
"zgo.at/goatcounter/v2/gctest"
9+
"zgo.at/zstd/zjson"
10+
"zgo.at/zstd/ztest"
11+
"zgo.at/zstd/ztime"
12+
)
13+
14+
func TestCampaignStats(t *testing.T) {
15+
ctx := gctest.DB(t)
16+
17+
site := goatcounter.MustGetSite(ctx)
18+
now := time.Date(2019, 8, 31, 14, 42, 0, 0, time.UTC)
19+
20+
gctest.StoreHits(ctx, t, false, []goatcounter.Hit{
21+
{Site: site.ID, CreatedAt: now, Query: "utm_campaign=one", FirstVisit: true},
22+
{Site: site.ID, CreatedAt: now, Query: "utm_campaign=one"},
23+
{Site: site.ID, CreatedAt: now, Query: "utm_campaign=two"},
24+
{Site: site.ID, CreatedAt: now, Query: "utm_campaign=three", FirstVisit: true},
25+
}...)
26+
27+
var have goatcounter.HitStats
28+
err := have.ListCampaigns(ctx, ztime.NewRange(now).To(now), nil, 10, 0)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
33+
want := `{
34+
"more": false,
35+
"stats": [
36+
{"count": 1, "id": "1", "name": "one"},
37+
{"count": 1, "id": "3", "name": "three"}
38+
]
39+
}`
40+
if d := ztest.Diff(zjson.MustMarshalString(have), want, ztest.DiffJSON); d != "" {
41+
t.Error(d)
42+
}
43+
}

cron/hit_stat.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
1818
return errors.Wrap(zdb.TX(ctx, func(ctx context.Context) error {
1919
type gt struct {
2020
count []int
21+
total int
2122
day string
2223
hour string
2324
pathID int64
@@ -50,6 +51,7 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
5051
hour, _ := strconv.ParseInt(h.CreatedAt.Format("15"), 10, 8)
5152
if h.FirstVisit {
5253
v.count[hour] += 1
54+
v.total += 1
5355
}
5456
grouped[k] = v
5557
}
@@ -77,7 +79,9 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
7779
// }
7880

7981
for _, v := range grouped {
80-
ins.Values(siteID, v.day, v.pathID, zjson.MustMarshal(v.count))
82+
if v.total > 0 {
83+
ins.Values(siteID, v.day, v.pathID, zjson.MustMarshal(v.count))
84+
}
8185
}
8286
return errors.Wrap(ins.Finish(), "updateHitStats hit_stats")
8387
}), "cron.updateHitStats")

cron/language_stat.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ func updateLanguageStats(ctx context.Context, hits []goatcounter.Hit) error {
5454
}
5555

5656
for _, v := range grouped {
57-
ins.Values(siteID, v.day, v.pathID, v.language, v.count)
57+
if v.count > 0 {
58+
ins.Values(siteID, v.day, v.pathID, v.language, v.count)
59+
}
5860
}
5961
return ins.Finish()
6062
}), "cron.updateLanguageStats")

cron/location_stat.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func updateLocationStats(ctx context.Context, hits []goatcounter.Hit) error {
6060
}
6161

6262
for _, v := range grouped {
63-
ins.Values(siteID, v.day, v.pathID, v.location, v.count)
63+
if v.count > 0 {
64+
ins.Values(siteID, v.day, v.pathID, v.location, v.count)
65+
}
6466
}
6567
return ins.Finish()
6668
}), "cron.updateLocationStats")

cron/location_stat_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestLocationStats(t *testing.T) {
3232
t.Fatal(err)
3333
}
3434

35-
want := `{false [{ET Ethiopia 1 <nil>} {ID Indonesia 0 <nil>}]}`
35+
want := `{false [{ET Ethiopia 1 <nil>}]}`
3636
out := fmt.Sprintf("%v", stats)
3737
if want != out {
3838
t.Errorf("\nwant: %s\nout: %s", want, out)
@@ -41,12 +41,12 @@ func TestLocationStats(t *testing.T) {
4141
// Update existing.
4242
gctest.StoreHits(ctx, t, false, []goatcounter.Hit{
4343
{Site: site.ID, CreatedAt: now, Location: "ID"},
44-
{Site: site.ID, CreatedAt: now, Location: "ID"},
44+
{Site: site.ID, CreatedAt: now, Location: "ID", FirstVisit: true},
4545
{Site: site.ID, CreatedAt: now, Location: "ET"},
4646
{Site: site.ID, CreatedAt: now, Location: "ET", FirstVisit: true},
4747
{Site: site.ID, CreatedAt: now, Location: "ET", FirstVisit: true},
4848
{Site: site.ID, CreatedAt: now, Location: "ET"},
49-
{Site: site.ID, CreatedAt: now, Location: "NZ"},
49+
{Site: site.ID, CreatedAt: now, Location: "NZ", FirstVisit: true},
5050
}...)
5151

5252
stats = goatcounter.HitStats{}
@@ -55,7 +55,7 @@ func TestLocationStats(t *testing.T) {
5555
t.Fatal(err)
5656
}
5757

58-
want = `{false [{ET Ethiopia 3 <nil>} {ID Indonesia 0 <nil>} {NZ New Zealand 0 <nil>}]}`
58+
want = `{false [{ET Ethiopia 3 <nil>} {ID Indonesia 1 <nil>} {NZ New Zealand 1 <nil>}]}`
5959
out = fmt.Sprintf("%v", stats)
6060
if want != out {
6161
t.Errorf("\nwant: %s\nout: %s", want, out)

cron/size_stat.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ func updateSizeStats(ctx context.Context, hits []goatcounter.Hit) error {
5959
}
6060

6161
for _, v := range grouped {
62-
ins.Values(siteID, v.day, v.pathID, v.width, v.count)
62+
if v.count > 0 {
63+
ins.Values(siteID, v.day, v.pathID, v.width, v.count)
64+
}
6365
}
6466
return ins.Finish()
6567
}), "cron.updateSizeStats")

cron/system_stat.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func updateSystemStats(ctx context.Context, hits []goatcounter.Hit) error {
6161
}
6262

6363
for _, v := range grouped {
64-
ins.Values(siteID, v.day, v.pathID, v.systemID, v.count)
64+
if v.count > 0 {
65+
ins.Values(siteID, v.day, v.pathID, v.systemID, v.count)
66+
}
6567
}
6668
return ins.Finish()
6769
}), "cron.updateSystemStats")

0 commit comments

Comments
 (0)