2018-02-19 04:26:01 +00:00
|
|
|
package counters
|
|
|
|
|
2019-07-26 23:29:42 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-02-26 10:34:38 +00:00
|
|
|
"sync"
|
2020-03-01 06:22:43 +00:00
|
|
|
"sync/atomic"
|
2020-02-26 10:34:38 +00:00
|
|
|
"time"
|
2019-07-26 23:29:42 +00:00
|
|
|
|
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"
|
2020-02-28 23:11:07 +00:00
|
|
|
"github.com/Azareal/Gosora/uutils"
|
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
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
type RVBucket struct {
|
2020-03-01 06:22:43 +00:00
|
|
|
counter int64
|
2020-02-26 10:34:38 +00:00
|
|
|
avg int
|
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-02-19 04:26:01 +00:00
|
|
|
// TODO: Make this lockless?
|
|
|
|
type DefaultRouteViewCounter struct {
|
2020-02-26 10:34:38 +00:00
|
|
|
buckets []*RVBucket //[RouteID]count
|
2018-03-08 03:59:47 +00:00
|
|
|
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) {
|
2020-02-26 10:34:38 +00:00
|
|
|
routeBuckets := make([]*RVBucket, len(routeMapEnum))
|
2018-02-19 04:26:01 +00:00
|
|
|
for bucketID, _ := range routeBuckets {
|
2020-02-26 10:34:38 +00:00
|
|
|
routeBuckets[bucketID] = &RVBucket{counter: 0, avg: 0}
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2020-02-24 11:15:17 +00:00
|
|
|
|
2020-02-26 10:34:38 +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-26 10:34:38 +00:00
|
|
|
insert: acc.Insert("viewchunks").Columns("count,avg,createdAt,route").Fields(fields).Prepare(),
|
|
|
|
insert5: acc.BulkInsert("viewchunks").Columns("count,avg,createdAt,route").Fields(fields, fields, fields, fields, fields).Prepare(),
|
|
|
|
}
|
|
|
|
if !c.Config.DisableAnalytics {
|
2021-05-03 00:36:29 +00:00
|
|
|
c.Tasks.FifteenMin.Add(co.Tick) // There could be a lot of routes, so we don't want to be running this every second
|
|
|
|
//c.Tasks.Sec.Add(co.Tick)
|
|
|
|
c.Tasks.Shutdown.Add(co.Tick)
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2019-07-28 03:46:19 +00:00
|
|
|
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
|
2020-03-01 06:22:43 +00:00
|
|
|
Count int64
|
2020-02-26 10:34:38 +00:00
|
|
|
Avg int
|
2020-02-24 11:15:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func (co *DefaultRouteViewCounter) Tick() (err error) {
|
2020-02-24 11:15:17 +00:00
|
|
|
var tb []RVCount
|
|
|
|
for routeID, b := range co.buckets {
|
2020-03-01 06:22:43 +00:00
|
|
|
var avg int
|
|
|
|
count := atomic.SwapInt64(&b.counter, 0)
|
2020-02-26 10:34:38 +00:00
|
|
|
b.Lock()
|
|
|
|
avg = b.avg
|
|
|
|
b.avg = 0
|
|
|
|
b.Unlock()
|
2020-02-24 11:15:17 +00:00
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-26 10:34:38 +00:00
|
|
|
tb = append(tb, RVCount{routeID, count, avg})
|
2020-02-24 11:15:17 +00:00
|
|
|
}
|
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 {
|
2020-02-26 10:34:38 +00:00
|
|
|
for ; len(tb) > (i + 5); i += 5 {
|
|
|
|
err := co.insert5Chunk(tb[i : i+5])
|
2020-02-24 11:15:17 +00:00
|
|
|
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++ {
|
2020-02-26 10:34:38 +00:00
|
|
|
it := tb[i]
|
|
|
|
err = co.insertChunk(it.Count, it.Avg, it.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-03-01 06:22:43 +00:00
|
|
|
func (co *DefaultRouteViewCounter) insertChunk(count int64, avg, route int) error {
|
2019-07-28 03:46:19 +00:00
|
|
|
routeName := reverseRouteMapEnum[route]
|
2020-03-01 06:22:43 +00:00
|
|
|
c.DebugLogf("Inserting vchunk with count %d, avg %d for route %s (%d)", count, avg, routeName, route)
|
2020-02-26 10:34:38 +00:00
|
|
|
_, err := co.insert.Exec(count, avg, 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 {
|
2020-02-26 10:34:38 +00:00
|
|
|
args := make([]interface{}, len(rvs)*3)
|
2020-02-24 11:15:17 +00:00
|
|
|
i := 0
|
|
|
|
for _, rv := range rvs {
|
|
|
|
routeName := reverseRouteMapEnum[rv.RouteID]
|
2020-02-26 10:34:38 +00:00
|
|
|
if rv.Avg == 0 {
|
2020-03-01 06:22:43 +00:00
|
|
|
c.DebugLogf("Queueing vchunk with count %d for routes %s (%d)", rv.Count, routeName, rv.RouteID)
|
2020-02-26 10:34:38 +00:00
|
|
|
} else {
|
2020-03-01 06:22:43 +00:00
|
|
|
c.DebugLogf("Queueing vchunk with count %d, avg %d for routes %s (%d)", rv.Count, rv.Avg, routeName, rv.RouteID)
|
2020-02-26 10:34:38 +00:00
|
|
|
}
|
2020-02-24 11:15:17 +00:00
|
|
|
args[i] = rv.Count
|
2020-02-26 10:34:38 +00:00
|
|
|
args[i+1] = rv.Avg
|
|
|
|
args[i+2] = routeName
|
|
|
|
i += 3
|
2020-02-24 11:15:17 +00:00
|
|
|
}
|
2020-02-26 10:34:38 +00:00
|
|
|
c.DebugDetailf("args: %+v\n", args)
|
2020-02-24 11:15:17 +00:00
|
|
|
_, err := co.insert5.Exec(args...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
func (co *DefaultRouteViewCounter) Bump(route int) {
|
2020-02-26 10:34:38 +00:00
|
|
|
if c.Config.DisableAnalytics {
|
|
|
|
return
|
|
|
|
}
|
2018-02-19 04:26:01 +00:00
|
|
|
// TODO: Test this check
|
2020-02-24 11:15:17 +00:00
|
|
|
b := co.buckets[route]
|
2020-03-01 06:22:43 +00:00
|
|
|
c.DebugDetail("bucket ", 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-03-01 06:22:43 +00:00
|
|
|
atomic.AddInt64(&b.counter, 1)
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2020-02-26 10:34:38 +00:00
|
|
|
|
|
|
|
// TODO: Eliminate the lock?
|
|
|
|
func (co *DefaultRouteViewCounter) Bump2(route int, t time.Time) {
|
|
|
|
if c.Config.DisableAnalytics {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: Test this check
|
|
|
|
b := co.buckets[route]
|
2020-03-01 06:22:43 +00:00
|
|
|
c.DebugDetail("bucket ", route, ": ", b)
|
2020-02-26 10:34:38 +00:00
|
|
|
if len(co.buckets) <= route || route < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
micro := int(time.Since(t).Microseconds())
|
2020-02-28 23:11:07 +00:00
|
|
|
//co.PerfCounter.Push(since, true)
|
2020-03-01 06:22:43 +00:00
|
|
|
atomic.AddInt64(&b.counter, 1)
|
2020-02-28 23:11:07 +00:00
|
|
|
b.Lock()
|
|
|
|
if micro != b.avg {
|
|
|
|
if b.avg == 0 {
|
|
|
|
b.avg = micro
|
|
|
|
} else {
|
|
|
|
b.avg = (micro + b.avg) / 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Eliminate the lock?
|
|
|
|
func (co *DefaultRouteViewCounter) Bump3(route int, nano int64) {
|
|
|
|
if c.Config.DisableAnalytics {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: Test this check
|
|
|
|
b := co.buckets[route]
|
2020-03-01 06:22:43 +00:00
|
|
|
c.DebugDetail("bucket ", route, ": ", b)
|
2020-02-28 23:11:07 +00:00
|
|
|
if len(co.buckets) <= route || route < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
micro := int((uutils.Nanotime() - nano) / 1000)
|
|
|
|
//co.PerfCounter.Push(since, true)
|
2020-03-01 06:22:43 +00:00
|
|
|
atomic.AddInt64(&b.counter, 1)
|
2020-02-26 10:34:38 +00:00
|
|
|
b.Lock()
|
|
|
|
if micro != b.avg {
|
|
|
|
if b.avg == 0 {
|
|
|
|
b.avg = micro
|
|
|
|
} else {
|
|
|
|
b.avg = (micro + b.avg) / 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.Unlock()
|
|
|
|
}
|