diff --git a/main.go b/main.go index e72c031..1e2b234 100644 --- a/main.go +++ b/main.go @@ -63,7 +63,7 @@ func main() { // Connect to database db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config)) - db.LogMode(true) + db.LogMode(false) if err != nil { // log.Fatal("Could not connect to database.") log.Fatal(err) diff --git a/models/summary.go b/models/summary.go index 5bf5c86..d34c26a 100644 --- a/models/summary.go +++ b/models/summary.go @@ -26,3 +26,8 @@ type SummaryItem struct { Key string `json:"key"` Total time.Duration `json:"total"` } + +type SummaryItemContainer struct { + Type uint8 + Items []SummaryItem +} diff --git a/routes/summary.go b/routes/summary.go index 564eabd..ba740b9 100644 --- a/routes/summary.go +++ b/routes/summary.go @@ -2,7 +2,6 @@ package routes import ( "crypto/md5" - "fmt" "net/http" "strconv" "time" @@ -64,7 +63,6 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) { cacheKey := getHash([]time.Time{from, to}) if _, ok := summaryCache[cacheKey]; !ok { // Cache Miss - fmt.Println("Cache miss") summary, err = h.SummarySrvc.GetSummary(from, to, user) // 'to' is always constant if err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/services/summary.go b/services/summary.go index 71dd88c..1af387c 100644 --- a/services/summary.go +++ b/services/summary.go @@ -20,25 +20,52 @@ func (srv *SummaryService) GetSummary(from, to time.Time, user *models.User) (*m return nil, err } + types := []uint8{models.SummaryProject, models.SummaryLanguage, models.SummaryEditor, models.SummaryOS} + + var projectItems []models.SummaryItem + var languageItems []models.SummaryItem + var editorItems []models.SummaryItem + var osItems []models.SummaryItem + + c := make(chan models.SummaryItemContainer) + for _, t := range types { + go srv.aggregateBy(heartbeats, t, c) + } + + 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 + } + } + close(c) + summary := &models.Summary{ UserID: user.ID, FromTime: &from, ToTime: &to, - Projects: srv.aggregateBy(heartbeats, models.SummaryProject), - Languages: srv.aggregateBy(heartbeats, models.SummaryLanguage), - Editors: srv.aggregateBy(heartbeats, models.SummaryEditor), - OperatingSystems: srv.aggregateBy(heartbeats, models.SummaryOS), + Projects: projectItems, + Languages: languageItems, + Editors: editorItems, + OperatingSystems: osItems, } return summary, nil } -func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, aggregationType uint8) []models.SummaryItem { +func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, summaryType uint8, c chan models.SummaryItemContainer) { durations := make(map[string]time.Duration) for i, h := range heartbeats { var key string - switch aggregationType { + switch summaryType { case models.SummaryProject: key = h.Project case models.SummaryEditor: @@ -70,5 +97,5 @@ func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, aggregati }) } - return items + c <- models.SummaryItemContainer{Type: summaryType, Items: items} }