Skip to content

Commit

Permalink
support workload histograms that take into account overdue tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Feb 2, 2021
1 parent 53a820e commit d92996e
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,125 @@ describe('padBuckets', () => {
})
).toEqual([0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
});

test('supports histogram buckets that begin in the past when tasks are overdue', async () => {
expect(
padBuckets(20, 3000, {
key: '2021-02-02T10:08:32.161Z-2021-02-02T10:09:32.161Z',
from: 1612260512161,
from_as_string: '2021-02-02T10:08:32.161Z',
to: 1612260572161,
to_as_string: '2021-02-02T10:09:32.161Z',
doc_count: 2,
histogram: {
buckets: [
{
key_as_string: '2021-02-02T10:08:30.000Z',
key: 1612260510000,
doc_count: 1,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: '2s',
doc_count: 1,
},
],
},
},
{
key_as_string: '2021-02-02T10:08:33.000Z',
key: 1612260513000,
doc_count: 0,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [],
},
},
{
key_as_string: '2021-02-02T10:08:36.000Z',
key: 1612260516000,
doc_count: 0,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [],
},
},
{
key_as_string: '2021-02-02T10:08:39.000Z',
key: 1612260519000,
doc_count: 0,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [],
},
},
{
key_as_string: '2021-02-02T10:08:42.000Z',
key: 1612260522000,
doc_count: 0,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [],
},
},
{
key_as_string: '2021-02-02T10:08:45.000Z',
key: 1612260525000,
doc_count: 0,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [],
},
},
{
key_as_string: '2021-02-02T10:08:48.000Z',
key: 1612260528000,
doc_count: 0,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [],
},
},
{
key_as_string: '2021-02-02T10:08:51.000Z',
key: 1612260531000,
doc_count: 0,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [],
},
},
{
key_as_string: '2021-02-02T10:08:54.000Z',
key: 1612260534000,
doc_count: 1,
interval: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: '60s',
doc_count: 1,
},
],
},
},
],
},
}).length
// we need to ensure overdue buckets don't cause us to over pad the timeline by adding additional
// buckets before and after the reported timeline
).toEqual(20);
});
});

function setTaskTypeCount(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,19 @@ export function padBuckets(
const firstBucket = histogram.buckets[0].key;
const lastBucket = histogram.buckets[histogram.buckets.length - 1].key;

const bucketsToPadBeforeFirstBucket = calculateBucketsBetween(firstBucket, from, pollInterval);
// detect when the first bucket is before the `from` so that we can take that into
// account by begining the timeline earlier
// This can happen when you have overdue tasks and Elasticsearch returns their bucket
// as begining before the `from`
const firstBucketStartsInThePast = firstBucket - from < 0;

const bucketsToPadBeforeFirstBucket = firstBucketStartsInThePast
? []
: calculateBucketsBetween(firstBucket, from, pollInterval);

const bucketsToPadAfterLast = calculateBucketsBetween(
lastBucket + pollInterval,
to,
firstBucketStartsInThePast ? to - pollInterval : to,
pollInterval
);

Expand Down

0 comments on commit d92996e

Please sign in to comment.