Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for add the test.

* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { MetricsUIAggregation } from '@kbn/metrics-data-access-plugin/common';
import { isCustomMetricRate, isInterfaceRateAgg, isMetricRate, isRate } from './is_rate';
import { SnapshotCustomMetricInput } from '../../../../../common/http_api';

const customMaxMetricMock: SnapshotCustomMetricInput = {
type: 'custom',
aggregation: 'max',
field: '',
id: '',
};

const customRateMetricMock: SnapshotCustomMetricInput = {
type: 'custom',
aggregation: 'rate',
field: '',
id: '',
};

const metricMock: MetricsUIAggregation = {
mock1: { derivative: {} },
mock2: { max: null, mock: { field: '' } },
mock3: {
aggregations: {},
terms: {},
sum_bucket: {},
},
};

describe('isRate', () => {
describe('isMetricRate', () => {
it('should return false when metric is undefined', () => {
expect(isMetricRate(undefined)).toEqual(false);
});
it('should return true when correct metric is passed', () => {
expect(isMetricRate(metricMock)).toEqual(true);
});
});

describe('isCustomMetricRate', () => {
it("should return false when aggregation isn't 'rate'", () => {
expect(isCustomMetricRate(customMaxMetricMock)).toEqual(false);
});
it("should return true when aggregation is equal to 'rate'", () => {
expect(isCustomMetricRate(customRateMetricMock)).toEqual(true);
});

describe('isInterfaceRateAgg', () => {
it('should return false if metric is undefined', () => {
expect(isInterfaceRateAgg(undefined)).toEqual(false);
});
it('should return true when correct metric is passed', () => {
expect(isInterfaceRateAgg(metricMock)).toEqual(true);
});
});

describe('isRate', () => {
it('should return false when incorrect metrics are provided', () => {
expect(isRate({} as MetricsUIAggregation, {} as SnapshotCustomMetricInput)).toEqual(false);
});

it('should return true when proper metric are provided', () => {
expect(isRate(metricMock, customRateMetricMock)).toEqual(true);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import { has } from 'lodash';
Copy link
Copy Markdown
Contributor

@crespocarlos crespocarlos Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add tests for the functions here?

import {
MetricsUIAggregation,
ESSumBucketAggRT,
ESDerivativeAggRT,
ESBasicMetricAggRT,
ESTermsWithAggregationRT,
isBasicMetricAgg,
isDerivativeAgg,
isSumBucketAgg,
isTermsWithAggregation,
} from '@kbn/metrics-data-access-plugin/common';
import { SnapshotCustomMetricInput } from '../../../../../common/http_api';

Expand All @@ -21,8 +21,8 @@ export const isMetricRate = (metric: MetricsUIAggregation | undefined): boolean
}
const values = Object.values(metric);
return (
values.some((agg) => ESDerivativeAggRT.is(agg)) &&
values.some((agg) => ESBasicMetricAggRT.is(agg) && has(agg, 'max'))
values.some((agg) => isDerivativeAgg(agg)) &&
values.some((agg) => isBasicMetricAgg(agg) && has(agg, 'max'))
);
};

Expand All @@ -36,8 +36,7 @@ export const isInterfaceRateAgg = (metric: MetricsUIAggregation | undefined) =>
}
const values = Object.values(metric);
return (
values.some((agg) => ESTermsWithAggregationRT.is(agg)) &&
values.some((agg) => ESSumBucketAggRT.is(agg))
values.some((agg) => isTermsWithAggregation(agg)) && values.some((agg) => isSumBucketAgg(agg))
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import { uniq } from 'lodash';
import {
type MetricsUIAggregation,
ESBasicMetricAggRT,
type MetricsAPITimerange,
isBasicMetricAgg,
} from '@kbn/metrics-data-access-plugin/common';
import { ESSearchClient } from '../../../lib/metrics/types';
import { calculateMetricInterval } from '../../../utils/calculate_metric_interval';
Expand Down Expand Up @@ -74,7 +74,7 @@ const aggregationsToModules = async (
): Promise<string[]> => {
const uniqueFields = Object.values(aggregations)
.reduce<Array<string | undefined>>((fields, agg) => {
if (ESBasicMetricAggRT.is(agg)) {
if (isBasicMetricAgg(agg)) {
return uniq(fields.concat(Object.values(agg).map((a) => a?.field)));
}
return fields;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export {
findInventoryFields,
metrics,
type InventoryModels,
isBasicMetricAgg,
isDerivativeAgg,
isSumBucketAgg,
isTermsWithAggregation,
} from './inventory_models';

export { podSnapshotMetricTypes } from './inventory_models/kubernetes/pod';
Expand All @@ -28,11 +32,7 @@ export {
InventoryVisTypeRT,
ItemTypeRT,
SnapshotMetricTypeRT,
ESSumBucketAggRT,
ESDerivativeAggRT,
ESBasicMetricAggRT,
SnapshotMetricTypeKeys,
ESTermsWithAggregationRT,
} from './inventory_models/types';

export type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { estypes } from '@elastic/elasticsearch';
import { POD_FIELD, HOST_FIELD, CONTAINER_FIELD } from '../constants';
import { host } from './host';
import { pod } from './kubernetes/pod';
Expand Down Expand Up @@ -69,3 +70,34 @@ export const findInventoryFields = (type: InventoryItemType) => {
return inventoryModel.fields;
}
};

export const isBasicMetricAgg = (
agg: unknown
): agg is Record<string, undefined | Pick<estypes.AggregationsMetricAggregationBase, 'field'>> => {
if (agg === null || typeof agg !== 'object') return false;

return Object.values(agg).some(
(value) =>
value === undefined ||
(value && 'field' in (value as estypes.AggregationsMetricAggregationBase))
);
};

export const isDerivativeAgg = (
agg: unknown
): agg is Pick<estypes.AggregationsAggregationContainer, 'derivative'> => {
return !!(agg as estypes.AggregationsAggregationContainer).derivative;
};

export const isSumBucketAgg = (
agg: unknown
): agg is Pick<estypes.AggregationsAggregationContainer, 'sum_bucket'> => {
return !!(agg as estypes.AggregationsAggregationContainer).sum_bucket;
};

export const isTermsWithAggregation = (
agg: unknown
): agg is Pick<estypes.AggregationsAggregationContainer, 'terms' | 'aggregations'> => {
const aggContainer = agg as estypes.AggregationsAggregationContainer;
return !!(aggContainer.aggregations && aggContainer.terms);
};
Original file line number Diff line number Diff line change
Expand Up @@ -235,37 +235,8 @@ export type TSVBMetricModelCreator = (
interval: string
) => TSVBMetricModel;

export const ESBasicMetricAggRT = rt.record(
rt.string,
rt.union([
rt.undefined,
rt.type({
field: rt.string,
}),
])
);

export const ESDerivativeAggRT = rt.type({
derivative: rt.type({
buckets_path: rt.string,
gap_policy: rt.keyof({ skip: null, insert_zeros: null }),
unit: rt.string,
}),
});

export const ESSumBucketAggRT = rt.type({
sum_bucket: rt.type({
buckets_path: rt.string,
}),
});

export type MetricsUIAggregation = Record<string, estypes.AggregationsAggregate>;

export const ESTermsWithAggregationRT = rt.type({
terms: rt.type({ field: rt.string }),
aggregations: rt.UnknownRecord,
});

export const SnapshotMetricTypeKeys = {
count: null,
cpuV2: null,
Expand Down