Updated pirsch and added country statistics to tracking page.

This commit is contained in:
Marvin Blum
2020-09-20 13:38:43 +02:00
committed by Marvin Blum
parent 388997a678
commit b6e14ed1aa
9 changed files with 104 additions and 3 deletions

View File

@@ -138,6 +138,21 @@ func GetBrowser(startDate, endDate time.Time) []pirsch.BrowserStats {
return browser
}
func GetCountry(startDate, endDate time.Time) []pirsch.CountryStats {
countries, err := analyzer.Country(&pirsch.Filter{From: startDate, To: endDate})
if err != nil {
logbuch.Error("Error reading country statistics", logbuch.Fields{"err": err})
return nil
}
for i := range countries {
countries[i].CountryCode.String = strings.ToUpper(countries[i].CountryCode.String)
}
return countries
}
func GetPlatform(startDate, endDate time.Time) *pirsch.VisitorStats {
return analyzer.Platform(&pirsch.Filter{From: startDate, To: endDate})
}

View File

@@ -7,6 +7,11 @@ import (
"github.com/emvi/logbuch"
"github.com/emvi/pirsch"
"os"
"path/filepath"
)
const (
geodbPath = "geodb"
)
var (
@@ -38,8 +43,10 @@ func NewTracker() (*pirsch.Tracker, context.CancelFunc) {
processor := pirsch.NewProcessor(store)
cancel := pirsch.RunAtMidnight(func() {
processTrackingData(processor)
updateGeoDB(tracker)
})
processTrackingData(processor) // run on startup
processTrackingData(processor)
updateGeoDB(tracker)
return tracker, cancel
}
@@ -58,3 +65,26 @@ func processTrackingData(processor *pirsch.Processor) {
logbuch.Info("Done processing tracking data")
}
}
func updateGeoDB(tracker *pirsch.Tracker) {
licenseKey := os.Getenv("MB_GEOLITE2_LICENSE_KEY")
if licenseKey == "" {
return
}
if err := pirsch.GetGeoLite2(geodbPath, licenseKey); err != nil {
logbuch.Error("Error loading GeoLite2", logbuch.Fields{"err": err})
return
}
geodb, err := pirsch.NewGeoDB(filepath.Join(geodbPath, pirsch.GeoLite2Filename))
if err != nil {
logbuch.Error("Error creating GeoDB", logbuch.Fields{"err": err})
return
}
tracker.SetGeoDB(geodb)
logbuch.Info("GeoDB updated")
}