Added hourly visitors and active visitors to tracking page.

This commit is contained in:
Marvin Blum
2020-06-26 02:02:00 +02:00
committed by Marvin Blum
parent 9bd2ff362e
commit 1276ed013b
5 changed files with 105 additions and 14 deletions

View File

@@ -63,6 +63,39 @@ func GetLanguages(start int) []pirsch.VisitorLanguage {
return languages
}
func GetHourlyVisitors(start int) (template.JS, template.JS) {
visitors, err := analyzer.HourlyVisitors(&pirsch.Filter{From: getStartTime(start), To: today()})
if err != nil {
logbuch.Error("Error reading hourly visitors", logbuch.Fields{"err": err})
return "", ""
}
return getLabelsAndDataHourly(visitors)
}
func GetHourlyVisitorsToday() (template.JS, template.JS) {
visitors, err := analyzer.HourlyVisitors(&pirsch.Filter{From: today(), To: today()})
if err != nil {
logbuch.Error("Error reading hourly visitors for today", logbuch.Fields{"err": err})
return "", ""
}
return getLabelsAndDataHourly(visitors)
}
func GetActiveVisitors() int {
visitors, err := analyzer.ActiveVisitors(time.Minute * 5)
if err != nil {
logbuch.Error("Error reading active visitors", logbuch.Fields{"err": err})
return 0
}
return visitors
}
func getStartTime(start int) time.Time {
startTime := today()
return startTime.Add(-time.Hour * 24 * time.Duration(start-1))
@@ -82,6 +115,20 @@ func getLabelsAndData(visitors []pirsch.VisitorsPerDay) (template.JS, template.J
return template.JS(labelsStr[:len(labelsStr)-1]), template.JS(dataStr[:len(dataStr)-1])
}
func getLabelsAndDataHourly(visitors []pirsch.HourlyVisitors) (template.JS, template.JS) {
var labels strings.Builder
var dp strings.Builder
for _, point := range visitors {
labels.WriteString(fmt.Sprintf("'%d',", point.Hour))
dp.WriteString(fmt.Sprintf("%d,", point.Visitors))
}
labelsStr := labels.String()
dataStr := dp.String()
return template.JS(labelsStr[:len(labelsStr)-1]), template.JS(dataStr[:len(dataStr)-1])
}
func today() time.Time {
now := time.Now()
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)