From d0ef53a17649f57a34177bbf2f7cac8c9857d995 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Thu, 3 Oct 2024 10:59:32 +0300 Subject: [PATCH] Fixes AJAX agent cache buttons --- app/classes/agent.php | 14 ++++++++++---- app/pages/agents.php | 17 +++++++++++++++-- app/templates/agent-list.php | 19 +++++++++++-------- public_html/static/agents.js | 34 ++++++++++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 16 deletions(-) diff --git a/app/classes/agent.php b/app/classes/agent.php index f84531a..e461aee 100644 --- a/app/classes/agent.php +++ b/app/classes/agent.php @@ -113,8 +113,8 @@ class Agent { // check for agent cache public function checkAgentCache($agent_id) { - $agent_cache_name = $agent_id . '_cache'; - $agent_cache_time = $agent_id . '_time'; + $agent_cache_name = 'agent' . $agent_id . '_cache'; + $agent_cache_time = 'agent' . $agent_id . '_time'; return isset($_SESSION[$agent_cache_name]) && isset($_SESSION[$agent_cache_time]) && (time() - $_SESSION[$agent_cache_time] < 600); } @@ -155,8 +155,8 @@ class Agent { // we need agent details for URL and JWT token $agent = $this->getAgentDetails($agent_id); - $agent_cache_name = $agent_id . '_cache'; - $agent_cache_time = $agent_id . '_time'; + $agent_cache_name = 'agent' . $agent_id . '_cache'; + $agent_cache_time = 'agent' . $agent_id . '_time'; // check if the cache is still valid, unless force-refresh is requested if (!$force && this->checkAgentCache($agent_id)) { @@ -193,6 +193,12 @@ class Agent { } + // clear agent cache + public function clearAgentCache($agent_id) { + $_SESSION["agent{$agent_id}_cache"] = ''; + $_SESSION["agent{$agent_id}_cache_time"] = ''; + } + } ?> diff --git a/app/pages/agents.php b/app/pages/agents.php index 0186c64..96b1bff 100644 --- a/app/pages/agents.php +++ b/app/pages/agents.php @@ -17,9 +17,22 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($result) { $_SESSION["agent{$agent}_cache"] = $result; $_SESSION["agent{$agent}_cache_time"] = time(); // store the cache time - echo json_encode(['status' => 'success']); + echo json_encode([ + 'status' => 'success', + 'message' => "Cache for agent {$agent} is stored." + ]); + } elseif ($result === null && !empty($agent)) { + unset($_SESSION["agent{$agent}_cache"]); + unset($_SESSION["agent{$agent}_cache_time"]); + echo json_encode([ + 'status' => 'success', + 'message' => "Cache for agent {$agent} is cleared." + ]); } else { - echo json_encode(['status' => 'error', 'message' => 'Invalid data']); + echo json_encode([ + 'status' => 'error', + 'message' => 'Invalid data' + ]); } //// if it's a GET request, it's read/load from cache diff --git a/app/templates/agent-list.php b/app/templates/agent-list.php index 9e96a03..8cb9797 100644 --- a/app/templates/agent-list.php +++ b/app/templates/agent-list.php @@ -22,13 +22,16 @@ // print_r($_SESSION); ?> - - - - - - + + + -

-
click a button to fetch data from the agent.
+ + + + + + +

+
click a button to display data from the agent.
diff --git a/public_html/static/agents.js b/public_html/static/agents.js index 3e8730f..27635ab 100644 --- a/public_html/static/agents.js +++ b/public_html/static/agents.js @@ -2,6 +2,8 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) { // FIXME make use of force variable let counter = 0; + const loadCacheButton = document.getElementById('agent' + agent_id + '-cache'); + const clearCacheButton = document.getElementById('agent' + agent_id + '-clear'); const resultElement = document.getElementById("result" + agent_id); const cacheInfoElement = document.getElementById("cacheInfo" + agent_id); @@ -56,15 +58,22 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) { // show the result in the html resultElement.innerHTML = JSON.stringify(result, null, 2); - // Show the cache timestamp from the session + // get the cache timestamp from the session const now = Date.now(); const cacheTimestamp = new Date(now); - // Display the cache retrieval date and time + // display the cache retrieval date and time const formattedDate = cacheTimestamp.toLocaleDateString(); const formattedTime = cacheTimestamp.toLocaleTimeString(); + cacheInfoElement.style.display = ''; cacheInfoElement.innerHTML = `cache refreshed on ${formattedDate} at ${formattedTime}`; + // show the cache buttons + loadCacheButton.disabled = false; + loadCacheButton.style.display = ''; + clearCacheButton.disabled = false; + clearCacheButton.style.display = ''; + // send the result to PHP to store in session saveResultToSession(result, agent_id); } @@ -165,6 +174,27 @@ function loadCache(agent_id) { } +// clear cache +function clearCache(agent_id) { + const loadCacheButton = document.getElementById('agent' + agent_id + '-cache'); + const clearCacheButton = document.getElementById('agent' + agent_id + '-clear'); + const cacheInfoElement = document.getElementById('cacheInfo' + agent_id); + const resultElement = document.getElementById("result" + agent_id); + + saveResultToSession(null, agent_id); + + // hides the cache buttons + // FIXME add a check whether the saveResult function was successful + loadCacheButton.disabled = true; + loadCacheButton.style.display = 'none'; + clearCacheButton.disabled = true; + clearCacheButton.style.display = 'none'; + cacheInfoElement.innerHTML = ''; + cacheInfoElement.style.display = 'none'; + resultElement.innerHTML = 'click a button to to display data from the agent.'; +} + + // we send result to PHP session, to be available to the whole app function saveResultToSession(result, agent_id) { var xhr = new XMLHttpRequest();