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

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 // Migrate database schema
db.AutoMigrate(&models.User{}) db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Heartbeat{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT") 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 // Services
heartbeatSrvc := &services.HeartbeatService{db} heartbeatSrvc := &services.HeartbeatService{db}

View File

@ -1,24 +1,31 @@
package models package models
import "time" import (
"time"
type AggregationType string "github.com/jinzhu/gorm"
)
const ( const (
AggregationProject AggregationType = "project" AggregationProject int = 0
AggregationLanguage AggregationType = "language" AggregationLanguage int = 1
AggregationEditor AggregationType = "editor" AggregationEditor int = 2
AggregationOS AggregationType = "os" AggregationOS int = 3
) )
type Aggregation struct { type Aggregation struct {
From time.Time gorm.Model
To time.Time User *User `gorm:"not null; association_foreignkey:ID"`
Type AggregationType UserID string `gorm:"not null; index:idx_user,idx_type_time_user"`
Items []AggregationItem 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 { type AggregationItem struct {
Key string AggregationID uint `gorm:"not null; association_foreignkey:ID"`
Total time.Duration Key string `gorm:"not null"`
Total time.Duration
} }

View File

@ -15,18 +15,18 @@ type HeartbeatReqTime time.Time
type Heartbeat struct { type Heartbeat struct {
gorm.Model gorm.Model
User *User `json:"user" gorm:"not_null; association_foreignkey:ID"` User *User `json:"user" gorm:"not null; association_foreignkey:ID"`
UserID string `json:"-" gorm:"not_null"` UserID string `json:"-" gorm:"not null; index:idx_time_user"`
Entity string `json:"entity" gorm:"not_null"` Entity string `json:"entity" gorm:"not null"`
Type string `json:"type"` Type string `json:"type"`
Category string `json:"category"` Category string `json:"category"`
Project string `json:"project; index:idx_project"` Project string `json:"project" gorm:"index:idx_project"`
Branch string `json:"branch"` 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"` IsWrite bool `json:"is_write"`
Editor string `json:"editor" gorm:"not_null; index:idx_editor"` Editor string `json:"editor" gorm:"not null"`
OperatingSystem string `json:"operating_system" gorm:"not_null; index:idx_os"` OperatingSystem string `json:"operating_system" gorm:"not null"`
Time *HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time"` Time *HeartbeatReqTime `json:"time" gorm:"type:timestamp; default:now(); index:idx_time,idx_time_user"`
} }
func (h *Heartbeat) Valid() bool { 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{} return &models.Aggregation{}
} }