From b72d4ea791a373620e5a222e0f90ee6755f36714 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Mon, 30 Sep 2024 11:55:23 +0300 Subject: [PATCH] Fixes to the agent API calls --- app/classes/agent.php | 53 ++++++++++++++++++++++++------------ app/templates/agent-list.php | 4 +-- doc/jilo-web.schema | 2 +- public_html/static/agents.js | 20 ++++++++++---- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/app/classes/agent.php b/app/classes/agent.php index 4a4b7e2..7d4c511 100644 --- a/app/classes/agent.php +++ b/app/classes/agent.php @@ -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 { - $query = $this->db->prepare($sql); - $query->bindParam(':platform_id', $platform_id); + $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); - // 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; + // 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 + $_SESSION[$agent_cache_name] = $response; + $_SESSION[$agent_cache_time] = time(); + return $response; } diff --git a/app/templates/agent-list.php b/app/templates/agent-list.php index f09a924..3920f2c 100644 --- a/app/templates/agent-list.php +++ b/app/templates/agent-list.php @@ -6,8 +6,8 @@

agent id: type , url - - + +

Result:

click a button to fetch data from the agent.
diff --git a/doc/jilo-web.schema b/doc/jilo-web.schema index 919ef66..2ea143f 100644 --- a/doc/jilo-web.schema +++ b/doc/jilo-web.schema @@ -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, diff --git a/public_html/static/agents.js b/public_html/static/agents.js index fa5be8a..ada61ec 100644 --- a/public_html/static/agents.js +++ b/public_html/static/agents.js @@ -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}
` + e.message; + resultElement.innerHTML = `Error: Invalid URL ${agentUrl}
` + 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); - 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) { // Display the error resultElement.innerHTML = "Error: Response is not a valid JSON.
Response: " + xhr.responseText; } } else { - resultElement.innerHTML = `Error: Unable to fetch data from ${url}
Status Code: ${xhr.status}
Status Text: ${xhr.statusText}
Response: ${xhr.responseText}`; + resultElement.innerHTML = `Error: Unable to fetch data from ${agentUrl}
Status Code: ${xhr.status}
Status Text: ${xhr.statusText}
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:
Unable to connect to ${url}
Check network connection or try again later.`; + resultElement.innerHTML = `Network Error:
Unable to connect to ${agentUrl}
Check network connection or try again later.`; }; // Handle the timeout event