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