2017-05-20 14:46:29 +02:00
|
|
|
package nodelist
|
|
|
|
|
|
|
|
import (
|
2018-01-07 21:00:56 +01:00
|
|
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
2017-05-20 14:46:29 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeList rewritten after: https://github.com/ffnord/ffmap-backend/blob/c33ebf62f013e18bf71b5a38bd058847340db6b7/lib/nodelist.py
|
|
|
|
type NodeList struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Timestamp jsontime.Time `json:"updated_at"` // Timestamp of the generation
|
|
|
|
List []*Node `json:"nodes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Position *Position `json:"position,omitempty"`
|
|
|
|
Status struct {
|
|
|
|
Online bool `json:"online"`
|
|
|
|
LastContact jsontime.Time `json:"lastcontact"`
|
|
|
|
Clients uint32 `json:"clients"`
|
|
|
|
} `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Position struct {
|
|
|
|
Lat float64 `json:"lat"`
|
|
|
|
Long float64 `json:"long"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNode(n *runtime.Node) (node *Node) {
|
|
|
|
if nodeinfo := n.Nodeinfo; nodeinfo != nil {
|
|
|
|
node = &Node{
|
|
|
|
ID: nodeinfo.NodeID,
|
|
|
|
Name: nodeinfo.Hostname,
|
|
|
|
}
|
|
|
|
if location := nodeinfo.Location; location != nil {
|
2017-11-23 13:01:39 +01:00
|
|
|
node.Position = &Position{Lat: location.Latitude, Long: location.Longitude}
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
node.Status.Online = n.Online
|
|
|
|
node.Status.LastContact = n.Lastseen
|
2017-12-27 02:12:15 +01:00
|
|
|
if statistics := n.Statistics; statistics != nil && n.Online {
|
2017-05-20 14:46:29 +02:00
|
|
|
node.Status.Clients = statistics.Clients.Total
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func transform(nodes *runtime.Nodes) *NodeList {
|
|
|
|
nodelist := &NodeList{
|
|
|
|
Version: "1.0.1",
|
|
|
|
Timestamp: jsontime.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nodeOrigin := range nodes.List {
|
|
|
|
node := NewNode(nodeOrigin)
|
|
|
|
if node != nil {
|
|
|
|
nodelist.List = append(nodelist.List, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodelist
|
|
|
|
}
|