|
1 | 1 | # Import the necessary modules |
2 | 2 | import json |
| 3 | +from typing import Any |
3 | 4 |
|
4 | 5 | from constructs import Construct |
5 | 6 | from imports.grafana.dashboard import Dashboard |
| 7 | +from imports.grafana.data_source import DataSource |
6 | 8 | from imports.grafana.folder import Folder |
7 | 9 |
|
8 | | -# Load the dashboard configuration from a JSON file |
9 | | -with open("dashboard.json", "r") as f: |
10 | | - data = json.load(f) |
11 | | - |
12 | | -# Convert the data to a JSON string |
13 | | -data = json.dumps(data) |
14 | 10 |
|
| 11 | +# Function for updating the datasource in the static dashboard.json config file. |
| 12 | +# This allowing the datasource and config to be updated at the same time. Aswell as removes production config issues where it uses dev/different datasource uid. |
| 13 | +def update_uid(obj: dict, uid: str) -> None: |
| 14 | + if isinstance(obj, dict): |
| 15 | + for key in obj: |
| 16 | + if key == "targets": |
| 17 | + for target in obj[key]: |
| 18 | + if target["datasource"]["type"] == "grafana-timestream-datasource": |
| 19 | + if target["datasource"]["uid"] != uid: |
| 20 | + target["datasource"]["uid"] = uid |
| 21 | + else: |
| 22 | + update_uid(obj[key], uid) |
| 23 | + elif isinstance(obj, list): |
| 24 | + for item in obj: |
| 25 | + update_uid(item, uid) |
15 | 26 |
|
16 | 27 | # Create a new component |
17 | 28 | class GrafanaDashboardComponent(Construct): |
18 | 29 | def __init__( |
19 | 30 | self, |
20 | 31 | scope: Construct, |
21 | 32 | id: str, |
| 33 | + data: Any, |
22 | 34 | ): |
23 | 35 | super().__init__(scope, id) |
24 | 36 |
|
25 | | - self.folder = Folder(self, "folder", title="AWS Flight Controller") |
| 37 | + self.folder = Folder( |
| 38 | + self, |
| 39 | + "folder", |
| 40 | + title="GCP Flight Controller" |
| 41 | + ) |
| 42 | + |
| 43 | + datasource_config = json.dumps({ |
| 44 | + 'authenticationType': 'gce', |
| 45 | + 'defaultProject': 'contino-squad0-fc', |
| 46 | + 'processingLocation': 'australia-southeast1', |
| 47 | + }) |
| 48 | + |
| 49 | + |
| 50 | + self.datasource = DataSource( |
| 51 | + self, |
| 52 | + "bigquery-datasource", |
| 53 | + name="Flight Controller Bigquery", |
| 54 | + type="grafana-bigquery-datasource", |
| 55 | + is_default=True, |
| 56 | + json_data_encoded=datasource_config |
| 57 | + ) |
| 58 | + |
| 59 | + # Update datasource UID |
| 60 | + update_uid(data, self.datasource.uid) |
| 61 | + |
| 62 | + # Convert the data to a JSON string |
| 63 | + data = json.dumps(data) |
| 64 | + |
26 | 65 |
|
27 | 66 | # Create a new Grafana dashboard |
28 | 67 | self.dashboard = Dashboard( |
|
0 commit comments