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

feat: add wakatime-compatible alltime endpoint

This commit is contained in:
Ferdinand Mütsch
2020-09-06 12:15:46 +02:00
parent 97cb29ee4d
commit 587ac6a330
9 changed files with 216 additions and 64 deletions

View File

@ -13,6 +13,15 @@ const (
SummaryMachine uint8 = 4
)
const (
IntervalToday string = "today"
IntervalLastDay string = "day"
IntervalLastWeek string = "week"
IntervalLastMonth string = "month"
IntervalLastYear string = "year"
IntervalAny string = "any"
)
const UnknownSummaryKey = "unknown"
type Summary struct {
@ -48,6 +57,31 @@ type SummaryViewModel struct {
ApiKey string
}
type SummaryParams struct {
From time.Time
To time.Time
User *User
Recompute bool
}
func SummaryTypes() []uint8 {
return []uint8{SummaryProject, SummaryLanguage, SummaryEditor, SummaryOS, SummaryMachine}
}
func (s *Summary) Types() []uint8 {
return SummaryTypes()
}
func (s *Summary) MappedItems() map[uint8]*[]*SummaryItem {
return map[uint8]*[]*SummaryItem{
SummaryProject: &s.Projects,
SummaryLanguage: &s.Languages,
SummaryEditor: &s.Editors,
SummaryOS: &s.OperatingSystems,
SummaryMachine: &s.Machines,
}
}
/* Augments the summary in a way that at least one item is present for every type.
If a summary has zero items for a given type, but one or more for any of the other types,
the total summary duration can be derived from those and inserted as a dummy-item with key "unknown"
@ -60,22 +94,13 @@ To avoid having to modify persisted data retrospectively, i.e. inserting a dummy
such is generated dynamically here, considering the "machine" for all old heartbeats "unknown".
*/
func (s *Summary) FillUnknown() {
types := []uint8{SummaryProject, SummaryLanguage, SummaryEditor, SummaryOS, SummaryMachine}
types := s.Types()
typeItems := s.MappedItems()
missingTypes := make([]uint8, 0)
typeItems := map[uint8]*[]*SummaryItem{
SummaryProject: &s.Projects,
SummaryLanguage: &s.Languages,
SummaryEditor: &s.Editors,
SummaryOS: &s.OperatingSystems,
SummaryMachine: &s.Machines,
}
var somePresentType uint8
for _, t := range types {
if len(*typeItems[t]) == 0 {
missingTypes = append(missingTypes, t)
} else {
somePresentType = t
}
}
@ -84,11 +109,7 @@ func (s *Summary) FillUnknown() {
return
}
// calculate total duration from any of the present sets of items
var timeSum time.Duration
for _, item := range *typeItems[somePresentType] {
timeSum += item.Total
}
timeSum := s.TotalTime()
// construct dummy item for all missing types
for _, t := range missingTypes {
@ -99,3 +120,21 @@ func (s *Summary) FillUnknown() {
})
}
}
func (s *Summary) TotalTime() time.Duration {
var timeSum time.Duration
mappedItems := s.MappedItems()
// calculate total duration from any of the present sets of items
for _, t := range s.Types() {
if items := mappedItems[t]; len(*items) > 0 {
for _, item := range *items {
timeSum += item.Total
}
break
}
}
return timeSum
}