OSDN Git Service

120e95010ae7075c1c9863a38b2cbf5c533dc46a
[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         LatestDailyUptimeMinutes uint64
21         Status                   uint8
22         CreatedAt                time.Time `json:"alias"`
23         UpdatedAt                time.Time `json:"alias"`
24 }
25
26 func (n *Node) MarshalJSON() ([]byte, error) {
27         status, ok := common.StatusLookupTable[n.Status]
28         if !ok {
29                 return nil, errors.New("fail to look up status")
30         }
31
32         return json.Marshal(&struct {
33                 Alias                    string    `json:"alias"`
34                 PublicKey                string    `json:"publickey"`
35                 Address                  string    `json:"address"`
36                 BestHeight               uint64    `json:"best_height"`
37                 LatestDailyUptimeMinutes uint64    `json:"latest_daily_uptime_minutes"`
38                 Status                   string    `json:"status"`
39                 UpdatedAt                time.Time `json:"updated_at"`
40         }{
41                 Alias:                    n.Alias,
42                 PublicKey:                n.PublicKey,
43                 Address:                  fmt.Sprintf("%s:%d", n.IP, n.Port),
44                 BestHeight:               n.BestHeight,
45                 LatestDailyUptimeMinutes: n.LatestDailyUptimeMinutes,
46                 Status: status,
47                 // TODO:
48                 UpdatedAt: time.Now(),
49         })
50 }