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