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

fix: make summary include timestamp of first and last heartbeat (resolve #18)

This commit is contained in:
Ferdinand Mütsch 2020-08-23 13:10:58 +02:00
parent 7818f6b094
commit ada0863f7c
3 changed files with 21 additions and 5 deletions

View File

@ -96,10 +96,25 @@ func (srv *SummaryService) Construct(from, to time.Time, user *models.User, reco
} }
close(c) close(c)
realFrom, realTo := from, to
if len(existingSummaries) > 0 {
realFrom = existingSummaries[0].FromTime
realTo = existingSummaries[len(existingSummaries)-1].ToTime
}
if len(heartbeats) > 0 {
t1, t2 := time.Time(heartbeats[0].Time), time.Time(heartbeats[len(heartbeats)-1].Time)
if t1.After(realFrom) && t1.Before(time.Date(realFrom.Year(), realFrom.Month(), realFrom.Day()+1, 0, 0, 0, 0, realFrom.Location())) {
realFrom = t1
}
if t2.Before(realTo) && t2.After(time.Date(realTo.Year(), realTo.Month(), realTo.Day()-1, 0, 0, 0, 0, realTo.Location())) {
realTo = t2
}
}
aggregatedSummary := &models.Summary{ aggregatedSummary := &models.Summary{
UserID: user.ID, UserID: user.ID,
FromTime: from, FromTime: realFrom,
ToTime: to, ToTime: realTo,
Projects: projectItems, Projects: projectItems,
Languages: languageItems, Languages: languageItems,
Editors: editorItems, Editors: editorItems,
@ -134,6 +149,7 @@ func (srv *SummaryService) GetByUserWithin(user *models.User, from, to time.Time
Where(&models.Summary{UserID: user.ID}). Where(&models.Summary{UserID: user.ID}).
Where("from_time >= ?", from). Where("from_time >= ?", from).
Where("to_time <= ?", to). Where("to_time <= ?", to).
Order("from_time asc").
Preload("Projects", "type = ?", models.SummaryProject). Preload("Projects", "type = ?", models.SummaryProject).
Preload("Languages", "type = ?", models.SummaryLanguage). Preload("Languages", "type = ?", models.SummaryLanguage).
Preload("Editors", "type = ?", models.SummaryEditor). Preload("Editors", "type = ?", models.SummaryEditor).

View File

@ -15,12 +15,12 @@ func StartOfWeek() time.Time {
func StartOfMonth() time.Time { func StartOfMonth() time.Time {
ref := time.Now() ref := time.Now()
return time.Date(ref.Year(), ref.Month(), 0, 0, 0, 0, 0, ref.Location()) return time.Date(ref.Year(), ref.Month(), 1, 0, 0, 0, 0, ref.Location())
} }
func StartOfYear() time.Time { func StartOfYear() time.Time {
ref := time.Now() ref := time.Now()
return time.Date(ref.Year(), time.January, 0, 0, 0, 0, 0, ref.Location()) return time.Date(ref.Year(), time.January, 1, 0, 0, 0, 0, ref.Location())
} }
// https://stackoverflow.com/a/18632496 // https://stackoverflow.com/a/18632496

View File

@ -1 +1 @@
1.7.2 1.7.3