2019-05-19 20:49:27 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-06-11 21:59:34 +03:00
|
|
|
"errors"
|
2020-11-22 00:30:56 +03:00
|
|
|
"sort"
|
2019-05-19 20:49:27 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-29 23:03:01 +03:00
|
|
|
NSummaryTypes uint8 = 99
|
2022-03-13 10:17:50 +03:00
|
|
|
SummaryUnknown uint8 = 98
|
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
|
2021-06-11 21:59:34 +03:00
|
|
|
SummaryLabel uint8 = 5
|
2022-01-02 15:39:20 +03:00
|
|
|
SummaryBranch uint8 = 6
|
2019-05-19 20:49:27 +03:00
|
|
|
)
|
|
|
|
|
2020-08-30 00:16:21 +03:00
|
|
|
const UnknownSummaryKey = "unknown"
|
2021-06-11 21:59:34 +03:00
|
|
|
const DefaultProjectLabel = "default"
|
2020-08-30 00:16:21 +03:00
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
type Summary struct {
|
2020-11-22 00:30:56 +03:00
|
|
|
ID uint `json:"-" gorm:"primary_key"`
|
|
|
|
User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
|
|
|
UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"`
|
2021-02-07 13:54:07 +03:00
|
|
|
FromTime CustomTime `json:"from" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
|
|
|
|
ToTime CustomTime `json:"to" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
|
2020-11-22 00:30:56 +03:00
|
|
|
Projects SummaryItems `json:"projects" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
|
|
|
Languages SummaryItems `json:"languages" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
|
|
|
Editors SummaryItems `json:"editors" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
|
|
|
OperatingSystems SummaryItems `json:"operating_systems" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
|
|
|
Machines SummaryItems `json:"machines" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
2022-01-02 15:39:20 +03:00
|
|
|
Labels SummaryItems `json:"labels" gorm:"-"` // labels are not persisted, but calculated at runtime, i.e. when summary is retrieved
|
|
|
|
Branches SummaryItems `json:"branches" gorm:"-"` // branches are not persisted, but calculated at runtime in case a project filter is applied
|
2021-12-22 17:44:37 +03:00
|
|
|
NumHeartbeats int `json:"-" gorm:"default:0"`
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-22 00:30:56 +03:00
|
|
|
type SummaryItems []*SummaryItem
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
type SummaryItem struct {
|
2021-12-15 12:50:16 +03:00
|
|
|
ID uint64 `json:"-" gorm:"primary_key"`
|
2020-11-06 19:09:41 +03:00
|
|
|
Summary *Summary `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
2019-10-10 00:26:28 +03:00
|
|
|
SummaryID uint `json:"-"`
|
2021-03-24 23:49:42 +03:00
|
|
|
Type uint8 `json:"-" gorm:"index:idx_type"`
|
2019-10-10 00:26:28 +03:00
|
|
|
Key string `json:"key"`
|
2021-02-07 13:54:07 +03:00
|
|
|
Total time.Duration `json:"total" swaggertype:"primitive,integer"`
|
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
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
type SummaryParams struct {
|
|
|
|
From time.Time
|
|
|
|
To time.Time
|
|
|
|
User *User
|
2022-01-02 22:04:29 +03:00
|
|
|
Filters *Filters
|
2020-09-06 13:15:46 +03:00
|
|
|
Recompute bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func SummaryTypes() []uint8 {
|
2022-01-02 15:39:20 +03:00
|
|
|
return []uint8{SummaryProject, SummaryLanguage, SummaryEditor, SummaryOS, SummaryMachine, SummaryLabel, SummaryBranch}
|
2021-06-11 21:59:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NativeSummaryTypes() []uint8 {
|
2022-01-02 15:39:20 +03:00
|
|
|
return []uint8{SummaryProject, SummaryLanguage, SummaryEditor, SummaryOS, SummaryMachine, SummaryBranch}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PersistedSummaryTypes() []uint8 {
|
2020-09-06 13:15:46 +03:00
|
|
|
return []uint8{SummaryProject, SummaryLanguage, SummaryEditor, SummaryOS, SummaryMachine}
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:30:56 +03:00
|
|
|
func (s *Summary) Sorted() *Summary {
|
|
|
|
sort.Sort(sort.Reverse(s.Projects))
|
|
|
|
sort.Sort(sort.Reverse(s.Machines))
|
|
|
|
sort.Sort(sort.Reverse(s.OperatingSystems))
|
|
|
|
sort.Sort(sort.Reverse(s.Languages))
|
|
|
|
sort.Sort(sort.Reverse(s.Editors))
|
2021-06-11 21:59:34 +03:00
|
|
|
sort.Sort(sort.Reverse(s.Labels))
|
2022-01-02 15:39:20 +03:00
|
|
|
sort.Sort(sort.Reverse(s.Branches))
|
2020-11-22 00:30:56 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
func (s *Summary) Types() []uint8 {
|
|
|
|
return SummaryTypes()
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:30:56 +03:00
|
|
|
func (s *Summary) MappedItems() map[uint8]*SummaryItems {
|
|
|
|
return map[uint8]*SummaryItems{
|
2020-09-06 13:15:46 +03:00
|
|
|
SummaryProject: &s.Projects,
|
|
|
|
SummaryLanguage: &s.Languages,
|
|
|
|
SummaryEditor: &s.Editors,
|
|
|
|
SummaryOS: &s.OperatingSystems,
|
|
|
|
SummaryMachine: &s.Machines,
|
2021-06-11 21:59:34 +03:00
|
|
|
SummaryLabel: &s.Labels,
|
2022-01-02 15:39:20 +03:00
|
|
|
SummaryBranch: &s.Branches,
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 01:06:48 +03:00
|
|
|
func (s *Summary) ItemsByType(summaryType uint8) *SummaryItems {
|
2022-03-17 13:08:40 +03:00
|
|
|
switch summaryType {
|
|
|
|
case SummaryProject:
|
|
|
|
return &s.Projects
|
|
|
|
case SummaryLanguage:
|
|
|
|
return &s.Languages
|
|
|
|
case SummaryEditor:
|
|
|
|
return &s.Editors
|
|
|
|
case SummaryOS:
|
|
|
|
return &s.OperatingSystems
|
|
|
|
case SummaryMachine:
|
|
|
|
return &s.Machines
|
|
|
|
case SummaryLabel:
|
|
|
|
return &s.Labels
|
|
|
|
case SummaryBranch:
|
|
|
|
return &s.Branches
|
|
|
|
}
|
|
|
|
return nil
|
2021-02-13 01:06:48 +03:00
|
|
|
}
|
|
|
|
|
2022-03-13 10:17:50 +03:00
|
|
|
func (s *Summary) KeepOnly(types map[uint8]bool) *Summary {
|
|
|
|
if len(types) == 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range SummaryTypes() {
|
|
|
|
if keep, ok := types[t]; !keep || !ok {
|
|
|
|
*s.ItemsByType(t) = []*SummaryItem{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
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".
|
|
|
|
*/
|
2021-06-11 21:59:34 +03:00
|
|
|
func (s *Summary) FillMissing() {
|
2020-09-06 13:15:46 +03:00
|
|
|
types := s.Types()
|
|
|
|
typeItems := s.MappedItems()
|
2020-08-30 00:16:21 +03:00
|
|
|
missingTypes := make([]uint8, 0)
|
|
|
|
|
|
|
|
for _, t := range types {
|
|
|
|
if len(*typeItems[t]) == 0 {
|
|
|
|
missingTypes = append(missingTypes, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// can't proceed if entire summary is empty
|
|
|
|
if len(missingTypes) == len(types) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct dummy item for all missing types
|
2021-06-12 10:12:28 +03:00
|
|
|
presentType, err := s.findFirstPresentType()
|
|
|
|
if err != nil {
|
|
|
|
return // all types are either zero or missing entirely, nothing to fill
|
|
|
|
}
|
2020-08-30 00:16:21 +03:00
|
|
|
for _, t := range missingTypes {
|
2021-06-11 21:59:34 +03:00
|
|
|
s.FillBy(presentType, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// inplace!
|
|
|
|
func (s *Summary) FillBy(fromType uint8, toType uint8) {
|
|
|
|
typeItems := s.MappedItems()
|
2021-06-12 12:09:24 +03:00
|
|
|
totalWanted := s.TotalTimeBy(fromType)
|
|
|
|
totalActual := s.TotalTimeBy(toType)
|
2021-06-11 21:59:34 +03:00
|
|
|
|
|
|
|
key := UnknownSummaryKey
|
|
|
|
if toType == SummaryLabel {
|
|
|
|
key = DefaultProjectLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
existingEntryIdx := -1
|
|
|
|
for i, item := range *typeItems[toType] {
|
|
|
|
if item.Key == key {
|
|
|
|
existingEntryIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-06-12 13:04:38 +03:00
|
|
|
|
|
|
|
total := (totalWanted - totalActual) / time.Second // workaround
|
|
|
|
if total > 0 {
|
|
|
|
if existingEntryIdx >= 0 {
|
|
|
|
(*typeItems[toType])[existingEntryIdx].Total = total
|
|
|
|
} else {
|
|
|
|
*typeItems[toType] = append(*typeItems[toType], &SummaryItem{
|
|
|
|
Type: toType,
|
|
|
|
Key: key,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
2020-08-30 00:16:21 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 13:15:46 +03:00
|
|
|
|
|
|
|
func (s *Summary) TotalTime() time.Duration {
|
|
|
|
var timeSum time.Duration
|
|
|
|
|
|
|
|
mappedItems := s.MappedItems()
|
2021-06-11 21:59:34 +03:00
|
|
|
t, err := s.findFirstPresentType()
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
for _, item := range *mappedItems[t] {
|
|
|
|
timeSum += item.Total
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
|
|
|
|
2020-09-12 00:24:51 +03:00
|
|
|
return timeSum * time.Second
|
2020-09-06 13:15:46 +03:00
|
|
|
}
|
2020-09-11 21:22:33 +03:00
|
|
|
|
2020-11-01 14:50:59 +03:00
|
|
|
func (s *Summary) TotalTimeBy(entityType uint8) (timeSum time.Duration) {
|
2020-09-11 21:22:33 +03:00
|
|
|
mappedItems := s.MappedItems()
|
2020-09-12 00:24:51 +03:00
|
|
|
if items := mappedItems[entityType]; len(*items) > 0 {
|
|
|
|
for _, item := range *items {
|
2020-11-01 14:50:59 +03:00
|
|
|
timeSum = timeSum + item.Total*time.Second
|
2020-09-12 00:24:51 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-01 14:50:59 +03:00
|
|
|
return timeSum
|
2020-09-12 00:24:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-01 14:50:59 +03:00
|
|
|
func (s *Summary) TotalTimeByKey(entityType uint8, key string) (timeSum time.Duration) {
|
2020-09-12 00:24:51 +03:00
|
|
|
mappedItems := s.MappedItems()
|
2020-09-11 21:22:33 +03:00
|
|
|
if items := mappedItems[entityType]; len(*items) > 0 {
|
|
|
|
for _, item := range *items {
|
|
|
|
if item.Key != key {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-01 14:50:59 +03:00
|
|
|
timeSum = timeSum + item.Total*time.Second
|
2020-09-11 21:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-01 14:50:59 +03:00
|
|
|
return timeSum
|
|
|
|
}
|
2020-09-11 21:22:33 +03:00
|
|
|
|
2021-12-26 19:02:14 +03:00
|
|
|
func (s *Summary) TotalTimeByFilter(filter FilterElement) time.Duration {
|
|
|
|
var total time.Duration
|
|
|
|
for _, f := range filter.filter {
|
|
|
|
total += s.TotalTimeByKey(filter.entity, f)
|
2020-11-01 14:50:59 +03:00
|
|
|
}
|
2021-12-26 19:02:14 +03:00
|
|
|
return total
|
2020-09-11 21:22:33 +03:00
|
|
|
}
|
2020-11-07 14:01:35 +03:00
|
|
|
|
2021-12-16 00:46:02 +03:00
|
|
|
func (s *Summary) MaxBy(entityType uint8) *SummaryItem {
|
|
|
|
var max *SummaryItem
|
|
|
|
mappedItems := s.MappedItems()
|
|
|
|
if items := mappedItems[entityType]; len(*items) > 0 {
|
|
|
|
for _, item := range *items {
|
|
|
|
if max == nil || item.Total > max.Total {
|
|
|
|
max = item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Summary) MaxByToString(entityType uint8) string {
|
|
|
|
max := s.MaxBy(entityType)
|
|
|
|
if max == nil {
|
|
|
|
return "-"
|
|
|
|
}
|
|
|
|
return max.Key
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
func (s *Summary) WithResolvedAliases(resolve AliasResolver) *Summary {
|
|
|
|
processAliases := func(origin []*SummaryItem) []*SummaryItem {
|
2022-01-02 15:39:20 +03:00
|
|
|
if origin == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-07 14:01:35 +03:00
|
|
|
target := make([]*SummaryItem, 0)
|
|
|
|
|
|
|
|
findItem := func(key string) *SummaryItem {
|
|
|
|
for _, item := range target {
|
|
|
|
if item.Key == key {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range origin {
|
|
|
|
// Add all "top-level" items, i.e. such without aliases
|
|
|
|
if key := resolve(item.Type, item.Key); key == item.Key {
|
|
|
|
target = append(target, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range origin {
|
|
|
|
// Add all remaining projects and merge with their alias
|
|
|
|
if key := resolve(item.Type, item.Key); key != item.Key {
|
|
|
|
if targetItem := findItem(key); targetItem != nil {
|
|
|
|
targetItem.Total += item.Total
|
|
|
|
} else {
|
|
|
|
target = append(target, &SummaryItem{
|
|
|
|
ID: item.ID,
|
|
|
|
SummaryID: item.SummaryID,
|
|
|
|
Type: item.Type,
|
|
|
|
Key: key,
|
|
|
|
Total: item.Total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve aliases
|
|
|
|
s.Projects = processAliases(s.Projects)
|
|
|
|
s.Editors = processAliases(s.Editors)
|
|
|
|
s.Languages = processAliases(s.Languages)
|
|
|
|
s.OperatingSystems = processAliases(s.OperatingSystems)
|
|
|
|
s.Machines = processAliases(s.Machines)
|
2021-06-11 21:59:34 +03:00
|
|
|
s.Labels = processAliases(s.Labels)
|
2022-01-02 15:39:20 +03:00
|
|
|
s.Branches = processAliases(s.Branches)
|
2020-11-07 14:01:35 +03:00
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2020-11-22 00:30:56 +03:00
|
|
|
|
2021-06-12 12:09:24 +03:00
|
|
|
func (s *Summary) findFirstPresentType() (uint8, error) {
|
|
|
|
for _, t := range s.Types() {
|
|
|
|
if s.TotalTimeBy(t) != 0 {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 127, errors.New("no type present")
|
|
|
|
}
|
|
|
|
|
2022-01-02 22:04:29 +03:00
|
|
|
func (s *SummaryParams) HasFilters() bool {
|
|
|
|
return s.Filters != nil && !s.Filters.IsEmpty()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SummaryParams) IsProjectDetails() bool {
|
|
|
|
if !s.HasFilters() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, entity, filters := s.Filters.One()
|
|
|
|
return entity == SummaryProject && len(filters) == 1 // exactly one
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SummaryParams) GetProjectFilter() string {
|
|
|
|
if !s.IsProjectDetails() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
_, _, filters := s.Filters.One()
|
|
|
|
return filters[0]
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:07:14 +03:00
|
|
|
func (s *SummaryItem) TotalFixed() time.Duration {
|
|
|
|
// this is a workaround, since currently, the total time of a summary item is mistakenly represented in seconds
|
|
|
|
// TODO: fix some day, while migrating persisted summary items
|
|
|
|
return s.Total * time.Second
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:30:56 +03:00
|
|
|
func (s SummaryItems) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SummaryItems) Less(i, j int) bool {
|
|
|
|
return s[i].Total < s[j].Total
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SummaryItems) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|