-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Metrics UI] Calculate interval based on the dataset's period (#50194)
* Calculate interval based on the dataset's period * Remove unused import * Handle empty data case * Update x-pack/legacy/plugins/infra/server/utils/calculate_metric_interval.ts Co-Authored-By: Chris Cowan <[email protected]> * Update x-pack/legacy/plugins/infra/server/routes/metrics_explorer/lib/populate_series_with_tsvb_data.ts Co-Authored-By: Chris Cowan <[email protected]>
- Loading branch information
1 parent
70c8a14
commit bc7b7df
Showing
3 changed files
with
185 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
x-pack/legacy/plugins/infra/server/utils/calculate_metric_interval.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { InfraBackendFrameworkAdapter, InfraFrameworkRequest } from '../lib/adapters/framework'; | ||
|
||
interface Options { | ||
indexPattern: string; | ||
timestampField: string; | ||
timerange: { | ||
from: number; | ||
to: number; | ||
}; | ||
} | ||
|
||
/** | ||
* Look at the data from metricbeat and get the max period for a given timerange. | ||
* This is useful for visualizing metric modules like s3 that only send metrics once per day. | ||
*/ | ||
export const calculateMetricInterval = async ( | ||
framework: InfraBackendFrameworkAdapter, | ||
request: InfraFrameworkRequest, | ||
options: Options, | ||
modules: string[] | ||
) => { | ||
const query = { | ||
allowNoIndices: true, | ||
index: options.indexPattern, | ||
ignoreUnavailable: true, | ||
body: { | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ | ||
range: { | ||
[options.timestampField]: { | ||
gte: options.timerange.from, | ||
lte: options.timerange.to, | ||
format: 'epoch_millis', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
size: 0, | ||
aggs: { | ||
modules: { | ||
terms: { | ||
field: 'event.dataset', | ||
include: modules, | ||
}, | ||
aggs: { | ||
period: { | ||
max: { | ||
field: 'metricset.period', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const resp = await framework.callWithRequest<{}, PeriodAggregationData>(request, 'search', query); | ||
|
||
// if ES doesn't return an aggregations key, something went seriously wrong. | ||
if (!resp.aggregations) { | ||
return; | ||
} | ||
|
||
const intervals = resp.aggregations.modules.buckets.map(a => a.period.value).filter(v => !!v); | ||
if (!intervals.length) { | ||
return; | ||
} | ||
|
||
return Math.max(...intervals) / 1000; | ||
}; | ||
|
||
interface PeriodAggregationData { | ||
modules: { | ||
buckets: Array<{ | ||
key: string; | ||
doc_count: number; | ||
period: { | ||
value: number; | ||
}; | ||
}>; | ||
}; | ||
} |