From 83e59745a50cc58ca3a0685d56b613ca13ac1967 Mon Sep 17 00:00:00 2001 From: Julian Labus Date: Thu, 4 Jan 2018 14:45:04 +0100 Subject: [PATCH] [TASK] add sites filter for outputs --- config_example.toml | 4 ++++ docs/docs_configuration.md | 12 ++++++++++++ output/all/filter.go | 1 + output/all/filter_sites.go | 24 +++++++++++++++++++++++ output/all/filter_sites_test.go | 34 +++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 output/all/filter_sites.go create mode 100644 output/all/filter_sites_test.go diff --git a/config_example.toml b/config_example.toml index 58ebc07..63a396a 100644 --- a/config_example.toml +++ b/config_example.toml @@ -52,6 +52,9 @@ offline_after = "10m" # List of nodeids of nodes that should be filtered out, so they won't appear in output #blacklist = ["00112233445566", "1337f0badead"] # +# List of site_codes of nodes that should be included in the output +#sites = ["ffhb"] +# # set has_location to true if you want to include only nodes that have geo-coordinates set # (setting this to false has no sensible effect, unless you'd want to hide nodes that have coordinates) #has_location = true @@ -71,6 +74,7 @@ path = "/var/www/html/meshviewer/data/meshviewer.json" #[nodes.output.meshviewer-ffrgb.filter] #no_owner = false +#sites = ["ffhb"] #blacklist = ["00112233445566", "1337f0badead"] #has_location = true diff --git a/docs/docs_configuration.md b/docs/docs_configuration.md index d8d84fe..1bc5219 100644 --- a/docs/docs_configuration.md +++ b/docs/docs_configuration.md @@ -196,6 +196,7 @@ enable = true [nodes.output.example.filter] no_owner = true blacklist = ["00112233445566", "1337f0badead"] +sites = ["ffhb"] has_location = true [nodes.output.example.filter.in_area] latitude_min = 34.30 @@ -222,6 +223,7 @@ For each output format there can be set different filters [nodes.output.example.filter] no_owner = true blacklist = ["00112233445566", "1337f0badead"] +sites = ["ffhb"] has_location = true [nodes.output.example.filter.in_area] latitude_min = 34.30 @@ -252,6 +254,16 @@ blacklist = ["00112233445566", "1337f0badead"] {% endmethod %} +### sites +{% method %} +List of site_codes of nodes that should be included in output +{% sample lang="toml" %} +```toml +sites = ["ffhb"] +``` +{% endmethod %} + + ### has_location {% method %} set has_location to true if you want to include only nodes that have geo-coordinates set diff --git a/output/all/filter.go b/output/all/filter.go index d8da125..5a159ef 100644 --- a/output/all/filter.go +++ b/output/all/filter.go @@ -18,6 +18,7 @@ func (f filterConfig) filtering(nodesOrigin *runtime.Nodes) *runtime.Nodes { f.HasLocation(), f.Blacklist(), f.InArea(), + f.Sites(), f.NoOwner(), } diff --git a/output/all/filter_sites.go b/output/all/filter_sites.go new file mode 100644 index 0000000..9452c68 --- /dev/null +++ b/output/all/filter_sites.go @@ -0,0 +1,24 @@ +package all + +import "github.com/FreifunkBremen/yanic/runtime" + +func (f filterConfig) Sites() filterFunc { + v, ok := f["sites"] + if !ok { + return noFilter + } + + list := make(map[string]interface{}) + for _, site := range v.([]interface{}) { + list[site.(string)] = true + } + + return func(node *runtime.Node) *runtime.Node { + if nodeinfo := node.Nodeinfo; nodeinfo != nil { + if _, ok := list[nodeinfo.System.SiteCode]; ok { + return node + } + } + return nil + } +} diff --git a/output/all/filter_sites_test.go b/output/all/filter_sites_test.go new file mode 100644 index 0000000..059248a --- /dev/null +++ b/output/all/filter_sites_test.go @@ -0,0 +1,34 @@ +package all + +import ( + "testing" + + "github.com/FreifunkBremen/yanic/data" + "github.com/FreifunkBremen/yanic/runtime" + "github.com/stretchr/testify/assert" +) + +func TestFilterSites(t *testing.T) { + assert := assert.New(t) + var config filterConfig + + config = map[string]interface{}{} + + filterSites := config.Sites() + + n := filterSites(&runtime.Node{Nodeinfo: &data.NodeInfo{}}) + assert.NotNil(n) + + config["sites"] = []interface{}{"ffhb"} + filterSites = config.Sites() + + n = filterSites(&runtime.Node{Nodeinfo: &data.NodeInfo{System: data.System{SiteCode: "ffxx"}}}) + assert.Nil(n) + + n = filterSites(&runtime.Node{Nodeinfo: &data.NodeInfo{System: data.System{SiteCode: "ffhb"}}}) + assert.NotNil(n) + + n = filterSites(&runtime.Node{}) + assert.Nil(n) + +}