2019-05-19 20:49:27 +03:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2020-02-20 16:28:55 +03:00
|
|
|
"crypto/md5"
|
2019-10-10 19:10:14 +03:00
|
|
|
"errors"
|
2020-09-29 19:55:07 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2020-02-20 16:28:55 +03:00
|
|
|
"github.com/patrickmn/go-cache"
|
2019-05-19 20:49:27 +03:00
|
|
|
"math"
|
2019-07-07 11:37:17 +03:00
|
|
|
"sort"
|
2020-02-20 16:28:55 +03:00
|
|
|
"strconv"
|
2019-05-19 20:49:27 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-03-31 13:22:17 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2019-05-19 20:49:27 +03:00
|
|
|
)
|
|
|
|
|
2020-10-16 13:00:20 +03:00
|
|
|
const HeartbeatDiffThreshold = 2 * time.Minute
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
type SummaryService struct {
|
2020-10-25 09:22:10 +03:00
|
|
|
Config *config.Config
|
|
|
|
Cache *cache.Cache
|
|
|
|
Db *gorm.DB
|
|
|
|
HeartbeatService *HeartbeatService
|
|
|
|
AliasService *AliasService
|
|
|
|
CustomRuleService *CustomRuleService
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 09:22:10 +03:00
|
|
|
func NewSummaryService(db *gorm.DB, heartbeatService *HeartbeatService, aliasService *AliasService, customRuleService *CustomRuleService) *SummaryService {
|
2020-05-24 18:32:26 +03:00
|
|
|
return &SummaryService{
|
2020-09-29 19:55:07 +03:00
|
|
|
Config: config.Get(),
|
2020-05-24 18:32:26 +03:00
|
|
|
Cache: cache.New(24*time.Hour, 24*time.Hour),
|
|
|
|
Db: db,
|
|
|
|
HeartbeatService: heartbeatService,
|
|
|
|
AliasService: aliasService,
|
2020-10-25 09:22:10 +03:00
|
|
|
CustomRuleService: customRuleService,
|
2020-05-24 18:32:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:10:14 +03:00
|
|
|
type Interval struct {
|
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
}
|
|
|
|
|
2020-10-09 22:37:16 +03:00
|
|
|
// TODO: simplify!
|
2019-11-07 14:56:05 +03:00
|
|
|
func (srv *SummaryService) Construct(from, to time.Time, user *models.User, recompute bool) (*models.Summary, error) {
|
|
|
|
var existingSummaries []*models.Summary
|
2020-02-20 16:28:55 +03:00
|
|
|
var cacheKey string
|
|
|
|
|
2019-11-07 14:56:05 +03:00
|
|
|
if recompute {
|
|
|
|
existingSummaries = make([]*models.Summary, 0)
|
|
|
|
} else {
|
2020-02-20 16:28:55 +03:00
|
|
|
cacheKey = getHash([]time.Time{from, to}, user)
|
|
|
|
if result, ok := srv.Cache.Get(cacheKey); ok {
|
|
|
|
return result.(*models.Summary), nil
|
|
|
|
}
|
2019-11-07 14:56:05 +03:00
|
|
|
summaries, err := srv.GetByUserWithin(user, from, to)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
existingSummaries = summaries
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2019-10-10 19:10:14 +03:00
|
|
|
missingIntervals := getMissingIntervals(from, to, existingSummaries)
|
|
|
|
|
|
|
|
heartbeats := make([]*models.Heartbeat, 0)
|
|
|
|
for _, interval := range missingIntervals {
|
|
|
|
hb, err := srv.HeartbeatService.GetAllWithin(interval.Start, interval.End, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
heartbeats = append(heartbeats, hb...)
|
|
|
|
}
|
|
|
|
|
2020-09-06 13:15:46 +03:00
|
|
|
types := models.SummaryTypes()
|
2019-05-19 22:00:19 +03:00
|
|
|
|
2019-10-11 09:00:02 +03:00
|
|
|
var projectItems []*models.SummaryItem
|
|
|
|
var languageItems []*models.SummaryItem
|
|
|
|
var editorItems []*models.SummaryItem
|
|
|
|
var osItems []*models.SummaryItem
|
2020-08-29 23:03:01 +03:00
|
|
|
var machineItems []*models.SummaryItem
|
2019-05-19 22:00:19 +03:00
|
|
|
|
2019-07-07 11:32:28 +03:00
|
|
|
if err := srv.AliasService.LoadUserAliases(user.ID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-19 22:00:19 +03:00
|
|
|
c := make(chan models.SummaryItemContainer)
|
|
|
|
for _, t := range types {
|
2019-07-06 18:53:20 +03:00
|
|
|
go srv.aggregateBy(heartbeats, t, user, c)
|
2019-05-19 22:00:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(types); i++ {
|
|
|
|
item := <-c
|
|
|
|
switch item.Type {
|
|
|
|
case models.SummaryProject:
|
|
|
|
projectItems = item.Items
|
|
|
|
case models.SummaryLanguage:
|
|
|
|
languageItems = item.Items
|
|
|
|
case models.SummaryEditor:
|
|
|
|
editorItems = item.Items
|
|
|
|
case models.SummaryOS:
|
|
|
|
osItems = item.Items
|
2020-08-29 23:03:01 +03:00
|
|
|
case models.SummaryMachine:
|
|
|
|
machineItems = item.Items
|
2019-05-19 22:00:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(c)
|
|
|
|
|
2020-08-23 14:10:58 +03:00
|
|
|
realFrom, realTo := from, to
|
|
|
|
if len(existingSummaries) > 0 {
|
2020-10-16 15:49:22 +03:00
|
|
|
realFrom = existingSummaries[0].FromTime.T()
|
|
|
|
realTo = existingSummaries[len(existingSummaries)-1].ToTime.T()
|
2020-08-30 00:16:21 +03:00
|
|
|
|
|
|
|
for _, summary := range existingSummaries {
|
|
|
|
summary.FillUnknown()
|
|
|
|
}
|
2020-08-23 14:10:58 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:10:14 +03:00
|
|
|
aggregatedSummary := &models.Summary{
|
2019-05-19 20:49:27 +03:00
|
|
|
UserID: user.ID,
|
2020-10-16 15:49:22 +03:00
|
|
|
FromTime: models.CustomTime(realFrom),
|
|
|
|
ToTime: models.CustomTime(realTo),
|
2019-05-19 22:00:19 +03:00
|
|
|
Projects: projectItems,
|
|
|
|
Languages: languageItems,
|
|
|
|
Editors: editorItems,
|
|
|
|
OperatingSystems: osItems,
|
2020-08-29 23:03:01 +03:00
|
|
|
Machines: machineItems,
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2019-10-10 19:10:14 +03:00
|
|
|
allSummaries := []*models.Summary{aggregatedSummary}
|
|
|
|
allSummaries = append(allSummaries, existingSummaries...)
|
|
|
|
|
|
|
|
summary, err := mergeSummaries(allSummaries)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-20 16:28:55 +03:00
|
|
|
if cacheKey != "" {
|
|
|
|
srv.Cache.SetDefault(cacheKey, summary)
|
|
|
|
}
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
return summary, nil
|
|
|
|
}
|
|
|
|
|
2019-10-11 09:00:02 +03:00
|
|
|
func (srv *SummaryService) Insert(summary *models.Summary) error {
|
2019-10-10 17:47:19 +03:00
|
|
|
if err := srv.Db.Create(summary).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:10:14 +03:00
|
|
|
func (srv *SummaryService) GetByUserWithin(user *models.User, from, to time.Time) ([]*models.Summary, error) {
|
|
|
|
var summaries []*models.Summary
|
|
|
|
if err := srv.Db.
|
|
|
|
Where(&models.Summary{UserID: user.ID}).
|
|
|
|
Where("from_time >= ?", from).
|
|
|
|
Where("to_time <= ?", to).
|
2020-08-23 14:10:58 +03:00
|
|
|
Order("from_time asc").
|
2019-10-10 19:32:17 +03:00
|
|
|
Preload("Projects", "type = ?", models.SummaryProject).
|
|
|
|
Preload("Languages", "type = ?", models.SummaryLanguage).
|
|
|
|
Preload("Editors", "type = ?", models.SummaryEditor).
|
|
|
|
Preload("OperatingSystems", "type = ?", models.SummaryOS).
|
2020-08-29 23:03:01 +03:00
|
|
|
Preload("Machines", "type = ?", models.SummaryMachine).
|
2019-10-10 19:10:14 +03:00
|
|
|
Find(&summaries).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return summaries, nil
|
|
|
|
}
|
|
|
|
|
2020-05-24 14:41:19 +03:00
|
|
|
// Will return *models.Index objects with only user_id and to_time filled
|
2019-10-11 09:00:02 +03:00
|
|
|
func (srv *SummaryService) GetLatestByUser() ([]*models.Summary, error) {
|
2019-10-10 00:26:28 +03:00
|
|
|
var summaries []*models.Summary
|
|
|
|
if err := srv.Db.
|
|
|
|
Table("summaries").
|
|
|
|
Select("user_id, max(to_time) as to_time").
|
|
|
|
Group("user_id").
|
|
|
|
Scan(&summaries).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return summaries, nil
|
|
|
|
}
|
|
|
|
|
2019-07-06 18:53:20 +03:00
|
|
|
func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, summaryType uint8, user *models.User, c chan models.SummaryItemContainer) {
|
2019-05-19 20:49:27 +03:00
|
|
|
durations := make(map[string]time.Duration)
|
|
|
|
|
|
|
|
for i, h := range heartbeats {
|
|
|
|
var key string
|
2019-05-19 22:00:19 +03:00
|
|
|
switch summaryType {
|
2019-05-19 20:49:27 +03:00
|
|
|
case models.SummaryProject:
|
|
|
|
key = h.Project
|
|
|
|
case models.SummaryEditor:
|
|
|
|
key = h.Editor
|
|
|
|
case models.SummaryLanguage:
|
|
|
|
key = h.Language
|
|
|
|
case models.SummaryOS:
|
|
|
|
key = h.OperatingSystem
|
2020-08-29 23:03:01 +03:00
|
|
|
case models.SummaryMachine:
|
|
|
|
key = h.Machine
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2019-05-20 19:44:16 +03:00
|
|
|
if key == "" {
|
2020-08-30 00:16:21 +03:00
|
|
|
key = models.UnknownSummaryKey
|
2019-05-20 19:44:16 +03:00
|
|
|
}
|
|
|
|
|
2019-07-06 18:53:20 +03:00
|
|
|
if aliasedKey, err := srv.AliasService.GetAliasOrDefault(user.ID, summaryType, key); err == nil {
|
|
|
|
key = aliasedKey
|
|
|
|
}
|
|
|
|
|
2019-05-19 20:49:27 +03:00
|
|
|
if _, ok := durations[key]; !ok {
|
|
|
|
durations[key] = time.Duration(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-16 13:49:36 +03:00
|
|
|
t1, t2, tdiff := h.Time.T(), heartbeats[i-1].Time.T(), time.Duration(0)
|
2020-10-16 13:00:20 +03:00
|
|
|
// This is a hack. The time difference between two heartbeats from two subsequent day (e.g. 23:59:59 and 00:00:01) are ignored.
|
|
|
|
// This is to prevent a discrepancy between summaries computed solely from heartbeats and summaries involving pre-aggregated per-day summaries.
|
|
|
|
// For the latter, a duration is already pre-computed and information about individual heartbeats is lost, so there can be no cross-day overflow.
|
|
|
|
// Essentially, we simply ignore such edge-case heartbeats here, which makes the eventual total duration potentially a bit shorter.
|
|
|
|
if t1.Day() == t2.Day() {
|
|
|
|
timePassed := t1.Sub(t2)
|
|
|
|
tdiff = time.Duration(int64(math.Min(float64(timePassed), float64(HeartbeatDiffThreshold))))
|
|
|
|
}
|
|
|
|
durations[key] += tdiff
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
|
|
|
|
2019-10-11 09:00:02 +03:00
|
|
|
items := make([]*models.SummaryItem, 0)
|
2019-05-19 20:49:27 +03:00
|
|
|
for k, v := range durations {
|
2019-10-11 09:00:02 +03:00
|
|
|
items = append(items, &models.SummaryItem{
|
2019-05-19 20:49:27 +03:00
|
|
|
Key: k,
|
|
|
|
Total: v / time.Second,
|
2019-10-10 19:32:17 +03:00
|
|
|
Type: summaryType,
|
2019-05-19 20:49:27 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-07 11:37:17 +03:00
|
|
|
sort.Slice(items, func(i, j int) bool {
|
|
|
|
return items[i].Total > items[j].Total
|
|
|
|
})
|
|
|
|
|
2019-05-19 22:00:19 +03:00
|
|
|
c <- models.SummaryItemContainer{Type: summaryType, Items: items}
|
2019-05-19 20:49:27 +03:00
|
|
|
}
|
2019-10-10 19:10:14 +03:00
|
|
|
|
|
|
|
func getMissingIntervals(from, to time.Time, existingSummaries []*models.Summary) []*Interval {
|
|
|
|
if len(existingSummaries) == 0 {
|
2019-11-08 01:11:19 +03:00
|
|
|
return []*Interval{{from, to}}
|
2019-10-10 19:10:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
intervals := make([]*Interval, 0)
|
|
|
|
|
|
|
|
// Pre
|
2020-10-16 15:49:22 +03:00
|
|
|
if from.Before(existingSummaries[0].FromTime.T()) {
|
|
|
|
intervals = append(intervals, &Interval{from, existingSummaries[0].FromTime.T()})
|
2019-10-10 19:10:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Between
|
|
|
|
for i := 0; i < len(existingSummaries)-1; i++ {
|
2020-10-16 15:49:22 +03:00
|
|
|
t1, t2 := existingSummaries[i].ToTime.T(), existingSummaries[i+1].FromTime.T()
|
2020-08-23 14:21:23 +03:00
|
|
|
if t1.Equal(t2) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// round to end of day / start of day, assuming that summaries are always generated on a per-day basis
|
|
|
|
td1 := time.Date(t1.Year(), t1.Month(), t1.Day()+1, 0, 0, 0, 0, t1.Location())
|
|
|
|
td2 := time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, t2.Location())
|
|
|
|
// one or more day missing in between?
|
|
|
|
if td1.Before(td2) {
|
2020-10-16 15:49:22 +03:00
|
|
|
intervals = append(intervals, &Interval{existingSummaries[i].ToTime.T(), existingSummaries[i+1].FromTime.T()})
|
2019-10-10 19:10:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post
|
2020-10-16 15:49:22 +03:00
|
|
|
if to.After(existingSummaries[len(existingSummaries)-1].ToTime.T()) {
|
|
|
|
intervals = append(intervals, &Interval{existingSummaries[len(existingSummaries)-1].ToTime.T(), to})
|
2019-10-10 19:10:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return intervals
|
|
|
|
}
|
2019-10-11 09:00:02 +03:00
|
|
|
|
|
|
|
func mergeSummaries(summaries []*models.Summary) (*models.Summary, error) {
|
|
|
|
if len(summaries) < 1 {
|
|
|
|
return nil, errors.New("no summaries given")
|
|
|
|
}
|
|
|
|
|
|
|
|
var minTime, maxTime time.Time
|
|
|
|
minTime = time.Now()
|
|
|
|
|
|
|
|
finalSummary := &models.Summary{
|
|
|
|
UserID: summaries[0].UserID,
|
|
|
|
Projects: make([]*models.SummaryItem, 0),
|
|
|
|
Languages: make([]*models.SummaryItem, 0),
|
|
|
|
Editors: make([]*models.SummaryItem, 0),
|
|
|
|
OperatingSystems: make([]*models.SummaryItem, 0),
|
2020-08-29 23:03:01 +03:00
|
|
|
Machines: make([]*models.SummaryItem, 0),
|
2019-10-11 09:00:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range summaries {
|
|
|
|
if s.UserID != finalSummary.UserID {
|
|
|
|
return nil, errors.New("users don't match")
|
|
|
|
}
|
|
|
|
|
2020-10-16 15:49:22 +03:00
|
|
|
if s.FromTime.T().Before(minTime) {
|
|
|
|
minTime = s.FromTime.T()
|
2019-10-11 09:00:02 +03:00
|
|
|
}
|
|
|
|
|
2020-10-16 15:49:22 +03:00
|
|
|
if s.ToTime.T().After(maxTime) {
|
|
|
|
maxTime = s.ToTime.T()
|
2019-10-11 09:00:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
finalSummary.Projects = mergeSummaryItems(finalSummary.Projects, s.Projects)
|
|
|
|
finalSummary.Languages = mergeSummaryItems(finalSummary.Languages, s.Languages)
|
|
|
|
finalSummary.Editors = mergeSummaryItems(finalSummary.Editors, s.Editors)
|
|
|
|
finalSummary.OperatingSystems = mergeSummaryItems(finalSummary.OperatingSystems, s.OperatingSystems)
|
2020-08-29 23:03:01 +03:00
|
|
|
finalSummary.Machines = mergeSummaryItems(finalSummary.Machines, s.Machines)
|
2019-10-11 09:00:02 +03:00
|
|
|
}
|
|
|
|
|
2020-10-16 15:49:22 +03:00
|
|
|
finalSummary.FromTime = models.CustomTime(minTime)
|
|
|
|
finalSummary.ToTime = models.CustomTime(maxTime)
|
2019-10-11 09:00:02 +03:00
|
|
|
|
|
|
|
return finalSummary, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeSummaryItems(existing []*models.SummaryItem, new []*models.SummaryItem) []*models.SummaryItem {
|
|
|
|
items := make(map[string]*models.SummaryItem)
|
|
|
|
|
|
|
|
// Build map from existing
|
|
|
|
for _, item := range existing {
|
|
|
|
items[item.Key] = item
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range new {
|
|
|
|
if it, ok := items[item.Key]; !ok {
|
|
|
|
items[item.Key] = item
|
|
|
|
} else {
|
|
|
|
(*it).Total += item.Total
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var i int
|
|
|
|
itemList := make([]*models.SummaryItem, len(items))
|
|
|
|
for k, v := range items {
|
|
|
|
itemList[i] = &models.SummaryItem{Key: k, Total: v.Total, Type: v.Type}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(itemList, func(i, j int) bool {
|
|
|
|
return itemList[i].Total > itemList[j].Total
|
|
|
|
})
|
|
|
|
|
|
|
|
return itemList
|
|
|
|
}
|
2020-02-20 16:28:55 +03:00
|
|
|
|
|
|
|
func getHash(times []time.Time, user *models.User) string {
|
|
|
|
digest := md5.New()
|
|
|
|
for _, t := range times {
|
|
|
|
digest.Write([]byte(strconv.Itoa(int(t.Unix()))))
|
|
|
|
}
|
|
|
|
digest.Write([]byte(user.ID))
|
|
|
|
return string(digest.Sum(nil))
|
|
|
|
}
|