From 47246b69552cafb1f32bc86b7f4a78728d156ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sat, 11 May 2019 20:07:58 +0200 Subject: [PATCH] Introduce aggregation model. --- main.go | 2 ++ models/aggregation.go | 31 +++++++++++++++++++------------ models/heartbeat.go | 16 ++++++++-------- services/aggregation.go | 2 +- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 07efbb8..60a5447 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,8 @@ func main() { // Migrate database schema db.AutoMigrate(&models.User{}) db.AutoMigrate(&models.Heartbeat{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT") + db.AutoMigrate(&models.Aggregation{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT") + db.AutoMigrate(&models.AggregationItem{}).AddForeignKey("aggregation_id", "aggregations(id)", "RESTRICT", "RESTRICT") // Services heartbeatSrvc := &services.HeartbeatService{db} diff --git a/models/aggregation.go b/models/aggregation.go index c506f60..bba68ea 100644 --- a/models/aggregation.go +++ b/models/aggregation.go @@ -1,24 +1,31 @@ package models -import "time" +import ( + "time" -type AggregationType string + "github.com/jinzhu/gorm" +) const ( - AggregationProject AggregationType = "project" - AggregationLanguage AggregationType = "language" - AggregationEditor AggregationType = "editor" - AggregationOS AggregationType = "os" + AggregationProject int = 0 + AggregationLanguage int = 1 + AggregationEditor int = 2 + AggregationOS int = 3 ) type Aggregation struct { - From time.Time - To time.Time - Type AggregationType - Items []AggregationItem + gorm.Model + User *User `gorm:"not null; association_foreignkey:ID"` + UserID string `gorm:"not null; index:idx_user,idx_type_time_user"` + From time.Time `gorm:"not null; index:idx_from,idx_type_time_user; default:now()"` + To time.Time `gorm:"not null; index:idx_to,idx_type_time_user; default:now()"` + Duration time.Duration `gorm:"-"` + Type uint8 `gorm:"not null; index:idx_type,idx_type_time_user"` + Items []AggregationItem } type AggregationItem struct { - Key string - Total time.Duration + AggregationID uint `gorm:"not null; association_foreignkey:ID"` + Key string `gorm:"not null"` + Total time.Duration } diff --git a/models/heartbeat.go b/models/heartbeat.go index 086637b..11ba61c 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -15,18 +15,18 @@ type HeartbeatReqTime time.Time type Heartbeat struct { gorm.Model - User *User `json:"user" gorm:"not_null; association_foreignkey:ID"` - UserID string `json:"-" gorm:"not_null"` - Entity string `json:"entity" gorm:"not_null"` + User *User `json:"user" gorm:"not null; association_foreignkey:ID"` + UserID string `json:"-" gorm:"not null; index:idx_time_user"` + Entity string `json:"entity" gorm:"not null"` Type string `json:"type"` Category string `json:"category"` - Project string `json:"project; index:idx_project"` + Project string `json:"project" gorm:"index:idx_project"` Branch string `json:"branch"` - Language string `json:"language" gorm:"not_null; index:idx_language"` + Language string `json:"language" gorm:"not null"` IsWrite bool `json:"is_write"` - Editor string `json:"editor" gorm:"not_null; index:idx_editor"` - OperatingSystem string `json:"operating_system" gorm:"not_null; index:idx_os"` - Time *HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time"` + Editor string `json:"editor" gorm:"not null"` + OperatingSystem string `json:"operating_system" gorm:"not null"` + Time *HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time,idx_time_user"` } func (h *Heartbeat) Valid() bool { diff --git a/services/aggregation.go b/services/aggregation.go index 2c31700..97ad35b 100644 --- a/services/aggregation.go +++ b/services/aggregation.go @@ -24,6 +24,6 @@ func (srv *AggregationService) Aggregate(from time.Time, to time.Time, user *mod } } -func (srv *AggregationService) aggregateBy(*[]models.Heartbeat, models.AggregationType) *models.Aggregation { +func (srv *AggregationService) aggregateBy(heartbeats *[]models.Heartbeat, aggregationType int) *models.Aggregation { return &models.Aggregation{} }