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

Untested. Useslices of pointersto structs everywhere.

This commit is contained in:
Ferdinand Mütsch 2019-05-17 08:40:03 +02:00
parent adb5abd4d2
commit 62e94f6635
3 changed files with 12 additions and 16 deletions

View File

@ -21,7 +21,7 @@ func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
return return
} }
var heartbeats []models.Heartbeat var heartbeats []*models.Heartbeat
user := r.Context().Value(models.UserKey).(*models.User) user := r.Context().Value(models.UserKey).(*models.User)
opSys, editor, _ := utils.ParseUserAgent(r.Header.Get("User-Agent")) opSys, editor, _ := utils.ParseUserAgent(r.Header.Get("User-Agent"))
@ -32,8 +32,7 @@ func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
return return
} }
for i := 0; i < len(heartbeats); i++ { for _, h := range heartbeats {
h := &heartbeats[i]
h.OperatingSystem = opSys h.OperatingSystem = opSys
h.Editor = editor h.Editor = editor
h.User = user h.User = user
@ -46,7 +45,7 @@ func (h *HeartbeatHandler) Post(w http.ResponseWriter, r *http.Request) {
} }
} }
if err := h.HeartbeatSrvc.InsertBatch(&heartbeats); err != nil { if err := h.HeartbeatSrvc.InsertBatch(heartbeats); err != nil {
w.WriteHeader(500) w.WriteHeader(500)
os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(err.Error())
return return

View File

@ -84,13 +84,10 @@ func (srv *AggregationService) aggregate(from, to time.Time, user *models.User)
return aggregations return aggregations
} }
func (srv *AggregationService) aggregateBy(heartbeats *[]models.Heartbeat, aggregationType uint8) []models.AggregationItem { func (srv *AggregationService) aggregateBy(heartbeats []*models.Heartbeat, aggregationType uint8) []models.AggregationItem {
beats := *heartbeats
durations := make(map[string]time.Duration) durations := make(map[string]time.Duration)
for i := 0; i < len(beats); i++ { for i, h := range heartbeats {
h := &beats[i]
var key string var key string
switch aggregationType { switch aggregationType {
case models.AggregationProject: case models.AggregationProject:
@ -111,7 +108,7 @@ func (srv *AggregationService) aggregateBy(heartbeats *[]models.Heartbeat, aggre
continue continue
} }
timePassed := h.Time.Time().Sub((&beats[i-1]).Time.Time()) timePassed := h.Time.Time().Sub(heartbeats[i-1].Time.Time())
timeThresholded := math.Min(float64(timePassed), float64(time.Duration(2)*time.Minute)) timeThresholded := math.Min(float64(timePassed), float64(time.Duration(2)*time.Minute))
durations[key] += time.Duration(int64(timeThresholded)) durations[key] += time.Duration(int64(timeThresholded))
} }

View File

@ -15,9 +15,9 @@ type HeartbeatService struct {
Db *gorm.DB Db *gorm.DB
} }
func (srv *HeartbeatService) InsertBatch(heartbeats *[]models.Heartbeat) error { func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error {
var batch []interface{} var batch []interface{}
for _, h := range *heartbeats { for _, h := range heartbeats {
batch = append(batch, h) batch = append(batch, h)
} }
@ -27,13 +27,13 @@ func (srv *HeartbeatService) InsertBatch(heartbeats *[]models.Heartbeat) error {
return nil return nil
} }
func (srv *HeartbeatService) GetAllFrom(date time.Time, user *models.User) (*[]models.Heartbeat, error) { func (srv *HeartbeatService) GetAllFrom(date time.Time, user *models.User) ([]*models.Heartbeat, error) {
var heartbeats []models.Heartbeat var heartbeats []*models.Heartbeat
if err := srv.Db. if err := srv.Db.
Where(&models.Heartbeat{UserID: user.ID}). Where(&models.Heartbeat{UserID: user.ID}).
Where("time > ?", date). Where("time > ?", date).
Find(&heartbeats).Error; err != nil { Find(heartbeats).Error; err != nil {
return nil, err return nil, err
} }
return &heartbeats, nil return heartbeats, nil
} }