2024-09-30 08:55:23 +00:00
function fetchData ( agent _id , url , endpoint , force = false ) {
2024-09-30 08:09:01 +00:00
let counter = 0 ;
const resultElement = document . getElementById ( "result" + agent _id ) ;
2024-09-27 06:49:50 +00:00
// Show loading text
2024-09-30 08:09:01 +00:00
resultElement . innerHTML = "Loading... (0 seconds)" ;
// Create an interval to update the counter every second
const intervalId = setInterval ( ( ) => {
counter ++ ;
resultElement . innerHTML = ` Loading... ( ${ counter } seconds) ` ;
} , 1000 ) ;
2024-09-27 06:49:50 +00:00
// Create an AJAX request
var xhr = new XMLHttpRequest ( ) ;
2024-09-30 08:55:23 +00:00
const agentUrl = url + endpoint ;
// FIXME for debugging purpose
console . log ( "Requesting URL:" , agentUrl ) ;
2024-09-30 08:09:01 +00:00
// Handle invalid URL error
try {
2024-09-30 08:55:23 +00:00
xhr . open ( "POST" , agentUrl , true ) ;
2024-09-30 08:09:01 +00:00
} catch ( e ) {
clearInterval ( intervalId ) ; // Stop the counter on error
2024-09-30 08:55:23 +00:00
resultElement . innerHTML = ` Error: Invalid URL ${ agentUrl } <br /> ` + e . message ;
2024-09-30 08:09:01 +00:00
return ; // Exit the function early
}
2024-09-27 06:49:50 +00:00
xhr . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
2024-09-30 08:09:01 +00:00
// Set a timeout in milliseconds (10 seconds = 10000 ms)
xhr . timeout = 10000 ;
// Handle the request state change
2024-09-27 06:49:50 +00:00
xhr . onreadystatechange = function ( ) {
2024-09-30 08:09:01 +00:00
if ( xhr . readyState === 4 ) {
clearInterval ( intervalId ) ; // Stop the counter when the request completes
clearTimeout ( requestTimeout ) ; // Clear the timeout if response is received
if ( xhr . status === 200 ) {
try {
// Parse and display the result
let result = JSON . parse ( xhr . responseText ) ;
2024-09-30 08:55:23 +00:00
if ( result . error ) {
resultElement . innerHTML = "Error: " + result . error ;
} else {
resultElement . innerHTML = JSON . stringify ( result , null , 2 ) ;
}
2024-09-30 08:09:01 +00:00
} catch ( e ) {
// Display the error
resultElement . innerHTML = "Error: Response is not a valid JSON.<br />Response: " + xhr . responseText ;
}
} else {
2024-09-30 08:55:23 +00:00
resultElement . innerHTML = ` Error: Unable to fetch data from ${ agentUrl } <br />Status Code: ${ xhr . status } <br />Status Text: ${ xhr . statusText } <br />Response: ${ xhr . responseText } ` ;
2024-09-30 08:09:01 +00:00
}
2024-09-27 06:49:50 +00:00
}
} ;
2024-09-30 08:09:01 +00:00
// Handle network-level errors (e.g., connection refused)
xhr . onerror = function ( ) {
clearInterval ( intervalId ) ; // Stop the counter on error
2024-09-30 08:55:23 +00:00
resultElement . innerHTML = ` Network Error:<br />Unable to connect to ${ agentUrl } <br />Check network connection or try again later. ` ;
2024-09-30 08:09:01 +00:00
} ;
// Handle the timeout event
xhr . ontimeout = function ( ) {
clearInterval ( intervalId ) ; // Stop the counter on timeout
resultElement . innerHTML = "Request timed out. Please try again." ;
} ;
// Additional manual timeout
var requestTimeout = setTimeout ( function ( ) {
if ( xhr . readyState !== 4 ) {
xhr . abort ( ) ; // Abort the request if still pending after timeout
clearInterval ( intervalId ) ; // Stop the counter
resultElement . innerHTML = "Request timed out." ;
}
} , 10000 ) ; // 10 seconds
2024-09-27 06:49:50 +00:00
// Send the AJAX request, with force flag
2024-09-30 08:09:01 +00:00
xhr . send ( "action=fetch&agent_id=" + agent _id + "&force=" + ( force ? 'true' : 'false' ) ) ;
// // If the request finishes quickly, set this up to show at least 1 second delay
// setTimeout(function() {
// if (counter === 0) {
// counter++;
// resultElement.innerHTML = `Loading... (${counter} seconds)`;
// }
// }, 1000); // Simulate a minimum 1 second delay for testing
2024-09-27 06:49:50 +00:00
}