2019-05-19 20:49:27 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-29 23:03:01 +03:00
|
|
|
NSummaryTypes uint8 = 99
|
2019-05-19 20:49:27 +03:00
|
|
|
SummaryProject uint8 = 0
|
|
|
|
SummaryLanguage uint8 = 1
|
|
|
|
SummaryEditor uint8 = 2
|
|
|
|
SummaryOS uint8 = 3
|
2020-08-29 23:03:01 +03:00
|
|
|
SummaryMachine uint8 = 4
|
2019-05-19 20:49:27 +03:00
|
|
|
)
|
|
|
|
|
2020-08-30 00:16:21 +03:00
|
|
|
const UnknownSummaryKey = "unknown"
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
type Summary struct {
|
2019-10-11 09:00:02 +03:00
|
|
|
ID uint `json:"-" gorm:"primary_key"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"`
|
2020-08-30 02:42:00 +03:00
|
|
|
FromTime time.Time `json:"from" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"`
|
|
|
|
ToTime time.Time `json:"to" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"`
|
2019-10-11 09:00:02 +03:00
|
|
|
Projects []*SummaryItem `json:"projects"`
|
|
|
|
Languages []*SummaryItem `json:"languages"`
|
|
|
|
Editors []*SummaryItem `json:"editors"`
|
|
|
|
OperatingSystems []*SummaryItem `json:"operating_systems"`
|
2020-08-29 23:03:01 +03:00
|
|
|
Machines []*SummaryItem `json:"machines"`
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type SummaryItem struct {
|
2019-10-10 00:26:28 +03:00
|
|
|
ID uint `json:"-" gorm:"primary_key"`
|
|
|
|
SummaryID uint `json:"-"`
|
2019-10-10 19:32:17 +03:00
|
|
|
Type uint8 `json:"-"`
|
2019-10-10 00:26:28 +03:00
|
|
|
Key string `json:"key"`
|
|
|
|
Total time.Duration `json:"total"`
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
2019-05-19 22:00:19 +03:00
|
|
|
|
|
|
|
type SummaryItemContainer struct {
|
|
|
|
Type uint8
|
2019-10-11 09:00:02 +03:00
|
|
|
Items []*SummaryItem
|
2019-05-19 22:00:19 +03:00
|
|
|
}
|
2020-03-31 12:24:44 +03:00
|
|
|
|
|
|
|
type SummaryViewModel struct {
|
|
|
|
*Summary
|
|
|
|
LanguageColors map[string]string
|
2020-05-24 14:41:19 +03:00
|
|
|
Error string
|
2020-05-24 17:34:32 +03:00
|
|
|
Success string
|
2020-05-24 22:19:05 +03:00
|
|
|
ApiKey string
|
2020-03-31 12:24:44 +03:00
|
|
|
}
|
2020-08-30 00:16:21 +03:00
|
|
|
|
|
|
|
/* 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"
|
|
|
|
for the missing type.
|
|
|
|
For instance, the machine type was introduced post hoc. Accordingly, no "machine"-information is present in
|
|
|
|
the data for old heartbeats and summaries. If a user has two years of data without machine information and
|
|
|
|
one day with such, a "machine"-chart plotted from that data will reference a way smaller absolute total amount
|
|
|
|
of time than the other ones.
|
|
|
|
To avoid having to modify persisted data retrospectively, i.e. inserting a dummy SummaryItem for the new type,
|
|
|
|
such is generated dynamically here, considering the "machine" for all old heartbeats "unknown".
|
|
|
|
*/
|
|
|
|
func (s *Summary) FillUnknown() {
|
|
|
|
types := []uint8{SummaryProject, SummaryLanguage, SummaryEditor, SummaryOS, SummaryMachine}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't proceed if entire summary is empty
|
|
|
|
if len(missingTypes) == len(types) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate total duration from any of the present sets of items
|
|
|
|
var timeSum time.Duration
|
|
|
|
for _, item := range *typeItems[somePresentType] {
|
|
|
|
timeSum += item.Total
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct dummy item for all missing types
|
|
|
|
for _, t := range missingTypes {
|
|
|
|
*typeItems[t] = append(*typeItems[t], &SummaryItem{
|
|
|
|
Type: t,
|
|
|
|
Key: UnknownSummaryKey,
|
|
|
|
Total: timeSum,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|