From d92996e71f1dadb0cdf653531d11c602548dfc7a Mon Sep 17 00:00:00 2001 From: Gidi Meir Morris Date: Tue, 2 Feb 2021 11:28:29 +0000 Subject: [PATCH] support workload histograms that take into account overdue tasks --- .../monitoring/workload_statistics.test.ts | 119 ++++++++++++++++++ .../server/monitoring/workload_statistics.ts | 13 +- 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts index 21c9f429814cac..ed7c48d046dc2b 100644 --- a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts +++ b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts @@ -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( diff --git a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts index 8002ee44d01ff3..8bd22bd88cf410 100644 --- a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts +++ b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts @@ -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 );