diff --git a/go.mod b/go.mod index fe0cd58..8a26474 100644 --- a/go.mod +++ b/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-20200625233854-9bc410bf1483 + github.com/emvi/pirsch v1.0.0 github.com/gorilla/mux v1.7.4 github.com/gosimple/slug v1.9.0 github.com/jmoiron/sqlx v1.2.0 diff --git a/go.sum b/go.sum index d6a34c3..0b2aff5 100644 --- a/go.sum +++ b/go.sum @@ -99,6 +99,8 @@ github.com/emvi/pirsch v0.0.0-20200625182355-75f0aba03718 h1:FOaeJg2A9rGb5WMZsnW github.com/emvi/pirsch v0.0.0-20200625182355-75f0aba03718/go.mod h1:+YmBbltJ3feZz9L/QQyqwywltYvQKBfzrGD51TPKl5g= github.com/emvi/pirsch v0.0.0-20200625233854-9bc410bf1483 h1:hTW+M/0XaAEn1+bpXL/z2DZAFU1xgvKk56OgL1lodSU= github.com/emvi/pirsch v0.0.0-20200625233854-9bc410bf1483/go.mod h1:+YmBbltJ3feZz9L/QQyqwywltYvQKBfzrGD51TPKl5g= +github.com/emvi/pirsch v1.0.0 h1:PAxXw98iZcN3CKamp0ShzKJYPRGagmTPaBGYHe9FHhk= +github.com/emvi/pirsch v1.0.0/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= diff --git a/main.go b/main.go index cf4a60f..3d24120 100644 --- a/main.go +++ b/main.go @@ -128,11 +128,27 @@ func serveTracking() http.HandlerFunc { start = 7 } - totalVisitorsLabels, totalVisitorsDps := tracking.GetTotalVisitors(start) - hourlyVisitorsLabels, hourlyVisitorsDps := tracking.GetHourlyVisitors(start) + var startDate, endDate time.Time + + if err := r.ParseForm(); err != nil { + logbuch.Warn("Error parsing tracking form", logbuch.Fields{"err": err}) + } else { + startDate, _ = time.Parse("2006-01-02", r.FormValue("start-date")) + endDate, _ = time.Parse("2006-01-02", r.FormValue("end-date")) + } + + if startDate.IsZero() || endDate.IsZero() { + startDate = time.Now().UTC().Add(-time.Hour * 24 * time.Duration(start)) + endDate = time.Now().UTC() + } + + totalVisitorsLabels, totalVisitorsDps := tracking.GetTotalVisitors(startDate, endDate) + hourlyVisitorsLabels, hourlyVisitorsDps := tracking.GetHourlyVisitors(startDate, endDate) hourlyVisitorsTodayLabels, hourlyVisitorsTodayDps := tracking.GetHourlyVisitorsToday() tplCache.RenderWithoutCache(w, "tracking.html", struct { Start int + StartDate time.Time + EndDate time.Time TotalVisitorsLabels template.JS TotalVisitorsDps template.JS PageVisits []tracking.PageVisits @@ -144,10 +160,12 @@ func serveTracking() http.HandlerFunc { ActiveVisitors int }{ start, + startDate, + endDate, totalVisitorsLabels, totalVisitorsDps, - tracking.GetPageVisits(start), - tracking.GetLanguages(start), + tracking.GetPageVisits(startDate, endDate), + tracking.GetLanguages(startDate, endDate), hourlyVisitorsLabels, hourlyVisitorsDps, hourlyVisitorsTodayLabels, diff --git a/static/style.css b/static/style.css index d547abe..16146b7 100644 --- a/static/style.css +++ b/static/style.css @@ -52,6 +52,19 @@ body { height: 300px; } +.tracking-form { + display: flex; + align-items: center; +} + +.tracking-form input { + margin: 3px 6px 3px 0; +} + +input { + padding: 2px; +} + section { margin: 80px 0; } @@ -81,4 +94,8 @@ p { .menu { display: block; } + + .tracking-form { + display: block; + } } diff --git a/template/tracking.html b/template/tracking.html index 6f0b5d2..7a2756e 100644 --- a/template/tracking.html +++ b/template/tracking.html @@ -4,7 +4,7 @@

Tracking

- This page shows tracking statistics for my website using Pirsch and Chart.Js. The data shows unique visitors. + This page shows tracking statistics for my website using Pirsch and Chart.Js. The data shows unique visitors. All times and dates are UTC.

Week @@ -13,11 +13,19 @@ Half Year Year

+
+ + + +

Active Visitors

- Active within the last five minutes: {{.ActiveVisitors}} + Active visitors within the last five minutes: {{.ActiveVisitors}} +

+

+ The next diagram shows active visitors for each hour of today.

@@ -27,10 +35,37 @@

Visitors Per Hour

+

+ This is the cumulated visitor count per hour on each day of the selected time frame. +

-

Pages Visits

+

Languages

+

+ Here are the top 10 languages used by my visitors. +

+ + + + + + + + + + {{range $data := .Languages}} + + + + + + {{end}} + +
LanguageAbsoluteRelative
{{if $data.Language}}{{$data.Language}}{{else}}(not set){{end}}{{$data.Visitors}}{{round (multiply $data.RelativeVisitors 100)}} %
+
+
+

Page Visits

{{range $i, $data := .PageVisits}} @@ -40,39 +75,14 @@ {{end}} -
-

Languages

-

- Here are the top 10 languages used by my visitors. -

- - - - - - - - - - {{range $data := .Languages}} - - - - - - {{end}} - -
LanguageAbsoluteRelative
{{if $data.Language}}{{$data.Language}}{{else}}(not set){{end}}{{$data.Visitors}}{{round (multiply $data.RelativeVisitors 100)}} %
-
-