gosora/common/counters.go
Azareal abfe0a472a Made a huge amount of progress on the Cosora Theme in the Control Panel, it's almost ready for deployment.
Added the view counter, it currently collects data without exposing it to the admin.

Added an API for scheduling tasks which run once every second and once every fifteen minutes.
Added a generated map containing all the routes with the function names as keys.
Added the request counter to the generated router.
Added more ARIA Labels and moved various bits and pieces into the CSS files for flexibility.
Removed a bunch of redundant avatar checks in the templates.
Improved the mobile friendliness of Cosora.
Fixed various issues in the other themes.
Refactored the file listener.
Gosora now resyncs newly created theme files not just modified ones.
Gosora now resyncs renamed theme files not just modified ones.
2017-12-10 03:43:30 +00:00

64 lines
1.7 KiB
Go

package common
import (
"database/sql"
"sync/atomic"
"../query_gen/lib"
)
var GlobalViewCounter *BufferedViewCounter
type BufferedViewCounter struct {
buckets [2]int64
currentBucket int64
insert *sql.Stmt
}
func NewGlobalViewCounter() (*BufferedViewCounter, error) {
acc := qgen.Builder.Accumulator()
counter := &BufferedViewCounter{
currentBucket: 0,
insert: acc.SimpleInsert("viewchunks", "count, createdAt", "?,UTC_TIMESTAMP()"),
}
//AddScheduledFifteenMinuteTask(counter.Tick)
AddScheduledSecondTask(counter.Tick)
return counter, acc.FirstError()
}
func (counter *BufferedViewCounter) Tick() (err error) {
var oldBucket = counter.currentBucket
var nextBucket int64
if counter.currentBucket == 1 {
nextBucket = 0
} else {
nextBucket = 1
}
atomic.AddInt64(&counter.buckets[oldBucket], counter.buckets[nextBucket])
atomic.StoreInt64(&counter.buckets[nextBucket], 0)
atomic.StoreInt64(&counter.currentBucket, nextBucket)
/*debugLog("counter.buckets[nextBucket]: ", counter.buckets[nextBucket])
debugLog("counter.buckets[oldBucket]: ", counter.buckets[oldBucket])
debugLog("counter.currentBucket:", counter.currentBucket)
debugLog("oldBucket:", oldBucket)
debugLog("nextBucket:", nextBucket)*/
var previousViewChunk = counter.buckets[oldBucket]
atomic.AddInt64(&counter.buckets[oldBucket], -previousViewChunk)
return counter.insertChunk(previousViewChunk)
}
func (counter *BufferedViewCounter) Bump() {
atomic.AddInt64(&counter.buckets[counter.currentBucket], 1)
}
func (counter *BufferedViewCounter) insertChunk(count int64) error {
if count == 0 {
return nil
}
debugLogf("Inserting a viewchunk with a count of %d", count)
_, err := counter.insert.Exec(count)
return err
}