generated from rochacbruno/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboards): split creation and deletion
- Loading branch information
1 parent
f81fd87
commit 473945c
Showing
5 changed files
with
39 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters