Skip to content

Commit

Permalink
Store page title in hit_stats (#102)
Browse files Browse the repository at this point in the history
So it can be easily selected for the UI.

Needs reindex.

Fixes #90
  • Loading branch information
arp242 committed Jan 14, 2020
1 parent a8c9f7b commit 957a8f5
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 32 deletions.
7 changes: 5 additions & 2 deletions cron/hit_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// site | 1
// day | 2019-12-05
// path | /jquery.html
// title | Why I'm still using jQuery in 2019
// stats | [0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0]
func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
txctx, tx, err := zdb.Begin(ctx)
Expand All @@ -37,6 +38,7 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
total int
day string
path string
title string
}
grouped := map[string]gt{}
for _, h := range hits {
Expand All @@ -50,6 +52,7 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {
if len(v.count) == 0 {
v.day = day
v.path = h.Path
v.title = h.Title
v.count, err = existingHitStats(ctx, tx, h.Site, day, v.path)
if err != nil {
return err
Expand All @@ -67,9 +70,9 @@ func updateHitStats(ctx context.Context, hits []goatcounter.Hit) error {

siteID := goatcounter.MustGetSite(ctx).ID
ins := bulk.NewInsert(txctx, tx,
"hit_stats", []string{"site", "day", "path", "stats", "total"})
"hit_stats", []string{"site", "day", "path", "title", "stats", "total"})
for _, v := range grouped {
ins.Values(siteID, v.day, v.path, jsonutil.MustMarshal(v.count), v.total)
ins.Values(siteID, v.day, v.path, v.title, jsonutil.MustMarshal(v.count), v.total)
}
err = ins.Finish()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cron/hit_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestHitStats(t *testing.T) {
now := time.Date(2019, 8, 31, 14, 42, 0, 0, time.UTC)

goatcounter.Memstore.Append([]goatcounter.Hit{
{Site: site.ID, CreatedAt: now, Path: "/asd"},
{Site: site.ID, CreatedAt: now, Path: "/asd", Title: "aSd"},
{Site: site.ID, CreatedAt: now, Path: "/asd/"}, // Trailing / should be sanitized and treated identical as /asd
{Site: site.ID, CreatedAt: now, Path: "/zxc"},
}...)
Expand All @@ -47,13 +47,13 @@ func TestHitStats(t *testing.T) {
t.Fatalf("len(stats) is not 2: %d", len(stats))
}

want0 := `{"Count":2,"Max":10,"Path":"/asd","RefScheme":null,"Stats":[{"Day":"2019-08-31","Days":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0]}]}`
want0 := `{"Count":2,"Max":10,"Path":"/asd","Title":"aSd","RefScheme":null,"Stats":[{"Day":"2019-08-31","Days":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0]}]}`
got0 := string(jsonutil.MustMarshal(stats[0]))
if got0 != want0 {
t.Errorf("first wrong\ngot: %s\nwant: %s", got0, want0)
}

want1 := `{"Count":1,"Max":10,"Path":"/zxc","RefScheme":null,"Stats":[{"Day":"2019-08-31","Days":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]}]}`
want1 := `{"Count":1,"Max":10,"Path":"/zxc","Title":"","RefScheme":null,"Stats":[{"Day":"2019-08-31","Days":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]}]}`
got1 := string(jsonutil.MustMarshal(stats[1]))
if got1 != want1 {
t.Errorf("second wrong\ngot: %s\nwant: %s", got1, want1)
Expand Down
4 changes: 4 additions & 0 deletions db/migrate/pgsql/2020-01-13-2-hit_stats_title.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
begin;
alter table hit_stats add column title varchar not null default '';
insert into version values ('2020-01-13-2-hit_stats_title');
commit;
4 changes: 4 additions & 0 deletions db/migrate/sqlite/2020-01-13-2-hit_stats_title.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
begin;
alter table hit_stats add column title varchar not null default '';
insert into version values ('2020-01-13-2-hit_stats_title');
commit;
4 changes: 3 additions & 1 deletion db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ insert into version values
('2019-12-20-1-dailystat'),
('2019-12-31-1-blank-days'),
('2020-01-02-1-bot'),
('2020-01-07-1-title-domain');
('2020-01-07-1-title-domain'),
('2020-01-13-2-hit_stats_title');

drop table if exists sites;
create table sites (
Expand Down Expand Up @@ -88,6 +89,7 @@ create table hit_stats (

day date not null check(day = strftime('%Y-%m-%d', day)),
path varchar not null,
title varchar not null default '',
stats varchar not null,
total integer not null default 0,

Expand Down
16 changes: 9 additions & 7 deletions hit.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ type HitStat struct {
Count int `db:"count"`
Max int `db:"-"`
Path string `db:"path"`
Title string `db:"title"`
RefScheme *string `db:"ref_scheme"`
Stats []Stat
}
Expand All @@ -301,6 +302,7 @@ var allDays = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
func (h *HitStats) List(ctx context.Context, start, end time.Time, exclude []string) (int, int, bool, error) {
db := zdb.MustGet(ctx)
site := MustGetSite(ctx)
l := zlog.Module("HitStats.List")

limit := site.Settings.Limits.Page
if limit == 0 {
Expand Down Expand Up @@ -339,8 +341,6 @@ func (h *HitStats) List(ctx context.Context, start, end time.Time, exclude []str
return 0, 0, false, errors.Wrap(err, "HitStats.List")
}

l := zlog.Module("HitStats.List")

err = db.SelectContext(ctx, h, db.Rebind(query), args...)
if err != nil {
return 0, 0, false, errors.Wrap(err, "HitStats.List")
Expand All @@ -357,15 +357,16 @@ func (h *HitStats) List(ctx context.Context, start, end time.Time, exclude []str
}
}

// Add stats
// Add stats and title
type stats struct {
Path string `json:"path"`
Day time.Time `json:"day"`
Stats []byte `json:"stats"`
Path string `db:"path"`
Title string `db:"title"`
Day time.Time `db:"day"`
Stats []byte `db:"stats"`
}
var st []stats
err = db.SelectContext(ctx, &st, `
select path, day, stats
select path, title, day, stats
from hit_stats
where
site=$1 and
Expand All @@ -386,6 +387,7 @@ func (h *HitStats) List(ctx context.Context, start, end time.Time, exclude []str
if s.Path == hh[i].Path {
var x []int
jsonutil.MustUnmarshal(s.Stats, &x)
hh[i].Title = s.Title
hh[i].Stats = append(hh[i].Stats, Stat{Day: s.Day.Format("2006-01-02"), Days: x})

// Get max.
Expand Down
31 changes: 21 additions & 10 deletions pack/pack.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions public/style_backend.css
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ form .err { color: red; display: block; }
word-break: break-all; /* don't make it wider for very long urls */
}

/* Ideally I'd like the … to be in the centre, rather than at the end. Need JS
* solution for that though :-/ */
.page-title, .rlink {
display: inline-block; max-width: 17.5rem;
text-overflow: ellipsis; white-space: nowrap; overflow: hidden;
}
.rlink {
min-width: 3em; /* Make very short paths (like just /) easier to click/touch. */
max-width: 17.5em;
display: inline-block;

/* Ideally I'd like the … to be in the centre, rather than at the end. Need
* JS solution for that though :-/ */
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}

.count-list tr {
Expand Down
3 changes: 2 additions & 1 deletion tpl/_backend_pages.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<tr id="{{$h.Path}}"{{if eq $h.Path $.ShowRefs}}class="target"{{end}}>
<td>{{$h.Count | nformat}}</td>
<td class="hide-mobile">
<a class="rlink" title="{{$h.Path}}" href="?showrefs={{$h.Path}}&period-start={{tformat $.PeriodStart ""}}&period-end={{tformat $.PeriodEnd ""}}#{{$h.Path}}">{{$h.Path}}</a>
<a class="rlink" title="{{$h.Path}}" href="?showrefs={{$h.Path}}&period-start={{tformat $.PeriodStart ""}}&period-end={{tformat $.PeriodEnd ""}}#{{$h.Path}}">{{$h.Path}}</a><br>
<small class="page-title" title="{{$h.Title}}">{{if $h.Title}}{{$h.Title}}{{else}}<em>(no title)</em>{{end}}</small>
</td>
<td>
<div class="show-mobile">
Expand Down

0 comments on commit 957a8f5

Please sign in to comment.