Skip to content

Instantly share code, notes, and snippets.

@hatamiarash7
Last active March 6, 2025 14:26
Show Gist options
  • Save hatamiarash7/0c7753312755efe9b446ddd1b8e4393d to your computer and use it in GitHub Desktop.
Save hatamiarash7/0c7753312755efe9b446ddd1b8e4393d to your computer and use it in GitHub Desktop.
Calculate Global Ping Average using ping.pe
// Flag to enable or disable filtering of 'ping-CN-*' elements
var excludeCN = true; // Set to `false` to disable filtering
// Select all <td> elements with specific id patterns
var last_pings = document.querySelectorAll('td[id^="ping-"][id$="-last"]');
var avg_pings = document.querySelectorAll('td[id^="ping-"][id$="-avg"]');
var best_pings = document.querySelectorAll('td[id^="ping-"][id$="-best"]');
var worst_pings = document.querySelectorAll('td[id^="ping-"][id$="-worst"]');
// Function to filter out elements with 'ping-CN-' in their id (if enabled)
function filterCN(elements) {
return excludeCN ? Array.from(elements).filter(td => !td.id.startsWith('ping-CN')) : Array.from(elements);
}
// Filter elements based on the `excludeCN` flag
var filtered_last = filterCN(last_pings);
var filtered_avg = filterCN(avg_pings);
var filtered_best = filterCN(best_pings);
var filtered_worst = filterCN(worst_pings);
// Function to extract numeric values from filtered elements
function extractValues(elements) {
return elements.map(td => parseFloat(td.textContent.trim())).filter(value => !isNaN(value));
}
// Extract values for all types
var last_values = extractValues(filtered_last);
var avg_values = extractValues(filtered_avg);
var best_values = extractValues(filtered_best);
var worst_values = extractValues(filtered_worst);
// Function to calculate the mean of an array of values
function calculateMean(values) {
return values.reduce((sum, value) => sum + value, 0) / values.length;
}
// Calculate means for all types
var last_mean = calculateMean(last_values);
var avg_mean = calculateMean(avg_values);
var best_mean = calculateMean(best_values);
var worst_mean = calculateMean(worst_values);
// Log the means to the console
console.log('Mean of all last:', last_mean);
console.log('Mean of all avg:', avg_mean);
console.log('Mean of all best:', best_mean);
console.log('Mean of all worst:', worst_mean);
// Function to extract values and locations from filtered elements
function extractValuesWithLocations(elements) {
return elements.map(td => {
const value = parseFloat(td.textContent.trim());
const location = td.id.split('-')[1]; // Extract location from id
return !isNaN(value) ? { value, location } : null;
}).filter(item => item !== null);
}
// Extract values and locations for avg
var avg_values_with_locations = extractValuesWithLocations(filtered_avg);
// Get top 5 highest values with locations for avg
var avg_top5_high = avg_values_with_locations.sort((a, b) => b.value - a.value).slice(0, 5);
// Log the top 5 highest avg values with locations
console.table(avg_top5_high);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment