Use data package from node-informat

This commit is contained in:
Julian Kornberger 2016-03-07 01:37:38 +01:00
parent c416386683
commit 646f8c5a9a
2 changed files with 38 additions and 32 deletions

41
main.go
View File

@ -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)

View File

@ -2,6 +2,7 @@ package models
import (
"encoding/json"
"github.com/ffdo/node-informant/gluon-collector/data"
"io/ioutil"
"log"
"os"
@ -13,16 +14,20 @@ import (
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:"-"`
}
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
}
@ -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)