Skip to content

Commit

Permalink
Drop Unique/_unique suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Nov 3, 2022
1 parent 26392d1 commit e098f03
Show file tree
Hide file tree
Showing 71 changed files with 633 additions and 626 deletions.
13 changes: 6 additions & 7 deletions cmd/goatcounter/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func dash(url, key string, rng ztime.Range) error {
})
for _, path := range data.hits.Hits {
left = append(left,
nr(fmt.Sprintf("%-6d %-38s", path.CountUnique, zli.Colorize(zstring.ElideCenter(path.Path, 37), zli.Bold))),
nr(fmt.Sprintf("%-6d %-38s", path.Count, zli.Colorize(zstring.ElideCenter(path.Path, 37), zli.Bold))),
nr(fmt.Sprintf(" %-38s", zstring.ElideLeft(path.Title, 37))),
nr(""),
)
Expand All @@ -259,7 +259,7 @@ func dash(url, key string, rng ztime.Range) error {
stat.Name = "(unknown)"
}
var (
p = float64(stat.CountUnique) / float64(data.total.TotalUniqueUTC) * 100
p = float64(stat.Count) / float64(data.total.TotalUTC) * 100
perc string
)
switch {
Expand Down Expand Up @@ -289,7 +289,7 @@ func dash(url, key string, rng ztime.Range) error {

// Combine the two columns
fmt.Printf("%s\n", zli.Colorize(zstring.AlignCenter(fmt.Sprintf(
"%s – %d of %d visits shown", rng.String(), data.hits.TotalUnique, data.total.TotalUnique),
"%s – %d of %d visits shown", rng.String(), data.hits.Total, data.total.Total),
78), zli.Bold))
fmt.Printf("┌%s┬%s┐\n", strings.Repeat("─", 48), strings.Repeat("─", 30))
m := zint.Max(len(left), len(right))
Expand Down Expand Up @@ -322,10 +322,9 @@ type (
dashboardData struct {
total goatcounter.TotalCount
hits struct {
Hits goatcounter.HitLists `json:"hits"`
Total int `json:"total"`
TotalUnique int `json:"total_unique"`
More bool `json:"more"`
Hits goatcounter.HitLists `json:"hits"`
Total int `json:"total"`
More bool `json:"more"`
}
stats map[string]stats
}
Expand Down
20 changes: 10 additions & 10 deletions cron/browser_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
func updateBrowserStats(ctx context.Context, hits []goatcounter.Hit) error {
return errors.Wrap(zdb.TX(ctx, func(ctx context.Context) error {
type gt struct {
countUnique int
day string
browserID int64
pathID int64
count int
day string
browserID int64
pathID int64
}
grouped := map[string]gt{}
for _, h := range hits {
Expand All @@ -39,31 +39,31 @@ func updateBrowserStats(ctx context.Context, hits []goatcounter.Hit) error {
day := h.CreatedAt.Format("2006-01-02")
k := day + strconv.FormatInt(h.BrowserID, 10) + strconv.FormatInt(h.PathID, 10)
v := grouped[k]
if v.countUnique == 0 {
if v.count == 0 {
v.day = day
v.browserID = h.BrowserID
v.pathID = h.PathID
}

if h.FirstVisit {
v.countUnique += 1
v.count += 1
}
grouped[k] = v
}

siteID := goatcounter.MustGetSite(ctx).ID
ins := zdb.NewBulkInsert(ctx, "browser_stats", []string{"site_id", "day",
"path_id", "browser_id", "count_unique"})
"path_id", "browser_id", "count"})
if zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL {
ins.OnConflict(`on conflict on constraint "browser_stats#site_id#path_id#day#browser_id" do update set
count_unique = browser_stats.count_unique + excluded.count_unique`)
count = browser_stats.count + excluded.count`)
} else {
ins.OnConflict(`on conflict(site_id, path_id, day, browser_id) do update set
count_unique = browser_stats.count_unique + excluded.count_unique`)
count = browser_stats.count + excluded.count`)
}

for _, v := range grouped {
ins.Values(siteID, v.day, v.pathID, v.browserID, v.countUnique)
ins.Values(siteID, v.day, v.pathID, v.browserID, v.count)
}
return ins.Finish()
}), "cron.updateBrowserStats")
Expand Down
22 changes: 11 additions & 11 deletions cron/campaign_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
func updateCampaignStats(ctx context.Context, hits []goatcounter.Hit) error {
return errors.Wrap(zdb.TX(ctx, func(ctx context.Context) error {
type gt struct {
countUnique int
day string
campaignID int64
ref string
pathID int64
count int
day string
campaignID int64
ref string
pathID int64
}
grouped := map[string]gt{}
for _, h := range hits {
Expand All @@ -31,32 +31,32 @@ func updateCampaignStats(ctx context.Context, hits []goatcounter.Hit) error {
day := h.CreatedAt.Format("2006-01-02")
k := day + strconv.FormatInt(*h.CampaignID, 10) + h.Ref + strconv.FormatInt(h.PathID, 10)
v := grouped[k]
if v.countUnique == 0 {
if v.count == 0 {
v.day = day
v.campaignID = *h.CampaignID
v.ref = h.Ref
v.pathID = h.PathID
}

if h.FirstVisit {
v.countUnique += 1
v.count += 1
}
grouped[k] = v
}

siteID := goatcounter.MustGetSite(ctx).ID
ins := zdb.NewBulkInsert(ctx, "campaign_stats", []string{"site_id", "day",
"path_id", "campaign_id", "ref", "count_unique"})
"path_id", "campaign_id", "ref", "count"})
if zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL {
ins.OnConflict(`on conflict on constraint "campaign_stats#site_id#path_id#campaign_id#ref#day" do update set
count_unique = campaign_stats.count_unique + excluded.count_unique`)
count = campaign_stats.count + excluded.count`)
} else {
ins.OnConflict(`on conflict(site_id, path_id, campaign_id, ref, day) do update set
count_unique = campaign_stats.count_unique + excluded.count_unique`)
count = campaign_stats.count + excluded.count`)
}

for _, v := range grouped {
ins.Values(siteID, v.day, v.pathID, v.campaignID, v.ref, v.countUnique)
ins.Values(siteID, v.day, v.pathID, v.campaignID, v.ref, v.count)
}
return ins.Finish()
}), "cron.updateCampaignStats")
Expand Down
4 changes: 2 additions & 2 deletions cron/email_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func reportText(ctx context.Context, site goatcounter.Site, user goatcounter.Use

fmt.Fprintf(b, " %-36s %9s %7s\n",
template.HTMLEscapeString(zstring.ElideLeft(path, 35)),
tplfunc.Number(p.CountUnique, user.Settings.NumberFormat),
tplfunc.Number(p.Count, user.Settings.NumberFormat),
diffStr[i])
}
args.TextPagesTable = template.HTML(b.String())
Expand All @@ -199,7 +199,7 @@ func reportText(ctx context.Context, site goatcounter.Site, user goatcounter.Use
}
fmt.Fprintf(b, " %-45s %9s\n",
template.HTMLEscapeString(zstring.ElideLeft(path, 44)),
tplfunc.Number(r.CountUnique, user.Settings.NumberFormat))
tplfunc.Number(r.Count, user.Settings.NumberFormat))
//refs[i].Path = zstring.ElideLeft(refs[i].Path, 44)
}
args.TextRefTable = template.HTML(b.String())
Expand Down
18 changes: 9 additions & 9 deletions cron/hit_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func updateHitCounts(ctx context.Context, hits []goatcounter.Hit) error {
return errors.Wrap(zdb.TX(ctx, func(ctx context.Context) error {
// Group by day + pathID
type gt struct {
totalUnique int
hour string
pathID int64
total int
hour string
pathID int64
}
grouped := map[string]gt{}
for _, h := range hits {
Expand All @@ -30,30 +30,30 @@ func updateHitCounts(ctx context.Context, hits []goatcounter.Hit) error {
hour := h.CreatedAt.Format("2006-01-02 15:00:00")
k := hour + strconv.FormatInt(h.PathID, 10)
v := grouped[k]
if v.totalUnique == 0 {
if v.total == 0 {
v.hour = hour
v.pathID = h.PathID
}

if h.FirstVisit {
v.totalUnique += 1
v.total += 1
}
grouped[k] = v
}

siteID := goatcounter.MustGetSite(ctx).ID
ins := zdb.NewBulkInsert(ctx, "hit_counts", []string{"site_id", "path_id",
"hour", "total_unique"})
"hour", "total"})
if zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL {
ins.OnConflict(`on conflict on constraint "hit_counts#site_id#path_id#hour" do update set
total_unique = hit_counts.total_unique + excluded.total_unique`)
total = hit_counts.total + excluded.total`)
} else {
ins.OnConflict(`on conflict(site_id, path_id, hour) do update set
total_unique = hit_counts.total_unique + excluded.total_unique`)
total = hit_counts.total + excluded.total`)
}

for _, v := range grouped {
ins.Values(siteID, v.pathID, v.hour, v.totalUnique)
ins.Values(siteID, v.pathID, v.hour, v.total)
}
return ins.Finish()
}), "cron.updateHitCounts")
Expand Down
37 changes: 18 additions & 19 deletions cron/hit_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
return errors.Wrap(zdb.TX(ctx, func(ctx context.Context) error {
type gt struct {
countUnique []int
day string
hour string
pathID int64
count []int
day string
hour string
pathID int64
}
grouped := map[string]gt{}
for _, h := range hits {
Expand All @@ -32,15 +32,15 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
dayHour := h.CreatedAt.Format("2006-01-02 15:00:00")
k := day + strconv.FormatInt(h.PathID, 10)
v := grouped[k]
if len(v.countUnique) == 0 {
if len(v.count) == 0 {
v.day = day
v.hour = dayHour
v.pathID = h.PathID
v.countUnique = make([]int, 24)
v.count = make([]int, 24)

if zdb.SQLDialect(ctx) == zdb.DialectSQLite {
var err error
v.countUnique, err = existingHitStats(ctx, h.Site, day, v.pathID)
v.count, err = existingHitStats(ctx, h.Site, day, v.pathID)
if err != nil {
return err
}
Expand All @@ -49,20 +49,20 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {

hour, _ := strconv.ParseInt(h.CreatedAt.Format("15"), 10, 8)
if h.FirstVisit {
v.countUnique[hour] += 1
v.count[hour] += 1
}
grouped[k] = v
}

siteID := goatcounter.MustGetSite(ctx).ID
ins := zdb.NewBulkInsert(ctx, "hit_stats", []string{"site_id", "day", "path_id", "stats_unique"})
ins := zdb.NewBulkInsert(ctx, "hit_stats", []string{"site_id", "day", "path_id", "stats"})
if zdb.SQLDialect(ctx) == zdb.DialectPostgreSQL {
ins.OnConflict(`on conflict on constraint "hit_stats#site_id#path_id#day" do update set
stats_unique = (
stats = (
with x as (
select
unnest(string_to_array(trim(hit_stats.stats_unique, '[]'), ',')::int[]) as orig,
unnest(string_to_array(trim(excluded.stats_unique, '[]'), ',')::int[]) as new
unnest(string_to_array(trim(hit_stats.stats, '[]'), ',')::int[]) as orig,
unnest(string_to_array(trim(excluded.stats, '[]'), ',')::int[]) as new
)
select '[' || array_to_string(array_agg(orig + new), ',') || ']' from x
) `)
Expand All @@ -72,24 +72,23 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
// it's kinda tricky with SQLite :-/
//
// ins.OnConflict(`on conflict(site_id, path_id, day) do update set
// stats_unique = excluded.stats_unique
// stats = excluded.stats
// `)
// }

for _, v := range grouped {
ins.Values(siteID, v.day, v.pathID, zjson.MustMarshal(v.countUnique))
ins.Values(siteID, v.day, v.pathID, zjson.MustMarshal(v.count))
}
return errors.Wrap(ins.Finish(), "updateHitStats hit_stats")
}), "cron.updateHitStats")
}

func existingHitStats(ctx context.Context, siteID int64, day string, pathID int64) ([]int, error) {

var ex []struct {
StatsUnique []byte `db:"stats_unique"`
Stats []byte `db:"stats"`
}
err := zdb.Select(ctx, &ex, `/* existingHitStats */
select stats_unique from hit_stats
select stats from hit_stats
where site_id=$1 and day=$2 and path_id=$3 limit 1`,
siteID, day, pathID)
if err != nil {
Expand All @@ -107,8 +106,8 @@ func existingHitStats(ctx context.Context, siteID int64, day string, pathID int6
}

var ru []int
if ex[0].StatsUnique != nil {
zjson.MustUnmarshal(ex[0].StatsUnique, &ru)
if ex[0].Stats != nil {
zjson.MustUnmarshal(ex[0].Stats, &ru)
}

return ru, nil
Expand Down
Loading

0 comments on commit e098f03

Please sign in to comment.