From 4e19a1c571e75f412d820c73fec3c6e986940a4a Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Wed, 2 Oct 2024 22:33:09 +0300 Subject: [PATCH] Enhances agent data display --- app/templates/agent-list.php | 5 +++-- app/templates/page-header.php | 1 + public_html/static/agents.js | 12 +++++++++++- public_html/static/css/agents.css | 11 +++++++++++ public_html/static/loadcache.php | 4 +++- 5 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 public_html/static/css/agents.css diff --git a/app/templates/agent-list.php b/app/templates/agent-list.php index 75c5748..ff4b83c 100644 --- a/app/templates/agent-list.php +++ b/app/templates/agent-list.php @@ -19,15 +19,16 @@ 'agent_id' => $agent['id'] ]; $jwt = $agentObject->generateAgentToken($payload, $agent['secret_key']); +// print_r($_SESSION); ?> - +

-
click a button to fetch data from the agent.
+
click a button to fetch data from the agent.
diff --git a/app/templates/page-header.php b/app/templates/page-header.php index 028210a..ac3a95e 100644 --- a/app/templates/page-header.php +++ b/app/templates/page-header.php @@ -25,6 +25,7 @@ + Jilo Web diff --git a/public_html/static/agents.js b/public_html/static/agents.js index 5b69e94..f05856e 100644 --- a/public_html/static/agents.js +++ b/public_html/static/agents.js @@ -55,13 +55,22 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) { } else { // show the result in the html resultElement.innerHTML = JSON.stringify(result, null, 2); - cacheInfoElement.innerHTML = ""; + + // Show the cache timestamp from the session + const cacheTimestamp = new Date(result.cache_time); + + // Display the cache retrieval date and time + const formattedDate = cacheTimestamp.toLocaleDateString(); + const formattedTime = cacheTimestamp.toLocaleTimeString(); + cacheInfoElement.innerHTML = `cache refreshed on ${formattedDate} at ${formattedTime}`; + // send the result to PHP to store in session saveResultToSession(result, agent_id); } } catch (e) { // Display the error resultElement.innerHTML = "Error: Response is not a valid JSON.
Response: " + xhr.responseText; + console.error("error:", e); } } else { resultElement.innerHTML = `Error: Unable to fetch data from ${agentUrl}
Status Code: ${xhr.status}
Status Text: ${xhr.statusText}
Response: ${xhr.responseText}`; @@ -139,6 +148,7 @@ function loadCache(agent_id) { } } catch (e) { resultElement.innerHTML = "Error loading cached data."; + console.error("error:", e); } } else { resultElement.innerHTML = `Error: Unable to load cache. Status code: ${xhr.status}`; diff --git a/public_html/static/css/agents.css b/public_html/static/css/agents.css new file mode 100644 index 0000000..7583683 --- /dev/null +++ b/public_html/static/css/agents.css @@ -0,0 +1,11 @@ + +.agent_result { + text-align: left; + font-family: monospace; + white-space: pre-wrap; + background-color: #f4f4f4; + padding: 10px; + border: 1px solid #ccc; + border-radius: 5px; + overflow-x: auto; +} diff --git a/public_html/static/loadcache.php b/public_html/static/loadcache.php index e513951..d5047a0 100644 --- a/public_html/static/loadcache.php +++ b/public_html/static/loadcache.php @@ -12,7 +12,9 @@ if (isset($_SESSION["agent{$agent}_cache"])) { echo json_encode([ 'status' => 'success', 'data' => $_SESSION["agent{$agent}_cache"], - 'cache_time' => $_SESSION["agent{$agent}_cache_time"] ?? time() // we store cache time in the session + // we store cache time in the session + // FIXME may need to move to file cache + 'cache_time' => $_SESSION["agent{$agent}_cache_time"] ?? time() ]); } else { // If no cached data exists