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

@@ -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")
}