7 "github.com/jinzhu/gorm"
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"
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
22 // create or update: https://github.com/jinzhu/gorm/issues/1307
23 func (m *monitor) upSertNode(node *config.Node) error {
25 node.PublicKey = fmt.Sprintf("%v", node.XPub.PublicKey().String())
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 {
34 ormNode.Alias = node.Alias
37 ormNode.Xpub = node.XPub.String()
39 ormNode.Host = node.Host
40 ormNode.Port = node.Port
41 return m.db.Where(&orm.Node{PublicKey: ormNode.PublicKey}).
47 }).FirstOrCreate(ormNode).Error
50 func (m *monitor) savePeerInfo(peerInfo *peers.PeerInfo) error {
51 xPub := &chainkd.XPub{}
52 if err := xPub.UnmarshalText([]byte(peerInfo.ID)); err != nil {
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,
61 BestHeight: peerInfo.Height,
62 // LatestDailyUptimeMinutes uint64
63 }).First(ormNode).Error; err != nil {
67 ormNodeLiveness := &orm.NodeLiveness{
69 BestHeight: ormNode.BestHeight,
70 AvgLantencyMS: sql.NullInt64{Int64: 1, Valid: true},
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 {