Change top_hits aggregation to top_metrics for snapshot api (#95644)

* Change top_hits aggregations to top_metrics for snapshot api

* Fix typing

* Remove top_hits types

* Add test for top metrics

* Fix functional test

* Add size 1 to top metrics aggregation

* change type for metadata rows

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Ester Martí Vilaseca 2021-04-06 14:32:16 +02:00 committed by GitHub
parent 8bf9ea44e7
commit 1d6f61f186
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 34 deletions

View file

@ -282,8 +282,10 @@ export const ESSumBucketAggRT = rt.type({
}),
});
export const ESTopHitsAggRT = rt.type({
top_hits: rt.object,
export const ESTopMetricsAggRT = rt.type({
top_metrics: rt.type({
metrics: rt.union([rt.array(rt.type({ field: rt.string })), rt.type({ field: rt.string })]),
}),
});
export interface SnapshotTermsWithAggregation {
@ -309,7 +311,7 @@ export const ESAggregationRT = rt.union([
ESSumBucketAggRT,
ESTermsWithAggregationRT,
ESCaridnalityAggRT,
ESTopHitsAggRT,
ESTopMetricsAggRT,
]);
export const MetricsUIAggregationRT = rt.record(rt.string, ESAggregationRT);

View file

@ -81,6 +81,46 @@ Object {
}
`;
exports[`convertHistogramBucketsToTimeseies(keys, options, buckets) should tranform top_metric aggregations 1`] = `
Object {
"columns": Array [
Object {
"name": "timestamp",
"type": "date",
},
Object {
"name": "metric_0",
"type": "number",
},
Object {
"name": "__metadata__",
"type": "number",
},
],
"id": "example-0",
"keys": Array [
"example-0",
],
"rows": Array [
Object {
"__metadata__": Array [
Object {
"host.ip": "testHostIp",
"host.name": "testHostName",
},
],
"metric_0": 1,
"timestamp": 1577836800000,
},
Object {
"__metadata__": Array [],
"metric_0": null,
"timestamp": 1577836860000,
},
],
}
`;
exports[`convertHistogramBucketsToTimeseies(keys, options, buckets) should work with keyed percentiles 1`] = `
Object {
"columns": Array [

View file

@ -118,4 +118,56 @@ describe('convertHistogramBucketsToTimeseies(keys, options, buckets)', () => {
convertHistogramBucketsToTimeseries(keys, { ...options }, bucketsWithMultipleKeyedPercentiles)
).toThrow();
});
it('should tranform top_metric aggregations', () => {
const topMetricOptions: MetricsAPIRequest = {
...options,
metrics: [
{ id: 'metric_0', aggregations: { metric_0: { avg: { field: 'system.cpu.user.pct' } } } },
{
id: '__metadata__',
aggregations: {
__metadata__: {
top_metrics: {
metrics: [{ field: 'host.name' }, { field: 'host.ip' }],
},
},
},
},
],
};
const bucketsWithTopAggregation = [
{
key: 1577836800000,
key_as_string: '2020-01-01T00:00:00.000Z',
doc_count: 1,
metric_0: { value: 1 },
__metadata__: {
top: [
{
sort: ['2021-03-30T13:46:27.684Z'],
metrics: {
'host.name': 'testHostName',
'host.ip': 'testHostIp',
},
},
],
},
},
{
key: 1577836860000,
key_as_string: '2020-01-01T00:01:00.000Z',
doc_count: 1,
metric_0: { value: null },
__metadata__: {
top: [],
},
},
];
expect(
convertHistogramBucketsToTimeseries(keys, topMetricOptions, bucketsWithTopAggregation)
).toMatchSnapshot();
});
});

View file

@ -19,7 +19,7 @@ import {
NormalizedMetricValueRT,
PercentilesTypeRT,
PercentilesKeyedTypeRT,
TopHitsTypeRT,
TopMetricsTypeRT,
MetricValueTypeRT,
} from '../types';
@ -57,8 +57,8 @@ const getValue = (valueObject: ValueObjectType) => {
return valueObject.value;
}
if (TopHitsTypeRT.is(valueObject)) {
return valueObject.hits.hits.map((hit) => hit._source);
if (TopMetricsTypeRT.is(valueObject)) {
return valueObject.top.map((res) => res.metrics);
}
return null;

View file

@ -26,27 +26,13 @@ export const PercentilesKeyedTypeRT = rt.type({
values: rt.array(rt.type({ key: rt.string, value: NumberOrNullRT })),
});
export const TopHitsTypeRT = rt.type({
hits: rt.type({
total: rt.type({
value: rt.number,
relation: rt.string,
}),
hits: rt.array(
rt.intersection([
rt.type({
_index: rt.string,
_id: rt.string,
_score: NumberOrNullRT,
_source: rt.object,
}),
rt.partial({
sort: rt.array(rt.union([rt.string, rt.number])),
max_score: NumberOrNullRT,
}),
])
),
}),
export const TopMetricsTypeRT = rt.type({
top: rt.array(
rt.type({
sort: rt.union([rt.array(rt.number), rt.array(rt.string)]),
metrics: rt.record(rt.string, rt.union([rt.number, rt.string, rt.null])),
})
),
});
export const MetricValueTypeRT = rt.union([
@ -54,7 +40,7 @@ export const MetricValueTypeRT = rt.union([
NormalizedMetricValueRT,
PercentilesTypeRT,
PercentilesKeyedTypeRT,
TopHitsTypeRT,
TopMetricsTypeRT,
]);
export type MetricValueType = rt.TypeOf<typeof MetricValueTypeRT>;

View file

@ -20,7 +20,7 @@ import { InfraSource } from '../../../lib/sources';
export const isIPv4 = (subject: string) => /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/.test(subject);
type RowWithMetadata = MetricsAPIRow & {
[META_KEY]: object[];
[META_KEY]: Array<Record<string, string | number | string[]>>;
};
export const applyMetadataToLastPath = (
@ -47,7 +47,7 @@ export const applyMetadataToLastPath = (
source.configuration.fields
);
// Set the label as the name and fallback to the id OR path.value
lastPath.label = get(firstMetaDoc, inventoryFields.name, lastPath.value);
lastPath.label = (firstMetaDoc[inventoryFields.name] ?? lastPath.value) as string;
// If the inventory fields contain an ip address, we need to try and set that
// on the path object. IP addersses are typically stored as multiple fields. We will
// use the first IPV4 address we find.

View file

@ -72,16 +72,19 @@ export const transformRequestToMetricsAPIRequest = async (
id: META_KEY,
aggregations: {
[META_KEY]: {
top_hits: {
top_metrics: {
size: 1,
_source: [inventoryFields.name],
sort: [{ [sourceOverrides?.timestamp ?? source.configuration.fields.timestamp]: 'desc' }],
metrics: [{ field: inventoryFields.name }],
sort: {
[source.configuration.fields.timestamp]: 'desc',
},
},
},
},
};
if (inventoryFields.ip) {
metaAggregation.aggregations[META_KEY].top_hits._source.push(inventoryFields.ip);
metaAggregation.aggregations[META_KEY].top_metrics.metrics.push({ field: inventoryFields.ip });
}
metricsApiRequest.metrics.push(metaAggregation);