2018-03-08 03:59:47 +00:00
|
|
|
package counters
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-02-25 04:25:50 +00:00
|
|
|
"sync/atomic"
|
2019-07-28 04:01:33 +00:00
|
|
|
|
|
|
|
c "github.com/Azareal/Gosora/common"
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2018-03-08 03:59:47 +00:00
|
|
|
|
|
|
|
var LangViewCounter *DefaultLangViewCounter
|
|
|
|
|
|
|
|
var langCodes = []string{
|
|
|
|
"unknown",
|
2020-02-25 04:25:50 +00:00
|
|
|
"",
|
2018-03-08 03:59:47 +00:00
|
|
|
"af",
|
|
|
|
"ar",
|
|
|
|
"az",
|
|
|
|
"be",
|
|
|
|
"bg",
|
|
|
|
"bs",
|
|
|
|
"ca",
|
|
|
|
"cs",
|
|
|
|
"cy",
|
|
|
|
"da",
|
|
|
|
"de",
|
|
|
|
"dv",
|
|
|
|
"el",
|
|
|
|
"en",
|
|
|
|
"eo",
|
|
|
|
"es",
|
|
|
|
"et",
|
|
|
|
"eu",
|
|
|
|
"fa",
|
|
|
|
"fi",
|
|
|
|
"fo",
|
|
|
|
"fr",
|
|
|
|
"gl",
|
|
|
|
"gu",
|
|
|
|
"he",
|
|
|
|
"hi",
|
|
|
|
"hr",
|
|
|
|
"hu",
|
|
|
|
"hy",
|
|
|
|
"id",
|
|
|
|
"is",
|
|
|
|
"it",
|
|
|
|
"ja",
|
|
|
|
"ka",
|
|
|
|
"kk",
|
|
|
|
"kn",
|
|
|
|
"ko",
|
|
|
|
"kok",
|
2021-03-23 20:59:34 +00:00
|
|
|
"kw",
|
2018-03-08 03:59:47 +00:00
|
|
|
"ky",
|
|
|
|
"lt",
|
|
|
|
"lv",
|
|
|
|
"mi",
|
|
|
|
"mk",
|
|
|
|
"mn",
|
|
|
|
"mr",
|
|
|
|
"ms",
|
|
|
|
"mt",
|
|
|
|
"nb",
|
|
|
|
"nl",
|
|
|
|
"nn",
|
|
|
|
"ns",
|
|
|
|
"pa",
|
|
|
|
"pl",
|
|
|
|
"ps",
|
|
|
|
"pt",
|
|
|
|
"qu",
|
|
|
|
"ro",
|
|
|
|
"ru",
|
|
|
|
"sa",
|
|
|
|
"se",
|
|
|
|
"sk",
|
|
|
|
"sl",
|
|
|
|
"sq",
|
|
|
|
"sr",
|
|
|
|
"sv",
|
|
|
|
"sw",
|
|
|
|
"syr",
|
|
|
|
"ta",
|
|
|
|
"te",
|
|
|
|
"th",
|
|
|
|
"tl",
|
|
|
|
"tn",
|
|
|
|
"tr",
|
|
|
|
"tt",
|
|
|
|
"ts",
|
|
|
|
"uk",
|
|
|
|
"ur",
|
|
|
|
"uz",
|
|
|
|
"vi",
|
|
|
|
"xh",
|
|
|
|
"zh",
|
|
|
|
"zu",
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultLangViewCounter struct {
|
2020-02-25 04:25:50 +00:00
|
|
|
//buckets []*MutexCounterBucket //[OSID]count
|
|
|
|
buckets []int64 //[OSID]count
|
2018-03-08 03:59:47 +00:00
|
|
|
codesToIndices map[string]int
|
|
|
|
|
2019-03-21 22:59:41 +00:00
|
|
|
insert *sql.Stmt
|
|
|
|
}
|
2018-03-08 03:59:47 +00:00
|
|
|
|
2019-03-21 22:59:41 +00:00
|
|
|
func NewDefaultLangViewCounter(acc *qgen.Accumulator) (*DefaultLangViewCounter, error) {
|
2019-07-28 04:01:33 +00:00
|
|
|
codesToIndices := make(map[string]int, len(langCodes))
|
2018-03-08 03:59:47 +00:00
|
|
|
for index, code := range langCodes {
|
|
|
|
codesToIndices[code] = index
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
co := &DefaultLangViewCounter{
|
2020-02-25 04:25:50 +00:00
|
|
|
buckets: make([]int64, len(langCodes)),
|
2018-03-08 03:59:47 +00:00
|
|
|
codesToIndices: codesToIndices,
|
2020-02-25 04:25:50 +00:00
|
|
|
insert: acc.Insert("viewchunks_langs").Columns("count,createdAt,lang").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 00:36:29 +00:00
|
|
|
c.Tasks.FifteenMin.Add(co.Tick)
|
|
|
|
//c.Tasks.Sec.Add(co.Tick)
|
|
|
|
c.Tasks.Shutdown.Add(co.Tick)
|
2019-07-28 04:01:33 +00:00
|
|
|
return co, acc.FirstError()
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
func (co *DefaultLangViewCounter) Tick() error {
|
2020-02-25 04:25:50 +00:00
|
|
|
for id := 0; id < len(co.buckets); id++ {
|
|
|
|
count := atomic.SwapInt64(&co.buckets[id], 0)
|
2021-04-27 10:20:26 +00:00
|
|
|
e := co.insertChunk(count, id) // TODO: Bulk insert for speed?
|
|
|
|
if e != nil {
|
|
|
|
return errors.Wrap(errors.WithStack(e), "langview counter")
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 04:25:50 +00:00
|
|
|
func (co *DefaultLangViewCounter) insertChunk(count int64, id int) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
langCode := langCodes[id]
|
2020-02-25 04:25:50 +00:00
|
|
|
if langCode == "" {
|
|
|
|
langCode = "none"
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
c.DebugLogf("Inserting a vchunk with a count of %d for lang %s (%d)", count, langCode, id)
|
2021-04-27 10:20:26 +00:00
|
|
|
_, e := co.insert.Exec(count, langCode)
|
|
|
|
return e
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
func (co *DefaultLangViewCounter) Bump(langCode string) (validCode bool) {
|
2019-02-23 06:29:19 +00:00
|
|
|
validCode = true
|
2019-07-28 04:01:33 +00:00
|
|
|
id, ok := co.codesToIndices[langCode]
|
2018-03-08 03:59:47 +00:00
|
|
|
if !ok {
|
|
|
|
// TODO: Tell the caller that the code's invalid
|
|
|
|
id = 0 // Unknown
|
2019-02-23 06:29:19 +00:00
|
|
|
validCode = false
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Test this check
|
2020-03-08 04:15:27 +00:00
|
|
|
c.DebugDetail("buckets ", id, ": ", co.buckets[id])
|
2019-07-28 04:01:33 +00:00
|
|
|
if len(co.buckets) <= id || id < 0 {
|
2019-02-23 06:29:19 +00:00
|
|
|
return validCode
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
2020-02-25 04:25:50 +00:00
|
|
|
atomic.AddInt64(&co.buckets[id], 1)
|
2019-02-23 06:29:19 +00:00
|
|
|
|
|
|
|
return validCode
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
2020-03-08 04:15:27 +00:00
|
|
|
|
|
|
|
func (co *DefaultLangViewCounter) Bump2(id int) {
|
|
|
|
// TODO: Test this check
|
|
|
|
c.DebugDetail("bucket ", id, ": ", co.buckets[id])
|
|
|
|
if len(co.buckets) <= id || id < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
atomic.AddInt64(&co.buckets[id], 1)
|
|
|
|
}
|