mirror of
https://github.com/Kugelschieber/marvinblum.git
synced 2026-01-18 06:40:27 +00:00
Implemented pirsch 1.5.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package tracking
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/emvi/logbuch"
|
||||
"github.com/emvi/pirsch"
|
||||
"html/template"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -9,13 +14,36 @@ const (
|
||||
statisticsDateFormat = "2006-01-02"
|
||||
)
|
||||
|
||||
type PageVisits struct {
|
||||
Path string
|
||||
Labels template.JS
|
||||
Data template.JS
|
||||
type PageVisitors struct {
|
||||
Path string
|
||||
Visitors int
|
||||
Labels template.JS
|
||||
Data template.JS
|
||||
}
|
||||
|
||||
/*func GetTotalVisitors(startDate, endDate time.Time) (template.JS, template.JS) {
|
||||
func GetActiveVisitors() ([]pirsch.Stats, int) {
|
||||
visitors, total, err := analyzer.ActiveVisitors(nil, time.Minute*10)
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading active visitors", logbuch.Fields{"err": err})
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
return visitors, total
|
||||
}
|
||||
|
||||
func GetHourlyVisitorsToday() (template.JS, template.JS) {
|
||||
visitors, err := analyzer.VisitorHours(&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 GetTotalVisitors(startDate, endDate time.Time) (template.JS, template.JS) {
|
||||
visitors, err := analyzer.Visitors(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
@@ -26,41 +54,36 @@ type PageVisits struct {
|
||||
return getLabelsAndData(visitors)
|
||||
}
|
||||
|
||||
func GetPageVisits(startDate, endDate time.Time) []PageVisits {
|
||||
func GetPageVisits(startDate, endDate time.Time) ([]PageVisitors, []PageVisitors) {
|
||||
visits, err := analyzer.PageVisitors(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading page statistics", logbuch.Fields{"err": err})
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
pageVisits := make([]PageVisits, len(visits))
|
||||
pageVisitors := make([]PageVisitors, len(visits))
|
||||
|
||||
for i, visit := range visits {
|
||||
labels, data := getLabelsAndData(visit.VisitorsPerDay)
|
||||
pageVisits[i] = PageVisits{visit.Path.String, labels, data}
|
||||
labels, data := getLabelsAndData(visit.Stats)
|
||||
pageVisitors[i] = PageVisitors{
|
||||
Path: visit.Path,
|
||||
Visitors: sumVisitors(visit.Stats),
|
||||
Labels: labels,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
return pageVisits
|
||||
pageRank := make([]PageVisitors, len(pageVisitors))
|
||||
copy(pageRank, pageVisitors)
|
||||
sort.Slice(pageRank, func(i, j int) bool {
|
||||
return pageRank[i].Visitors > pageRank[j].Visitors
|
||||
})
|
||||
return pageVisitors, pageRank
|
||||
}
|
||||
|
||||
func GetPages(startDate, endDate time.Time) []pirsch.Stats {
|
||||
pages, err := analyzer.Pages(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading page statistics", logbuch.Fields{"err": err})
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(pages) > 10 {
|
||||
return pages[:10]
|
||||
}
|
||||
|
||||
return pages
|
||||
}
|
||||
|
||||
func GetLanguages(startDate, endDate time.Time) []pirsch.Stats {
|
||||
languages, _, err := analyzer.Languages(&pirsch.Filter{From: startDate, To: endDate})
|
||||
func GetLanguages(startDate, endDate time.Time) []pirsch.LanguageStats {
|
||||
languages, err := analyzer.Languages(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading language statistics", logbuch.Fields{"err": err})
|
||||
@@ -74,7 +97,7 @@ func GetLanguages(startDate, endDate time.Time) []pirsch.Stats {
|
||||
return languages
|
||||
}
|
||||
|
||||
func GetReferrer(startDate, endDate time.Time) []pirsch.Stats {
|
||||
func GetReferrer(startDate, endDate time.Time) []pirsch.ReferrerStats {
|
||||
referrer, err := analyzer.Referrer(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
@@ -89,7 +112,7 @@ func GetReferrer(startDate, endDate time.Time) []pirsch.Stats {
|
||||
return referrer
|
||||
}
|
||||
|
||||
func GetOS(startDate, endDate time.Time) []pirsch.Stats {
|
||||
func GetOS(startDate, endDate time.Time) []pirsch.OSStats {
|
||||
os, err := analyzer.OS(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
@@ -100,7 +123,7 @@ func GetOS(startDate, endDate time.Time) []pirsch.Stats {
|
||||
return os
|
||||
}
|
||||
|
||||
func GetBrowser(startDate, endDate time.Time) []pirsch.Stats {
|
||||
func GetBrowser(startDate, endDate time.Time) []pirsch.BrowserStats {
|
||||
browser, err := analyzer.Browser(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
@@ -111,51 +134,21 @@ func GetBrowser(startDate, endDate time.Time) []pirsch.Stats {
|
||||
return browser
|
||||
}
|
||||
|
||||
func GetPlatform(startDate, endDate time.Time) *pirsch.Stats {
|
||||
platform, err := analyzer.Platform(&pirsch.Filter{From: startDate, To: endDate})
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading platform statistics", logbuch.Fields{"err": err})
|
||||
return nil
|
||||
}
|
||||
|
||||
return platform
|
||||
func GetPlatform(startDate, endDate time.Time) *pirsch.VisitorStats {
|
||||
return analyzer.Platform(&pirsch.Filter{From: startDate, To: endDate})
|
||||
}
|
||||
|
||||
func GetHourlyVisitorsToday() (template.JS, template.JS) {
|
||||
visitors, err := analyzer.HourlyVisitors(&pirsch.Filter{From: today(), To: today()})
|
||||
func sumVisitors(stats []pirsch.Stats) int {
|
||||
sum := 0
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading hourly visitors for today", logbuch.Fields{"err": err})
|
||||
return "", ""
|
||||
for _, s := range stats {
|
||||
sum += s.Visitors
|
||||
}
|
||||
|
||||
return getLabelsAndDataHourly(visitors)
|
||||
return sum
|
||||
}
|
||||
|
||||
func GetActiveVisitors() int {
|
||||
visitors, err := analyzer.ActiveVisitors(pirsch.NullTenant, time.Minute*5)
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading active visitors", logbuch.Fields{"err": err})
|
||||
return 0
|
||||
}
|
||||
|
||||
return visitors
|
||||
}
|
||||
|
||||
func GetActiveVisitorPages() []pirsch.Stats {
|
||||
pages, err := analyzer.ActiveVisitorsPages(pirsch.NullTenant, time.Second*30)
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading active visitor pages", logbuch.Fields{"err": err})
|
||||
return nil
|
||||
}
|
||||
|
||||
return pages
|
||||
}
|
||||
|
||||
func getLabelsAndData(visitors []pirsch.VisitorsPerDay) (template.JS, template.JS) {
|
||||
func getLabelsAndData(visitors []pirsch.Stats) (template.JS, template.JS) {
|
||||
var labels strings.Builder
|
||||
var dp strings.Builder
|
||||
|
||||
@@ -169,7 +162,7 @@ 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.Stats) (template.JS, template.JS) {
|
||||
func getLabelsAndDataHourly(visitors []pirsch.VisitorTimeStats) (template.JS, template.JS) {
|
||||
var labels strings.Builder
|
||||
var dp strings.Builder
|
||||
|
||||
@@ -181,7 +174,7 @@ func getLabelsAndDataHourly(visitors []pirsch.Stats) (template.JS, template.JS)
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user