Skip to content

Instantly share code, notes, and snippets.

@ashap5
Created October 9, 2024 19:24
Show Gist options
  • Save ashap5/66863186972bd25c72e13c55fbb76477 to your computer and use it in GitHub Desktop.
Save ashap5/66863186972bd25c72e13c55fbb76477 to your computer and use it in GitHub Desktop.
const https = require('https');
// The URL to send the request to
const url = 'https://192.168.2.205:21487/rNKTonIGZbq_jmFddv2oAg/metrics/transfer';
// Variable to store the last bytesTransferredByUserId state
let previousData = null;
// Function to send the request and process the response
function fetchData() {
const options = {
rejectUnauthorized: false // Ignore SSL errors for self-signed certificates (not recommended for production)
};
https.get(url, options, (res) => {
let data = '';
// Collect response data
res.on('data', chunk => {
data += chunk;
});
// Parse and process the response when it's fully received
res.on('end', () => {
try {
const parsedData = JSON.parse(data);
const currentData = parsedData.bytesTransferredByUserId;
if (previousData) {
// Calculate the difference in bytes since the last request
console.log("Data transferred since last request (in MB):");
for (const userId in currentData) {
const currentBytes = currentData[userId];
const previousBytes = previousData[userId] || 0;
const bytesTransferred = currentBytes - previousBytes;
const mbTransferred = bytesTransferred / (1024 * 1024);
console.log(`User ${userId}: ${mbTransferred.toFixed(2)} MB`);
}
} else {
console.log("Initial data received. Waiting for the next interval to calculate differences.");
}
// Update the previousData with the current state for the next interval
previousData = currentData;
} catch (error) {
console.error("Error parsing JSON response:", error);
}
});
}).on('error', (err) => {
console.error("Request failed:", err);
});
}
// Fetch data every 5 seconds
setInterval(fetchData, 5000);
// Initial call to fetch data immediately when the app starts
fetchData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment