OSDN Git Service

clean orm
authorHAOYUatHZ <haoyu@protonmail.com>
Mon, 5 Aug 2019 06:33:44 +0000 (14:33 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Mon, 5 Aug 2019 06:33:44 +0000 (14:33 +0800)
toolbar/precog/database/orm/node.go
toolbar/precog/database/orm/node_liveness.go [new file with mode: 0644]
toolbar/precog/monitor/monitor.go

index 892c592..a5b60ac 100644 (file)
@@ -1,53 +1,15 @@
 package orm
 
 import (
-       "database/sql"
-       "encoding/json"
        "time"
-
-       "github.com/vapor/toolbar/precog/common"
 )
 
 type Node struct {
-       Alias           string
-       PublicKey       string
-       Host            string
-       Port            uint16
-       BestHeight      uint64
-       LantencyMS      sql.NullInt64
-       ActiveBeginTime time.Time
-       Status          uint8
-       CreatedAt       time.Time
-       UpdatedAt       time.Time
-}
-
-func (n *Node) MarshalJSON() ([]byte, error) {
-       status := common.StatusMap[n.Status]
-       var lantency int64
-       var activeMinutes uint64
-       switch n.Status {
-       case common.NodeHealthyStatus, common.NodeCongestedStatus, common.NodeOrphanStatus:
-               lantency = n.LantencyMS.Int64
-               activeMinutes = uint64(time.Since(n.ActiveBeginTime).Minutes())
-       }
-
-       return json.Marshal(&struct {
-               Alias         string `json:"alias"`
-               PublicKey     string `json:"public_key"`
-               Host          string `json:"host"`
-               Port          uint16 `json:"port"`
-               BestHeight    uint64 `json:"best_height"`
-               LantencyMS    int64  `json:"lantency_ms,omitempty"`
-               ActiveMinutes uint64 `json:"active_minutes,omitempty"`
-               Status        string `json:"status"`
-       }{
-               Alias:         n.Alias,
-               PublicKey:     n.PublicKey,
-               Host:          n.Host,
-               Port:          n.Port,
-               BestHeight:    n.BestHeight,
-               LantencyMS:    lantency,
-               ActiveMinutes: activeMinutes,
-               Status:        status,
-       })
+       Alias      string
+       PublicKey  string
+       Host       string
+       Port       uint16
+       BestHeight uint64
+       CreatedAt  time.Time
+       UpdatedAt  time.Time
 }
diff --git a/toolbar/precog/database/orm/node_liveness.go b/toolbar/precog/database/orm/node_liveness.go
new file mode 100644 (file)
index 0000000..7ba45eb
--- /dev/null
@@ -0,0 +1,18 @@
+package orm
+
+import (
+       "database/sql"
+       "time"
+)
+
+type NodeLiveness struct {
+       NodeID        uint16
+       ProbeTimes    uint64
+       ResponseTimes uint64
+       AvgLantencyMS sql.NullInt64
+       Status        uint8
+       CreatedAt     time.Time
+       UpdatedAt     time.Time
+
+       Node *Node `gorm:"foreignkey:NodeID"`
+}
index 3609f88..2787bc2 100644 (file)
@@ -36,11 +36,10 @@ func (m *monitor) Run() {
 func (m *monitor) updateBootstrapNodes() {
        for _, node := range m.cfg.Nodes {
                ormNode := &orm.Node{
-                       PublicKey:       node.PublicKey.String(),
-                       Alias:           node.Alias,
-                       Host:            node.Host,
-                       Port:            node.Port,
-                       ActiveBeginTime: time.Now(),
+                       PublicKey: node.PublicKey.String(),
+                       Alias:     node.Alias,
+                       Host:      node.Host,
+                       Port:      node.Port,
                }
 
                if err := m.db.Where(&orm.Node{PublicKey: ormNode.PublicKey}).