OSDN Git Service

fix
[bytom/vapor.git] / toolbar / precog / monitor / stats.go
1 package monitor
2
3 import (
4         "database/sql"
5         "fmt"
6
7         "github.com/jinzhu/gorm"
8
9         "github.com/vapor/crypto/ed25519/chainkd"
10         "github.com/vapor/netsync/peers"
11         "github.com/vapor/toolbar/precog/common"
12         "github.com/vapor/toolbar/precog/config"
13         "github.com/vapor/toolbar/precog/database/orm"
14 )
15
16 // TODO: get lantency
17 // TODO: get best_height
18 // TODO: decide check_height("best best_height" - "confirmations")
19 // TODO: get blockhash by check_height, get latency
20 // TODO: update lantency, active_time and status
21
22 // create or update: https://github.com/jinzhu/gorm/issues/1307
23 func (m *monitor) upSertNode(node *config.Node) error {
24         if node.XPub != nil {
25                 node.PublicKey = fmt.Sprintf("%v", node.XPub.PublicKey().String())
26         }
27
28         ormNode := &orm.Node{PublicKey: node.PublicKey}
29         if err := m.db.Where(&orm.Node{PublicKey: node.PublicKey}).First(ormNode).Error; err != nil && err != gorm.ErrRecordNotFound {
30                 return err
31         }
32
33         if node.Alias != "" {
34                 ormNode.Alias = node.Alias
35         }
36         if node.XPub != nil {
37                 ormNode.Xpub = node.XPub.String()
38         }
39         ormNode.Host = node.Host
40         ormNode.Port = node.Port
41         return m.db.Where(&orm.Node{PublicKey: ormNode.PublicKey}).
42                 Assign(&orm.Node{
43                         Xpub:  ormNode.Xpub,
44                         Alias: ormNode.Alias,
45                         Host:  ormNode.Host,
46                         Port:  ormNode.Port,
47                 }).FirstOrCreate(ormNode).Error
48 }
49
50 func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
51         xPub := &chainkd.XPub{}
52         if err := xPub.UnmarshalText([]byte(peerInfo.ID)); err != nil {
53                 return err
54         }
55
56         ormNode := &orm.Node{}
57         if err := m.db.Model(&orm.Node{}).Where(&orm.Node{PublicKey: xPub.PublicKey().String()}).
58                 UpdateColumn(&orm.Node{
59                         Alias:      peerInfo.Moniker,
60                         Xpub:       peerInfo.ID,
61                         BestHeight: peerInfo.Height,
62                         // LatestDailyUptimeMinutes uint64
63                 }).First(ormNode).Error; err != nil {
64                 return err
65         }
66
67         ormNodeLiveness := &orm.NodeLiveness{
68                 NodeID:        ormNode.ID,
69                 BestHeight:    ormNode.BestHeight,
70                 AvgLantencyMS: sql.NullInt64{Int64: 1, Valid: true},
71                 // PingTimes     uint64
72                 // PongTimes     uint64
73         }
74         if err := m.db.Model(&orm.NodeLiveness{}).Where("node_id = ? AND status != ?", ormNode.ID, common.NodeOfflineStatus).
75                 UpdateColumn(&orm.NodeLiveness{
76                         BestHeight:    ormNodeLiveness.BestHeight,
77                         AvgLantencyMS: ormNodeLiveness.AvgLantencyMS,
78                 }).FirstOrCreate(ormNodeLiveness).Error; err != nil {
79                 return err
80         }
81
82         return nil
83 }