2019-05-05 23:36:49 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2019-05-11 18:49:56 +03:00
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-05-05 23:36:49 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HeartbeatReqTime time.Time
|
|
|
|
|
|
|
|
type Heartbeat struct {
|
2019-05-19 20:49:27 +03:00
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
User *User `json:"-" gorm:"not null; index:idx_time_user"`
|
2019-05-11 21:07:58 +03:00
|
|
|
UserID string `json:"-" gorm:"not null; index:idx_time_user"`
|
|
|
|
Entity string `json:"entity" gorm:"not null"`
|
2019-05-11 18:49:56 +03:00
|
|
|
Type string `json:"type"`
|
|
|
|
Category string `json:"category"`
|
2019-05-11 21:07:58 +03:00
|
|
|
Project string `json:"project" gorm:"index:idx_project"`
|
2019-05-11 18:49:56 +03:00
|
|
|
Branch string `json:"branch"`
|
2019-05-11 21:07:58 +03:00
|
|
|
Language string `json:"language" gorm:"not null"`
|
2019-05-11 18:49:56 +03:00
|
|
|
IsWrite bool `json:"is_write"`
|
2019-05-11 21:07:58 +03:00
|
|
|
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"`
|
2019-05-11 18:49:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heartbeat) Valid() bool {
|
|
|
|
return h.User != nil && h.UserID != "" && h.Entity != "" && h.Language != "" && h.Editor != "" && h.OperatingSystem != "" && h.Time != nil
|
2019-05-05 23:36:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *HeartbeatReqTime) UnmarshalJSON(b []byte) error {
|
|
|
|
s := strings.Split(strings.Trim(string(b), "\""), ".")[0]
|
|
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t := time.Unix(i, 0)
|
|
|
|
*j = HeartbeatReqTime(t)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-11 18:49:56 +03:00
|
|
|
func (j *HeartbeatReqTime) Scan(value interface{}) error {
|
|
|
|
switch value.(type) {
|
|
|
|
case int64:
|
|
|
|
*j = HeartbeatReqTime(time.Unix(123456, 0))
|
|
|
|
break
|
|
|
|
case time.Time:
|
|
|
|
*j = HeartbeatReqTime(value.(time.Time))
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return errors.New(fmt.Sprintf("Unsupported type"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j HeartbeatReqTime) Value() (driver.Value, error) {
|
|
|
|
return time.Time(j), nil
|
|
|
|
}
|
|
|
|
|
2019-05-05 23:36:49 +03:00
|
|
|
func (j HeartbeatReqTime) String() string {
|
|
|
|
t := time.Time(j)
|
|
|
|
return t.Format("2006-01-02 15:04:05")
|
|
|
|
}
|
2019-05-17 03:05:38 +03:00
|
|
|
|
|
|
|
func (j HeartbeatReqTime) Time() time.Time {
|
|
|
|
return time.Time(j)
|
|
|
|
}
|