2018-03-08 03:59:47 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import "database/sql"
|
2019-04-19 06:36:26 +00:00
|
|
|
import c "github.com/Azareal/Gosora/common"
|
2018-10-27 03:21:02 +00:00
|
|
|
import "github.com/Azareal/Gosora/query_gen"
|
2018-03-08 03:59:47 +00:00
|
|
|
|
|
|
|
var LangViewCounter *DefaultLangViewCounter
|
|
|
|
|
|
|
|
var langCodes = []string{
|
|
|
|
"unknown",
|
2018-03-17 08:16:43 +00:00
|
|
|
"none",
|
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",
|
|
|
|
"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 {
|
|
|
|
buckets []*RWMutexCounterBucket //[OSID]count
|
|
|
|
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) {
|
2018-03-08 03:59:47 +00:00
|
|
|
var langBuckets = make([]*RWMutexCounterBucket, len(langCodes))
|
|
|
|
for bucketID, _ := range langBuckets {
|
|
|
|
langBuckets[bucketID] = &RWMutexCounterBucket{counter: 0}
|
|
|
|
}
|
|
|
|
var codesToIndices = make(map[string]int)
|
|
|
|
for index, code := range langCodes {
|
|
|
|
codesToIndices[code] = index
|
|
|
|
}
|
|
|
|
|
|
|
|
counter := &DefaultLangViewCounter{
|
|
|
|
buckets: langBuckets,
|
|
|
|
codesToIndices: codesToIndices,
|
|
|
|
insert: acc.Insert("viewchunks_langs").Columns("count, createdAt, lang").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c.AddScheduledFifteenMinuteTask(counter.Tick)
|
|
|
|
//c.AddScheduledSecondTask(counter.Tick)
|
|
|
|
c.AddShutdownTask(counter.Tick)
|
2018-03-08 03:59:47 +00:00
|
|
|
return counter, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (counter *DefaultLangViewCounter) Tick() error {
|
|
|
|
for id, bucket := range counter.buckets {
|
|
|
|
var count int
|
|
|
|
bucket.RLock()
|
|
|
|
count = bucket.counter
|
|
|
|
bucket.counter = 0 // TODO: Add a SetZero method to reduce the amount of duplicate code between the OS and agent counters?
|
|
|
|
bucket.RUnlock()
|
|
|
|
|
|
|
|
err := counter.insertChunk(count, id) // TODO: Bulk insert for speed?
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (counter *DefaultLangViewCounter) insertChunk(count int, id int) error {
|
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var langCode = langCodes[id]
|
2019-04-19 06:36:26 +00:00
|
|
|
c.DebugLogf("Inserting a viewchunk with a count of %d for lang %s (%d)", count, langCode, id)
|
2018-03-08 03:59:47 +00:00
|
|
|
_, err := counter.insert.Exec(count, langCode)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:29:19 +00:00
|
|
|
func (counter *DefaultLangViewCounter) Bump(langCode string) (validCode bool) {
|
|
|
|
validCode = true
|
2018-03-08 03:59:47 +00:00
|
|
|
id, ok := counter.codesToIndices[langCode]
|
|
|
|
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
|
2019-04-19 06:36:26 +00:00
|
|
|
c.DebugDetail("counter.buckets[", id, "]: ", counter.buckets[id])
|
2018-03-08 03:59:47 +00:00
|
|
|
if len(counter.buckets) <= id || id < 0 {
|
2019-02-23 06:29:19 +00:00
|
|
|
return validCode
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|
|
|
|
counter.buckets[id].Lock()
|
|
|
|
counter.buckets[id].counter++
|
|
|
|
counter.buckets[id].Unlock()
|
2019-02-23 06:29:19 +00:00
|
|
|
|
|
|
|
return validCode
|
2018-03-08 03:59:47 +00:00
|
|
|
}
|