From f4bad7d8569a52bf16fa3dd9ecd03a9cd5d34d97 Mon Sep 17 00:00:00 2001 From: Geno Date: Wed, 17 Feb 2021 23:35:12 +0100 Subject: [PATCH] nodeinfo without empty values --- data/nodeinfo.go | 12 ++++++------ database/influxdb/global_test.go | 16 +++++++++++++--- database/influxdb/node.go | 8 +++++--- database/influxdb/node_test.go | 10 ++++++++-- output/geojson/geojson.go | 2 +- output/geojson/geojson_test.go | 7 ++++++- output/meshviewer-ffrgb/struct.go | 12 ++++++++---- output/meshviewer-ffrgb/struct_test.go | 10 ++++++++++ output/meshviewer/nodes_test.go | 7 ++++++- output/nodelist/nodelist_test.go | 7 ++++++- output/raw/raw_test.go | 7 ++++++- runtime/stats.go | 6 ++++-- runtime/stats_test.go | 9 +++++++-- 13 files changed, 86 insertions(+), 27 deletions(-) diff --git a/data/nodeinfo.go b/data/nodeinfo.go index 86cff15..2b1f6df 100644 --- a/data/nodeinfo.go +++ b/data/nodeinfo.go @@ -57,27 +57,27 @@ type Location struct { // Software struct type Software struct { - Autoupdater struct { + Autoupdater *struct { Enabled bool `json:"enabled,omitempty"` Branch string `json:"branch,omitempty"` } `json:"autoupdater,omitempty"` - BatmanAdv struct { + BatmanAdv *struct { Version string `json:"version,omitempty"` Compat int `json:"compat,omitempty"` } `json:"batman-adv,omitempty"` - Babeld struct { + Babeld *struct { Version string `json:"version,omitempty"` } `json:"babeld,omitempty"` - Fastd struct { + Fastd *struct { Enabled bool `json:"enabled,omitempty"` Version string `json:"version,omitempty"` PublicKey string `json:"public_key,omitempty"` } `json:"fastd,omitempty"` - Firmware struct { + Firmware *struct { Base string `json:"base,omitempty"` Release string `json:"release,omitempty"` } `json:"firmware,omitempty"` - StatusPage struct { + StatusPage *struct { API int `json:"api"` } `json:"status-page,omitempty"` } diff --git a/database/influxdb/global_test.go b/database/influxdb/global_test.go index 7eadd7f..4ae69c7 100644 --- a/database/influxdb/global_test.go +++ b/database/influxdb/global_test.go @@ -134,9 +134,19 @@ func createTestNodes() *runtime.Nodes { }, }, } - nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1" - nodeData.Nodeinfo.Software.Autoupdater.Enabled = true - nodeData.Nodeinfo.Software.Autoupdater.Branch = "stable" + nodeData.Nodeinfo.Software.Firmware = &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{ + Release: "2016.1.6+entenhausen1", + } + nodeData.Nodeinfo.Software.Autoupdater = &struct { + Enabled bool `json:"enabled,omitempty"` + Branch string `json:"branch,omitempty"` + }{ + Enabled: true, + Branch: "stable", + } nodes.AddNode(nodeData) nodes.AddNode(&runtime.Node{ diff --git a/database/influxdb/node.go b/database/influxdb/node.go index 85cdfbe..73e3dee 100644 --- a/database/influxdb/node.go +++ b/database/influxdb/node.go @@ -77,9 +77,11 @@ func (conn *Connection) InsertNode(node *runtime.Node) { // Hardware tags.SetString("model", nodeinfo.Hardware.Model) fields["nproc"] = nodeinfo.Hardware.Nproc - tags.SetString("firmware_base", nodeinfo.Software.Firmware.Base) - tags.SetString("firmware_release", nodeinfo.Software.Firmware.Release) - if nodeinfo.Software.Autoupdater.Enabled { + if nodeinfo.Software.Firmware != nil { + tags.SetString("firmware_base", nodeinfo.Software.Firmware.Base) + tags.SetString("firmware_release", nodeinfo.Software.Firmware.Release) + } + if nodeinfo.Software.Autoupdater != nil && nodeinfo.Software.Autoupdater.Enabled { tags.SetString("autoupdater", nodeinfo.Software.Autoupdater.Branch) } else { tags.SetString("autoupdater", runtime.DISABLED_AUTOUPDATER) diff --git a/database/influxdb/node_test.go b/database/influxdb/node_test.go index cfc5768..8ae136a 100644 --- a/database/influxdb/node_test.go +++ b/database/influxdb/node_test.go @@ -68,7 +68,7 @@ func TestToInflux(t *testing.T) { }, }, Software: data.Software{ - Autoupdater: struct { + Autoupdater: &struct { Enabled bool `json:"enabled,omitempty"` Branch string `json:"branch,omitempty"` }{ @@ -111,12 +111,18 @@ func TestToInflux(t *testing.T) { Mac: "BAFF1E5", }, Software: data.Software{ - Autoupdater: struct { + Autoupdater: &struct { Enabled bool `json:"enabled,omitempty"` Branch string `json:"branch,omitempty"` }{ Enabled: false, }, + Firmware: &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{ + Base: "gluon", + }, }, }, Statistics: &data.Statistics{ diff --git a/output/geojson/geojson.go b/output/geojson/geojson.go index d237cfc..44f7ba5 100644 --- a/output/geojson/geojson.go +++ b/output/geojson/geojson.go @@ -39,7 +39,7 @@ func newNodePoint(n *runtime.Node) (point *geojson.Feature) { point.Properties["model"] = nodeinfo.Hardware.Model description.WriteString("Model: " + nodeinfo.Hardware.Model + "\n") } - if fw := nodeinfo.Software.Firmware; fw.Release != "" { + if fw := nodeinfo.Software.Firmware; fw != nil && fw.Release != "" { point.Properties["firmware"] = fw.Release description.WriteString("Firmware: " + fw.Release + "\n") } diff --git a/output/geojson/geojson_test.go b/output/geojson/geojson_test.go index 6c0f4d4..b73196b 100644 --- a/output/geojson/geojson_test.go +++ b/output/geojson/geojson_test.go @@ -90,7 +90,12 @@ func createTestNodes() *runtime.Nodes { }, }, } - nodeData.Nodeinfo.Software.Firmware.Release = "2019.1~exp42" + nodeData.Nodeinfo.Software.Firmware = &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{ + Release: "2019.1~exp42", + } nodes.AddNode(nodeData) nodes.AddNode(&runtime.Node{ diff --git a/output/meshviewer-ffrgb/struct.go b/output/meshviewer-ffrgb/struct.go index 6807bf1..1b4f5e3 100644 --- a/output/meshviewer-ffrgb/struct.go +++ b/output/meshviewer-ffrgb/struct.go @@ -100,10 +100,14 @@ func NewNode(nodes *runtime.Nodes, n *runtime.Node) *Node { Latitude: location.Latitude, } } - node.Firmware = nodeinfo.Software.Firmware - node.Autoupdater = Autoupdater{ - Enabled: nodeinfo.Software.Autoupdater.Enabled, - Branch: nodeinfo.Software.Autoupdater.Branch, + if nodeinfo.Software.Firmware != nil { + node.Firmware = *nodeinfo.Software.Firmware + } + if autoupdater := nodeinfo.Software.Autoupdater; autoupdater != nil { + node.Autoupdater = Autoupdater{ + Enabled: autoupdater.Enabled, + Branch: autoupdater.Branch, + } } node.Nproc = nodeinfo.Hardware.Nproc node.Model = nodeinfo.Hardware.Model diff --git a/output/meshviewer-ffrgb/struct_test.go b/output/meshviewer-ffrgb/struct_test.go index a049d2a..74ad7ab 100644 --- a/output/meshviewer-ffrgb/struct_test.go +++ b/output/meshviewer-ffrgb/struct_test.go @@ -37,6 +37,16 @@ func TestRegister(t *testing.T) { Longitude: 13.3, Latitude: 8.7, }, + Software: data.Software{ + Autoupdater: &struct { + Enabled bool `json:"enabled,omitempty"` + Branch string `json:"branch,omitempty"` + }{}, + Firmware: &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{}, + }, }, Statistics: &data.Statistics{ Memory: data.Memory{ diff --git a/output/meshviewer/nodes_test.go b/output/meshviewer/nodes_test.go index 2a49dd0..5029f41 100644 --- a/output/meshviewer/nodes_test.go +++ b/output/meshviewer/nodes_test.go @@ -38,7 +38,12 @@ func createTestNodes() *runtime.Nodes { }, }, } - nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1" + nodeData.Nodeinfo.Software.Firmware = &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{ + Release: "2016.1.6+entenhausen1", + } nodes.AddNode(nodeData) nodes.AddNode(&runtime.Node{ diff --git a/output/nodelist/nodelist_test.go b/output/nodelist/nodelist_test.go index 33e95ff..8a0eed4 100644 --- a/output/nodelist/nodelist_test.go +++ b/output/nodelist/nodelist_test.go @@ -32,7 +32,12 @@ func createTestNodes() *runtime.Nodes { }, }, } - nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1" + nodeData.Nodeinfo.Software.Firmware = &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{ + Release: "2016.1.6+entenhausen1", + } nodes.AddNode(nodeData) nodes.AddNode(&runtime.Node{ diff --git a/output/raw/raw_test.go b/output/raw/raw_test.go index 567ccad..1aca734 100644 --- a/output/raw/raw_test.go +++ b/output/raw/raw_test.go @@ -32,7 +32,12 @@ func createTestNodes() *runtime.Nodes { }, }, } - nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1" + nodeData.Nodeinfo.Software.Firmware = &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{ + Release: "2016.1.6+entenhausen1", + } nodes.AddNode(nodeData) nodes.AddNode(&runtime.Node{ diff --git a/runtime/stats.go b/runtime/stats.go index bc792c0..50d30e3 100644 --- a/runtime/stats.go +++ b/runtime/stats.go @@ -92,8 +92,10 @@ func (s *GlobalStats) Add(node *Node) { } if info := node.Nodeinfo; info != nil { s.Models.Increment(info.Hardware.Model) - s.Firmwares.Increment(info.Software.Firmware.Release) - if info.Software.Autoupdater.Enabled { + if info.Software.Firmware != nil { + s.Firmwares.Increment(info.Software.Firmware.Release) + } + if info.Software.Autoupdater != nil && info.Software.Autoupdater.Enabled { s.Autoupdater.Increment(info.Software.Autoupdater.Branch) } else { s.Autoupdater.Increment(DISABLED_AUTOUPDATER) diff --git a/runtime/stats_test.go b/runtime/stats_test.go index d68fee0..877642a 100644 --- a/runtime/stats_test.go +++ b/runtime/stats_test.go @@ -94,7 +94,12 @@ func createTestNodes() *Nodes { }, }, } - nodeData.Nodeinfo.Software.Firmware.Release = "2016.1.6+entenhausen1" + nodeData.Nodeinfo.Software.Firmware = &struct { + Base string `json:"base,omitempty"` + Release string `json:"release,omitempty"` + }{ + Release: "2016.1.6+entenhausen1", + } nodes.AddNode(nodeData) nodes.AddNode(&Node{ @@ -110,7 +115,7 @@ func createTestNodes() *Nodes { Model: "TP-Link 841", }, Software: data.Software{ - Autoupdater: struct { + Autoupdater: &struct { Enabled bool `json:"enabled,omitempty"` Branch string `json:"branch,omitempty"` }{