forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtplfunc.go
178 lines (157 loc) · 5.14 KB
/
tplfunc.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
// Copyright © 2019 Martin Tournoij <[email protected]>
// This file is part of GoatCounter and published under the terms of the EUPL
// v1.2, which can be found in the LICENSE file or at http://eupl12.zgo.at
package goatcounter
import (
"context"
"fmt"
"html/template"
"math"
"strings"
"time"
"zgo.at/zhttp"
"zgo.at/zlog"
"zgo.at/zvalidate"
)
func init() {
zhttp.FuncMap["validate"] = zvalidate.TemplateError
zhttp.FuncMap["has_errors"] = zvalidate.TemplateHasErrors
zhttp.FuncMap["error_code"] = func(err error) string { return zhttp.ErrorCode(err) }
zhttp.FuncMap["nformat2"] = func(n int, s Site) string {
return zhttp.FuncMap["nformat"].(func(int, rune) string)(n, s.Settings.NumberFormat)
}
zhttp.FuncMap["parent_site"] = func(ctx context.Context, id *int64) string {
var s Site
err := s.ByID(ctx, *id)
if err != nil {
zlog.Error(err)
return ""
}
return s.URL()
}
var (
ss = time.Date(2019, 9, 16, 0, 0, 0, 0, time.UTC)
sl = time.Date(2019, 11, 7, 0, 0, 0, 0, time.UTC)
)
zhttp.FuncMap["beforeSize"] = func(createdAt time.Time) bool { return createdAt.Before(ss) }
zhttp.FuncMap["beforeLoc"] = func(createdAt time.Time) bool { return createdAt.Before(sl) }
// Implemented as function for performance.
zhttp.FuncMap["bar_chart"] = BarChart
zhttp.FuncMap["horizontal_chart"] = HorizontalChart
}
func BarChart(ctx context.Context, stats []Stat, max int) template.HTML {
site := MustGetSite(ctx)
now := time.Now().In(site.Settings.Timezone.Loc())
_, offset := now.Zone()
// Round to next hour for TZ offset of 9.5 hours, instead of down.
if offset%3600 != 0 {
offset += 1900
}
offset /= 3600
applyOffset(offset, stats)
var b strings.Builder
today := now.Format("2006-01-02")
hour := now.Hour()
for _, stat := range stats {
for shour, s := range stat.Days {
// Don't show stuff in the future.
if stat.Day == today && shour > hour {
break
}
// Double div so that the title is on the entire column, instead of
// just the coloured area. No need to add the inner one if there's
// no data – saves quite a bit in the total filesize.
inner := ""
h := math.Round(float64(s) / float64(max) / 0.01)
if h > 0 {
inner = fmt.Sprintf(`<div style="height: %.0f%%;"></div>`, h)
}
b.WriteString(fmt.Sprintf(`<div title="%s %[2]d:00 – %[2]d:59, %s views">%s</div>`,
stat.Day, shour, zhttp.Tnformat(s, site.Settings.NumberFormat), inner))
}
}
return template.HTML(b.String())
}
// The database stores everything in UTC, so we need to apply
// the offset.
//
// Let's say we have two days with an offset of UTC+2, this means we
// need to transform this:
//
// 2019-12-05 → [0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0]
// 2019-12-06 → [0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0]
// 2019-12-07 → [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
//
// To:
//
// 2019-12-05 → [0,0,0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0]
// 2019-12-06 → [1,0,0,0,0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0]
// 2019-12-07 → [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
//
// And skip the first 2 hours of the first day.
//
// Or, for UTC-2:
//
// 2019-12-04 → [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
// 2019-12-05 → [0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0,0,0]
// 2019-12-06 → [0,0,0,0,0,0,0,0,0,4,7,0,0,0,0,0,0,0,0,0,1,0,0,0]
//
// And skip the last 2 hours of the last day.
//
// Offsets that are not whole hours (e.g. 6:30) are treated like 7:00. I don't
// know how to do that otherwise.
func applyOffset(offset int, stats []Stat) {
switch {
case offset > 0:
popped := make([]int, offset)
for i := range stats {
stats[i].Days = append(popped, stats[i].Days...)
o := len(stats[i].Days) - offset
popped = stats[i].Days[o:]
stats[i].Days = stats[i].Days[:o]
}
case offset < 0:
offset = -offset
popped := make([]int, offset)
for i := range stats {
stats[i].Days = append(stats[i].Days, popped...)
popped = stats[i].Days[:offset]
stats[i].Days = stats[i].Days[offset:]
}
}
}
func HorizontalChart(ctx context.Context, stats Stats, total, parentTotal int, cutoff float32, link bool) template.HTML {
tag := "p"
if link {
tag = "a"
}
totalPerc := float32(0.0)
var b strings.Builder
for _, s := range stats {
perc := float32(s.Count) / float32(total) * 100
totalPerc += perc
if parentTotal > 0 {
perc = float32(s.Count) / float32(parentTotal) * 100
}
if perc < cutoff { // Group as "Other" later.
break
}
browser := s.Name
if browser == "" {
browser = "(unknown)"
}
title := fmt.Sprintf("%s: %.1f%% – %s hits in total",
template.HTMLEscapeString(browser), perc,
zhttp.Tnformat(s.Count, MustGetSite(ctx).Settings.NumberFormat))
b.WriteString(fmt.Sprintf(
`<%[4]s href="#_" title="%[1]s"><small>%[2]s</small> <span style="width: %[3]f%%">%.1[3]f%%</span></%[4]s>`,
title, template.HTMLEscapeString(browser), perc, tag))
}
// Add "(other)" part.
if totalPerc < 100 {
b.WriteString(fmt.Sprintf(
`<%[2]s href="#_" title="(other): %.1[1]f%%" class="other"><small>(other)</small> <span style="width: %[1]f%%">%.1[1]f%%</span></%[2]s>`,
100-totalPerc, tag))
}
return template.HTML(b.String())
}