1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: prevent large difference between aggregated and recomputed summaries (resolve #354)

This commit is contained in:
Ferdinand Mütsch 2022-04-18 16:06:32 +02:00
parent 5276f68918
commit 91c765202c
2 changed files with 19 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package services
import (
"github.com/duke-git/lancet/v2/mathutil"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"time"
@ -59,13 +60,26 @@ func (srv *DurationService) Get(from, to time.Time, user *models.User, filters *
continue
}
dur := d1.Time.T().Sub(latest.Time.T().Add(latest.Duration))
if dur > HeartbeatDiffThreshold {
dur = HeartbeatDiffThreshold
sameDay := d1.Time.T().Day() == latest.Time.T().Day()
dur := time.Duration(mathutil.Min(
int64(d1.Time.T().Sub(latest.Time.T().Add(latest.Duration))),
int64(HeartbeatDiffThreshold),
))
// skip heartbeats that span across two adjacent summaries (assuming there are no more than 1 summary per day)
// this is relevant to prevent the time difference between generating summaries from raw heartbeats and aggregating pre-generated summaries
// for the latter case, the very last heartbeat of a day won't be counted, so we don't want to count it here either
// another option would be to adapt the Summarize() method to always append up to HeartbeatDiffThreshold seconds to a day's very last duration
if !sameDay {
dur = 0
}
latest.Duration += dur
if dur >= HeartbeatDiffThreshold || latest.GroupHash != d1.GroupHash {
// start new "group" if:
// (a) heartbeats were too far apart each other,
// (b) if they are of a different entity or,
// (c) if they span across two days
if dur >= HeartbeatDiffThreshold || latest.GroupHash != d1.GroupHash || !sameDay {
list := mapping[d1.GroupHash]
if d0 := list[len(list)-1]; d0 != d1 {
mapping[d1.GroupHash] = append(mapping[d1.GroupHash], d1)

View File

@ -390,7 +390,7 @@ func (srv *SummaryService) getMissingIntervals(from, to time.Time, summaries []*
// Between
for i := 0; i < len(summaries)-1; i++ {
t1, t2 := summaries[i].ToTime.T(), summaries[i+1].FromTime.T()
if t1.Equal(t2) || t1.Equal(to) || t1.After(to) { // TODO: check after
if t1.Equal(t2) || t1.Equal(to) || t1.After(to) {
continue
}