* stats: refactor

This commit is contained in:
Simon Zolin 2019-10-07 15:55:09 +03:00
parent 652816ceb1
commit 4165688f25

View file

@ -455,6 +455,41 @@ func (s *statsCtx) Update(e Entry) {
s.unitLock.Unlock()
}
func (s *statsCtx) loadUnits(lastID uint32) []*unitDB {
tx := s.beginTxn(false)
if tx == nil {
return nil
}
units := []*unitDB{} //per-hour units
firstID := lastID - s.limit + 1
for i := firstID; i != lastID; i++ {
u := s.loadUnitFromDB(tx, i)
if u == nil {
u = &unitDB{}
u.NResult = make([]uint64, rLast)
}
units = append(units, u)
}
_ = tx.Rollback()
s.unitLock.Lock()
cu := serialize(s.unit)
cuID := s.unit.id
s.unitLock.Unlock()
if cuID != lastID {
units = units[1:]
}
units = append(units, cu)
if len(units) != int(s.limit) {
log.Fatalf("len(units) != s.limit: %d %d", len(units), s.limit)
}
return units
}
/* Algorithm:
. Prepare array of N units, where N is the value of "limit" configuration setting
. Load data for the most recent units from file
@ -486,36 +521,11 @@ func (s *statsCtx) Update(e Entry) {
func (s *statsCtx) getData(timeUnit TimeUnit) map[string]interface{} {
d := map[string]interface{}{}
tx := s.beginTxn(false)
if tx == nil {
return nil
}
units := []*unitDB{} //per-hour units
lastID := s.conf.UnitID()
firstID := lastID - s.limit + 1
for i := firstID; i != lastID; i++ {
u := s.loadUnitFromDB(tx, i)
if u == nil {
u = &unitDB{}
u.NResult = make([]uint64, rLast)
}
units = append(units, u)
}
_ = tx.Rollback()
s.unitLock.Lock()
cu := serialize(s.unit)
cuID := s.unit.id
s.unitLock.Unlock()
if cuID != lastID {
units = units[1:]
}
units = append(units, cu)
if len(units) != int(s.limit) {
log.Fatalf("len(units) != s.limit: %d %d", len(units), s.limit)
units := s.loadUnits(lastID)
if units == nil {
return nil
}
// per time unit counters: