diff --git a/app/classes/agent.php b/app/classes/agent.php index c777bde..4a4b7e2 100644 --- a/app/classes/agent.php +++ b/app/classes/agent.php @@ -123,15 +123,20 @@ class Agent { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $agent[0]['url']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 10); // timeout 10 seconds + $response = curl_exec($ch); + $curl_error = curl_error($ch); // curl error for debugging + 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; } - return $response; } diff --git a/app/pages/agents.php b/app/pages/agents.php index 2fb60d9..e232b8e 100644 --- a/app/pages/agents.php +++ b/app/pages/agents.php @@ -10,8 +10,11 @@ $agentObject = new Agent($dbWeb); if ($_SERVER['REQUEST_METHOD'] == 'POST') { // FIXME code here +// header("Location: $app_root?platform=$platform_id&page=config"); +// exit(); $force = isset($_POST['force']) && $_POST['force'] == 'true'; + $agent_id = $_POST['agent']; $result = fetchAgent($agent_id, $force); if ($result !== false) { @@ -20,9 +23,6 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { echo json_encode(['error' => 'Failed to fetch API data']); } -// header("Location: $app_root?platform=$platform_id&page=config"); -// exit(); - // no form submitted, show the templates } else { $agentDetails = $agentObject->getAgentDetails($platform_id); diff --git a/app/templates/agent-list.php b/app/templates/agent-list.php index 74197cc..f09a924 100644 --- a/app/templates/agent-list.php +++ b/app/templates/agent-list.php @@ -6,10 +6,10 @@
agent id= htmlspecialchars($agent['id']) ?>: type = htmlspecialchars($agent['agent_type_id']) ?>, url = htmlspecialchars($agent['url']) ?> - - + +
Result:
-click a button to fetch data from the agent.+
click a button to fetch data from the agent.diff --git a/public_html/static/agents.js b/public_html/static/agents.js index c8a57f8..fa5be8a 100644 --- a/public_html/static/agents.js +++ b/public_html/static/agents.js @@ -1,20 +1,85 @@ -function fetchData(force = false) { +function fetchData(agent_id, url, force = false) { + + let counter = 0; + const resultElement = document.getElementById("result" + agent_id); + // Show loading text - document.getElementById("result").innerHTML = "Loading..."; + resultElement.innerHTML = "Loading... (0 seconds)"; + + // Create an interval to update the counter every second + const intervalId = setInterval(() => { + counter++; + resultElement.innerHTML = `Loading... (${counter} seconds)`; + }, 1000); // Create an AJAX request var xhr = new XMLHttpRequest(); - xhr.open("POST", "index.php?page=agents", true); + + // Handle invalid URL error + try { + xhr.open("POST", url, true); + } catch (e) { + clearInterval(intervalId); // Stop the counter on error + resultElement.innerHTML = `Error: Invalid URL ${url}