Fixes to the agent API calls
parent
37398b5986
commit
b72d4ea791
|
@ -9,19 +9,30 @@ class Agent {
|
|||
|
||||
// get details of a specified agent ID (or all) in a specified platform 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
|
||||
platform_id = :platform_id';
|
||||
|
||||
if ($agent_id !== '') {
|
||||
$sql .= ' AND id = :agent_id';
|
||||
$query = $this->db->prepare($sql);
|
||||
$query->execute([
|
||||
':platform_id' => $platform_id,
|
||||
':agent_id' => $agent_id,
|
||||
]);
|
||||
} else {
|
||||
$sql .= ' AND ja.id = :agent_id';
|
||||
}
|
||||
|
||||
$query = $this->db->prepare($sql);
|
||||
|
||||
$query->bindParam(':platform_id', $platform_id);
|
||||
if ($agent_id !== '') {
|
||||
$query->bindParam(':agent_id', $agent_id);
|
||||
}
|
||||
|
||||
$query->execute();
|
||||
|
@ -126,17 +137,25 @@ class Agent {
|
|||
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // timeout 10 seconds
|
||||
|
||||
$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);
|
||||
|
||||
// general curl error
|
||||
if ($curl_error) {
|
||||
return json_encode(['error' => 'curl 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
|
||||
if ($response !== false) {
|
||||
$_SESSION[$agent_cache_name] = $response;
|
||||
$_SESSION[$agent_cache_time] = time();
|
||||
} else {
|
||||
$response = "Error: " . $curl_error;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
<?php foreach ($agentDetails as $agent) { ?>
|
||||
<p class="card-text">
|
||||
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']) ?>', true)">force refresh</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']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button>
|
||||
</p>
|
||||
<p>Result:</p>
|
||||
<pre id="result<?= htmlspecialchars($agent['id']) ?>">click a button to fetch data from the agent.</pre>
|
||||
|
|
|
@ -43,7 +43,7 @@ CREATE TABLE jilo_agents (
|
|||
CREATE TABLE jilo_agent_types (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
description TEXT,
|
||||
endponts TEXT
|
||||
endpoint TEXT
|
||||
);
|
||||
CREATE TABLE logs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
function fetchData(agent_id, url, force = false) {
|
||||
function fetchData(agent_id, url, endpoint, force = false) {
|
||||
|
||||
let counter = 0;
|
||||
const resultElement = document.getElementById("result" + agent_id);
|
||||
|
@ -14,13 +14,17 @@ function fetchData(agent_id, url, force = false) {
|
|||
|
||||
// Create an AJAX request
|
||||
var xhr = new XMLHttpRequest();
|
||||
const agentUrl = url + endpoint;
|
||||
|
||||
// FIXME for debugging purpose
|
||||
console.log("Requesting URL:", agentUrl);
|
||||
|
||||
// Handle invalid URL error
|
||||
try {
|
||||
xhr.open("POST", url, true);
|
||||
xhr.open("POST", agentUrl, true);
|
||||
} catch (e) {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -39,13 +43,17 @@ function fetchData(agent_id, url, force = false) {
|
|||
try {
|
||||
// Parse and display the result
|
||||
let result = JSON.parse(xhr.responseText);
|
||||
if (result.error) {
|
||||
resultElement.innerHTML = "Error: " + result.error;
|
||||
} else {
|
||||
resultElement.innerHTML = JSON.stringify(result, null, 2);
|
||||
}
|
||||
} catch (e) {
|
||||
// Display the error
|
||||
resultElement.innerHTML = "Error: Response is not a valid JSON.<br />Response: " + xhr.responseText;
|
||||
}
|
||||
} 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)
|
||||
xhr.onerror = function() {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue