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

Parallelization.

This commit is contained in:
Ferdinand Mütsch 2019-05-19 21:00:19 +02:00
parent 1caf962917
commit cc1a9bbca1
4 changed files with 40 additions and 10 deletions

View File

@ -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)

View File

@ -26,3 +26,8 @@ type SummaryItem struct {
Key string `json:"key"`
Total time.Duration `json:"total"`
}
type SummaryItemContainer struct {
Type uint8
Items []SummaryItem
}

View File

@ -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)

View File

@ -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}
}