Skip to content

Commit

Permalink
[Infra] Replace MetricsUIAggregation in favor of `estypes.Aggregati…
Browse files Browse the repository at this point in the history
…onsAggregate` (elastic#190495)

Closes [elastic#190311](elastic#190311)

## Summary

This PR replaces `MetricsUIAggregation` in favor of
`estypes.AggregationsAggregate`. Now the `MetricsUIAggregation` uses
estypes.AggregationsAggregate and the `MetricsUIAggregationRT `is
removed which also allows us to remove the `ESAggregationRT`. Instead of
maintaining the runtime types we now rely on the types provided by
elasticsearch.

A follow-up issue will be linked here to address the other aggregation
types related changes:
[elastic#190497](elastic#190497)

## Testing
- Check the types 
- Host page should load with no errors
- To test if the API works use the request provided in the
[x-pack/plugins/observability_solution/infra/server/routes/infra/README.md](https://github.com/elastic/kibana/compare/main...jennypavlova:kibana:190311-infra-replace-metricsuiaggregation-in-favor-of-estypesaggregationsaggregate?expand=1#diff-e853fdd3f4073eff8ff8a4df6a657f8cb5fefaa231be0116fe3692ae929f26a8)
- if the body is not valid 400 is returned

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
jennypavlova and kibanamachine authored Aug 21, 2024
1 parent fdae134 commit 22031df
Show file tree
Hide file tree
Showing 29 changed files with 94 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import * as rt from 'io-ts';
import { MetricsAPISeriesRT, MetricsAPIRow } from '../metrics_api';
import { MetricsAPISeriesRT, type MetricsAPIRow } from '@kbn/metrics-data-access-plugin/common';

const AggValueRT = rt.type({
value: rt.number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

export * from './metadata_api';
export * from './metrics_explorer';
export * from './metrics_api';
export * from './snapshot_api';
export * from './host_details';
export * from './infra';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { createLiteralValueFromUndefinedRT } from '@kbn/io-ts-utils';
import * as rt from 'io-ts';
import { SnapshotMetricTypeRT, ItemTypeRT } from '@kbn/metrics-data-access-plugin/common';
import { MetricsAPISeriesRT } from './metrics_api';
import { MetricsAPISeriesRT } from '@kbn/metrics-data-access-plugin/common';

export const SnapshotNodePathRT = rt.intersection([
rt.type({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
initGetLogAnalysisIdFormatsRoute,
} from './routes/log_analysis';
import { initMetadataRoute } from './routes/metadata';
import { initMetricsAPIRoute } from './routes/metrics_api';
import { initMetricsSourceConfigurationRoutes } from './routes/metrics_sources';
import { initNodeDetailsRoute } from './routes/node_details';
import { initOverviewRoute } from './routes/overview';
Expand Down Expand Up @@ -54,7 +53,6 @@ export const registerRoutes = (libs: InfraBackendLibs) => {
initValidateLogAnalysisIndicesRoute(libs);
initGetLogEntryExamplesRoute(libs);
initMetricsExplorerViewRoutes(libs);
initMetricsAPIRoute(libs);
initMetadataRoute(libs);
initInventoryMetaRoute(libs);
initInventoryViewRoutes(libs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import { has } from 'lodash';
import {
MetricsUIAggregation,
MetricsUIAggregationRT,
ESSumBucketAggRT,
ESTermsWithAggregationRT,
ESDerivativeAggRT,
ESBasicMetricAggRT,
ESTermsWithAggregationRT,
} from '@kbn/metrics-data-access-plugin/common';
import { SnapshotCustomMetricInput } from '../../../../../common/http_api';

export const isMetricRate = (metric: MetricsUIAggregation | undefined): boolean => {
if (!MetricsUIAggregationRT.is(metric)) {
if (!metric) {
return false;
}
const values = Object.values(metric);
Expand All @@ -32,7 +31,7 @@ export const isCustomMetricRate = (customMetric: SnapshotCustomMetricInput) => {
};

export const isInterfaceRateAgg = (metric: MetricsUIAggregation | undefined) => {
if (!MetricsUIAggregationRT.is(metric)) {
if (!metric) {
return false;
}
const values = Object.values(metric);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { decodeOrThrow } from '@kbn/io-ts-utils';
import type { MetricsAPIResponse, MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';
import { TIMESTAMP_FIELD } from '../../../common/constants';
import { MetricsAPIRequest, MetricsAPIResponse } from '../../../common/http_api';
import {
ESSearchClient,
CompositeResponseRT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import moment from 'moment';
import { MetricsAPITimerange } from '../../../../../common/http_api';
import type { MetricsAPITimerange } from '@kbn/metrics-data-access-plugin/common';
import { calculateAuto } from './calculate_auto';
import {
getUnitValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { MetricsAPITimerange } from '../../../../common/http_api';
import type { MetricsAPITimerange } from '@kbn/metrics-data-access-plugin/common';
import { calculateBucketSize } from './calculate_bucket_size';

export const calculateDateHistogramOffset = (timerange: MetricsAPITimerange): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import { isArray, isNumber } from 'lodash';
import { MetricsAPIRequest } from '../../../../common/http_api';
import { ESSearchClient } from '../types';
import type { MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';
import { calculateMetricInterval } from '../../../utils/calculate_metric_interval';
import type { ESSearchClient } from '../types';

export const calculatedInterval = async (search: ESSearchClient, options: MetricsAPIRequest) => {
const useModuleInterval =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import { MetricsAPIRequest } from '../../../../common/http_api';
import moment from 'moment';
import { convertBucketsToMetricsApiSeries } from './convert_buckets_to_metrics_series';
import type { MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';

const keys = ['example-0'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import { get, values, first } from 'lodash';
import * as rt from 'io-ts';
import {
import type {
MetricsAPIRequest,
MetricsAPISeries,
MetricsAPIColumn,
MetricsAPIRow,
} from '../../../../common/http_api/metrics_api';
} from '@kbn/metrics-data-access-plugin/common';
import {
Bucket,
BasicMetricValueRT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import type { MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';
import { createAggregations, createCompositeAggregations } from './create_aggregations';
import moment from 'moment';
import { MetricsAPIRequest } from '../../../../common/http_api';

const options: MetricsAPIRequest = {
timerange: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import { AggregationOptionsByType } from '@kbn/es-types';

import Boom from '@hapi/boom';
import { type MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';
import { afterKeyObjectRT } from '../../../../common/http_api';
import { TIMESTAMP_FIELD } from '../../../../common/constants';
import { MetricsAPIRequest } from '../../../../common/http_api/metrics_api';
import { calculateDateHistogramOffset } from './calculate_date_histogram_offset';
import { createMetricsAggregations } from './create_metrics_aggregations';
import { calculateBucketSize } from './calculate_bucket_size';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import { MetricsAPIRequest } from '../../../../common/http_api';
import moment from 'moment';
import { createMetricsAggregations } from './create_metrics_aggregations';
import type { MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';

const options: MetricsAPIRequest = {
timerange: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* 2.0.
*/

import { MetricsUIAggregation } from '@kbn/metrics-data-access-plugin/common';
import { MetricsAPIRequest } from '../../../../common/http_api/metrics_api';
import type { MetricsUIAggregation } from '@kbn/metrics-data-access-plugin/common';
import type { MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';

export const createMetricsAggregations = (options: MetricsAPIRequest): MetricsUIAggregation => {
const { metrics } = options;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
*/

import { get, last, first, isArray } from 'lodash';
import { findInventoryFields } from '@kbn/metrics-data-access-plugin/common';
import {
SnapshotRequest,
SnapshotNodePath,
SnapshotNode,
MetricsAPISeries,
MetricsAPIRow,
} from '../../../../common/http_api';
type MetricsAPIRow,
type MetricsAPISeries,
findInventoryFields,
} from '@kbn/metrics-data-access-plugin/common';
import { SnapshotRequest, SnapshotNodePath, SnapshotNode } from '../../../../common/http_api';
import { META_KEY } from './constants';

export const isIPv4 = (subject: string) => /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/.test(subject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
*/

import { uniq } from 'lodash';
import { MetricsUIAggregation, ESBasicMetricAggRT } from '@kbn/metrics-data-access-plugin/common';
import { MetricsAPITimerange } from '../../../../common/http_api';
import {
type MetricsUIAggregation,
ESBasicMetricAggRT,
type MetricsAPITimerange,
} from '@kbn/metrics-data-access-plugin/common';
import { ESSearchClient } from '../../../lib/metrics/types';
import { calculateMetricInterval } from '../../../utils/calculate_metric_interval';
import { getMetricsAggregations, InfraSnapshotRequestOptions } from './get_metrics_aggregations';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import { i18n } from '@kbn/i18n';
import { JsonObject } from '@kbn/utility-types';
import {
import type {
InventoryItemType,
MetricsUIAggregation,
MetricsUIAggregationRT,
} from '@kbn/metrics-data-access-plugin/common';
import { findInventoryModel } from '@kbn/metrics-data-access-plugin/common';
import { networkTraffic } from '@kbn/metrics-data-access-plugin/common';
Expand Down Expand Up @@ -54,7 +53,7 @@ export const getMetricsAggregations = (
const { metrics } = options;
return metrics.reduce((aggs, metric, index) => {
const aggregation = metricToAggregation(options.nodeType, metric, index);
if (!MetricsUIAggregationRT.is(aggregation)) {
if (!aggregation) {
throw new Error(
i18n.translate('xpack.infra.snapshot.missingSnapshotMetricError', {
defaultMessage: 'The aggregation for {metric} for {nodeType} is not available.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { MetricsAPIRequest, MetricsAPIResponse } from '../../../../common/http_api';
import type { MetricsAPIResponse, MetricsAPIRequest } from '@kbn/metrics-data-access-plugin/common';
import { ESSearchClient } from '../../../lib/metrics/types';
import { query } from '../../../lib/metrics';

Expand Down
Loading

0 comments on commit 22031df

Please sign in to comment.