From 91221b5cb92b49cb9a0749c52b468d26ccd3d9d3 Mon Sep 17 00:00:00 2001 From: Doruk Date: Tue, 17 Jun 2025 19:38:51 +0200 Subject: [PATCH] keep track of original order instead of iterating through all buckets --- server/uptime-calculator.js | 43 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/server/uptime-calculator.js b/server/uptime-calculator.js index c03d64c53..54404939e 100644 --- a/server/uptime-calculator.js +++ b/server/uptime-calculator.js @@ -899,33 +899,34 @@ class UptimeCalculator { } // 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)) { const timestampNum = parseInt(timestamp); - // Find the appropriate bucket for this data point - // For daily data (> 30 days), timestamps are at start of day - // We need to find which bucket this day belongs to - for (let i = 0; i < buckets.length; i++) { - const bucket = buckets[i]; + // Move to the correct bucket (since data is sorted, we only need to move forward) + while (currentBucketIndex < buckets.length && + timestampNum >= buckets[currentBucketIndex].end) { + currentBucketIndex++; + } - if (days > 30) { - // For daily data, check if the timestamp falls within the bucket's day range - // Add 86400 (1 day in seconds) to include the whole day - if (timestampNum >= bucket.start && timestampNum < bucket.end) { - bucket.up += dataPoint.up || 0; - bucket.down += dataPoint.down || 0; + // Check if we're within a valid bucket + if (currentBucketIndex < buckets.length) { + const bucket = buckets[currentBucketIndex]; + + if (timestampNum >= bucket.start && timestampNum < bucket.end) { + 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.pending += dataPoint.pending || 0; - break; - } - } else { - // For minute/hourly data, use exact timestamp matching - 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; + } else { + // Minute/hourly data doesn't track maintenance/pending separately + bucket.maintenance += 0; + bucket.pending += 0; } } }