2016-12-11 11:53:03 +01:00
|
|
|
package count
|
2016-12-11 10:58:58 +01:00
|
|
|
|
2016-12-11 14:50:01 +01:00
|
|
|
import (
|
|
|
|
"time"
|
2016-12-23 16:03:11 +02:00
|
|
|
|
2018-04-27 16:25:01 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2018-04-24 10:28:23 +02:00
|
|
|
"github.com/usefathom/fathom/pkg/datastore"
|
2018-04-27 17:28:03 +02:00
|
|
|
"github.com/usefathom/fathom/pkg/models"
|
2016-12-11 10:58:58 +01:00
|
|
|
)
|
|
|
|
|
2016-12-25 18:24:53 +02:00
|
|
|
func getLastArchivedDate() string {
|
2017-01-25 22:48:24 +01:00
|
|
|
value, _ := datastore.GetOption("last_archived")
|
2018-04-27 16:25:01 +02:00
|
|
|
if value == "" {
|
|
|
|
return time.Now().AddDate(-1, 0, 0).Format("2006-01-02")
|
|
|
|
}
|
|
|
|
|
2016-12-25 18:24:53 +02:00
|
|
|
return value
|
2016-12-24 15:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Archive aggregates data into daily totals
|
|
|
|
func Archive() {
|
2018-04-26 20:40:00 +02:00
|
|
|
start := time.Now()
|
2016-12-24 15:14:25 +02:00
|
|
|
|
2018-04-26 20:40:00 +02:00
|
|
|
lastArchived := getLastArchivedDate()
|
2016-12-24 15:14:25 +02:00
|
|
|
CreatePageviewTotals(lastArchived)
|
2018-04-26 20:40:00 +02:00
|
|
|
CreateVisitorTotals(lastArchived)
|
2016-12-24 15:14:25 +02:00
|
|
|
CreateScreenTotals(lastArchived)
|
|
|
|
CreateLanguageTotals(lastArchived)
|
|
|
|
CreateBrowserTotals(lastArchived)
|
|
|
|
CreateReferrerTotals(lastArchived)
|
2018-04-26 19:30:15 +02:00
|
|
|
datastore.SetOption("last_archived", time.Now().Format("2006-01-02"))
|
2018-04-26 20:40:00 +02:00
|
|
|
|
|
|
|
end := time.Now()
|
|
|
|
log.Infof("finished aggregating metrics. ran for %dms.", (end.UnixNano()-start.UnixNano())/1000000)
|
2016-12-24 15:14:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 14:52:52 +02:00
|
|
|
func calculatePercentagesOfTotal(totals []*models.Total, total int) []*models.Total {
|
2016-12-24 15:57:37 +02:00
|
|
|
// calculate percentage values for each point
|
2018-05-02 14:52:52 +02:00
|
|
|
for _, p := range totals {
|
|
|
|
p.PercentageOfTotal = float64(p.Count) / float64(total) * 100.00
|
2016-12-11 14:50:01 +01:00
|
|
|
}
|
2017-01-09 21:07:41 +01:00
|
|
|
|
2018-05-02 14:52:52 +02:00
|
|
|
return totals
|
2016-12-11 12:52:10 +01:00
|
|
|
}
|