Skip to content

Commit

Permalink
Show difference of pageviews compared to previous period
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed May 18, 2023
1 parent 0230ac7 commit 6240cf1
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ require (
zgo.at/zlog v0.0.0-20211017235224-dd4772ddf860
zgo.at/zprof v0.0.0-20211217104121-c3c12596d8f0
zgo.at/zstd v0.0.0-20230518015225-0920e0f78cb1
zgo.at/ztpl v0.0.0-20230518031629-dd2227d96ecd
zgo.at/ztpl v0.0.0-20230518214526-3f09e105c03c
zgo.at/zvalidate v0.0.0-20221021025449-cb54fa8efade
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum

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

4 changes: 4 additions & 0 deletions hit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ func GetTotalCount(ctx context.Context, rng ztime.Range, pathFilter []int64, noE
//
// The return value is in the same order as paths.
func (h HitLists) Diff(ctx context.Context, rng, prev ztime.Range) ([]float64, error) {
if len(h) == 0 {
return nil, nil
}

d := -rng.End.Sub(rng.Start)
prev = ztime.NewRange(rng.Start.Add(d)).To(rng.End.Add(d))

Expand Down
4 changes: 4 additions & 0 deletions public/backend.css
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ small.go { display: none; }
.count-list td { vertical-align: top; }
.count-list th { text-align: left; }
.count-list .col-count { width: 5rem; text-align: right; }
.count-list .col-count-diff { font-size:.9rem; }
.count-list .col-path { width: 20rem; }
.label-event { background-color: var(--event-bg); border-radius: 1em; padding: .1em .3em; }
.count-list td[colspan="3"] { /* "nothing to display" */
text-align: left;
width: auto;
}
.pages-list:not(.pages-list-text) >.load-more { display: block; margin-top: -.7em; width: max-content; }
.plus { color: var(--plus); }
.minus { color: var(--minus); }

.count-list tr:target,
.count-list tr.target { background-color: inherit; }
Expand Down Expand Up @@ -178,6 +181,7 @@ tr.target .chart-left { display: block; }

.count-list-text .col-idx { width: 1em; color: var(--text-table-rank-text); }
.count-list-text .col-n { text-align: right; width: 1em; } /* Hint to make the column as small as possible */
.count-list-text .col-diff { text-align: right; width: 1em; } /* Hint to make the column as small as possible */
.count-list-text .col-d { width: 12em; font-size: 1.2em; padding: 0; text-align: center; }
.count-list-text .col-d { vertical-align: top; }
.count-list-text .col-d span { display: inline-block; margin-top: .8em; padding-top: 2px; line-height: 1em;
Expand Down
3 changes: 3 additions & 0 deletions public/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@

--box-bg: #666; /* Signup box on the homepage, footer */
--box-shadow: #cdc8a4; /* box-shadow on most boxes on the homepage, /help page navigation, etc. */

--plus: #008837; /* Positive numbers (+5%) */
--minus: #ac3ad3; /* Negative numbers (-5%) */
}
3 changes: 3 additions & 0 deletions public/vars.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@

--box-bg: #f6f3da; /* Signup box on the homepage, footer */
--box-shadow: #cdc8a4; /* box-shadow on most boxes on the homepage, /help page navigation, etc. */

--plus: #008837; /* Positive numbers (+5%) */
--minus: #ac3ad3; /* Negative numbers (-5%) */
}
14 changes: 13 additions & 1 deletion tpl/_dashboard_pages_rows.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
>
{{if not $.User.Settings.FewerNumbers}}
<td class="col-count">
<span>{{nformat $h.Count $.User}}</span>
<span>{{nformat $h.Count $.User}}</span><br>

{{$d := index $.Diff $i}}
<span
class="col-count-diff {{if is_inf $d}}{{else if gt $d 0.0}}plus{{else if lt $d 0.0}}minus{{end}}"
title="{{t $.Context "tooltip/change-period|Change compared to previous period"}}"
>
{{if is_inf $d}}
<i>{{t $.Context "new-paren|(new)"}}</i>
{{else}}
{{if gt $d 0.0}}+{{else if lt $d 0.0}}–{{end}}{{printf "%.0f" (max (round (abs $d) 0) 1)}}%
{{end}}
</span>
</td>
{{end}}
<td class="col-path hide-mobile">
Expand Down
1 change: 1 addition & 0 deletions tpl/_dashboard_pages_text.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{{if not $.User.Settings.FewerNumbers}}
<th class="col-n">{{t .Context "dashboard/pages/visits|Visits"}}</th>
{{end}}
<th class="col-diff">{{t .Context "dashboard/pages/change|Change"}}</th>
<th class="col-p">{{t .Context "dashboard/pages/path|Path"}}</th>
<th class="col-t">{{t .Context "dashboard/pages/title|Title"}}</th>
<th class="col-d" title="{{t .Context "dashboard/pages/stats-tooltip|Every bar represents 1/12th of the selected time range"}}">{{t .Context "dashboard/pages/stats|Stats"}}</th>
Expand Down
8 changes: 8 additions & 0 deletions tpl/_dashboard_pages_text_rows.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<td class="col-idx">{{sum $.Offset $i}}</td>
{{if not $.User.Settings.FewerNumbers}}
<td class="col-n col-count">{{nformat $h.Count $.User}}</td>
{{$d := index $.Diff $i}}
<td class="col-diff {{if is_inf $d}}{{else if gt $d 0.0}}plus{{else if lt $d 0.0}}minus{{end}}">
{{if is_inf $d}}
<i>{{t $.Context "new-paren|(new)"}}</i>
{{else}}
{{if gt $d 0.0}}+{{else if lt $d 0.0}}–{{end}}{{printf "%.0f" (max (round (abs $d) 0) 1)}}%
{{end}}
</td>
{{end}}
<td class="col-p">
<a class="load-refs rlink" href="#">{{$h.Path}}</a>
Expand Down
8 changes: 8 additions & 0 deletions widgets/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Pages struct {
Refs goatcounter.HitStats
Max int
Exclude []int64
Diff []float64
}

func (w Pages) Name() string { return "pages" }
Expand Down Expand Up @@ -84,6 +85,11 @@ func (w *Pages) GetData(ctx context.Context, a Args) (bool, error) {
w.Display, w.More, err = w.Pages.List(ctx, a.Rng, a.PathFilter, w.Exclude, w.Limit, a.Daily)
errs.Append(err)

if !goatcounter.MustGetUser(ctx).Settings.FewerNumbers {
w.Diff, err = w.Pages.Diff(ctx, a.Rng, a.Rng)
errs.Append(err)
}

wg.Wait()

for _, p := range w.Pages {
Expand Down Expand Up @@ -174,11 +180,13 @@ func (w Pages) RenderHTML(ctx context.Context, shared SharedData) (string, any)
Style string
Refs goatcounter.HitStats
ShowRefs int64
Diff []float64
}{
ctx, shared.Site, shared.User,
w.id, w.loaded, w.err, w.Pages, shared.Args.Rng, shared.Args.Daily,
shared.Args.ForcedDaily, 1, w.Max,
w.Display, shared.Total, shared.TotalEvents, w.More,
w.Style, w.Refs, shared.Args.ShowRefs,
w.Diff,
}
}

0 comments on commit 6240cf1

Please sign in to comment.