Fixes to the agent API calls

main
Yasen Pramatarov 2024-09-30 11:55:23 +03:00
parent 37398b5986
commit b72d4ea791
4 changed files with 53 additions and 26 deletions

View File

@ -9,19 +9,30 @@ class Agent {
// get details of a specified agent ID (or all) in a specified platform ID // get details of a specified agent ID (or all) in a specified platform ID
public function getAgentDetails($platform_id, $agent_id = '') { public function getAgentDetails($platform_id, $agent_id = '') {
$sql = 'SELECT * FROM jilo_agents $sql = 'SELECT
ja.id,
ja.platform_id,
ja.agent_type_id,
ja.url,
ja.secret_key,
jat.description AS agent_description,
jat.endpoint AS agent_endpoint
FROM
jilo_agents ja
JOIN
jilo_agent_types jat ON ja.agent_type_id = jat.id
WHERE WHERE
platform_id = :platform_id'; platform_id = :platform_id';
if ($agent_id !== '') { if ($agent_id !== '') {
$sql .= ' AND id = :agent_id'; $sql .= ' AND ja.id = :agent_id';
$query = $this->db->prepare($sql); }
$query->execute([
':platform_id' => $platform_id, $query = $this->db->prepare($sql);
':agent_id' => $agent_id,
]); $query->bindParam(':platform_id', $platform_id);
} else { if ($agent_id !== '') {
$query = $this->db->prepare($sql); $query->bindParam(':agent_id', $agent_id);
$query->bindParam(':platform_id', $platform_id);
} }
$query->execute(); $query->execute();
@ -126,17 +137,25 @@ class Agent {
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // timeout 10 seconds curl_setopt($ch, CURLOPT_TIMEOUT, 10); // timeout 10 seconds
$response = curl_exec($ch); $response = curl_exec($ch);
$curl_error = curl_error($ch); // curl error for debugging $curl_error = curl_error($ch);
$curl_errno = curl_errno($ch);
curl_close($ch); curl_close($ch);
// Cache the result and the timestamp if the response is successful // general curl error
if ($response !== false) { if ($curl_error) {
$_SESSION[$agent_cache_name] = $response; return json_encode(['error' => 'curl error: ' . $curl_error]);
$_SESSION[$agent_cache_time] = time();
} else {
$response = "Error: " . $curl_error;
} }
// other custom error(s)
if (strpos($response, 'Auth header not received') !== false) {
return json_encode(['error' => 'Auth header not received']);
}
// Cache the result and the timestamp if the response is successful
$_SESSION[$agent_cache_name] = $response;
$_SESSION[$agent_cache_time] = time();
return $response; return $response;
} }

View File

@ -6,8 +6,8 @@
<?php foreach ($agentDetails as $agent) { ?> <?php foreach ($agentDetails as $agent) { ?>
<p class="card-text"> <p class="card-text">
agent id<?= htmlspecialchars($agent['id']) ?>: type <?= htmlspecialchars($agent['agent_type_id']) ?>, url <?= htmlspecialchars($agent['url']) ?> agent id<?= htmlspecialchars($agent['id']) ?>: type <?= htmlspecialchars($agent['agent_type_id']) ?>, url <?= htmlspecialchars($agent['url']) ?>
<button onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>')">fetch data</button> <button onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>')">fetch data</button>
<button onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', true)">force refresh</button> <button onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button>
</p> </p>
<p>Result:</p> <p>Result:</p>
<pre id="result<?= htmlspecialchars($agent['id']) ?>">click a button to fetch data from the agent.</pre> <pre id="result<?= htmlspecialchars($agent['id']) ?>">click a button to fetch data from the agent.</pre>

View File

@ -43,7 +43,7 @@ CREATE TABLE jilo_agents (
CREATE TABLE jilo_agent_types ( CREATE TABLE jilo_agent_types (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT, description TEXT,
endponts TEXT endpoint TEXT
); );
CREATE TABLE logs ( CREATE TABLE logs (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,

View File

@ -1,4 +1,4 @@
function fetchData(agent_id, url, force = false) { function fetchData(agent_id, url, endpoint, force = false) {
let counter = 0; let counter = 0;
const resultElement = document.getElementById("result" + agent_id); const resultElement = document.getElementById("result" + agent_id);
@ -14,13 +14,17 @@ function fetchData(agent_id, url, force = false) {
// Create an AJAX request // Create an AJAX request
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
const agentUrl = url + endpoint;
// FIXME for debugging purpose
console.log("Requesting URL:", agentUrl);
// Handle invalid URL error // Handle invalid URL error
try { try {
xhr.open("POST", url, true); xhr.open("POST", agentUrl, true);
} catch (e) { } catch (e) {
clearInterval(intervalId); // Stop the counter on error clearInterval(intervalId); // Stop the counter on error
resultElement.innerHTML = `Error: Invalid URL ${url}<br />` + e.message; resultElement.innerHTML = `Error: Invalid URL ${agentUrl}<br />` + e.message;
return; // Exit the function early return; // Exit the function early
} }
@ -39,13 +43,17 @@ function fetchData(agent_id, url, force = false) {
try { try {
// Parse and display the result // Parse and display the result
let result = JSON.parse(xhr.responseText); let result = JSON.parse(xhr.responseText);
resultElement.innerHTML = JSON.stringify(result, null, 2); if (result.error) {
resultElement.innerHTML = "Error: " + result.error;
} else {
resultElement.innerHTML = JSON.stringify(result, null, 2);
}
} catch (e) { } catch (e) {
// Display the error // Display the error
resultElement.innerHTML = "Error: Response is not a valid JSON.<br />Response: " + xhr.responseText; resultElement.innerHTML = "Error: Response is not a valid JSON.<br />Response: " + xhr.responseText;
} }
} else { } else {
resultElement.innerHTML = `Error: Unable to fetch data from ${url}<br />Status Code: ${xhr.status}<br />Status Text: ${xhr.statusText}<br />Response: ${xhr.responseText}`; resultElement.innerHTML = `Error: Unable to fetch data from ${agentUrl}<br />Status Code: ${xhr.status}<br />Status Text: ${xhr.statusText}<br />Response: ${xhr.responseText}`;
} }
} }
}; };
@ -53,7 +61,7 @@ function fetchData(agent_id, url, force = false) {
// Handle network-level errors (e.g., connection refused) // Handle network-level errors (e.g., connection refused)
xhr.onerror = function() { xhr.onerror = function() {
clearInterval(intervalId); // Stop the counter on error clearInterval(intervalId); // Stop the counter on error
resultElement.innerHTML = `Network Error:<br />Unable to connect to ${url}<br />Check network connection or try again later.`; resultElement.innerHTML = `Network Error:<br />Unable to connect to ${agentUrl}<br />Check network connection or try again later.`;
}; };
// Handle the timeout event // Handle the timeout event