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

View File

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

View File

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