Divide meshviewer/nodes.go into multiple files
This commit is contained in:
parent
98451979f6
commit
81368deb78
@ -4,81 +4,14 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/FreifunkBremen/yanic/jsontime"
|
|
||||||
"github.com/FreifunkBremen/yanic/runtime"
|
"github.com/FreifunkBremen/yanic/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NodesV1 struct, to support legacy meshviewer (which are in master branch)
|
type nodeBuilder func(*runtime.Nodes) interface{}
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// NodesV2 struct, to support new version of meshviewer (which are in legacy develop branch or newer)
|
var nodeFormats = map[int]nodeBuilder{
|
||||||
// i.e. https://github.com/ffnord/meshviewer/tree/dev or https://github.com/ffrgb/meshviewer/tree/develop
|
1: BuildNodesV1,
|
||||||
type NodesV2 struct {
|
2: BuildNodesV2,
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start all services to manage Nodes
|
// Start all services to manage Nodes
|
||||||
@ -101,14 +34,14 @@ func saveMeshviewer(config *runtime.Config, nodes *runtime.Nodes) {
|
|||||||
defer nodes.RUnlock()
|
defer nodes.RUnlock()
|
||||||
if path := config.Meshviewer.NodesPath; path != "" {
|
if path := config.Meshviewer.NodesPath; path != "" {
|
||||||
version := config.Meshviewer.Version
|
version := config.Meshviewer.Version
|
||||||
switch version {
|
builder := nodeFormats[version]
|
||||||
case 1:
|
|
||||||
runtime.SaveJSON(GetNodesV1(nodes), path)
|
if builder != nil {
|
||||||
case 2:
|
runtime.SaveJSON(builder(nodes), path)
|
||||||
runtime.SaveJSON(GetNodesV2(nodes), path)
|
} else {
|
||||||
default:
|
|
||||||
log.Panicf("invalid nodes version: %d", version)
|
log.Panicf("invalid nodes version: %d", version)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if path := config.Meshviewer.GraphPath; path != "" {
|
if path := config.Meshviewer.GraphPath; path != "" {
|
||||||
|
@ -10,13 +10,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNodesV1(t *testing.T) {
|
func TestNodesV1(t *testing.T) {
|
||||||
nodes := GetNodesV1(createTestNodes())
|
nodes := BuildNodesV1(createTestNodes()).(*NodesV1)
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
assert.Len(nodes.List, 2)
|
assert.Len(nodes.List, 2)
|
||||||
}
|
}
|
||||||
func TestNodesV2(t *testing.T) {
|
func TestNodesV2(t *testing.T) {
|
||||||
nodes := GetNodesV2(createTestNodes())
|
nodes := BuildNodesV2(createTestNodes()).(*NodesV2)
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
assert.Len(nodes.List, 2)
|
assert.Len(nodes.List, 2)
|
||||||
|
44
meshviewer/nodes_v1.go
Normal file
44
meshviewer/nodes_v1.go
Normal file
@ -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
|
||||||
|
}
|
41
meshviewer/nodes_v2.go
Normal file
41
meshviewer/nodes_v2.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user