Skip to content

Commit

Permalink
Merge f5afacf into 88975d2
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire authored Jun 3, 2017
2 parents 88975d2 + f5afacf commit 54058b3
Show file tree
Hide file tree
Showing 24 changed files with 689 additions and 85 deletions.
16 changes: 12 additions & 4 deletions cmd/yanic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"syscall"

"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/database/all"
"github.com/FreifunkBremen/yanic/meshviewer"
allDB "github.com/FreifunkBremen/yanic/database/all"
"github.com/FreifunkBremen/yanic/output"
allOutput "github.com/FreifunkBremen/yanic/output/all"
"github.com/FreifunkBremen/yanic/respond"
"github.com/FreifunkBremen/yanic/rrd"
"github.com/FreifunkBremen/yanic/runtime"
Expand Down Expand Up @@ -42,7 +43,7 @@ func main() {
panic(err)
}

connections, err = all.Connect(config.Database.Connection)
connections, err = allDB.Connect(config.Database.Connection)
if err != nil {
panic(err)
}
Expand All @@ -56,7 +57,14 @@ func main() {

nodes = runtime.NewNodes(config)
nodes.Start()
meshviewer.Start(config, nodes)

outputs, err := allOutput.Register(nodes, config.Nodes.Output)
if err != nil {
panic(err)
}

output.Start(outputs, config)
defer output.Close()

if config.Respondd.Enable {
collector = respond.NewCollector(connections, nodes, config.Respondd.Interface, config.Respondd.Port)
Expand Down
24 changes: 23 additions & 1 deletion config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ offline_after = "10m"
prune_after = "7d"


[meshviewer]
[[nodes.output.meshviewer]]
enable = true
# structur of nodes.json, which to support
# version 1 is to support legacy meshviewer (which are in master branch)
# i.e. https://github.com/ffnord/meshviewer/tree/master
Expand All @@ -48,6 +49,27 @@ nodes_path = "/var/www/html/meshviewer/data/nodes.json"
# path where to store graph.json
graph_path = "/var/www/html/meshviewer/data/graph.json"

[nodes.output.meshviewer.filter]
# no_owner = true
has_location = true
blacklist = ["vpnid"]

[nodes.output.meshviewer.filter.in_area]
latitude_min = 34.30
latitude_max = 71.85
longitude_min = -24.96
longitude_max = 39.72

[[nodes.output.template]]
enable = false
template_path = "/var/lib/collector/html-template.tmp"
output_path = "/var/www/html/index.html"

[[nodes.output.nodelist]]
enable = true
path = "/var/www/html/meshviewer/data/nodelist.json"


[database]
# cleaning data of measurement node,
# which are older than 7d
Expand Down
16 changes: 16 additions & 0 deletions contrib/example-template.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function ffhbCurrentStats(data) {
$("#freifunk").html("
<h1><a href="https://bremen.freifunk.net/" style="color: #444;">bremen.freifunk.net</a></h1>
<p>
Nutzer: <span id="freifunk_clients">0</span><br>
<i style="font-style: italic;">(auf <span id="freifunk_nodes">0</span> Geräte verteilt)</i>
</p>
<p style="text-align: right;">
<a href="https://events.ffhb.de/meshviewer">mehr</a>
</p>");

$("#freifunk_clients").html(data.Clients);
$("#freifunk_nodes").html(data.Nodes);
};

ffhbCurrentStats({{json .GlobalStatistic}});
2 changes: 1 addition & 1 deletion data/nodeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package data
type NodeInfo struct {
NodeID string `json:"node_id"`
Network Network `json:"network"`
Owner *Owner `json:"-"` // Removed for privacy reasons
Owner *Owner `json:"owner"`
System System `json:"system"`
Hostname string `json:"hostname"`
Location *Location `json:"location,omitempty"`
Expand Down
50 changes: 0 additions & 50 deletions meshviewer/nodes.go

This file was deleted.

37 changes: 37 additions & 0 deletions output/all/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package all

import (
"github.com/FreifunkBremen/yanic/output"
"github.com/FreifunkBremen/yanic/runtime"
)

type Output struct {
output.Output
nodes *runtime.Nodes
list []output.Output
}

func Register(nodes *runtime.Nodes, configuration interface{}) (output.Output, error) {
var list []output.Output
allOutputs := configuration.(map[string][]interface{})
for outputType, outputRegister := range output.Adapters {
outputConfigs := allOutputs[outputType]
for _, config := range outputConfigs {
output, err := outputRegister(nodes, config)
if err != nil {
return nil, err
}
if output == nil {
continue
}
list = append(list, output)
}
}
return &Output{list: list, nodes: nodes}, nil
}

func (o *Output) Save() {
for _, item := range o.list {
item.Save()
}
}
6 changes: 6 additions & 0 deletions output/all/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package all

import (
_ "github.com/FreifunkBremen/yanic/output/meshviewer"
_ "github.com/FreifunkBremen/yanic/output/template"
)
37 changes: 37 additions & 0 deletions output/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package output

import (
"time"

"github.com/FreifunkBremen/yanic/runtime"
)

var quit chan struct{}

// Start workers of database
// WARNING: Do not override this function
// you should use New()
func Start(output Output, config *runtime.Config) {
quit = make(chan struct{})
go saveWorker(output, config.Nodes.SaveInterval.Duration)
}

func Close() {
if quit != nil {
close(quit)
}
}

// save periodically to output
func saveWorker(output Output, saveInterval time.Duration) {
ticker := time.NewTicker(saveInterval)
for {
select {
case <-ticker.C:
output.Save()
case <-quit:
ticker.Stop()
return
}
}
}
157 changes: 157 additions & 0 deletions output/meshviewer/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package meshviewer

import (
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/runtime"
)

type filter func(node *runtime.Node) *runtime.Node

// Config Filter
type filterConfig map[string]interface{}

func (f filterConfig) Blacklist() *map[string]interface{} {
if v, ok := f["blacklist"]; ok {
list := make(map[string]interface{})
for _, nodeid := range v.([]interface{}) {
list[nodeid.(string)] = true
}
return &list
}
return nil
}

func (f filterConfig) NoOwner() bool {
if v, ok := f["no_owner"]; ok {
return v.(bool)
}
return true
}
func (f filterConfig) HasLocation() *bool {
if v, ok := f["has_location"].(bool); ok {
return &v
}
return nil
}

type area struct {
xA float64
xB float64
yA float64
yB float64
}

func (f filterConfig) InArea() *area {
if areaConfigInt, ok := f["in_area"]; ok {
areaConfig := areaConfigInt.(map[string]interface{})
a := area{}
if v, ok := areaConfig["latitude_min"]; ok {
a.xA = v.(float64)
}
if v, ok := areaConfig["latitude_max"]; ok {
a.xB = v.(float64)
}
if v, ok := areaConfig["longitude_min"]; ok {
a.yA = v.(float64)
}
if v, ok := areaConfig["longitude_max"]; ok {
a.yB = v.(float64)
}
return &a
}
return nil
}

// Create Filter
func createFilter(config filterConfig) filter {
return func(n *runtime.Node) *runtime.Node {
//maybe cloning of this object is better?
node := n

if config.NoOwner() {
node = filterNoOwner(node)
}
if ok := config.HasLocation(); ok != nil {
node = filterHasLocation(node, *ok)
}
if area := config.InArea(); area != nil {
node = filterLocationInArea(node, *area)
}
if list := config.Blacklist(); list != nil {
node = filterBlacklist(node, *list)
}

return node
}
}

func filterBlacklist(node *runtime.Node, list map[string]interface{}) *runtime.Node {
if node != nil {
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
if _, ok := list[nodeinfo.NodeID]; !ok {
return node
}
}
}
return nil
}

func filterNoOwner(node *runtime.Node) *runtime.Node {
if node == nil {
return nil
}
return &runtime.Node{
Address: node.Address,
Firstseen: node.Firstseen,
Lastseen: node.Lastseen,
Online: node.Online,
Statistics: node.Statistics,
Nodeinfo: &data.NodeInfo{
NodeID: node.Nodeinfo.NodeID,
Network: node.Nodeinfo.Network,
System: node.Nodeinfo.System,
Owner: nil,
Hostname: node.Nodeinfo.Hostname,
Location: node.Nodeinfo.Location,
Software: node.Nodeinfo.Software,
Hardware: node.Nodeinfo.Hardware,
VPN: node.Nodeinfo.VPN,
Wireless: node.Nodeinfo.Wireless,
},
Neighbours: node.Neighbours,
}
}

func filterHasLocation(node *runtime.Node, withLocation bool) *runtime.Node {
if node != nil {
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
if withLocation {
if location := nodeinfo.Location; location != nil {
return node
}
} else {
if location := nodeinfo.Location; location == nil {
return node
}
}
}
}
return nil
}

func filterLocationInArea(node *runtime.Node, a area) *runtime.Node {
if node != nil {
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
if location := nodeinfo.Location; location != nil {
if location.Latitude >= a.xA && location.Latitude <= a.xB {
if location.Longtitude >= a.yA && location.Longtitude <= a.yB {
return node
}
}
} else {
return node
}
}
}
return nil
}
File renamed without changes.
Loading

0 comments on commit 54058b3

Please sign in to comment.