OSDN Git Service

add
[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          string
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 int64
27         var activeMinutes uint64
28         switch n.Status {
29         case common.NodeHealthyStatus, common.NodeCongestedStatus, common.NodeOrphanStatus:
30                 lantency = n.LantencyMS.Int64
31                 activeMinutes = uint64(time.Since(n.ActiveBeginTime).Minutes())
32         }
33
34         return json.Marshal(&struct {
35                 Alias         string `json:"alias"`
36                 PubKey        string `json:"pubkey"`
37                 Host          string `json:"host"`
38                 Port          uint16 `json:"port"`
39                 BestHeight    uint64 `json:"best_height"`
40                 LantencyMS    int64  `json:"lantency_ms,omitempty"`
41                 ActiveMinutes uint64 `json:"active_minutes,omitempty"`
42                 Status        string `json:"status"`
43         }{
44                 Alias:         n.Alias,
45                 PubKey:        n.PubKey,
46                 Host:          n.Host,
47                 Port:          n.Port,
48                 BestHeight:    n.BestHeight,
49                 LantencyMS:    lantency,
50                 ActiveMinutes: activeMinutes,
51                 Status:        status,
52         })
53 }