OSDN Git Service

update
[bytom/vapor.git] / toolbar / precog / database / orm / node.go
1 package orm
2
3 import (
4         "database/sql"
5         "encoding/json"
6         "time"
7
8         "github.com/vapor/toolbar/precog/common"
9 )
10
11 type Node struct {
12         Alias           string
13         PubKey          chainkd.XPub
14         Host            string
15         Port            uint16
16         BestHeight      uint64
17         LantencyMS      sql.NullInt64
18         ActiveBeginTime time.Time
19         Status          uint8
20         CreatedAt       time.Time
21         UpdatedAt       time.Time
22 }
23
24 func (n *Node) MarshalJSON() ([]byte, error) {
25         status := common.StatusMap[n.Status]
26         var lantency uint64
27         var activeTime time.Duration
28         if n.Status != common.NodeOfflineStatus {
29                 lantency = n.LantencyMS
30                 activeTime = time.Since(n.ActiveBeginTime)
31         }
32
33         return json.Marshal(&struct {
34                 Alias      string        `json:"alias"`
35                 PubKey     chainkd.XPub  `json:"pubkey"`
36                 Host       string        `json:"host"`
37                 Port       uint16        `json:"port"`
38                 BestHeight uint64        `json:"best_height"`
39                 LantencyMS uint64        `json:"lantency_ms,omitempty"`
40                 ActiveTime time.Duration `json:"active_time,omitempty"`
41                 Status     string        `json:"status"`
42         }{
43                 Alias:      n.Alias,
44                 PubKey:     n.PubKey,
45                 Host:       n.Host,
46                 Port:       n.Port,
47                 BestHeight: n.BestHeight,
48                 LantencyMS: lantency,
49                 activeTime: activeTime,
50                 Status:     status,
51         })
52 }