yanic/models/nodes.go

138 lines
2.6 KiB
Go
Raw Normal View History

package models
2015-12-29 14:05:47 +01:00
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"sync"
"time"
2016-03-12 03:23:12 +01:00
2016-03-15 23:26:30 +01:00
"github.com/FreifunkBremen/respond-collector/data"
2016-03-20 17:10:39 +01:00
"github.com/FreifunkBremen/respond-collector/jsontime"
2015-12-29 14:05:47 +01:00
)
2016-03-07 01:37:38 +01:00
// Node struct
2015-12-29 14:05:47 +01:00
type Node struct {
2016-03-20 17:10:39 +01:00
Firstseen jsontime.Time `json:"firstseen"`
Lastseen jsontime.Time `json:"lastseen"`
Statistics *data.Statistics `json:"statistics"`
Nodeinfo *data.NodeInfo `json:"nodeinfo"`
Neighbours *data.Neighbours `json:"-"`
2015-12-29 14:05:47 +01:00
}
2016-03-07 01:37:38 +01:00
type NodeElement struct {
NodeId string
}
// Nodes struct: cache DB of Node's structs
2015-12-29 14:05:47 +01:00
type Nodes struct {
Version int `json:"version"`
2016-03-20 17:10:39 +01:00
Timestamp jsontime.Time `json:"timestamp"`
2016-03-07 01:37:38 +01:00
List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID
2016-03-20 18:30:44 +01:00
config *Config
2015-12-29 14:05:47 +01:00
sync.Mutex
}
2016-03-20 18:30:44 +01:00
// NewNodes create Nodes structs
func NewNodes(config *Config) *Nodes {
2015-12-29 14:05:47 +01:00
nodes := &Nodes{
2016-03-20 18:30:44 +01:00
List: make(map[string]*Node),
config: config,
2015-12-29 14:05:47 +01:00
}
2016-03-20 18:30:44 +01:00
if config.Nodes.NodesPath != "" {
nodes.load()
}
go nodes.worker()
nodes.Version = 2
2015-12-29 14:05:47 +01:00
return nodes
}
2016-03-20 16:25:33 +01:00
// Update a Node
func (nodes *Nodes) Update(nodeID string, res *data.ResponseData) {
2016-03-20 17:10:39 +01:00
now := jsontime.Now()
2015-12-29 14:05:47 +01:00
nodes.Lock()
2016-02-25 21:24:54 +01:00
node, _ := nodes.List[nodeID]
2015-12-29 14:05:47 +01:00
if node == nil {
node = &Node{
2016-03-20 16:25:33 +01:00
Firstseen: now,
2015-12-29 14:05:47 +01:00
}
2016-02-25 21:24:54 +01:00
nodes.List[nodeID] = node
2015-12-29 14:05:47 +01:00
}
nodes.Unlock()
node.Lastseen = now
2016-03-20 16:25:33 +01:00
// Update neighbours
if val := res.Neighbours; val != nil {
node.Neighbours = val
}
// Update nodeinfo
if val := res.NodeInfo; val != nil {
node.Nodeinfo = val
}
// Update statistics
if val := res.Statistics; val != nil {
node.Statistics = val
}
2015-12-29 14:05:47 +01:00
}
2016-03-20 18:30:44 +01:00
// Periodically saves the cached DB to json file
func (nodes *Nodes) worker() {
c := time.Tick(time.Second * 5)
2015-12-29 14:05:47 +01:00
for range c {
2016-03-07 02:29:31 +01:00
log.Println("saving", len(nodes.List), "nodes")
2016-03-20 18:30:44 +01:00
nodes.Timestamp = jsontime.Now()
2016-03-07 02:29:31 +01:00
nodes.Lock()
2016-03-20 18:30:44 +01:00
// serialize nodes
save(nodes, nodes.config.Nodes.NodesPath)
if path := nodes.config.Nodes.GraphsPath; path != "" {
2016-03-23 16:22:12 +01:00
save(nodes.BuildGraph(), path)
2016-03-07 12:05:53 +01:00
}
2016-03-20 18:30:44 +01:00
2016-03-07 02:29:31 +01:00
nodes.Unlock()
2015-12-29 14:05:47 +01:00
}
}
2016-03-20 18:30:44 +01:00
func (nodes *Nodes) load() {
path := nodes.config.Nodes.NodesPath
log.Println("loading", path)
if data, err := ioutil.ReadFile(path); err == nil {
if err := json.Unmarshal(data, nodes); err == nil {
log.Println("loaded", len(nodes.List), "nodes")
} else {
log.Println("failed to unmarshal nodes:", err)
}
} else {
log.Println("failed loading cached nodes:", err)
}
}
2016-03-07 02:29:31 +01:00
func save(input interface{}, outputFile string) {
data, err := json.Marshal(input)
if err != nil {
2016-02-19 11:30:42 +01:00
log.Panic(err)
2016-02-19 11:13:30 +01:00
}
2015-12-29 14:05:47 +01:00
tmpFile := outputFile + ".tmp"
2016-03-20 18:30:44 +01:00
if err := ioutil.WriteFile(tmpFile, data, 0644); err != nil {
2016-02-19 11:30:42 +01:00
log.Panic(err)
}
2016-03-20 18:30:44 +01:00
if err := os.Rename(tmpFile, outputFile); err != nil {
2016-02-19 11:30:42 +01:00
log.Panic(err)
}
2015-12-29 14:05:47 +01:00
}