From 72aaee1ec865f83e2f15ec7ae764134e31fbbb95 Mon Sep 17 00:00:00 2001 From: FerTV Date: Thu, 14 Nov 2024 12:23:42 +0100 Subject: [PATCH] feat(dashboard): show scenario configuration in config-btn --- nebula/frontend/app.py | 20 ++++++++++++ nebula/frontend/templates/dashboard.html | 39 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/nebula/frontend/app.py b/nebula/frontend/app.py index 9748c236..43397a5b 100755 --- a/nebula/frontend/app.py +++ b/nebula/frontend/app.py @@ -284,6 +284,26 @@ async def get_notes_for_scenario(scenario_name: str): return JSONResponse({"status": "error", "message": "Notes not found for the specified scenario"}) +@app.get("/nebula/dashboard/{scenario_name}/config") +async def get_config_for_scenario(scenario_name: str): + json_path = os.path.join(os.environ.get("NEBULA_CONFIG_DIR"), scenario_name, "scenario.json") + logging.info(f"[FER] json_path: {json_path}") + + try: + with open(json_path) as file: + scenarios_data = json.load(file) + + if scenarios_data: + return JSONResponse({"status": "success", "config": scenarios_data}) + else: + return JSONResponse({"status": "error", "message": "Configuration not found for the specified scenario"}) + + except FileNotFoundError: + return JSONResponse({"status": "error", "message": "scenario.json file not found"}) + except json.JSONDecodeError: + return JSONResponse({"status": "error", "message": "Error decoding JSON file"}) + + @app.post("/nebula/login") async def nebula_login( request: Request, diff --git a/nebula/frontend/templates/dashboard.html b/nebula/frontend/templates/dashboard.html index 37cbb51e..4e976f26 100755 --- a/nebula/frontend/templates/dashboard.html +++ b/nebula/frontend/templates/dashboard.html @@ -147,6 +147,8 @@

Scenarios in the database

class="label btn btn-dark">Real-time metrics + {% if status == "running" %} Stop scenario @@ -169,6 +171,12 @@

Scenarios in the database

class="btn btn-dark mt-2 save-note-btn" style="float: right;">Save + + + + + {% endfor %} @@ -238,6 +246,12 @@

Scenarios in the database

saveNotes(this.dataset.scenarioName); }); }); + + document.querySelectorAll('[id^=config-btn]').forEach(button => { + button.addEventListener('click', function () { + toggleConfigRow(this.dataset.scenarioName); + }); + }); }); async function toggleNotesRow(scenarioName) { @@ -265,6 +279,31 @@

Scenarios in the database

notesRow.style.display = notesRow.style.display === 'none' ? '' : 'none'; } + async function toggleConfigRow(scenarioName) { + const configRow = document.getElementById('config-row-' + scenarioName); + const configTextElement = document.getElementById('config-text-' + scenarioName); + + if (configRow.style.display === 'none') { + try { + const response = await fetch(`/nebula/dashboard/${scenarioName}/config`); + const data = await response.json(); + + console.log("[FER] ", data.config) + + if (data.status === 'success') { + configTextElement.value = JSON.stringify(data.config, null, 2); + } else { + configTextElement.value = 'No configuration available.'; + } + } catch (error) { + console.error('Error:', error); + alert('An error occurred while retrieving the configuration.'); + return; + } + } + configRow.style.display = configRow.style.display === 'none' ? '' : 'none'; + } + async function saveNotes(scenarioName) { const notesText = document.getElementById('notes-text-' + scenarioName).value;