mirror of
https://github.com/Kugelschieber/marvinblum.git
synced 2026-01-18 14:50:27 +00:00
Updated pirsch, simplified statistics code.
This commit is contained in:
2
go.mod
2
go.mod
@@ -8,7 +8,7 @@ require (
|
||||
github.com/cenkalti/backoff/v4 v4.0.2 // indirect
|
||||
github.com/emvi/api-go v0.0.0-20191210194347-0a945446f6a8
|
||||
github.com/emvi/logbuch v1.1.1
|
||||
github.com/emvi/pirsch v0.0.0-20200624123353-86381b017755
|
||||
github.com/emvi/pirsch v0.0.0-20200625132706-9e7bfe9bb4f4
|
||||
github.com/gorilla/mux v1.7.4
|
||||
github.com/gosimple/slug v1.9.0
|
||||
github.com/jmoiron/sqlx v1.2.0
|
||||
|
||||
2
go.sum
2
go.sum
@@ -89,6 +89,8 @@ github.com/emvi/pirsch v0.0.0-20200623193552-b3a3d4a6434d h1:+HRIYgA9AaXlGFQfhXp
|
||||
github.com/emvi/pirsch v0.0.0-20200623193552-b3a3d4a6434d/go.mod h1:+YmBbltJ3feZz9L/QQyqwywltYvQKBfzrGD51TPKl5g=
|
||||
github.com/emvi/pirsch v0.0.0-20200624123353-86381b017755 h1:TdiDC7+IfV6giMtFNvbYKFlSPJJZewr9jfh3KGcr8QQ=
|
||||
github.com/emvi/pirsch v0.0.0-20200624123353-86381b017755/go.mod h1:+YmBbltJ3feZz9L/QQyqwywltYvQKBfzrGD51TPKl5g=
|
||||
github.com/emvi/pirsch v0.0.0-20200625132706-9e7bfe9bb4f4 h1:6C28hkiULQkBHhDqxRHt6PxY16/i8iTVGq5PENd8U3A=
|
||||
github.com/emvi/pirsch v0.0.0-20200625132706-9e7bfe9bb4f4/go.mod h1:+YmBbltJ3feZz9L/QQyqwywltYvQKBfzrGD51TPKl5g=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/exoscale/egoscale v0.18.1/go.mod h1:Z7OOdzzTOz1Q1PjQXumlz9Wn/CddH0zSYdCF3rnBKXE=
|
||||
|
||||
@@ -47,8 +47,8 @@
|
||||
data: {
|
||||
labels: [{{.TotalVisitorsLabels}}],
|
||||
datasets: [{
|
||||
backgroundColor: "rgba(0, 0, 0, 0.05)",
|
||||
borderColor: "#000",
|
||||
backgroundColor: "rgba(127, 127, 127, 0.05)",
|
||||
borderColor: "#7f7f7f",
|
||||
label: "Total Visitors",
|
||||
data: [{{.TotalVisitorsDps}}]
|
||||
}]
|
||||
@@ -61,8 +61,8 @@
|
||||
data: {
|
||||
labels: [{{$data.Labels}}],
|
||||
datasets: [{
|
||||
backgroundColor: "rgba(0, 0, 0, 0.05)",
|
||||
borderColor: "#000",
|
||||
backgroundColor: "rgba(127, 127, 127, 0.05)",
|
||||
borderColor: "#7f7f7f",
|
||||
label: "Page Visits",
|
||||
data: [{{$data.Data}}]
|
||||
}]
|
||||
|
||||
@@ -2,7 +2,6 @@ package tracking
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/emvi/logbuch"
|
||||
"github.com/emvi/pirsch"
|
||||
"html/template"
|
||||
"strings"
|
||||
@@ -20,63 +19,33 @@ type PageVisits struct {
|
||||
}
|
||||
|
||||
func GetTotalVisitors(start int) (template.JS, template.JS) {
|
||||
query := `SELECT "date" "day",
|
||||
CASE WHEN "visitors_per_day".visitors IS NULL THEN 0 ELSE "visitors_per_day".visitors END
|
||||
FROM (SELECT * FROM generate_series(date($1), date(now()), interval '1 day') "date") AS date_series
|
||||
LEFT JOIN "visitors_per_day" ON date("visitors_per_day"."day") = date("date")
|
||||
ORDER BY "date" ASC`
|
||||
startTime := today()
|
||||
startTime = startTime.Add(-time.Hour * 24 * time.Duration(start-1))
|
||||
logbuch.Debug("Reading total visitors since", logbuch.Fields{"since": startTime})
|
||||
var visitors []pirsch.VisitorsPerDay
|
||||
|
||||
if err := db.Select(&visitors, query, startTime); err != nil {
|
||||
logbuch.Error("Error reading total visitors", logbuch.Fields{"err": err, "since": startTime})
|
||||
return "", ""
|
||||
}
|
||||
|
||||
today := today()
|
||||
visitorsToday, err := store.VisitorsPerDay(today)
|
||||
analyzer := pirsch.NewAnalyzer(store)
|
||||
visitors, err := analyzer.Visitors(&pirsch.Filter{From: startTime, To: today()})
|
||||
|
||||
if err != nil {
|
||||
logbuch.Error("Error reading total visitors for today", logbuch.Fields{"err": err, "since": startTime})
|
||||
return "", ""
|
||||
}
|
||||
|
||||
visitors[len(visitors)-1].Visitors = visitorsToday
|
||||
return getLabelsAndData(visitors)
|
||||
}
|
||||
|
||||
func GetPageVisits(start int) []PageVisits {
|
||||
pathsQuery := `SELECT * FROM (SELECT DISTINCT "path" FROM "visitors_per_page" WHERE day >= $1) AS paths ORDER BY length("path") ASC`
|
||||
query := `SELECT "date" "day",
|
||||
CASE WHEN "visitors_per_page".visitors IS NULL THEN 0 ELSE "visitors_per_page".visitors END
|
||||
FROM (SELECT * FROM generate_series(date($1), date(now() - INTERVAL '1 day'), interval '1 day') "date") AS date_series
|
||||
LEFT JOIN "visitors_per_page" ON date("visitors_per_page"."day") = date("date") AND "visitors_per_page"."path" = $2
|
||||
ORDER BY "date" ASC, length("visitors_per_page"."path") ASC`
|
||||
startTime := today()
|
||||
startTime = startTime.Add(-time.Hour * 24 * time.Duration(start-1))
|
||||
logbuch.Debug("Reading page visits", logbuch.Fields{"since": startTime})
|
||||
var paths []string
|
||||
analyzer := pirsch.NewAnalyzer(store)
|
||||
visits, err := analyzer.PageVisits(&pirsch.Filter{From: startTime, To: today()})
|
||||
|
||||
if err := db.Select(&paths, pathsQuery, startTime); err != nil {
|
||||
logbuch.Error("Error reading distinct paths", logbuch.Fields{"err": err})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO add visitors for today
|
||||
pageVisits := make([]PageVisits, len(paths))
|
||||
pageVisits := make([]PageVisits, len(visits))
|
||||
|
||||
for i, path := range paths {
|
||||
var visitors []pirsch.VisitorsPerDay
|
||||
|
||||
if err := db.Select(&visitors, query, startTime, path); err != nil {
|
||||
logbuch.Error("Error reading visitor for path", logbuch.Fields{"err": err, "since": startTime})
|
||||
return nil
|
||||
}
|
||||
|
||||
labels, data := getLabelsAndData(visitors)
|
||||
pageVisits[i] = PageVisits{path, labels, data}
|
||||
for i, visit := range visits {
|
||||
labels, data := getLabelsAndData(visit.Visits)
|
||||
pageVisits[i] = PageVisits{visit.Path, labels, data}
|
||||
}
|
||||
|
||||
return pageVisits
|
||||
@@ -98,5 +67,5 @@ func getLabelsAndData(visitors []pirsch.VisitorsPerDay) (template.JS, template.J
|
||||
|
||||
func today() time.Time {
|
||||
now := time.Now()
|
||||
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user