* stats: refactor

This commit is contained in:
Simon Zolin 2019-10-07 15:55:09 +03:00
parent 652816ceb1
commit 4165688f25
1 changed files with 38 additions and 28 deletions

View File

@ -455,6 +455,41 @@ func (s *statsCtx) Update(e Entry) {
s.unitLock.Unlock() 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: /* Algorithm:
. Prepare array of N units, where N is the value of "limit" configuration setting . Prepare array of N units, where N is the value of "limit" configuration setting
. Load data for the most recent units from file . 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{} { func (s *statsCtx) getData(timeUnit TimeUnit) map[string]interface{} {
d := 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() lastID := s.conf.UnitID()
firstID := lastID - s.limit + 1 firstID := lastID - s.limit + 1
for i := firstID; i != lastID; i++ { units := s.loadUnits(lastID)
u := s.loadUnitFromDB(tx, i) if units == nil {
if u == nil { return 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)
} }
// per time unit counters: // per time unit counters: