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/cenkalti/backoff/v4 v4.0.2 // indirect
|
||||||
github.com/emvi/api-go v0.0.0-20191210194347-0a945446f6a8
|
github.com/emvi/api-go v0.0.0-20191210194347-0a945446f6a8
|
||||||
github.com/emvi/logbuch v1.1.1
|
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/gorilla/mux v1.7.4
|
||||||
github.com/gosimple/slug v1.9.0
|
github.com/gosimple/slug v1.9.0
|
||||||
github.com/jmoiron/sqlx v1.2.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-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 h1:TdiDC7+IfV6giMtFNvbYKFlSPJJZewr9jfh3KGcr8QQ=
|
||||||
github.com/emvi/pirsch v0.0.0-20200624123353-86381b017755/go.mod h1:+YmBbltJ3feZz9L/QQyqwywltYvQKBfzrGD51TPKl5g=
|
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/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/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=
|
github.com/exoscale/egoscale v0.18.1/go.mod h1:Z7OOdzzTOz1Q1PjQXumlz9Wn/CddH0zSYdCF3rnBKXE=
|
||||||
|
|||||||
@@ -47,8 +47,8 @@
|
|||||||
data: {
|
data: {
|
||||||
labels: [{{.TotalVisitorsLabels}}],
|
labels: [{{.TotalVisitorsLabels}}],
|
||||||
datasets: [{
|
datasets: [{
|
||||||
backgroundColor: "rgba(0, 0, 0, 0.05)",
|
backgroundColor: "rgba(127, 127, 127, 0.05)",
|
||||||
borderColor: "#000",
|
borderColor: "#7f7f7f",
|
||||||
label: "Total Visitors",
|
label: "Total Visitors",
|
||||||
data: [{{.TotalVisitorsDps}}]
|
data: [{{.TotalVisitorsDps}}]
|
||||||
}]
|
}]
|
||||||
@@ -61,8 +61,8 @@
|
|||||||
data: {
|
data: {
|
||||||
labels: [{{$data.Labels}}],
|
labels: [{{$data.Labels}}],
|
||||||
datasets: [{
|
datasets: [{
|
||||||
backgroundColor: "rgba(0, 0, 0, 0.05)",
|
backgroundColor: "rgba(127, 127, 127, 0.05)",
|
||||||
borderColor: "#000",
|
borderColor: "#7f7f7f",
|
||||||
label: "Page Visits",
|
label: "Page Visits",
|
||||||
data: [{{$data.Data}}]
|
data: [{{$data.Data}}]
|
||||||
}]
|
}]
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package tracking
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/emvi/logbuch"
|
|
||||||
"github.com/emvi/pirsch"
|
"github.com/emvi/pirsch"
|
||||||
"html/template"
|
"html/template"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -20,63 +19,33 @@ type PageVisits struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetTotalVisitors(start int) (template.JS, template.JS) {
|
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 := today()
|
||||||
startTime = startTime.Add(-time.Hour * 24 * time.Duration(start-1))
|
startTime = startTime.Add(-time.Hour * 24 * time.Duration(start-1))
|
||||||
logbuch.Debug("Reading total visitors since", logbuch.Fields{"since": startTime})
|
analyzer := pirsch.NewAnalyzer(store)
|
||||||
var visitors []pirsch.VisitorsPerDay
|
visitors, err := analyzer.Visitors(&pirsch.Filter{From: startTime, To: today()})
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logbuch.Error("Error reading total visitors for today", logbuch.Fields{"err": err, "since": startTime})
|
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
|
|
||||||
visitors[len(visitors)-1].Visitors = visitorsToday
|
|
||||||
return getLabelsAndData(visitors)
|
return getLabelsAndData(visitors)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPageVisits(start int) []PageVisits {
|
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 := today()
|
||||||
startTime = startTime.Add(-time.Hour * 24 * time.Duration(start-1))
|
startTime = startTime.Add(-time.Hour * 24 * time.Duration(start-1))
|
||||||
logbuch.Debug("Reading page visits", logbuch.Fields{"since": startTime})
|
analyzer := pirsch.NewAnalyzer(store)
|
||||||
var paths []string
|
visits, err := analyzer.PageVisits(&pirsch.Filter{From: startTime, To: today()})
|
||||||
|
|
||||||
if err := db.Select(&paths, pathsQuery, startTime); err != nil {
|
if err != nil {
|
||||||
logbuch.Error("Error reading distinct paths", logbuch.Fields{"err": err})
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO add visitors for today
|
pageVisits := make([]PageVisits, len(visits))
|
||||||
pageVisits := make([]PageVisits, len(paths))
|
|
||||||
|
|
||||||
for i, path := range paths {
|
for i, visit := range visits {
|
||||||
var visitors []pirsch.VisitorsPerDay
|
labels, data := getLabelsAndData(visit.Visits)
|
||||||
|
pageVisits[i] = PageVisits{visit.Path, labels, data}
|
||||||
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}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pageVisits
|
return pageVisits
|
||||||
@@ -98,5 +67,5 @@ func getLabelsAndData(visitors []pirsch.VisitorsPerDay) (template.JS, template.J
|
|||||||
|
|
||||||
func today() time.Time {
|
func today() time.Time {
|
||||||
now := time.Now()
|
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