Fixes cache buttons for agents data
parent
4e827e62f0
commit
784532c44d
|
@ -6,23 +6,34 @@ require '../app/classes/agent.php';
|
||||||
|
|
||||||
$agentObject = new Agent($dbWeb);
|
$agentObject = new Agent($dbWeb);
|
||||||
|
|
||||||
// if a form is submitted, it's from the edit page
|
// if it's a POST request, it's saving to cache
|
||||||
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||||
|
|
||||||
// FIXME code here
|
// read the JSON sent from javascript
|
||||||
// header("Location: $app_root?platform=$platform_id&page=config");
|
$data = file_get_contents("php://input");
|
||||||
// exit();
|
$result = json_decode($data, true);
|
||||||
|
|
||||||
$force = isset($_POST['force']) && $_POST['force'] == 'true';
|
// store the data in the session
|
||||||
$agent_id = $_POST['agent'];
|
if ($result) {
|
||||||
$result = fetchAgent($agent_id, $force);
|
$_SESSION["agent{$agent}_cache"] = $result;
|
||||||
|
$_SESSION["agent{$agent}_cache_time"] = time(); // store the cache time
|
||||||
if ($result !== false) {
|
echo json_encode(['status' => 'success']);
|
||||||
echo $result; // Return the API response as JSON
|
|
||||||
} else {
|
} else {
|
||||||
echo json_encode(['error' => 'Failed to fetch API data']);
|
echo json_encode(['status' => 'error', 'message' => 'Invalid data']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//// if it's a GET request, it's read/load from cache
|
||||||
|
//} elseif ($loadcache === true) {
|
||||||
|
//
|
||||||
|
// // check if cached data exists in session
|
||||||
|
// if (isset($_SESSION["agent{$agent}_cache"])) {
|
||||||
|
// // return the cached data in JSON format
|
||||||
|
// echo json_encode(['status' => 'success', 'data' => $_SESSION["agent{$agent}_cache"]]);
|
||||||
|
// } else {
|
||||||
|
// // if no cached data exists
|
||||||
|
// echo json_encode(['status' => 'error', 'message' => 'No cached data found']);
|
||||||
|
// }
|
||||||
|
|
||||||
// no form submitted, show the templates
|
// no form submitted, show the templates
|
||||||
} else {
|
} else {
|
||||||
$agentDetails = $agentObject->getAgentDetails($platform_id);
|
$agentDetails = $agentObject->getAgentDetails($platform_id);
|
||||||
|
|
|
@ -20,9 +20,10 @@
|
||||||
];
|
];
|
||||||
$jwt = $agentObject->generateAgentToken($payload, $agent['secret_key']);
|
$jwt = $agentObject->generateAgentToken($payload, $agent['secret_key']);
|
||||||
?>
|
?>
|
||||||
<?php if (isset($_SESSION["{$agent['id']}_cache"])) { ?>
|
<?php if (isset($_SESSION["agent{$agent['id']}_cache"])) { ?>
|
||||||
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="load recently cached data" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($jwt) ?>')">load cache</button>
|
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="load recently cached data" onclick="loadCache('<?= htmlspecialchars($agent['id']) ?>')">load cache</button>
|
||||||
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get fresh data from agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button>
|
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get fresh data from agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($jwt) ?>', true)">force refresh</button>
|
||||||
|
<span id="cacheInfo<?= htmlspecialchars($agent['id']) ?>" style="margin: 5px 0;"></span>
|
||||||
<?php } else { ?>
|
<?php } else { ?>
|
||||||
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get data from the agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($jwt) ?>')">fetch data</button>
|
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get data from the agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($jwt) ?>')">fetch data</button>
|
||||||
<button class="btn btn-light" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="no cache to refresh">force refresh</button>
|
<button class="btn btn-light" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="no cache to refresh">force refresh</button>
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
|
function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
|
||||||
|
// FIXME make use of force variable
|
||||||
|
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
const resultElement = document.getElementById("result" + agent_id);
|
const resultElement = document.getElementById("result" + agent_id);
|
||||||
|
const cacheInfoElement = document.getElementById("cacheInfo" + agent_id);
|
||||||
|
|
||||||
// Show loading text
|
// Show loading text
|
||||||
resultElement.innerHTML = "Loading... (0 seconds)";
|
resultElement.innerHTML = "Loading... (0 seconds)";
|
||||||
|
@ -51,7 +53,11 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
resultElement.innerHTML = "Error: " + result.error;
|
resultElement.innerHTML = "Error: " + result.error;
|
||||||
} else {
|
} else {
|
||||||
|
// show the result in the html
|
||||||
resultElement.innerHTML = JSON.stringify(result, null, 2);
|
resultElement.innerHTML = JSON.stringify(result, null, 2);
|
||||||
|
cacheInfoElement.innerHTML = "";
|
||||||
|
// send the result to PHP to store in session
|
||||||
|
saveResultToSession(result, agent_id);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Display the error
|
// Display the error
|
||||||
|
@ -96,3 +102,73 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
|
||||||
// }, 1000); // Simulate a minimum 1 second delay for testing
|
// }, 1000); // Simulate a minimum 1 second delay for testing
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// load the result from cache
|
||||||
|
function loadCache(agent_id) {
|
||||||
|
const resultElement = document.getElementById("result" + agent_id);
|
||||||
|
const cacheInfoElement = document.getElementById("cacheInfo" + agent_id);
|
||||||
|
resultElement.innerHTML = "Loading cached data...";
|
||||||
|
|
||||||
|
// Fetch the cached data from PHP
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("GET", "static/loadcache.php?agent="+agent_id, true);
|
||||||
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
|
xhr.onreadystatechange = function() {
|
||||||
|
if (xhr.readyState === 4) {
|
||||||
|
if (xhr.status === 200) {
|
||||||
|
try {
|
||||||
|
let response = JSON.parse(xhr.responseText);
|
||||||
|
|
||||||
|
if (response.status === 'success') {
|
||||||
|
// Display the cached data
|
||||||
|
resultElement.innerHTML = JSON.stringify(response.data, null, 2);
|
||||||
|
|
||||||
|
// Get the cache timestamp from the session
|
||||||
|
const cacheTimestamp = new Date(response.cache_time);
|
||||||
|
|
||||||
|
// Display the cache retrieval date and time
|
||||||
|
const formattedDate = cacheTimestamp.toLocaleDateString();
|
||||||
|
const formattedTime = cacheTimestamp.toLocaleTimeString();
|
||||||
|
cacheInfoElement.innerHTML = `cache retrieved on ${formattedDate} at ${formattedTime}`;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
resultElement.innerHTML = "No cached data found.";
|
||||||
|
cacheInfoElement.innerHTML = "";
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
resultElement.innerHTML = "Error loading cached data.";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resultElement.innerHTML = `Error: Unable to load cache. Status code: ${xhr.status}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.onerror = function() {
|
||||||
|
resultElement.innerHTML = "Network error occurred while fetching the cached data.";
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// we send result to PHP session, to be available to the whole app
|
||||||
|
function saveResultToSession(result, agent_id) {
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("POST", "?page=agents&agent="+agent_id, true);
|
||||||
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
|
xhr.onreadystatechange = function() {
|
||||||
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
||||||
|
console.log("Data saved to session successfully.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.onerror = function() {
|
||||||
|
console.error("Error saving data to session.");
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.send(JSON.stringify(result));
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
session_name('jilo');
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
$agent = $_GET['agent'];
|
||||||
|
|
||||||
|
// Check if cached data exists in session
|
||||||
|
if (isset($_SESSION["agent{$agent}_cache"])) {
|
||||||
|
|
||||||
|
// return status, the data, and caching time - in JSON
|
||||||
|
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
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
// If no cached data exists
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'No cached data found']);
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
Reference in New Issue