Skip to content

Commit

Permalink
feat(dashboards): split creation and deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaumebeuzeboc committed Feb 7, 2024
1 parent f81fd87 commit 473945c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
44 changes: 24 additions & 20 deletions cos_registration_server/api/dashboards.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 5 additions & 3 deletions cos_registration_server/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand All @@ -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)
4 changes: 4 additions & 0 deletions cos_registration_server/cos_registration_server/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 3 additions & 1 deletion cos_registration_server/cos_registration_server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
)
3 changes: 3 additions & 0 deletions cos_registration_server/cos_registration_server/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 473945c

Please sign in to comment.