Use data package from node-informat
This commit is contained in:
parent
c416386683
commit
646f8c5a9a
41
main.go
41
main.go
@ -8,11 +8,10 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"reflect"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/ffdo/node-informant/gluon-collector/data"
|
||||
"github.com/monitormap/micro-daemon/models"
|
||||
"github.com/monitormap/micro-daemon/respond"
|
||||
"github.com/monitormap/micro-daemon/websocketserver"
|
||||
@ -51,27 +50,29 @@ func main() {
|
||||
go nodes.Saver(outputNodesFile, saveInterval)
|
||||
go aliases.Saver(outputAliasesFile, saveInterval)
|
||||
respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) {
|
||||
var result map[string]interface{}
|
||||
json.Unmarshal(res.Raw, &result)
|
||||
|
||||
nodeID, _ := result["node_id"].(string)
|
||||
|
||||
if nodeID == "" {
|
||||
log.Println("unable to parse node_id")
|
||||
return
|
||||
switch coll.CollectType {
|
||||
case "neighbours":
|
||||
result := &data.NeighbourStruct{}
|
||||
if json.Unmarshal(res.Raw, result) == nil {
|
||||
node := nodes.Get(result.NodeId)
|
||||
node.Neighbours = result
|
||||
}
|
||||
|
||||
node := nodes.Get(nodeID)
|
||||
|
||||
// Set result
|
||||
elem := reflect.ValueOf(node).Elem()
|
||||
field := elem.FieldByName(strings.Title(coll.CollectType))
|
||||
|
||||
if !reflect.DeepEqual(field, result) {
|
||||
wsserverForNodes.SendAll(node)
|
||||
case "nodeinfo":
|
||||
result := &data.NodeInfo{}
|
||||
if json.Unmarshal(res.Raw, result) == nil {
|
||||
node := nodes.Get(result.NodeId)
|
||||
node.Nodeinfo = result
|
||||
}
|
||||
case "statistics":
|
||||
result := &data.StatisticsStruct{}
|
||||
if json.Unmarshal(res.Raw, result) == nil {
|
||||
node := nodes.Get(result.NodeId)
|
||||
node.Statistics = result
|
||||
}
|
||||
default:
|
||||
log.Println("unknown CollectType:", coll.CollectType)
|
||||
}
|
||||
|
||||
field.Set(reflect.ValueOf(result))
|
||||
})
|
||||
go respondDaemon.ListenAndSend(collectInterval)
|
||||
|
||||
|
@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/ffdo/node-informant/gluon-collector/data"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
@ -9,24 +10,28 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//Node struct
|
||||
// Node struct
|
||||
type Node struct {
|
||||
Firstseen time.Time `json:"firstseen"`
|
||||
Lastseen time.Time `json:"lastseen"`
|
||||
Statistics interface{} `json:"statistics"`
|
||||
Nodeinfo interface{} `json:"nodeinfo"`
|
||||
Neighbours interface{} `json:"neighbours"`
|
||||
Statistics *data.StatisticsStruct `json:"statistics"`
|
||||
Nodeinfo *data.NodeInfo `json:"nodeinfo"`
|
||||
Neighbours *data.NeighbourStruct `json:"-"`
|
||||
}
|
||||
|
||||
//Nodes struct: cache DB of Node's structs
|
||||
type NodeElement struct {
|
||||
NodeId string
|
||||
}
|
||||
|
||||
// Nodes struct: cache DB of Node's structs
|
||||
type Nodes struct {
|
||||
Version int `json:"version"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
List map[string]*Node `json:"nodes"` // the current nodemap
|
||||
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
//NewNodes create Nodes structs (cache DB)
|
||||
// NewNodes create Nodes structs (cache DB)
|
||||
func NewNodes() *Nodes {
|
||||
nodes := &Nodes{
|
||||
Version: 1,
|
||||
@ -36,7 +41,7 @@ func NewNodes() *Nodes {
|
||||
return nodes
|
||||
}
|
||||
|
||||
//Get a Node by nodeid
|
||||
// Get a Node by nodeid
|
||||
func (nodes *Nodes) Get(nodeID string) *Node {
|
||||
now := time.Now()
|
||||
|
||||
@ -56,7 +61,7 @@ func (nodes *Nodes) Get(nodeID string) *Node {
|
||||
return node
|
||||
}
|
||||
|
||||
//Saver to save the cached DB to json file
|
||||
// Saves the cached DB to json file periodically
|
||||
func (nodes *Nodes) Saver(outputFile string, saveInterval time.Duration) {
|
||||
c := time.Tick(saveInterval)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user