Introduce aggregation model.

This commit is contained in:
Ferdinand Mütsch 2019-05-11 20:07:58 +02:00
parent 959ce8ab11
commit 47246b6955
4 changed files with 30 additions and 21 deletions

View File

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

View File

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

View File

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

View File

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