gosora/common/analytics.go

92 lines
2.8 KiB
Go
Raw Normal View History

2021-05-03 01:57:53 +00:00
package common
import (
2022-02-21 03:32:53 +00:00
"database/sql"
"log"
"time"
2021-05-03 01:57:53 +00:00
2022-02-21 03:32:53 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2021-05-03 01:57:53 +00:00
)
var Analytics AnalyticsStore
type AnalyticsTimeRange struct {
2022-02-21 03:32:53 +00:00
Quantity int
Unit string
Slices int
SliceWidth int
Range string
2021-05-03 01:57:53 +00:00
}
type AnalyticsStore interface {
2022-02-21 03:32:53 +00:00
FillViewMap(tbl string, tr *AnalyticsTimeRange, labelList []int64, viewMap map[int64]int64, param string, args ...interface{}) (map[int64]int64, error)
2021-05-03 01:57:53 +00:00
}
type DefaultAnalytics struct {
}
func NewDefaultAnalytics() *DefaultAnalytics {
2022-02-21 03:32:53 +00:00
return &DefaultAnalytics{}
2021-05-03 01:57:53 +00:00
}
/*
2022-02-21 03:32:53 +00:00
rows, e := qgen.NewAcc().Select("viewchunks_systems").Columns("count,createdAt").Where("system=?").DateCutoff("createdAt", timeRange.Quantity, timeRange.Unit).Query(system)
if e != nil && e != sql.ErrNoRows {
return c.InternalError(e, w, r)
}
viewMap, e = c.AnalyticsRowsToViewMap(rows, labelList, viewMap)
if e != nil {
return c.InternalError(e, w, r)
}
2021-05-03 01:57:53 +00:00
*/
func (s *DefaultAnalytics) FillViewMap(tbl string, tr *AnalyticsTimeRange, labelList []int64, viewMap map[int64]int64, param string, args ...interface{}) (map[int64]int64, error) {
2022-02-21 03:32:53 +00:00
ac := qgen.NewAcc().Select(tbl).Columns("count,createdAt")
if param != "" {
ac = ac.Where(param + "=?")
}
rows, e := ac.DateCutoff("createdAt", tr.Quantity, tr.Unit).Query(args...)
if e != nil && e != sql.ErrNoRows {
return nil, e
}
return AnalyticsRowsToViewMap(rows, labelList, viewMap)
2021-05-03 01:57:53 +00:00
}
// TODO: Clamp it rather than using an offset off the current time to avoid chaotic changes in stats as adjacent sets converge and diverge?
func AnalyticsTimeRangeToLabelList(tr *AnalyticsTimeRange) (revLabelList []int64, labelList []int64, viewMap map[int64]int64) {
2022-02-21 03:32:53 +00:00
viewMap = make(map[int64]int64)
currentTime := time.Now().Unix()
for i := 1; i <= tr.Slices; i++ {
label := currentTime - int64(i*tr.SliceWidth)
revLabelList = append(revLabelList, label)
viewMap[label] = 0
}
labelList = append(labelList, revLabelList...)
return revLabelList, labelList, viewMap
2021-05-03 01:57:53 +00:00
}
func AnalyticsRowsToViewMap(rows *sql.Rows, labelList []int64, viewMap map[int64]int64) (map[int64]int64, error) {
2022-02-21 03:32:53 +00:00
defer rows.Close()
for rows.Next() {
var count int64
var createdAt time.Time
e := rows.Scan(&count, &createdAt)
if e != nil {
return viewMap, e
}
unixCreatedAt := createdAt.Unix()
// TODO: Bulk log this
if Dev.SuperDebug {
log.Print("count: ", count)
log.Print("createdAt: ", createdAt, " - ", unixCreatedAt)
}
for _, value := range labelList {
if unixCreatedAt > value {
viewMap[value] += count
break
}
}
}
return viewMap, rows.Err()
2021-05-03 01:57:53 +00:00
}