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
// 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;
}
}
}