mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Parallelization.
This commit is contained in:
parent
1caf962917
commit
cc1a9bbca1
2
main.go
2
main.go
@ -63,7 +63,7 @@ func main() {
|
|||||||
|
|
||||||
// Connect to database
|
// Connect to database
|
||||||
db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
|
db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
|
||||||
db.LogMode(true)
|
db.LogMode(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// log.Fatal("Could not connect to database.")
|
// log.Fatal("Could not connect to database.")
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -26,3 +26,8 @@ type SummaryItem struct {
|
|||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
Total time.Duration `json:"total"`
|
Total time.Duration `json:"total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SummaryItemContainer struct {
|
||||||
|
Type uint8
|
||||||
|
Items []SummaryItem
|
||||||
|
}
|
||||||
|
@ -2,7 +2,6 @@ package routes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@ -64,7 +63,6 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
cacheKey := getHash([]time.Time{from, to})
|
cacheKey := getHash([]time.Time{from, to})
|
||||||
if _, ok := summaryCache[cacheKey]; !ok {
|
if _, ok := summaryCache[cacheKey]; !ok {
|
||||||
// Cache Miss
|
// Cache Miss
|
||||||
fmt.Println("Cache miss")
|
|
||||||
summary, err = h.SummarySrvc.GetSummary(from, to, user) // 'to' is always constant
|
summary, err = h.SummarySrvc.GetSummary(from, to, user) // 'to' is always constant
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
@ -20,25 +20,52 @@ func (srv *SummaryService) GetSummary(from, to time.Time, user *models.User) (*m
|
|||||||
return nil, err
|
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{
|
summary := &models.Summary{
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
FromTime: &from,
|
FromTime: &from,
|
||||||
ToTime: &to,
|
ToTime: &to,
|
||||||
Projects: srv.aggregateBy(heartbeats, models.SummaryProject),
|
Projects: projectItems,
|
||||||
Languages: srv.aggregateBy(heartbeats, models.SummaryLanguage),
|
Languages: languageItems,
|
||||||
Editors: srv.aggregateBy(heartbeats, models.SummaryEditor),
|
Editors: editorItems,
|
||||||
OperatingSystems: srv.aggregateBy(heartbeats, models.SummaryOS),
|
OperatingSystems: osItems,
|
||||||
}
|
}
|
||||||
|
|
||||||
return summary, nil
|
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)
|
durations := make(map[string]time.Duration)
|
||||||
|
|
||||||
for i, h := range heartbeats {
|
for i, h := range heartbeats {
|
||||||
var key string
|
var key string
|
||||||
switch aggregationType {
|
switch summaryType {
|
||||||
case models.SummaryProject:
|
case models.SummaryProject:
|
||||||
key = h.Project
|
key = h.Project
|
||||||
case models.SummaryEditor:
|
case models.SummaryEditor:
|
||||||
@ -70,5 +97,5 @@ func (srv *SummaryService) aggregateBy(heartbeats []*models.Heartbeat, aggregati
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return items
|
c <- models.SummaryItemContainer{Type: summaryType, Items: items}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user