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

fix: save heartbeats and summaries with millisecond time precision (resolve #49)

This commit is contained in:
Ferdinand Mütsch
2020-08-30 01:42:00 +02:00
parent 50b7a9ec3d
commit 75b33d5e42
6 changed files with 13 additions and 10 deletions

View File

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

View File

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

View File

@@ -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"`