From 81368deb788ab400b81771213da57b8b60b2e3d7 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Sun, 16 Apr 2017 15:12:23 +0200 Subject: [PATCH] Divide meshviewer/nodes.go into multiple files --- meshviewer/nodes.go | 87 +++++----------------------------------- meshviewer/nodes_test.go | 4 +- meshviewer/nodes_v1.go | 44 ++++++++++++++++++++ meshviewer/nodes_v2.go | 41 +++++++++++++++++++ 4 files changed, 97 insertions(+), 79 deletions(-) create mode 100644 meshviewer/nodes_v1.go create mode 100644 meshviewer/nodes_v2.go diff --git a/meshviewer/nodes.go b/meshviewer/nodes.go index 818ba1c..bbb09cb 100644 --- a/meshviewer/nodes.go +++ b/meshviewer/nodes.go @@ -4,81 +4,14 @@ import ( "log" "time" - "github.com/FreifunkBremen/yanic/jsontime" "github.com/FreifunkBremen/yanic/runtime" ) -// NodesV1 struct, to support legacy meshviewer (which are in master branch) -// i.e. https://github.com/ffnord/meshviewer/tree/master -type NodesV1 struct { - Version int `json:"version"` - Timestamp jsontime.Time `json:"timestamp"` // Timestamp of the generation - List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID -} +type nodeBuilder func(*runtime.Nodes) interface{} -// NodesV2 struct, to support new version of meshviewer (which are in legacy develop branch or newer) -// i.e. https://github.com/ffnord/meshviewer/tree/dev or https://github.com/ffrgb/meshviewer/tree/develop -type NodesV2 struct { - Version int `json:"version"` - Timestamp jsontime.Time `json:"timestamp"` // Timestamp of the generation - List []*Node `json:"nodes"` // the current nodemap, as array -} - -// GetNodesV1 transform data to legacy meshviewer -func GetNodesV1(nodes *runtime.Nodes) *NodesV1 { - meshviewerNodes := &NodesV1{ - Version: 1, - List: make(map[string]*Node), - Timestamp: jsontime.Now(), - } - - for nodeID := range nodes.List { - nodeOrigin := nodes.List[nodeID] - - if nodeOrigin.Statistics == nil { - continue - } - - node := &Node{ - Firstseen: nodeOrigin.Firstseen, - Lastseen: nodeOrigin.Lastseen, - Flags: Flags{ - Online: nodeOrigin.Online, - Gateway: nodeOrigin.Gateway, - }, - Nodeinfo: nodeOrigin.Nodeinfo, - } - node.Statistics = NewStatistics(nodeOrigin.Statistics) - meshviewerNodes.List[nodeID] = node - } - return meshviewerNodes -} - -// GetNodesV2 transform data to modern meshviewers -func GetNodesV2(nodes *runtime.Nodes) *NodesV2 { - meshviewerNodes := &NodesV2{ - Version: 2, - Timestamp: jsontime.Now(), - } - - for nodeID := range nodes.List { - nodeOrigin := nodes.List[nodeID] - if nodeOrigin.Statistics == nil { - continue - } - node := &Node{ - Firstseen: nodeOrigin.Firstseen, - Lastseen: nodeOrigin.Lastseen, - Flags: Flags{ - Online: nodeOrigin.Online, - Gateway: nodeOrigin.Gateway, - }, - Nodeinfo: nodeOrigin.Nodeinfo, - } - node.Statistics = NewStatistics(nodeOrigin.Statistics) - meshviewerNodes.List = append(meshviewerNodes.List, node) - } - return meshviewerNodes +var nodeFormats = map[int]nodeBuilder{ + 1: BuildNodesV1, + 2: BuildNodesV2, } // Start all services to manage Nodes @@ -101,14 +34,14 @@ func saveMeshviewer(config *runtime.Config, nodes *runtime.Nodes) { defer nodes.RUnlock() if path := config.Meshviewer.NodesPath; path != "" { version := config.Meshviewer.Version - switch version { - case 1: - runtime.SaveJSON(GetNodesV1(nodes), path) - case 2: - runtime.SaveJSON(GetNodesV2(nodes), path) - default: + builder := nodeFormats[version] + + if builder != nil { + runtime.SaveJSON(builder(nodes), path) + } else { log.Panicf("invalid nodes version: %d", version) } + } if path := config.Meshviewer.GraphPath; path != "" { diff --git a/meshviewer/nodes_test.go b/meshviewer/nodes_test.go index c8ef0cb..bd08d5e 100644 --- a/meshviewer/nodes_test.go +++ b/meshviewer/nodes_test.go @@ -10,13 +10,13 @@ import ( ) func TestNodesV1(t *testing.T) { - nodes := GetNodesV1(createTestNodes()) + nodes := BuildNodesV1(createTestNodes()).(*NodesV1) assert := assert.New(t) assert.Len(nodes.List, 2) } func TestNodesV2(t *testing.T) { - nodes := GetNodesV2(createTestNodes()) + nodes := BuildNodesV2(createTestNodes()).(*NodesV2) assert := assert.New(t) assert.Len(nodes.List, 2) diff --git a/meshviewer/nodes_v1.go b/meshviewer/nodes_v1.go new file mode 100644 index 0000000..c8656ca --- /dev/null +++ b/meshviewer/nodes_v1.go @@ -0,0 +1,44 @@ +package meshviewer + +import ( + "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/runtime" +) + +// NodesV1 struct, to support legacy meshviewer (which are in master branch) +// i.e. https://github.com/ffnord/meshviewer/tree/master +type NodesV1 struct { + Version int `json:"version"` + Timestamp jsontime.Time `json:"timestamp"` // Timestamp of the generation + List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID +} + +// BuildNodesV1 transforms data to legacy meshviewer +func BuildNodesV1(nodes *runtime.Nodes) interface{} { + meshviewerNodes := &NodesV1{ + Version: 1, + List: make(map[string]*Node), + Timestamp: jsontime.Now(), + } + + for nodeID := range nodes.List { + nodeOrigin := nodes.List[nodeID] + + if nodeOrigin.Statistics == nil { + continue + } + + node := &Node{ + Firstseen: nodeOrigin.Firstseen, + Lastseen: nodeOrigin.Lastseen, + Flags: Flags{ + Online: nodeOrigin.Online, + Gateway: nodeOrigin.Gateway, + }, + Nodeinfo: nodeOrigin.Nodeinfo, + } + node.Statistics = NewStatistics(nodeOrigin.Statistics) + meshviewerNodes.List[nodeID] = node + } + return meshviewerNodes +} diff --git a/meshviewer/nodes_v2.go b/meshviewer/nodes_v2.go new file mode 100644 index 0000000..61e524d --- /dev/null +++ b/meshviewer/nodes_v2.go @@ -0,0 +1,41 @@ +package meshviewer + +import ( + "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/runtime" +) + +// NodesV2 struct, to support new version of meshviewer (which are in legacy develop branch or newer) +// i.e. https://github.com/ffnord/meshviewer/tree/dev or https://github.com/ffrgb/meshviewer/tree/develop +type NodesV2 struct { + Version int `json:"version"` + Timestamp jsontime.Time `json:"timestamp"` // Timestamp of the generation + List []*Node `json:"nodes"` // the current nodemap, as array +} + +// BuildNodesV2 transforms data to modern meshviewers +func BuildNodesV2(nodes *runtime.Nodes) interface{} { + meshviewerNodes := &NodesV2{ + Version: 2, + Timestamp: jsontime.Now(), + } + + for nodeID := range nodes.List { + nodeOrigin := nodes.List[nodeID] + if nodeOrigin.Statistics == nil { + continue + } + node := &Node{ + Firstseen: nodeOrigin.Firstseen, + Lastseen: nodeOrigin.Lastseen, + Flags: Flags{ + Online: nodeOrigin.Online, + Gateway: nodeOrigin.Gateway, + }, + Nodeinfo: nodeOrigin.Nodeinfo, + } + node.Statistics = NewStatistics(nodeOrigin.Statistics) + meshviewerNodes.List = append(meshviewerNodes.List, node) + } + return meshviewerNodes +}