diff --git a/main.go b/main.go index bf2229b..2d70c35 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,8 @@ func main() { } nodes = models.NewNodes(config) + nodes.Start() + if config.Respondd.Enable { collectInterval := time.Second * time.Duration(config.Respondd.CollectInterval) collector = respond.NewCollector(db, nodes, collectInterval, config.Respondd.Interface) diff --git a/models/nodes.go b/models/nodes.go index f481dc3..d3a4ad6 100644 --- a/models/nodes.go +++ b/models/nodes.go @@ -39,12 +39,15 @@ func NewNodes(config *Config) *Nodes { if config.Nodes.NodesPath != "" { nodes.load() } - go nodes.worker() nodes.Version = 2 return nodes } +func (nodes *Nodes) Start() { + go nodes.worker() +} + // Update a Node func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) *Node { now := jsontime.Now() @@ -95,8 +98,8 @@ func (nodes *Nodes) GetNodesMini() *meshviewer.Nodes { List: make(map[string]*meshviewer.Node), Timestamp: nodes.Timestamp, } - for nodeID := range nodes.List { + for nodeID := range nodes.List { node, _ := meshviewerNodes.List[nodeID] nodeOrigin := nodes.List[nodeID] if node == nil { diff --git a/models/nodes_test.go b/models/nodes_test.go index 970a110..0335ffd 100644 --- a/models/nodes_test.go +++ b/models/nodes_test.go @@ -49,7 +49,7 @@ func TestLoadAndSave(t *testing.T) { config := &Config{} config.Nodes.NodesPath = "testdata/nodes.json" - nodes := &Nodes{config: config} + nodes := NewNodes(config) nodes.load() tmpfile, _ := ioutil.TempFile("/tmp", "nodes") @@ -73,3 +73,29 @@ func TestUpdateNodes(t *testing.T) { assert.Equal(1, len(nodes.List)) } + +func TestGlobalStats(t *testing.T) { + stats := createTestNodes().GlobalStats() + + assert := assert.New(t) + assert.EqualValues(uint32(1), stats.Nodes) + assert.EqualValues(uint32(23), stats.Clients) +} + +func TestNodesMini(t *testing.T) { + mini := createTestNodes().GetNodesMini() + + assert := assert.New(t) + assert.Equal(1, len(mini.List)) +} + +func createTestNodes() *Nodes { + nodes := NewNodes(&Config{}) + + res := &data.ResponseData{ + Statistics: &data.Statistics{}, + } + res.Statistics.Clients.Total = 23 + nodes.Update("abcdef012345", res) + return nodes +}