OSDN Git Service

fix
[bytom/vapor.git] / toolbar / precog / database / orm / node.go
1 package orm
2
3 import (
4         "encoding/json"
5         "errors"
6         "fmt"
7         "time"
8
9         "github.com/vapor/toolbar/precog/common"
10 )
11
12 type Node struct {
13         ID                       uint16 `gorm:"primary_key"`
14         Alias                    string
15         Xpub                     string
16         PublicKey                string
17         IP                       string
18         Port                     uint16
19         BestHeight               uint64
20         AvgLantencyMS            sql.NullInt64
21         LatestDailyUptimeMinutes uint64
22         Status                   uint8
23         CreatedAt                time.Time `json:"alias"`
24         UpdatedAt                time.Time `json:"alias"`
25 }
26
27 func (n *Node) MarshalJSON() ([]byte, error) {
28         status, ok := common.StatusLookupTable[n.Status]
29         if !ok {
30                 return nil, errors.New("fail to look up status")
31         }
32
33         avgLantencyMS := 0
34         if n.AvgLantencyMS.Valid {
35                 avgLantencyMS = n.AvgLantencyMS.Int64
36         }
37
38         return json.Marshal(&struct {
39                 Alias                    string    `json:"alias"`
40                 PublicKey                string    `json:"publickey"`
41                 Address                  string    `json:"address"`
42                 BestHeight               uint64    `json:"best_height"`
43                 AvgLantencyMS            int64     `json:"avg_lantency_ms"`
44                 LatestDailyUptimeMinutes uint64    `json:"latest_daily_uptime_minutes"`
45                 Status                   string    `json:"status"`
46                 UpdatedAt                time.Time `json:"updated_at"`
47         }{
48                 Alias:                    n.Alias,
49                 PublicKey:                n.PublicKey,
50                 Address:                  fmt.Sprintf("%s:%d", n.IP, n.Port),
51                 BestHeight:               n.BestHeight,
52                 AvgLantencyMS:            avgLantencyMS,
53                 LatestDailyUptimeMinutes: n.LatestDailyUptimeMinutes,
54                 Status:    status,
55                 UpdatedAt: time.Now(),
56         })
57 }