2018-02-19 04:26:01 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-07-11 21:26:05 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2019-07-28 03:46:19 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-02-19 04:26:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: Rename this?
|
|
|
|
var GlobalViewCounter *DefaultViewCounter
|
|
|
|
|
|
|
|
// TODO: Rename this and shard it?
|
|
|
|
type DefaultViewCounter struct {
|
|
|
|
buckets [2]int64
|
|
|
|
currentBucket int64
|
|
|
|
|
|
|
|
insert *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2018-05-27 09:36:35 +00:00
|
|
|
func NewGlobalViewCounter(acc *qgen.Accumulator) (*DefaultViewCounter, error) {
|
2019-07-28 03:46:19 +00:00
|
|
|
co := &DefaultViewCounter{
|
2018-02-19 04:26:01 +00:00
|
|
|
currentBucket: 0,
|
2020-02-23 09:08:47 +00:00
|
|
|
insert: acc.Insert("viewchunks").Columns("count,createdAt,route").Fields("?,UTC_TIMESTAMP(),''").Prepare(),
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
2021-05-03 00:36:29 +00:00
|
|
|
c.Tasks.FifteenMin.Add(co.Tick) // This is run once every fifteen minutes to match the frequency of the RouteViewCounter
|
|
|
|
//c.Tasks.Sec.Add(co.Tick)
|
|
|
|
c.Tasks.Shutdown.Add(co.Tick)
|
2019-07-28 03:46:19 +00:00
|
|
|
return co, acc.FirstError()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
// TODO: Simplify the atomics used here
|
2019-07-28 03:46:19 +00:00
|
|
|
func (co *DefaultViewCounter) Tick() (err error) {
|
|
|
|
oldBucket := co.currentBucket
|
2018-02-19 04:26:01 +00:00
|
|
|
var nextBucket int64 // 0
|
2019-07-28 03:46:19 +00:00
|
|
|
if co.currentBucket == 0 {
|
2018-02-19 04:26:01 +00:00
|
|
|
nextBucket = 1
|
|
|
|
}
|
2019-07-28 03:46:19 +00:00
|
|
|
atomic.AddInt64(&co.buckets[oldBucket], co.buckets[nextBucket])
|
|
|
|
atomic.StoreInt64(&co.buckets[nextBucket], 0)
|
|
|
|
atomic.StoreInt64(&co.currentBucket, nextBucket)
|
2018-02-19 04:26:01 +00:00
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
previousViewChunk := co.buckets[oldBucket]
|
|
|
|
atomic.AddInt64(&co.buckets[oldBucket], -previousViewChunk)
|
|
|
|
err = co.insertChunk(previousViewChunk)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(errors.WithStack(err), "req counter")
|
|
|
|
}
|
|
|
|
return nil
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
func (co *DefaultViewCounter) Bump() {
|
|
|
|
atomic.AddInt64(&co.buckets[co.currentBucket], 1)
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 03:46:19 +00:00
|
|
|
func (co *DefaultViewCounter) insertChunk(count int64) error {
|
2018-02-19 04:26:01 +00:00
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-28 03:46:19 +00:00
|
|
|
c.DebugLogf("Inserting a vchunk with a count of %d", count)
|
|
|
|
_, err := co.insert.Exec(count)
|
2018-02-19 04:26:01 +00:00
|
|
|
return err
|
|
|
|
}
|