2018-02-19 04:26:01 +00:00
|
|
|
package counters
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-07-26 23:29:42 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2019-07-28 03:46:19 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-07-26 23:29:42 +00:00
|
|
|
)
|
2018-02-19 04:26:01 +00:00
|
|
|
|
|
|
|
var RouteViewCounter *DefaultRouteViewCounter
|
|
|
|
|
|
|
|
// TODO: Make this lockless?
|
|
|
|
type DefaultRouteViewCounter struct {
|
2018-03-08 03:59:47 +00:00
|
|
|
buckets []*RWMutexCounterBucket //[RouteID]count
|
|
|
|
insert *sql.Stmt
|
2020-02-24 11:15:17 +00:00
|
|
|
insert5 *sql.Stmt
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 22:59:41 +00:00
|
|
|
func NewDefaultRouteViewCounter(acc *qgen.Accumulator) (*DefaultRouteViewCounter, error) {
|
2019-07-28 03:46:19 +00:00
|
|
|
routeBuckets := make([]*RWMutexCounterBucket, len(routeMapEnum))
|
2018-02-19 04:26:01 +00:00
|
|
|
for bucketID, _ := range routeBuckets {
|
|
|
|
routeBuckets[bucketID] = &RWMutexCounterBucket{counter: 0}
|
|
|
|
}
|
2020-02-24 11:15:17 +00:00
|
|
|
|
|
|
|
fields := "?,UTC_TIMESTAMP(),?"
|
2019-07-28 03:46:19 +00:00
|
|
|
co := &DefaultRouteViewCounter{
|
2018-03-08 03:59:47 +00:00
|
|
|
buckets: routeBuckets,
|
2020-02-24 11:15:17 +00:00
|
|
|
insert: acc.Insert("viewchunks").Columns("count,createdAt,route").Fields(fields).Prepare(),
|
|
|
|
insert5: acc.BulkInsert("viewchunks").Columns("count,createdAt,route").Fields(fields,fields,fields,fields,fields).Prepare(),
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2019-07-28 03:46:19 +00:00
|
|
|
c.AddScheduledFifteenMinuteTask(co.Tick) // There could be a lot of routes, so we don't want to be running this every second
|
|
|
|
//c.AddScheduledSecondTask(co.Tick)
|
|
|
|
c.AddShutdownTask(co.Tick)
|
|
|
|
return co, acc.FirstError()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 11:15:17 +00:00
|
|
|
type RVCount struct {
|
|
|
|
RouteID int
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
func (co *DefaultRouteViewCounter) Tick() error {
|
2020-02-24 11:15:17 +00:00
|
|
|
var tb []RVCount
|
|
|
|
for routeID, b := range co.buckets {
|
2018-02-19 04:26:01 +00:00
|
|
|
var count int
|
2020-02-24 11:15:17 +00:00
|
|
|
b.RLock()
|
|
|
|
count = b.counter
|
|
|
|
b.counter = 0
|
|
|
|
b.RUnlock()
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tb = append(tb, RVCount{routeID,count})
|
|
|
|
}
|
2018-02-19 04:26:01 +00:00
|
|
|
|
2020-02-24 11:15:17 +00:00
|
|
|
// TODO: Expand on this?
|
|
|
|
var i int
|
|
|
|
if len(tb) >= 5 {
|
|
|
|
for ; len(tb) > (i+5); i += 5 {
|
|
|
|
err := co.insert5Chunk(tb[i:i+5])
|
|
|
|
if err != nil {
|
|
|
|
c.DebugLogf("tb: %+v\n", tb)
|
|
|
|
c.DebugLog("i: ", i)
|
|
|
|
return errors.Wrap(errors.WithStack(err), "route counter x 5")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ; len(tb) > i; i++ {
|
|
|
|
err := co.insertChunk(tb[i].Count, tb[i].RouteID)
|
2018-02-19 04:26:01 +00:00
|
|
|
if err != nil {
|
2020-02-24 11:15:17 +00:00
|
|
|
c.DebugLogf("tb: %+v\n", tb)
|
|
|
|
c.DebugLog("i: ", i)
|
2019-07-28 03:46:19 +00:00
|
|
|
return errors.Wrap(errors.WithStack(err), "route counter")
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 11:15:17 +00:00
|
|
|
|
2018-02-19 04:26:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-24 11:15:17 +00:00
|
|
|
func (co *DefaultRouteViewCounter) insertChunk(count, route int) error {
|
2019-07-28 03:46:19 +00:00
|
|
|
routeName := reverseRouteMapEnum[route]
|
|
|
|
c.DebugLogf("Inserting a vchunk with a count of %d for route %s (%d)", count, routeName, route)
|
|
|
|
_, err := co.insert.Exec(count, routeName)
|
2018-02-19 04:26:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-24 11:15:17 +00:00
|
|
|
func (co *DefaultRouteViewCounter) insert5Chunk(rvs []RVCount) error {
|
|
|
|
args := make([]interface{}, len(rvs) * 2)
|
|
|
|
i := 0
|
|
|
|
for _, rv := range rvs {
|
|
|
|
routeName := reverseRouteMapEnum[rv.RouteID]
|
|
|
|
c.DebugLogf("Queueing a vchunk with a count of %d for routes %s (%d)", rv.Count, routeName, rv.RouteID)
|
|
|
|
args[i] = rv.Count
|
|
|
|
args[i+1] = routeName
|
|
|
|
i += 2
|
|
|
|
}
|
|
|
|
c.DebugLogf("args: %+v\n", args)
|
|
|
|
_, err := co.insert5.Exec(args...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
func (co *DefaultRouteViewCounter) Bump(route int) {
|
2018-02-19 04:26:01 +00:00
|
|
|
// TODO: Test this check
|
2020-02-24 11:15:17 +00:00
|
|
|
b := co.buckets[route]
|
|
|
|
c.DebugDetail("co.buckets[", route, "]: ", b)
|
2019-07-28 03:46:19 +00:00
|
|
|
if len(co.buckets) <= route || route < 0 {
|
2018-02-19 04:26:01 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-24 11:15:17 +00:00
|
|
|
// TODO: Avoid lock by using atomic increment?
|
|
|
|
b.Lock()
|
|
|
|
b.counter++
|
|
|
|
b.Unlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|