const axios = require("axios");
async function getSourceMetrics(apiKey, source) {
try {
const response = await axios.get(`https://scrape.st/metrics/${source}`, {
headers: {
"x-api-key": apiKey,
"Content-Type": "application/json",
},
});
const metrics = response.data;
console.log(`${source} Metrics:`, metrics);
// Analyze source performance
analyzeSourcePerformance(source, metrics);
return metrics;
} catch (error) {
if (error.response?.status === 404) {
console.error(`Source '${source}' not found`);
} else {
console.error(`Failed to fetch ${source} metrics:`, error.response?.data || error.message);
}
throw error;
}
}
function analyzeSourcePerformance(source, metrics) {
const sourceLatency = metrics.source_latency_ms;
const internalLatency = metrics.internal_latency_ms;
console.log(`\n${source.toUpperCase()} Performance Analysis:`);
console.log(`Source Latency - P50: ${sourceLatency.p50}ms, P95: ${sourceLatency.p95}ms, P99: ${sourceLatency.p99}ms`);
console.log(
`Internal Latency - P50: ${internalLatency.p50}ms, P95: ${internalLatency.p95}ms, P99: ${internalLatency.p99}ms`,
);
console.log(`Total Requests: ${metrics.count.source} in ${metrics.window_hours} hours`);
// Source-specific analysis
switch (source) {
case "twitter":
case "x":
analyzeTwitterMetrics(metrics);
break;
case "reddit":
analyzeRedditMetrics(metrics);
break;
case "news":
analyzeNewsMetrics(metrics);
break;
}
}
function analyzeTwitterMetrics(metrics) {
const sourceLatency = metrics.source_latency_ms;
// Twitter-specific thresholds
if (sourceLatency.p95 > 800) {
console.warn("⚠️ Twitter API latency is high (P95 > 800ms)");
}
if (sourceLatency.p99 > 2000) {
console.warn("⚠️ Twitter API experiencing extreme latency (P99 > 2000ms)");
}
console.log("✅ Twitter API performance is within acceptable range");
}
function analyzeRedditMetrics(metrics) {
const sourceLatency = metrics.source_latency_ms;
// Reddit-specific thresholds
if (sourceLatency.p95 > 600) {
console.warn("⚠️ Reddit API latency is high (P95 > 600ms)");
}
console.log("✅ Reddit API performance is within acceptable range");
}
function analyzeNewsMetrics(metrics) {
const sourceLatency = metrics.source_latency_ms;
// News-specific thresholds
if (sourceLatency.p95 > 1000) {
console.warn("⚠️ News source latency is high (P95 > 1000ms)");
}
console.log("✅ News source performance is within acceptable range");
}
// Usage
getSourceMetrics("your_api_key_here", "twitter");
getSourceMetrics("your_api_key_here", "reddit");