keep track of original order instead of iterating through all buckets

This commit is contained in:
Doruk 2025-06-17 19:38:51 +02:00
parent 88b3cfc640
commit 91221b5cb9

View file

@ -899,33 +899,34 @@ class UptimeCalculator {
} }
// Aggregate available data into buckets // Aggregate available data into buckets
// Since data is sorted, we can optimize by tracking current bucket index
let currentBucketIndex = 0;
for (const [ timestamp, dataPoint ] of Object.entries(availableData)) { for (const [ timestamp, dataPoint ] of Object.entries(availableData)) {
const timestampNum = parseInt(timestamp); const timestampNum = parseInt(timestamp);
// Find the appropriate bucket for this data point // Move to the correct bucket (since data is sorted, we only need to move forward)
// For daily data (> 30 days), timestamps are at start of day while (currentBucketIndex < buckets.length &&
// We need to find which bucket this day belongs to timestampNum >= buckets[currentBucketIndex].end) {
for (let i = 0; i < buckets.length; i++) { currentBucketIndex++;
const bucket = buckets[i]; }
if (days > 30) { // Check if we're within a valid bucket
// For daily data, check if the timestamp falls within the bucket's day range if (currentBucketIndex < buckets.length) {
// Add 86400 (1 day in seconds) to include the whole day const bucket = buckets[currentBucketIndex];
if (timestampNum >= bucket.start && timestampNum < bucket.end) {
bucket.up += dataPoint.up || 0; if (timestampNum >= bucket.start && timestampNum < bucket.end) {
bucket.down += dataPoint.down || 0; bucket.up += dataPoint.up || 0;
bucket.down += dataPoint.down || 0;
if (days > 30) {
// Daily data includes maintenance and pending
bucket.maintenance += dataPoint.maintenance || 0; bucket.maintenance += dataPoint.maintenance || 0;
bucket.pending += dataPoint.pending || 0; bucket.pending += dataPoint.pending || 0;
break; } else {
} // Minute/hourly data doesn't track maintenance/pending separately
} else { bucket.maintenance += 0;
// For minute/hourly data, use exact timestamp matching bucket.pending += 0;
if (timestampNum >= bucket.start && timestampNum < bucket.end && dataPoint) {
bucket.up += dataPoint.up || 0;
bucket.down += dataPoint.down || 0;
bucket.maintenance += 0; // UptimeCalculator treats maintenance as up
bucket.pending += 0; // UptimeCalculator doesn't track pending separately
break;
} }
} }
} }