Skip to content

fix(cdmq): off-by-one duration calculation for partial boundary documents in calcAvg #211

Description

@atheurer

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions