Problem Description
In cdmq/cdm.js, metric queries divide the time domain into resolution windows with inclusive millisecond timestamps:
timeWindowDuration = thisEnd - thisBegin + 1 (cdm.js:3517)
- Stored
metric_data.duration is generated as end - begin + 1 (rickshaw-gen-docs:501)
- Query denominator is
totalWeightTimesMetrics = timeWindowDuration * numMetricIds (cdm.js:3518)
However, when trimming partial documents that straddle window boundaries in calcAvg(), docDuration is calculated using end - begin without + 1:
https://github.com/perftool-incubator/CommonDataModel/blob/main/queries/cdmq/cdm.js#L3593-L3604
Object.keys(partialDocs).forEach((id) => {
var docDuration = partialDocs[id].end - partialDocs[id].begin;
if (partialDocs[id].begin < thisBegin) {
docDuration -= thisBegin - partialDocs[id].begin;
}
if (partialDocs[id].end > thisEnd) {
docDuration -= partialDocs[id].end - thisEnd;
}
var valueTimesWeight = partialDocs[id].value * docDuration;
sumValueTimesWeight += valueTimesWeight;
sumWeight += docDuration;
});
Because + 1 is omitted, each boundary document contributes 1 ms less duration than it actually spans inside the window. For example:
- A document spanning
[900, 1100] inside window [1000, 2000] has an inclusive overlap of [1000, 1100] (101 ms).
- The current calculation produces
(1100 - 900) - (1000 - 900) = 200 - 100 = 100 ms.
Proposed Solution
Simplify the boundary overlap calculation to directly compute the clamped inclusive interval:
Object.keys(partialDocs).forEach((id) => {
var overlapBegin = Math.max(partialDocs[id].begin, thisBegin);
var overlapEnd = Math.min(partialDocs[id].end, thisEnd);
var docDuration = overlapEnd - overlapBegin + 1;
var valueTimesWeight = partialDocs[id].value * docDuration;
sumValueTimesWeight += valueTimesWeight;
sumWeight += docDuration;
});
Problem Description
In
cdmq/cdm.js, metric queries divide the time domain into resolution windows with inclusive millisecond timestamps:timeWindowDuration = thisEnd - thisBegin + 1(cdm.js:3517)metric_data.durationis generated asend - begin + 1(rickshaw-gen-docs:501)totalWeightTimesMetrics = timeWindowDuration * numMetricIds(cdm.js:3518)However, when trimming partial documents that straddle window boundaries in
calcAvg(),docDurationis calculated usingend - beginwithout+ 1:https://github.com/perftool-incubator/CommonDataModel/blob/main/queries/cdmq/cdm.js#L3593-L3604
Because
+ 1is omitted, each boundary document contributes 1 ms less duration than it actually spans inside the window. For example:[900, 1100]inside window[1000, 2000]has an inclusive overlap of[1000, 1100](101 ms).(1100 - 900) - (1000 - 900) = 200 - 100 = 100 ms.Proposed Solution
Simplify the boundary overlap calculation to directly compute the clamped inclusive interval: