mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
feat: total heartbeats per summary (resolve #283)
This commit is contained in:
@@ -57,6 +57,8 @@ func (srv *DurationService) Get(from, to time.Time, user *models.User) (models.D
|
||||
mapping[d1.GroupHash] = append(mapping[d1.GroupHash], d1)
|
||||
}
|
||||
latest = d1
|
||||
} else {
|
||||
latest.NumHeartbeats++
|
||||
}
|
||||
|
||||
count++
|
||||
|
||||
@@ -140,6 +140,7 @@ func (suite *DurationServiceTestSuite) TestDurationService_Get() {
|
||||
assert.Nil(suite.T(), err)
|
||||
assert.Len(suite.T(), durations, 1)
|
||||
assert.Equal(suite.T(), HeartbeatDiffThreshold, durations.First().Duration)
|
||||
assert.Equal(suite.T(), 1, durations.First().NumHeartbeats)
|
||||
|
||||
/* TEST 3 */
|
||||
from, to = suite.TestStartTime, suite.TestStartTime.Add(1*time.Hour)
|
||||
@@ -155,6 +156,9 @@ func (suite *DurationServiceTestSuite) TestDurationService_Get() {
|
||||
assert.Equal(suite.T(), TestEditorGoland, durations[0].Editor)
|
||||
assert.Equal(suite.T(), TestEditorGoland, durations[1].Editor)
|
||||
assert.Equal(suite.T(), TestEditorVscode, durations[2].Editor)
|
||||
assert.Equal(suite.T(), 2, durations[0].NumHeartbeats)
|
||||
assert.Equal(suite.T(), 1, durations[1].NumHeartbeats)
|
||||
assert.Equal(suite.T(), 3, durations[2].NumHeartbeats)
|
||||
}
|
||||
|
||||
func filterHeartbeats(from, to time.Time, heartbeats []*models.Heartbeat) []*models.Heartbeat {
|
||||
|
||||
@@ -117,10 +117,8 @@ func (srv *SummaryService) Retrieve(from, to time.Time, user *models.User) (*mod
|
||||
|
||||
func (srv *SummaryService) Summarize(from, to time.Time, user *models.User) (*models.Summary, error) {
|
||||
// Initialize and fetch data
|
||||
var durations models.Durations
|
||||
if result, err := srv.durationService.Get(from, to, user); err == nil {
|
||||
durations = result
|
||||
} else {
|
||||
durations, err := srv.durationService.Get(from, to, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -169,6 +167,7 @@ func (srv *SummaryService) Summarize(from, to time.Time, user *models.User) (*mo
|
||||
Editors: editorItems,
|
||||
OperatingSystems: osItems,
|
||||
Machines: machineItems,
|
||||
NumHeartbeats: durations.TotalNumHeartbeats(),
|
||||
}
|
||||
|
||||
return summary.Sorted(), nil
|
||||
@@ -303,6 +302,7 @@ func (srv *SummaryService) mergeSummaries(summaries []*models.Summary) (*models.
|
||||
finalSummary.OperatingSystems = srv.mergeSummaryItems(finalSummary.OperatingSystems, s.OperatingSystems)
|
||||
finalSummary.Machines = srv.mergeSummaryItems(finalSummary.Machines, s.Machines)
|
||||
finalSummary.Labels = srv.mergeSummaryItems(finalSummary.Labels, s.Labels)
|
||||
finalSummary.NumHeartbeats += s.NumHeartbeats
|
||||
|
||||
processed[hash] = true
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ func (suite *SummaryServiceTestSuite) SetupSuite() {
|
||||
Machine: TestMachine1,
|
||||
Time: models.CustomTime(suite.TestStartTime),
|
||||
Duration: 150 * time.Second,
|
||||
NumHeartbeats: 2,
|
||||
},
|
||||
{
|
||||
UserID: TestUserId,
|
||||
@@ -54,6 +55,7 @@ func (suite *SummaryServiceTestSuite) SetupSuite() {
|
||||
Machine: TestMachine1,
|
||||
Time: models.CustomTime(suite.TestStartTime.Add((30 + 130) * time.Second)),
|
||||
Duration: 20 * time.Second,
|
||||
NumHeartbeats: 1,
|
||||
},
|
||||
{
|
||||
UserID: TestUserId,
|
||||
@@ -64,6 +66,7 @@ func (suite *SummaryServiceTestSuite) SetupSuite() {
|
||||
Machine: TestMachine1,
|
||||
Time: models.CustomTime(suite.TestStartTime.Add(3 * time.Minute)),
|
||||
Duration: 15 * time.Second,
|
||||
NumHeartbeats: 3,
|
||||
},
|
||||
}
|
||||
suite.TestLabels = []*models.ProjectLabel{
|
||||
@@ -114,6 +117,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Summarize() {
|
||||
assert.Equal(suite.T(), from, result.FromTime.T())
|
||||
assert.Equal(suite.T(), to, result.ToTime.T())
|
||||
assert.Zero(suite.T(), result.TotalTime())
|
||||
assert.Zero(suite.T(), result.NumHeartbeats)
|
||||
assert.Empty(suite.T(), result.Projects)
|
||||
|
||||
/* TEST 2 */
|
||||
@@ -127,6 +131,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Summarize() {
|
||||
assert.Equal(suite.T(), suite.TestDurations[0].Time.T(), result.FromTime.T())
|
||||
assert.Equal(suite.T(), suite.TestDurations[0].Time.T(), result.ToTime.T())
|
||||
assert.Equal(suite.T(), 150*time.Second, result.TotalTime())
|
||||
assert.Equal(suite.T(), 2, result.NumHeartbeats)
|
||||
assertNumAllItems(suite.T(), 1, result, "")
|
||||
|
||||
/* TEST 3 */
|
||||
@@ -142,6 +147,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Summarize() {
|
||||
assert.Equal(suite.T(), 185*time.Second, result.TotalTime())
|
||||
assert.Equal(suite.T(), 170*time.Second, result.TotalTimeByKey(models.SummaryEditor, TestEditorGoland))
|
||||
assert.Equal(suite.T(), 15*time.Second, result.TotalTimeByKey(models.SummaryEditor, TestEditorVscode))
|
||||
assert.Equal(suite.T(), 6, result.NumHeartbeats)
|
||||
assert.Len(suite.T(), result.Editors, 2)
|
||||
assertNumAllItems(suite.T(), 1, result, "e")
|
||||
}
|
||||
@@ -176,6 +182,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
Editors: []*models.SummaryItem{},
|
||||
OperatingSystems: []*models.SummaryItem{},
|
||||
Machines: []*models.SummaryItem{},
|
||||
NumHeartbeats: 100,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -189,6 +196,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
assert.NotNil(suite.T(), result)
|
||||
assert.Len(suite.T(), result.Projects, 1)
|
||||
assert.Equal(suite.T(), summaries[0].Projects[0].Total*time.Second, result.TotalTime())
|
||||
assert.Equal(suite.T(), 100, result.NumHeartbeats)
|
||||
suite.DurationService.AssertNumberOfCalls(suite.T(), "Get", 2)
|
||||
|
||||
/* TEST 2 */
|
||||
@@ -210,6 +218,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
Editors: []*models.SummaryItem{},
|
||||
OperatingSystems: []*models.SummaryItem{},
|
||||
Machines: []*models.SummaryItem{},
|
||||
NumHeartbeats: 100,
|
||||
},
|
||||
{
|
||||
ID: uint(rand.Uint32()),
|
||||
@@ -227,6 +236,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
Editors: []*models.SummaryItem{},
|
||||
OperatingSystems: []*models.SummaryItem{},
|
||||
Machines: []*models.SummaryItem{},
|
||||
NumHeartbeats: 100,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -241,6 +251,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
assert.Equal(suite.T(), 185*time.Second+90*time.Minute, result.TotalTime())
|
||||
assert.Equal(suite.T(), 185*time.Second+45*time.Minute, result.TotalTimeByKey(models.SummaryProject, TestProject1))
|
||||
assert.Equal(suite.T(), 45*time.Minute, result.TotalTimeByKey(models.SummaryProject, TestProject2))
|
||||
assert.Equal(suite.T(), 206, result.NumHeartbeats)
|
||||
suite.DurationService.AssertNumberOfCalls(suite.T(), "Get", 2+1)
|
||||
|
||||
/* TEST 3 */
|
||||
@@ -263,6 +274,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
Editors: []*models.SummaryItem{},
|
||||
OperatingSystems: []*models.SummaryItem{},
|
||||
Machines: []*models.SummaryItem{},
|
||||
NumHeartbeats: 100,
|
||||
},
|
||||
{
|
||||
ID: uint(rand.Uint32()),
|
||||
@@ -280,6 +292,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
Editors: []*models.SummaryItem{},
|
||||
OperatingSystems: []*models.SummaryItem{},
|
||||
Machines: []*models.SummaryItem{},
|
||||
NumHeartbeats: 100,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -294,6 +307,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Retrieve() {
|
||||
assert.Equal(suite.T(), 90*time.Minute, result.TotalTime())
|
||||
assert.Equal(suite.T(), 45*time.Minute, result.TotalTimeByKey(models.SummaryProject, TestProject1))
|
||||
assert.Equal(suite.T(), 45*time.Minute, result.TotalTimeByKey(models.SummaryProject, TestProject2))
|
||||
assert.Equal(suite.T(), 200, result.NumHeartbeats)
|
||||
suite.DurationService.AssertNumberOfCalls(suite.T(), "Get", 2+1+1)
|
||||
}
|
||||
|
||||
@@ -384,6 +398,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Aliased() {
|
||||
assert.NotNil(suite.T(), result)
|
||||
assert.Zero(suite.T(), result.TotalTimeByKey(models.SummaryProject, TestProject1))
|
||||
assert.NotZero(suite.T(), result.TotalTimeByKey(models.SummaryProject, TestProject2))
|
||||
assert.Equal(suite.T(), 6, result.NumHeartbeats)
|
||||
}
|
||||
|
||||
func (suite *SummaryServiceTestSuite) TestSummaryService_Aliased_ProjectLabels() {
|
||||
@@ -422,6 +437,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Aliased_ProjectLabels()
|
||||
assert.Nil(suite.T(), err)
|
||||
assert.NotNil(suite.T(), result)
|
||||
assert.Equal(suite.T(), 195*time.Second, result.TotalTimeByKey(models.SummaryLabel, TestProjectLabel1))
|
||||
assert.Equal(suite.T(), 6, result.NumHeartbeats)
|
||||
}
|
||||
|
||||
func filterDurations(from, to time.Time, durations models.Durations) models.Durations {
|
||||
|
||||
Reference in New Issue
Block a user