2018-02-19 04:26:01 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-07-28 04:01:33 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
"github.com/pkg/errors"
|
2018-02-19 04:26:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var AgentViewCounter *DefaultAgentViewCounter
|
|
|
|
|
|
|
|
type DefaultAgentViewCounter struct {
|
|
|
|
agentBuckets []*RWMutexCounterBucket //[AgentID]count
|
|
|
|
insert *sql.Stmt
|
|
|
|
}
|
|
|
|
|
2019-03-12 09:13:57 +00:00
|
|
|
func NewDefaultAgentViewCounter(acc *qgen.Accumulator) (*DefaultAgentViewCounter, error) {
|
2018-02-19 04:26:01 +00:00
|
|
|
var agentBuckets = make([]*RWMutexCounterBucket, len(agentMapEnum))
|
|
|
|
for bucketID, _ := range agentBuckets {
|
|
|
|
agentBuckets[bucketID] = &RWMutexCounterBucket{counter: 0}
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
co := &DefaultAgentViewCounter{
|
2018-02-19 04:26:01 +00:00
|
|
|
agentBuckets: agentBuckets,
|
|
|
|
insert: acc.Insert("viewchunks_agents").Columns("count, createdAt, browser").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
c.AddScheduledFifteenMinuteTask(co.Tick)
|
|
|
|
//c.AddScheduledSecondTask(co.Tick)
|
|
|
|
c.AddShutdownTask(co.Tick)
|
|
|
|
return co, acc.FirstError()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
func (co *DefaultAgentViewCounter) Tick() error {
|
|
|
|
for agentID, agentBucket := range co.agentBuckets {
|
2018-02-19 04:26:01 +00:00
|
|
|
var count int
|
|
|
|
agentBucket.RLock()
|
|
|
|
count = agentBucket.counter
|
|
|
|
agentBucket.counter = 0
|
|
|
|
agentBucket.RUnlock()
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
err := co.insertChunk(count, agentID) // TODO: Bulk insert for speed?
|
2018-02-19 04:26:01 +00:00
|
|
|
if err != nil {
|
2019-07-28 04:01:33 +00:00
|
|
|
return errors.Wrap(errors.WithStack(err), "agent counter")
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
func (co *DefaultAgentViewCounter) insertChunk(count int, agent int) error {
|
2018-02-19 04:26:01 +00:00
|
|
|
if count == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
agentName := reverseAgentMapEnum[agent]
|
|
|
|
c.DebugLogf("Inserting a vchunk with a count of %d for agent %s (%d)", count, agentName, agent)
|
|
|
|
_, err := co.insert.Exec(count, agentName)
|
2018-02-19 04:26:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-28 04:01:33 +00:00
|
|
|
func (co *DefaultAgentViewCounter) Bump(agent int) {
|
2018-02-19 04:26:01 +00:00
|
|
|
// TODO: Test this check
|
2019-07-28 04:01:33 +00:00
|
|
|
c.DebugDetail("co.agentBuckets[", agent, "]: ", co.agentBuckets[agent])
|
|
|
|
if len(co.agentBuckets) <= agent || agent < 0 {
|
2018-02-19 04:26:01 +00:00
|
|
|
return
|
|
|
|
}
|
2019-07-28 04:01:33 +00:00
|
|
|
co.agentBuckets[agent].Lock()
|
|
|
|
co.agentBuckets[agent].counter++
|
|
|
|
co.agentBuckets[agent].Unlock()
|
2018-02-19 04:26:01 +00:00
|
|
|
}
|