mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
feat: basic implementation of branch statistics
This commit is contained in:
@@ -25,6 +25,8 @@ const (
|
||||
TestOsWin = "Windows"
|
||||
TestMachine1 = "muety-desktop"
|
||||
TestMachine2 = "muety-work"
|
||||
TestBranchMaster = "master"
|
||||
TestBranchDev = "dev"
|
||||
MinUnixTime1 = 1601510400000 * 1e6
|
||||
)
|
||||
|
||||
|
||||
@@ -214,6 +214,7 @@ func (srv *HeartbeatService) updateEntityUserCacheByHeartbeat(hb *models.Heartbe
|
||||
go srv.updateEntityUserCache(models.SummaryEditor, hb.Editor, hb.User)
|
||||
go srv.updateEntityUserCache(models.SummaryOS, hb.OperatingSystem, hb.User)
|
||||
go srv.updateEntityUserCache(models.SummaryMachine, hb.Machine, hb.User)
|
||||
go srv.updateEntityUserCache(models.SummaryBranch, hb.Branch, hb.User)
|
||||
}
|
||||
|
||||
func (srv *HeartbeatService) notifyBatch(heartbeats []*models.Heartbeat) {
|
||||
|
||||
@@ -90,6 +90,10 @@ func (srv *SummaryService) Aliased(from, to time.Time, user *models.User, f Summ
|
||||
summary.FillBy(models.SummaryProject, models.SummaryLabel) // first fill up labels from projects
|
||||
summary.FillMissing() // then, full up types which are entirely missing
|
||||
|
||||
if withBranches := filters != nil && filters.Project != nil && filters.Project.Exists(); !withBranches {
|
||||
summary.Branches = nil
|
||||
}
|
||||
|
||||
srv.cache.SetDefault(cacheKey, summary)
|
||||
return summary.Sorted(), nil
|
||||
}
|
||||
@@ -134,7 +138,10 @@ func (srv *SummaryService) Summarize(from, to time.Time, user *models.User, filt
|
||||
return nil, err
|
||||
}
|
||||
|
||||
types := models.NativeSummaryTypes()
|
||||
types := models.PersistedSummaryTypes()
|
||||
if filters != nil && filters.Project != nil && filters.Project.Exists() {
|
||||
types = append(types, models.SummaryBranch)
|
||||
}
|
||||
|
||||
typedAggregations := make(chan models.SummaryItemContainer)
|
||||
defer close(typedAggregations)
|
||||
@@ -148,6 +155,7 @@ func (srv *SummaryService) Summarize(from, to time.Time, user *models.User, filt
|
||||
var editorItems []*models.SummaryItem
|
||||
var osItems []*models.SummaryItem
|
||||
var machineItems []*models.SummaryItem
|
||||
var branchItems []*models.SummaryItem
|
||||
|
||||
for i := 0; i < len(types); i++ {
|
||||
item := <-typedAggregations
|
||||
@@ -162,6 +170,8 @@ func (srv *SummaryService) Summarize(from, to time.Time, user *models.User, filt
|
||||
osItems = item.Items
|
||||
case models.SummaryMachine:
|
||||
machineItems = item.Items
|
||||
case models.SummaryBranch:
|
||||
branchItems = item.Items
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,6 +189,7 @@ func (srv *SummaryService) Summarize(from, to time.Time, user *models.User, filt
|
||||
Editors: editorItems,
|
||||
OperatingSystems: osItems,
|
||||
Machines: machineItems,
|
||||
Branches: branchItems,
|
||||
NumHeartbeats: durations.TotalNumHeartbeats(),
|
||||
}
|
||||
|
||||
@@ -285,6 +296,7 @@ func (srv *SummaryService) mergeSummaries(summaries []*models.Summary) (*models.
|
||||
OperatingSystems: make([]*models.SummaryItem, 0),
|
||||
Machines: make([]*models.SummaryItem, 0),
|
||||
Labels: make([]*models.SummaryItem, 0),
|
||||
Branches: make([]*models.SummaryItem, 0),
|
||||
}
|
||||
|
||||
var processed = map[time.Time]bool{}
|
||||
@@ -314,6 +326,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.Branches = srv.mergeSummaryItems(finalSummary.Branches, s.Branches)
|
||||
finalSummary.NumHeartbeats += s.NumHeartbeats
|
||||
|
||||
processed[hash] = true
|
||||
|
||||
@@ -42,6 +42,7 @@ func (suite *SummaryServiceTestSuite) SetupSuite() {
|
||||
Editor: TestEditorGoland,
|
||||
OperatingSystem: TestOsLinux,
|
||||
Machine: TestMachine1,
|
||||
Branch: TestBranchMaster,
|
||||
Time: models.CustomTime(suite.TestStartTime),
|
||||
Duration: 150 * time.Second,
|
||||
NumHeartbeats: 2,
|
||||
@@ -53,6 +54,7 @@ func (suite *SummaryServiceTestSuite) SetupSuite() {
|
||||
Editor: TestEditorGoland,
|
||||
OperatingSystem: TestOsLinux,
|
||||
Machine: TestMachine1,
|
||||
Branch: TestBranchMaster,
|
||||
Time: models.CustomTime(suite.TestStartTime.Add((30 + 130) * time.Second)),
|
||||
Duration: 20 * time.Second,
|
||||
NumHeartbeats: 1,
|
||||
@@ -64,6 +66,7 @@ func (suite *SummaryServiceTestSuite) SetupSuite() {
|
||||
Editor: TestEditorVscode,
|
||||
OperatingSystem: TestOsLinux,
|
||||
Machine: TestMachine1,
|
||||
Branch: TestBranchDev,
|
||||
Time: models.CustomTime(suite.TestStartTime.Add(3 * time.Minute)),
|
||||
Duration: 15 * time.Second,
|
||||
NumHeartbeats: 3,
|
||||
@@ -145,6 +148,13 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Summarize() {
|
||||
assert.Equal(suite.T(), suite.TestDurations[0].Time.T(), result.FromTime.T())
|
||||
assert.Equal(suite.T(), suite.TestDurations[len(suite.TestDurations)-1].Time.T(), result.ToTime.T())
|
||||
assert.Equal(suite.T(), 185*time.Second, result.TotalTime())
|
||||
assert.Equal(suite.T(), 185*time.Second, result.TotalTimeBy(models.SummaryProject))
|
||||
assert.Equal(suite.T(), 185*time.Second, result.TotalTimeBy(models.SummaryOS))
|
||||
assert.Equal(suite.T(), 185*time.Second, result.TotalTimeBy(models.SummaryMachine))
|
||||
assert.Equal(suite.T(), 185*time.Second, result.TotalTimeBy(models.SummaryLanguage))
|
||||
assert.Equal(suite.T(), 185*time.Second, result.TotalTimeBy(models.SummaryEditor))
|
||||
assert.Zero(suite.T(), result.TotalTimeBy(models.SummaryBranch)) // no filters -> no branches contained
|
||||
assert.Zero(suite.T(), result.TotalTimeBy(models.SummaryLabel))
|
||||
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)
|
||||
@@ -400,6 +410,7 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Aliased() {
|
||||
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)
|
||||
assert.Nil(suite.T(), result.Branches)
|
||||
}
|
||||
|
||||
func (suite *SummaryServiceTestSuite) TestSummaryService_Aliased_ProjectLabels() {
|
||||
@@ -464,7 +475,8 @@ func (suite *SummaryServiceTestSuite) TestSummaryService_Filters() {
|
||||
suite.TestLabels[1].Label: suite.TestLabels[1:2],
|
||||
}, nil).Once()
|
||||
|
||||
sut.Aliased(from, to, suite.TestUser, sut.Summarize, filters, false)
|
||||
result, _ := sut.Aliased(from, to, suite.TestUser, sut.Summarize, filters, false)
|
||||
assert.NotNil(suite.T(), result.Branches) // project filters were applied -> include branches
|
||||
|
||||
effectiveFilters := suite.DurationService.Calls[0].Arguments[3].(*models.Filters)
|
||||
assert.Contains(suite.T(), effectiveFilters.Project, TestProject1) // because actually requested
|
||||
|
||||
Reference in New Issue
Block a user