From b7ae15496d5b3b6993cb40813667df584d1f7a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 31 Jan 2021 18:29:50 +0100 Subject: [PATCH] fix: attempt to directly hash struct again --- models/heartbeat.go | 34 +++++++++++++++++----------------- models/shared.go | 4 ++++ 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/models/heartbeat.go b/models/heartbeat.go index ecefefe..9412fbd 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -9,22 +9,22 @@ import ( ) type Heartbeat struct { - ID uint `gorm:"primary_key"` - User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - UserID string `json:"-" gorm:"not null; index:idx_time_user"` - Entity string `json:"entity" gorm:"not null; index:idx_entity"` - Type string `json:"type"` - Category string `json:"category"` - Project string `json:"project"` - Branch string `json:"branch"` - Language string `json:"language" gorm:"index:idx_language"` - IsWrite bool `json:"is_write"` - Editor string `json:"editor"` - OperatingSystem string `json:"operating_system"` - Machine string `json:"machine"` - Time CustomTime `json:"time" gorm:"type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time,idx_time_user"` - Hash string `json:"-" gorm:"type:varchar(17); uniqueIndex"` - languageRegex *regexp.Regexp + ID uint `gorm:"primary_key" hash:"ignore"` + User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" hash:"ignore"` + UserID string `json:"-" gorm:"not null; index:idx_time_user"` + Entity string `json:"entity" gorm:"not null; index:idx_entity"` + Type string `json:"type"` + Category string `json:"category"` + Project string `json:"project"` + Branch string `json:"branch"` + Language string `json:"language" gorm:"index:idx_language"` + IsWrite bool `json:"is_write"` + Editor string `json:"editor"` + OperatingSystem string `json:"operating_system"` + Machine string `json:"machine"` + Time CustomTime `json:"time" gorm:"type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time,idx_time_user"` + Hash string `json:"-" gorm:"type:varchar(17); uniqueIndex"` + languageRegex *regexp.Regexp `hash:"ignore"` } func (h *Heartbeat) Valid() bool { @@ -92,7 +92,7 @@ func (h *Heartbeat) String() string { // essentially double the space required for heartbeats, so we decided to go this way. func (h *Heartbeat) Hashed() *Heartbeat { - hash, err := hashstructure.Hash(h.String(), hashstructure.FormatV2, nil) + hash, err := hashstructure.Hash(h, hashstructure.FormatV2, nil) if err != nil { logbuch.Error("CRITICAL ERROR: failed to hash struct – %v", err) } diff --git a/models/shared.go b/models/shared.go index 1612040..2cde688 100644 --- a/models/shared.go +++ b/models/shared.go @@ -71,6 +71,10 @@ func (j *CustomTime) Scan(value interface{}) error { return nil } +func (j *CustomTime) Hash() (uint64, error) { + return uint64((j.T().UnixNano() / 1000) / 1000), nil +} + func (j CustomTime) Value() (driver.Value, error) { t := time.Unix(0, j.T().UnixNano()/int64(time.Millisecond)*int64(time.Millisecond)) // round to millisecond precision return t, nil