Skip to content
Snippets Groups Projects
Commit f9d3952f authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

Template the home dashboard

parent a3aaaf2e
Branches
Tags
No related merge requests found
...@@ -5,11 +5,14 @@ Grafana Organization management helpers. ...@@ -5,11 +5,14 @@ Grafana Organization management helpers.
import random import random
import string import string
import logging import logging
import jinja2
import json
import os
from typing import Dict, List, Union from typing import Dict, List, Union
from datetime import datetime from datetime import datetime
from brian_dashboard_manager.grafana.utils.request import AdminRequest, \ from brian_dashboard_manager.grafana.utils.request import AdminRequest, \
TokenRequest TokenRequest
from brian_dashboard_manager.grafana.dashboard import create_dashboard
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -94,8 +97,20 @@ def delete_expired_api_tokens(request: AdminRequest, org_id: int) -> bool: ...@@ -94,8 +97,20 @@ def delete_expired_api_tokens(request: AdminRequest, org_id: int) -> bool:
return True return True
def set_home_dashboard(request: TokenRequest, dashboard_id: int): def set_home_dashboard(request: TokenRequest, is_staff):
file = os.path.abspath(os.path.join(
os.path.dirname(__file__),
'..',
'templating',
'templates',
'homedashboard.json.j2'))
with open(file) as f:
template = jinja2.Template(f.read())
rendered = template.render({'staff': is_staff})
rendered = json.loads(rendered)
dashboard = create_dashboard(request, rendered)
r = request.put('api/org/preferences', json={ r = request.put('api/org/preferences', json={
'homeDashboardId': dashboard_id 'homeDashboardId': dashboard.get('id')
}) })
return r and r.get('message') == 'Preferences updated' return r and r.get('message') == 'Preferences updated'
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
This module is responsible for the This module is responsible for the
entire provisioning lifecycle. entire provisioning lifecycle.
""" """
import os
import logging import logging
import time import time
import json import json
...@@ -15,8 +16,7 @@ from brian_dashboard_manager.grafana.organization import \ ...@@ -15,8 +16,7 @@ from brian_dashboard_manager.grafana.organization import \
get_organizations, create_organization, create_api_token, \ get_organizations, create_organization, create_api_token, \
delete_api_token, delete_expired_api_tokens, set_home_dashboard delete_api_token, delete_expired_api_tokens, set_home_dashboard
from brian_dashboard_manager.grafana.dashboard import \ from brian_dashboard_manager.grafana.dashboard import \
get_dashboard_definitions, create_dashboard, find_dashboard, \ get_dashboard_definitions, create_dashboard, delete_dashboard
delete_dashboard
from brian_dashboard_manager.grafana.datasource import \ from brian_dashboard_manager.grafana.datasource import \
check_provisioned, create_datasource check_provisioned, create_datasource
from brian_dashboard_manager.grafana.folder import find_folder, \ from brian_dashboard_manager.grafana.folder import find_folder, \
...@@ -105,8 +105,10 @@ def provision_maybe(config): ...@@ -105,8 +105,10 @@ def provision_maybe(config):
f.truncate() f.truncate()
try: try:
# don't conditionally provision in dev
val = os.environ.get('FLASK_ENV') != 'development'
now = datetime.datetime.now() now = datetime.datetime.now()
write_timestamp(now.timestamp(), True) write_timestamp(now.timestamp(), val)
provision(config) provision(config)
finally: finally:
now = datetime.datetime.now() now = datetime.datetime.now()
...@@ -321,8 +323,6 @@ def provision(config): ...@@ -321,8 +323,6 @@ def provision(config):
logger.info('Provisioning static dashboards') logger.info('Provisioning static dashboards')
for dashboard in get_dashboard_definitions(): for dashboard in get_dashboard_definitions():
if dashboard['title'] not in excluded_dashboards: if dashboard['title'] not in excluded_dashboards:
if dashboard['title'].lower() == 'home':
dashboard['uid'] = 'home'
create_dashboard(token_request, dashboard) create_dashboard(token_request, dashboard)
else: else:
delete_dashboard(token_request, dashboard) delete_dashboard(token_request, dashboard)
...@@ -330,9 +330,8 @@ def provision(config): ...@@ -330,9 +330,8 @@ def provision(config):
# Home dashboard is always called "Home" # Home dashboard is always called "Home"
# Make sure it's set for the organization # Make sure it's set for the organization
logger.info('Configuring Home dashboard') logger.info('Configuring Home dashboard')
home_dashboard = find_dashboard(token_request, 'Home') is_staff = org['name'] == 'GÉANT Staff'
if home_dashboard: set_home_dashboard(token_request, is_staff)
set_home_dashboard(token_request, home_dashboard['id'])
logger.info(f'Time to complete: {time.time() - start}') logger.info(f'Time to complete: {time.time() - start}')
for org_id, token in tokens: for org_id, token in tokens:
......
{ {
"annotations": { "annotations": {
"list": [ "list": [
{ {
"builtIn": 1, "builtIn": 1,
"datasource": "-- Grafana --", "datasource": "-- Grafana --",
"enable": true, "enable": true,
...@@ -16,50 +16,52 @@ ...@@ -16,50 +16,52 @@
"gnetId": null, "gnetId": null,
"graphTooltip": 0, "graphTooltip": 0,
"id": 49, "id": 49,
"uid": "home",
"iteration": 1595947519970, "iteration": 1595947519970,
"links": [ "links": [
{ {
"asDropdown": true, "asDropdown": true,
"icon": "external link", "icon": "external link",
"tags": [ "tags": [
"services" "services"
], ],
"targetBlank": true, "targetBlank": true,
"title": "Services", "title": "Services",
"type": "dashboards" "type": "dashboards"
}, },
{ {
"asDropdown": true, "asDropdown": true,
"icon": "external link", "icon": "external link",
"tags": [ "tags": [
"infrastructure" "infrastructure"
], ],
"targetBlank": true, "targetBlank": true,
"title": "Infrastructure", "title": "Infrastructure",
"type": "dashboards" "type": "dashboards"
}, },
{ {
"asDropdown": true, "asDropdown": true,
"icon": "external link", "icon": "external link",
"tags": [ "tags": [
"customers" "customers"
], ],
"targetBlank": true, "targetBlank": true,
"title": "NREN Access", "title": "NREN Access",
"type": "dashboards" "type": "dashboards"
}, },
{ {
"asDropdown": true, "asDropdown": true,
"icon": "external link", "icon": "external link",
"tags": [ "tags": [
"peers" "peers"
], ],
"targetBlank": true, "targetBlank": true,
"title": "Peers", "title": "Peers",
"type": "dashboards" "type": "dashboards"
} }
], ],
"panels": [ "panels": [
{% if staff %}
{ {
"aliasColors": {}, "aliasColors": {},
"bars": false, "bars": false,
...@@ -636,12 +638,99 @@ ...@@ -636,12 +638,99 @@
"alignLevel": null "alignLevel": null
} }
} }
{% else %}
{
"datasource": "PollerInfluxDB",
"fieldConfig": {
"defaults": {
"custom": {
"align": null,
"filterable": false
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"folderId": null,
"gridPos": {
"h": 24,
"w": 24,
"x": 0,
"y": 0
},
"headings": true,
"id": 2,
"limit": 100,
"pluginVersion": "7.2.1",
"query": "",
"recent": false,
"repeat": null,
"search": true,
"starred": true,
"tags": [],
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"value"
],
"type": "field"
},
{
"params": [],
"type": "mean"
}
]
],
"tags": []
}
],
"timeFrom": null,
"timeShift": null,
"title": "Dashboards",
"type": "dashlist"
}
{% endif %}
], ],
"schemaVersion": 26, "schemaVersion": 26,
"style": "dark", "style": "dark",
"tags": [], "tags": [],
"templating": { "templating": {
"list": [ "list": [
{% if staff %}
{ {
"allValue": null, "allValue": null,
"datasource": "PollerInfluxDB", "datasource": "PollerInfluxDB",
...@@ -684,6 +773,7 @@ ...@@ -684,6 +773,7 @@
"type": "query", "type": "query",
"useTags": false "useTags": false
} }
{% endif %}
] ]
}, },
"time": { "time": {
...@@ -706,4 +796,4 @@ ...@@ -706,4 +796,4 @@
"timezone": "", "timezone": "",
"title": "Home", "title": "Home",
"version": 1 "version": 1
} }
\ No newline at end of file
...@@ -366,6 +366,30 @@ def test_provision(data_config, mocker, client): ...@@ -366,6 +366,30 @@ def test_provision(data_config, mocker, client):
url=f"http://{data_config['hostname']}/api/datasources", url=f"http://{data_config['hostname']}/api/datasources",
callback=datasources) callback=datasources)
def createdashboard(request):
return 200, {}, json.dumps({'id': 666})
responses.add_callback(
method=responses.POST,
url=f"http://{data_config['hostname']}/api/dashboards/db",
callback=createdashboard)
def preferences(request):
return 200, {}, json.dumps({'message': 'Preferences updated'})
responses.add_callback(
method=responses.PUT,
url=f"http://{data_config['hostname']}/api/org/preferences",
callback=preferences)
def homedashboard(request):
return 404, {}, ''
responses.add_callback(
method=responses.GET,
url=f"http://{data_config['hostname']}/api/dashboards/uid/home",
callback=homedashboard)
PROVISIONED_ORGANIZATION = { PROVISIONED_ORGANIZATION = {
'name': data_config['organizations'][0], 'name': data_config['organizations'][0],
'id': 0 'id': 0
...@@ -416,7 +440,7 @@ def test_provision(data_config, mocker, client): ...@@ -416,7 +440,7 @@ def test_provision(data_config, mocker, client):
'brian_dashboard_manager.grafana.provision.create_dashboard') 'brian_dashboard_manager.grafana.provision.create_dashboard')
# we dont care about this, just mark it created # we dont care about this, just mark it created
# we dont care about this, tested separately # we dont care about this, tested separately
_mocked_create_dashboard.return_value = None _mocked_create_dashboard.return_value = {'id': 666}
_mocked_delete_api_token = mocker.patch( _mocked_delete_api_token = mocker.patch(
'brian_dashboard_manager.grafana.provision.delete_api_token') 'brian_dashboard_manager.grafana.provision.delete_api_token')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment