From 75b33d5e42e8990064886528bda7659dfe866d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 30 Aug 2020 01:42:00 +0200 Subject: [PATCH] fix: save heartbeats and summaries with millisecond time precision (resolve #49) --- go.mod | 2 +- go.sum | 2 ++ models/heartbeat.go | 4 +--- models/shared.go | 9 ++++++--- models/summary.go | 4 ++-- version.txt | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 07d0d67..5a5c4df 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/patrickmn/go-cache v2.1.0+incompatible github.com/rubenv/sql-migrate v0.0.0-20200402132117-435005d389bc github.com/satori/go.uuid v1.2.0 - github.com/t-tiger/gorm-bulk-insert v0.0.0-20191014134946-beb77b81825f + github.com/t-tiger/gorm-bulk-insert v1.3.0 golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c gopkg.in/ini.v1 v1.50.0 ) diff --git a/go.sum b/go.sum index 525940d..2d244d4 100644 --- a/go.sum +++ b/go.sum @@ -318,6 +318,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/t-tiger/gorm-bulk-insert v0.0.0-20191014134946-beb77b81825f h1:Op5lFYUNE7tPxu6gJfwkgY8HMIWpLqiLApBJfGs71U8= github.com/t-tiger/gorm-bulk-insert v0.0.0-20191014134946-beb77b81825f/go.mod h1:SK1RZT4TR1aMUNGtbk6YxTPgx2D/gfbxB571QGnAV+c= +github.com/t-tiger/gorm-bulk-insert v1.3.0 h1:9k7BaVEhw/3fsvh6GTOBwJ2RXk3asc5xs5m6hwozq20= +github.com/t-tiger/gorm-bulk-insert v1.3.0/go.mod h1:ruDlk8xDl+8sX4bA7PQuYly9YEb3pbp1eP2LCyeRrFY= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= diff --git a/models/heartbeat.go b/models/heartbeat.go index c1cb872..8c4088f 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -5,8 +5,6 @@ import ( "time" ) -type CustomTime time.Time - type Heartbeat struct { ID uint `gorm:"primary_key"` User *User `json:"-" gorm:"not null"` @@ -21,7 +19,7 @@ type Heartbeat struct { 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"` + Time CustomTime `json:"time" gorm:"type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time,idx_time_user"` languageRegex *regexp.Regexp } diff --git a/models/shared.go b/models/shared.go index 01d820f..8cb5712 100644 --- a/models/shared.go +++ b/models/shared.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/jinzhu/gorm" + "math" "strconv" "strings" "time" @@ -23,13 +24,15 @@ type KeyStringValue struct { Value string `gorm:"type:text"` } +type CustomTime time.Time + func (j *CustomTime) UnmarshalJSON(b []byte) error { - s := strings.Split(strings.Trim(string(b), "\""), ".")[0] + s := strings.Replace(strings.Trim(string(b), "\""), ".", "", 1) // TODO: not always three decimal points! i, err := strconv.ParseInt(s, 10, 64) if err != nil { return err } - t := time.Unix(i, 0) + t := time.Unix(0, i*int64(math.Pow10(19-len(s)))) *j = CustomTime(t) return nil } @@ -60,7 +63,7 @@ func (j CustomTime) Value() (driver.Value, error) { func (j CustomTime) String() string { t := time.Time(j) - return t.Format("2006-01-02 15:04:05") + return t.Format("2006-01-02 15:04:05.000") } func (j CustomTime) Time() time.Time { diff --git a/models/summary.go b/models/summary.go index d9a1e0f..8bfb6f0 100644 --- a/models/summary.go +++ b/models/summary.go @@ -18,8 +18,8 @@ const UnknownSummaryKey = "unknown" type Summary struct { ID uint `json:"-" gorm:"primary_key"` UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"` - FromTime time.Time `json:"from" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user"` - ToTime time.Time `json:"to" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user"` + FromTime time.Time `json:"from" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"` + ToTime time.Time `json:"to" gorm:"not null; type:timestamp(3); default:CURRENT_TIMESTAMP(3); index:idx_time_summary_user"` Projects []*SummaryItem `json:"projects"` Languages []*SummaryItem `json:"languages"` Editors []*SummaryItem `json:"editors"` diff --git a/version.txt b/version.txt index 0bfbd57..fe4e75f 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.8.2 \ No newline at end of file +1.8.3 \ No newline at end of file