Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ function beautify(options) {
log(`${options.logId} Beautify: ${options.result.length} results`);
let preFirstValue = null;
let postLastValue = null;
let removedBorderValues = []; // Keep track of removed border values for potential restoration

if (options.ignoreNull === 'true') { // include nulls and replace them with last value
options.ignoreNull = true;
Expand Down Expand Up @@ -684,6 +685,7 @@ function beautify(options) {
// remove all not requested points
if (options.result[i].ts < options.start) {
preFirstValue = options.result[i].val !== null ? options.result[i] : null;
removedBorderValues.push(options.result[i]); // Keep track of removed values
options.result.splice(i, 1);
i--;
continue;
Expand All @@ -692,11 +694,43 @@ function beautify(options) {
postLastValue = options.result[i].val !== null ? options.result[i] : null;

if (options.result[i].ts > options.end) {
// Keep track of all values being removed after the end time
for (let j = i; j < options.result.length; j++) {
removedBorderValues.push(options.result[j]);
}
options.result.splice(i, options.result.length - i);
break;
}
}

// Special handling for removeBorderValues: true with count specified
// If we removed all data due to time filtering but had data originally, and removeBorderValues is true,
// we should preserve the closest data point(s) to satisfy the count requirement
if (options.removeBorderValues && options.count && options.result.length === 0 && removedBorderValues.length > 0) {
log(`${options.logId} removeBorderValues + count edge case: restoring up to ${options.count} closest data points`);

// Sort removed border values by distance from the time range
const sortedByDistance = removedBorderValues.map(item => {
let distance;
if (item.ts < options.start) {
distance = options.start - item.ts;
} else if (item.ts > options.end) {
distance = item.ts - options.end;
} else {
distance = 0; // Should not happen since these were removed as border values
}
return { item, distance };
}).sort((a, b) => a.distance - b.distance);

// Restore up to 'count' closest data points
for (let i = 0; i < Math.min(options.count, sortedByDistance.length); i++) {
options.result.push(sortedByDistance[i].item);
}

// Sort the result by timestamp to maintain order
options.result.sort((a, b) => a.ts - b.ts);
}

// check start and stop
if (options.result.length && options.aggregate !== 'none' && !options.removeBorderValues) {
const firstTS = options.result[0].ts;
Expand Down Expand Up @@ -823,9 +857,20 @@ function sendResponse(adapter, msg, options, data, startTime) {
}
options.result = aggregateData.result;

if (options.debugLog) {
(options.log || console.log)(`${options.logId} Before beautify: ${options.result.length} results, count: ${options.count}, removeBorderValues: ${options.removeBorderValues}`);
}

beautify(options);

if (options.debugLog) {
(options.log || console.log)(`${options.logId} After beautify: ${options.result.length} results`);
}

if (options.aggregate === 'none' && options.count && options.result.length > options.count) {
if (options.debugLog) {
(options.log || console.log)(`${options.logId} Applying count limit: ${options.result.length} -> ${options.count}, removeBorderValues: ${options.removeBorderValues}`);
}
options.result.splice(0, options.result.length - options.count);
}
aggregateData.result = options.result;
Expand Down