Added time of day to tracking page.

This commit is contained in:
Marvin Blum
2020-09-24 19:55:22 +02:00
committed by Marvin Blum
parent 991ff57254
commit b6eb05e90c
4 changed files with 93 additions and 41 deletions

View File

@@ -142,6 +142,7 @@ func serveTracking() http.HandlerFunc {
OS []pirsch.OSStats OS []pirsch.OSStats
Countries []pirsch.CountryStats Countries []pirsch.CountryStats
Platform *pirsch.VisitorStats Platform *pirsch.VisitorStats
TimeOfDay []pirsch.TimeOfDayVisitors
HourlyVisitorsTodayLabels template.JS HourlyVisitorsTodayLabels template.JS
HourlyVisitorsTodayDps template.JS HourlyVisitorsTodayDps template.JS
ActiveVisitors int ActiveVisitors int
@@ -162,6 +163,7 @@ func serveTracking() http.HandlerFunc {
tracking.GetOS(startDate, endDate), tracking.GetOS(startDate, endDate),
tracking.GetCountry(startDate, endDate), tracking.GetCountry(startDate, endDate),
tracking.GetPlatform(startDate, endDate), tracking.GetPlatform(startDate, endDate),
tracking.GetVisitorTimeOfDay(startDate, endDate),
hourlyVisitorsTodayLabels, hourlyVisitorsTodayLabels,
hourlyVisitorsTodayDps, hourlyVisitorsTodayDps,
activeVisitors, activeVisitors,

View File

@@ -225,6 +225,30 @@
</tbody> </tbody>
</table> </table>
</section> </section>
<section>
<h2>Time of Day</h2>
<table>
<thead>
<tr>
<th>Time</th>
{{range $day := .TimeOfDay}}
<th>{{format $day.Day "Mon 01/02"}}</th>
{{end}}
</tr>
</thead>
<tbody>
{{$global := .}}
{{range $i := (intRange 0 24)}}
<tr>
<td>{{$i}}</td>
{{range $j, $day := $global.TimeOfDay}}
<td>{{(index (index $global.TimeOfDay $j).Stats $i).Visitors}}</td>
{{end}}
</tr>
{{end}}
</tbody>
</table>
</section>
<section> <section>
<h2>Page Visits</h2> <h2>Page Visits</h2>
</section> </section>

View File

@@ -40,6 +40,7 @@ func (cache *Cache) load() {
"format": func(t time.Time, layout string) string { return t.Format(layout) }, "format": func(t time.Time, layout string) string { return t.Format(layout) },
"multiply": func(f, x float64) float64 { return f * x }, "multiply": func(f, x float64) float64 { return f * x },
"round": func(f float64) string { return fmt.Sprintf("%.2f", f) }, "round": func(f float64) string { return fmt.Sprintf("%.2f", f) },
"intRange": intRange,
} }
var err error var err error
cache.tpl, err = template.New("").Funcs(funcMap).ParseGlob(templateDir) cache.tpl, err = template.New("").Funcs(funcMap).ParseGlob(templateDir)
@@ -100,3 +101,17 @@ func (cache *Cache) Clear() {
defer cache.m.Unlock() defer cache.m.Unlock()
cache.cache = make(map[string][]byte) cache.cache = make(map[string][]byte)
} }
func intRange(start, end int) []int {
if end-start < 0 {
return []int{}
}
r := make([]int, end-start)
for i := 0; i < end-start; i++ {
r[i] = start + i
}
return r
}

View File

@@ -157,6 +157,17 @@ func GetPlatform(startDate, endDate time.Time) *pirsch.VisitorStats {
return analyzer.Platform(&pirsch.Filter{From: startDate, To: endDate}) return analyzer.Platform(&pirsch.Filter{From: startDate, To: endDate})
} }
func GetVisitorTimeOfDay(startDate, endDate time.Time) []pirsch.TimeOfDayVisitors {
visitors, err := analyzer.TimeOfDay(&pirsch.Filter{From: startDate, To: endDate})
if err != nil {
logbuch.Error("Error reading visitor time of day statistics", logbuch.Fields{"err": err})
return nil
}
return visitors
}
func sumVisitors(stats []pirsch.Stats) int { func sumVisitors(stats []pirsch.Stats) int {
sum := 0 sum := 0