From be01a4d98c5f62b2ab2a270f4ed5890824d366ef Mon Sep 17 00:00:00 2001
From: Samuel Roberts <sam.roberts@geant.org>
Date: Wed, 23 Feb 2022 14:17:12 +0000
Subject: [PATCH] add grouping by location to aggregate provisioning
 potentially other fields too, although it's not immediately clear if this
 would be useful

---
 brian_dashboard_manager/grafana/provision.py  |  4 ++-
 brian_dashboard_manager/templating/helpers.py | 31 ++++++++++---------
 test/test_aggregrate.py                       |  1 +
 3 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/brian_dashboard_manager/grafana/provision.py b/brian_dashboard_manager/grafana/provision.py
index 23caf74..494c6ec 100644
--- a/brian_dashboard_manager/grafana/provision.py
+++ b/brian_dashboard_manager/grafana/provision.py
@@ -155,6 +155,7 @@ AGG_DASHBOARDS = {
     'COPERNICUS': {
         'tag': 'copernicus',
         'dashboard_name': 'COPERNICUS',
+        'group_by': 'location',
         'interfaces': []
     }
 }
@@ -213,7 +214,8 @@ def provision_aggregate(token_request, folder,
     name = dash['dashboard_name']
     tag = dash['tag']
     interfaces = dash['interfaces']
-    data = get_aggregate_interface_data(interfaces, name)
+    group_field = dash.get('group_by', 'remote')
+    data = get_aggregate_interface_data(interfaces, name, group_field)
 
     dashboard = get_aggregate_dashboard_data(
         f'Aggregate - {name}', data, ds_name, tag)
diff --git a/brian_dashboard_manager/templating/helpers.py b/brian_dashboard_manager/templating/helpers.py
index fbd95d2..a6331f4 100644
--- a/brian_dashboard_manager/templating/helpers.py
+++ b/brian_dashboard_manager/templating/helpers.py
@@ -157,28 +157,30 @@ def get_interface_data(interfaces):
     return result
 
 
-def get_aggregate_interface_data(interfaces, agg_type):
+def get_aggregate_interface_data(interfaces, agg_type, group_field):
     """
-    Helper for grouping interfaces into groups of remotes
+    Helper for grouping interfaces into groups by fields, eg. remotes
     (ISP/NREN/...) used for aggregate dashboards
     Extracts information from interfaces to be used in panels.
 
-    Aggregate dashboards have aggregates at the top for all remotes
-    as well as aggregate panels for specific remotes.
-    This builds a dict with interfaces for each remote
+    Aggregate dashboards have aggregates at the top for all groups
+    as well as aggregate panels for specific groups.
+    This builds a dict with interfaces for each group
     and one with all interfaces.
     """
 
     result = []
 
-    def reduce_func(prev, curr):
-        remotes = prev.get(curr['remote'], [])
-        remotes.append(curr)
-        all_agg = prev.get('EVERYSINGLETARGET', [])
-        all_agg.append(curr)
-        prev[curr['remote']] = remotes
-        prev['EVERYSINGLETARGET'] = all_agg
-        return prev
+    def get_reduce_func_for_field(field):
+        def reduce_func(prev, curr):
+            groups = prev.get(curr[field], [])
+            groups.append(curr)
+            all_agg = prev.get('EVERYSINGLETARGET', [])
+            all_agg.append(curr)
+            prev[curr[field]] = groups
+            prev['EVERYSINGLETARGET'] = all_agg
+            return prev
+        return reduce_func
 
     for interface in interfaces:
 
@@ -193,9 +195,10 @@ def get_aggregate_interface_data(interfaces, agg_type):
             'interface': interface_name,
             'hostname': host,
             'remote': remote,
+            'location': location,
             'alias': f"{location} - {remote} ({interface_name})",
         })
-    return reduce(reduce_func, result, {})
+    return reduce(get_reduce_func_for_field(group_field), result, {})
 
 
 def get_aggregate_targets(targets):
diff --git a/test/test_aggregrate.py b/test/test_aggregrate.py
index a992dc5..d0484a2 100644
--- a/test/test_aggregrate.py
+++ b/test/test_aggregrate.py
@@ -11,6 +11,7 @@ DEFAULT_REQUEST_HEADERS = {
 TEST_DASHBOARD = {
     "tag": "TEST_AGGREGATE",
     "dashboard_name": "TEST CLS Peers",
+    "group_by": "remote",
     "interfaces": [
         {
             "router": "mx1.gen.ch.geant.net",
-- 
GitLab