From 473945c61194e33181366467bc856064a44179fd Mon Sep 17 00:00:00 2001 From: Guillaumebeuzeboc Date: Wed, 7 Feb 2024 12:00:17 +0100 Subject: [PATCH] feat(dashboards): split creation and deletion --- cos_registration_server/api/dashboards.py | 44 ++++++++++--------- cos_registration_server/api/views.py | 8 ++-- .../cos_registration_server/asgi.py | 4 ++ .../cos_registration_server/settings.py | 4 +- .../cos_registration_server/wsgi.py | 3 ++ 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/cos_registration_server/api/dashboards.py b/cos_registration_server/api/dashboards.py index 31ae22d..a8265d8 100644 --- a/cos_registration_server/api/dashboards.py +++ b/cos_registration_server/api/dashboards.py @@ -1,32 +1,36 @@ import json -from os import mkdir, makedirs +from os import mkdir, remove from pathlib import Path from shutil import rmtree from devices.models import Device from django.conf import settings -def add_dashboards(): + +def add_dashboards(device): dashboard_path = Path(settings.GRAFANA_DASHBOARD_PATH) - rmtree(dashboard_path, ignore_errors=True) - makedirs(dashboard_path, exist_ok=True) - for device in Device.objects.all(): - for dashboard in device.grafana_dashboards: - dashboard_file = ( - f'{device.uid}-{dashboard["dashboard"]["title"]}.json' - ) - with open(dashboard_path / dashboard_file, "w") as file: - json.dump(dashboard, file) - -def update_dashboards(): + for dashboard in device.grafana_dashboards: + dashboard_title = dashboard["title"].replace(" ", "_") + dashboard_file = f"{device.uid}-{dashboard_title}.json" + with open(dashboard_path / dashboard_file, "w") as file: + json.dump(dashboard, file) + + +def delete_dashboards(device): + dashboard_path = Path(settings.GRAFANA_DASHBOARD_PATH) + + def _is_device_dashboard(p: Path) -> bool: + return p.is_file() and p.name.startswith(device.uid) + + for dashboard in filter( + _is_device_dashboard, Path(dashboard_path).glob("*") + ): + remove(dashboard) + + +def update_all_dashboards(): dashboard_path = Path(settings.GRAFANA_DASHBOARD_PATH) rmtree(dashboard_path, ignore_errors=True) mkdir(dashboard_path) for device in Device.objects.all(): - for dashboard in device.grafana_dashboards: - dashboard_title = dashboard["title"].replace(" ","_") - dashboard_file = ( - f"{device.uid}-{dashboard_title}.json" - ) - with open(dashboard_path / dashboard_file, "w") as file: - json.dump(dashboard, file) + add_dashboards(device) diff --git a/cos_registration_server/api/views.py b/cos_registration_server/api/views.py index f454138..9efee76 100644 --- a/cos_registration_server/api/views.py +++ b/cos_registration_server/api/views.py @@ -3,7 +3,7 @@ from django.http import HttpResponse, JsonResponse from rest_framework.parsers import JSONParser -from .dashboards import update_dashboards +from .dashboards import add_dashboards, delete_dashboards def devices(request): @@ -18,7 +18,7 @@ def devices(request): serialized = DeviceSerializer(data=data) if serialized.is_valid(): serialized.save() - update_dashboards() + add_dashboards(serialized.instance) return JsonResponse(serialized.data, status=201) return JsonResponse(serialized.errors, status=400) @@ -37,9 +37,11 @@ def device(request, uid): serialized = DeviceSerializer(device, data=data, partial=True) if serialized.is_valid(): serialized.save() - update_dashboards() + delete_dashboards(serialized.instance) + add_dashboards(serialized.instance) return JsonResponse(serialized.data) return JsonResponse(serialized.errors, status=400) elif request.method == "DELETE": device.delete() + delete_dashboards(device) return HttpResponse(status=204) diff --git a/cos_registration_server/cos_registration_server/asgi.py b/cos_registration_server/cos_registration_server/asgi.py index a96de7c..5bc1dec 100644 --- a/cos_registration_server/cos_registration_server/asgi.py +++ b/cos_registration_server/cos_registration_server/asgi.py @@ -10,9 +10,13 @@ import os from django.core.asgi import get_asgi_application +from api.dashboards import update_all_dashboards + os.environ.setdefault( "DJANGO_SETTINGS_MODULE", "cos_registration_server.settings" ) +update_all_dashboards() + application = get_asgi_application() diff --git a/cos_registration_server/cos_registration_server/settings.py b/cos_registration_server/cos_registration_server/settings.py index a7b731f..3dfee55 100644 --- a/cos_registration_server/cos_registration_server/settings.py +++ b/cos_registration_server/cos_registration_server/settings.py @@ -148,4 +148,6 @@ USE_X_FORWARDED_HOST = True # Where robot dashboards are written on disk -GRAFANA_DASHBOARD_PATH = os.getenv("GRAFANA_DASHBOARD_PATH", "grafana_dashboards/") +GRAFANA_DASHBOARD_PATH = os.getenv( + "GRAFANA_DASHBOARD_PATH", "grafana_dashboards/" +) diff --git a/cos_registration_server/cos_registration_server/wsgi.py b/cos_registration_server/cos_registration_server/wsgi.py index 794e519..71cce71 100644 --- a/cos_registration_server/cos_registration_server/wsgi.py +++ b/cos_registration_server/cos_registration_server/wsgi.py @@ -10,9 +10,12 @@ import os from django.core.wsgi import get_wsgi_application +from api.dashboards import update_all_dashboards os.environ.setdefault( "DJANGO_SETTINGS_MODULE", "cos_registration_server.settings" ) +update_all_dashboards() + application = get_wsgi_application()