From c5030e2351b6e493a94350a2ae3f16d04d62c29c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Tue, 18 Jan 2022 18:20:52 +0000 Subject: [PATCH 1/7] Remove invalid imports across public/server --- .../infra/common/alerting/metrics/types.ts | 8 +++ .../inventory/components/expression.test.tsx | 13 ++-- .../inventory/components/expression.tsx | 65 ++++++++----------- .../inventory/components/expression_chart.tsx | 26 ++++---- .../inventory/components/validation.tsx | 14 ++-- .../infra/public/alerting/inventory/index.ts | 9 +-- x-pack/plugins/infra/public/plugin.ts | 9 ++- x-pack/plugins/infra/server/features.ts | 8 ++- .../server/lib/alerting/common/messages.ts | 2 +- .../infra/server/lib/alerting/common/types.ts | 35 ---------- .../evaluate_condition.ts | 17 ++--- ...er_inventory_metric_threshold_rule_type.ts | 41 ++++++------ .../inventory_metric_threshold/types.ts | 27 -------- .../metric_anomaly/metric_anomaly_executor.ts | 15 ++--- .../lib/alerting/metric_threshold/types.ts | 22 ++----- 15 files changed, 116 insertions(+), 195 deletions(-) delete mode 100644 x-pack/plugins/infra/server/lib/alerting/common/types.ts delete mode 100644 x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts diff --git a/x-pack/plugins/infra/common/alerting/metrics/types.ts b/x-pack/plugins/infra/common/alerting/metrics/types.ts index 19812a7d375171..0ad7ca1c2aba58 100644 --- a/x-pack/plugins/infra/common/alerting/metrics/types.ts +++ b/x-pack/plugins/infra/common/alerting/metrics/types.ts @@ -37,6 +37,14 @@ export enum Aggregators { P99 = 'p99', } +export enum AlertStates { + OK, + ALERT, + WARNING, + NO_DATA, + ERROR, +} + const metricAnomalyNodeTypeRT = rt.union([rt.literal('hosts'), rt.literal('k8s')]); const metricAnomalyMetricRT = rt.union([ rt.literal('memory_usage'), diff --git a/x-pack/plugins/infra/public/alerting/inventory/components/expression.test.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/expression.test.tsx index ccb7a0e34d7364..c8cd2da45c5c32 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/components/expression.test.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/expression.test.tsx @@ -5,17 +5,14 @@ * 2.0. */ -import { mountWithIntl, shallowWithIntl, nextTick } from '@kbn/test/jest'; -// We are using this inside a `jest.mock` call. Jest requires dynamic dependencies to be prefixed with `mock` -import { coreMock as mockCoreMock } from 'src/core/public/mocks'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { InventoryMetricConditions } from '../../../../server/lib/alerting/inventory_metric_threshold/types'; +import { mountWithIntl, nextTick, shallowWithIntl } from '@kbn/test/jest'; import React from 'react'; -import { Expressions, AlertContextMeta, ExpressionRow, defaultExpression } from './expression'; import { act } from 'react-dom/test-utils'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { Comparator } from '../../../../server/lib/alerting/metric_threshold/types'; +// We are using this inside a `jest.mock` call. Jest requires dynamic dependencies to be prefixed with `mock` +import { coreMock as mockCoreMock } from 'src/core/public/mocks'; +import { Comparator, InventoryMetricConditions } from '../../../../common/alerting/metrics'; import { SnapshotCustomMetricInput } from '../../../../common/http_api/snapshot_api'; +import { AlertContextMeta, defaultExpression, ExpressionRow, Expressions } from './expression'; jest.mock('../../../containers/metrics_source/use_source_via_http', () => ({ useSourceViaHttp: () => ({ diff --git a/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx index 5734347b8909d9..58721f097dc525 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx @@ -6,71 +6,62 @@ */ import { Unit } from '@elastic/datemath'; -import React, { useCallback, useMemo, useEffect, useState, ChangeEvent } from 'react'; import { + EuiButtonEmpty, + EuiButtonIcon, + EuiCheckbox, + EuiFieldSearch, EuiFlexGroup, EuiFlexItem, - EuiButtonIcon, + EuiFormRow, + EuiHealth, + EuiIcon, EuiSpacer, EuiText, - EuiFormRow, - EuiButtonEmpty, - EuiFieldSearch, - EuiCheckbox, EuiToolTip, - EuiIcon, - EuiHealth, } from '@elastic/eui'; -import { FormattedMessage } from '@kbn/i18n-react'; import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n-react'; import { debounce, omit } from 'lodash'; -import { toMetricOpt } from '../../../../common/snapshot_metric_i18n'; -import { - Comparator, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../server/lib/alerting/metric_threshold/types'; +import React, { ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react'; import { euiStyled } from '../../../../../../../src/plugins/kibana_react/common'; import { - ThresholdExpression, ForLastExpression, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../triggers_actions_ui/public/common'; -import { IErrorObject, RuleTypeParamsExpressionProps, + ThresholdExpression, } from '../../../../../triggers_actions_ui/public'; -import { MetricsExplorerKueryBar } from '../../../pages/metrics/metrics_explorer/components/kuery_bar'; -import { useSourceViaHttp } from '../../../containers/metrics_source/use_source_via_http'; -import { sqsMetricTypes } from '../../../../common/inventory_models/aws_sqs/toolbar_items'; +import { Comparator, InventoryMetricConditions } from '../../../../common/alerting/metrics'; +import { + SnapshotCustomMetricInput, + SnapshotCustomMetricInputRT, +} from '../../../../common/http_api/snapshot_api'; +import { findInventoryModel } from '../../../../common/inventory_models'; import { ec2MetricTypes } from '../../../../common/inventory_models/aws_ec2/toolbar_items'; -import { s3MetricTypes } from '../../../../common/inventory_models/aws_s3/toolbar_items'; import { rdsMetricTypes } from '../../../../common/inventory_models/aws_rds/toolbar_items'; -import { hostMetricTypes } from '../../../../common/inventory_models/host/toolbar_items'; +import { s3MetricTypes } from '../../../../common/inventory_models/aws_s3/toolbar_items'; +import { sqsMetricTypes } from '../../../../common/inventory_models/aws_sqs/toolbar_items'; import { containerMetricTypes } from '../../../../common/inventory_models/container/toolbar_items'; +import { hostMetricTypes } from '../../../../common/inventory_models/host/toolbar_items'; import { podMetricTypes } from '../../../../common/inventory_models/pod/toolbar_items'; -import { findInventoryModel } from '../../../../common/inventory_models'; import { InventoryItemType, SnapshotMetricType, SnapshotMetricTypeRT, } from '../../../../common/inventory_models/types'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { InventoryMetricConditions } from '../../../../server/lib/alerting/inventory_metric_threshold/types'; -import { MetricExpression } from './metric'; -import { NodeTypeExpression } from './node_type'; +import { toMetricOpt } from '../../../../common/snapshot_metric_i18n'; +import { DerivedIndexPattern } from '../../../containers/metrics_source'; +import { useSourceViaHttp } from '../../../containers/metrics_source/use_source_via_http'; +import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; import { InfraWaffleMapOptions } from '../../../lib/lib'; +import { MetricsExplorerKueryBar } from '../../../pages/metrics/metrics_explorer/components/kuery_bar'; import { convertKueryToElasticSearchQuery } from '../../../utils/kuery'; -import { - SnapshotCustomMetricInput, - SnapshotCustomMetricInputRT, -} from '../../../../common/http_api/snapshot_api'; - -import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; -import { DerivedIndexPattern } from '../../../containers/metrics_source'; - import { ExpressionChart } from './expression_chart'; +import { MetricExpression } from './metric'; +import { NodeTypeExpression } from './node_type'; +import { QUERY_INVALID } from './validation'; + const FILTER_TYPING_DEBOUNCE_MS = 500; -export const QUERY_INVALID = Symbol('QUERY_INVALID'); export interface AlertContextMeta { options?: Partial; diff --git a/x-pack/plugins/infra/public/alerting/inventory/components/expression_chart.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/expression_chart.tsx index a83aa2ec12676c..4902a2d309b202 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/components/expression_chart.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/expression_chart.tsx @@ -4,35 +4,33 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React, { useMemo, useCallback } from 'react'; import { Axis, Chart, niceTimeFormatter, Position, Settings } from '@elastic/charts'; -import { first, last } from 'lodash'; -import moment from 'moment'; import { EuiText } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n-react'; +import { first, last } from 'lodash'; +import moment from 'moment'; +import React, { useCallback, useMemo } from 'react'; +import { InventoryMetricConditions } from '../../../../common/alerting/metrics'; import { Color } from '../../../../common/color_palette'; -import { MetricsExplorerRow, MetricsExplorerAggregation } from '../../../../common/http_api'; -import { MetricExplorerSeriesChart } from '../../../pages/metrics/metrics_explorer/components/series_chart'; -import { MetricsExplorerChartType } from '../../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options'; -import { calculateDomain } from '../../../pages/metrics/metrics_explorer/components/helpers/calculate_domain'; -import { getMetricId } from '../../../pages/metrics/metrics_explorer/components/helpers/get_metric_id'; +import { MetricsExplorerAggregation, MetricsExplorerRow } from '../../../../common/http_api'; +import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { InventoryMetricConditions } from '../../../../server/lib/alerting/inventory_metric_threshold/types'; import { useSnapshot } from '../../../pages/metrics/inventory_view/hooks/use_snaphot'; -import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; +import { useWaffleOptionsContext } from '../../../pages/metrics/inventory_view/hooks/use_waffle_options'; import { createInventoryMetricFormatter } from '../../../pages/metrics/inventory_view/lib/create_inventory_metric_formatter'; - +import { calculateDomain } from '../../../pages/metrics/metrics_explorer/components/helpers/calculate_domain'; +import { getMetricId } from '../../../pages/metrics/metrics_explorer/components/helpers/get_metric_id'; +import { MetricExplorerSeriesChart } from '../../../pages/metrics/metrics_explorer/components/series_chart'; +import { MetricsExplorerChartType } from '../../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options'; import { ChartContainer, + getChartTheme, LoadingState, NoDataState, TIME_LABELS, tooltipProps, - getChartTheme, } from '../../common/criterion_preview_chart/criterion_preview_chart'; import { ThresholdAnnotations } from '../../common/criterion_preview_chart/threshold_annotations'; -import { useWaffleOptionsContext } from '../../../pages/metrics/inventory_view/hooks/use_waffle_options'; interface Props { expression: InventoryMetricConditions; diff --git a/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx index 561bb39c6dce72..85782a7fa3dab3 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx @@ -6,21 +6,17 @@ */ import { i18n } from '@kbn/i18n'; -import { - InventoryMetricConditions, - Comparator, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../server/lib/alerting/inventory_metric_threshold/types'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { ValidationResult } from '../../../../../triggers_actions_ui/public/types'; -import { QUERY_INVALID } from './expression'; +import type { ValidationResult } from '../../../../../triggers_actions_ui/public'; +import { Comparator, InventoryMetricConditions } from '../../../../common/alerting/metrics'; + +export const QUERY_INVALID = Symbol('QUERY_INVALID'); export function validateMetricThreshold({ criteria, filterQuery, }: { criteria: InventoryMetricConditions[]; - filterQuery?: string | symbol; + filterQuery?: string | typeof QUERY_INVALID; }): ValidationResult { const validationResult = { errors: {} }; const errors: { diff --git a/x-pack/plugins/infra/public/alerting/inventory/index.ts b/x-pack/plugins/infra/public/alerting/inventory/index.ts index aa44369f8eb2c9..ca9f383aa28b0f 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/index.ts +++ b/x-pack/plugins/infra/public/alerting/inventory/index.ts @@ -7,15 +7,12 @@ import { i18n } from '@kbn/i18n'; import React from 'react'; +import { AlertTypeParams as RuleTypeParams } from '../../../../alerting/common'; +import { ObservabilityRuleTypeModel } from '../../../../observability/public'; import { InventoryMetricConditions, METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../server/lib/alerting/inventory_metric_threshold/types'; - -import { ObservabilityRuleTypeModel } from '../../../../observability/public'; - -import { AlertTypeParams as RuleTypeParams } from '../../../../alerting/common'; +} from '../../../common/alerting/metrics'; import { validateMetricThreshold } from './components/validation'; import { formatReason } from './rule_data_formatters'; diff --git a/x-pack/plugins/infra/public/plugin.ts b/x-pack/plugins/infra/public/plugin.ts index bc3aff9f016372..1eb016f5829392 100644 --- a/x-pack/plugins/infra/public/plugin.ts +++ b/x-pack/plugins/infra/public/plugin.ts @@ -10,6 +10,9 @@ import { AppMountParameters, PluginInitializerContext } from 'kibana/public'; import { from } from 'rxjs'; import { map } from 'rxjs/operators'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/public'; +import { createInventoryMetricRuleType } from './alerting/inventory'; +import { createLogThresholdRuleType } from './alerting/log_threshold'; +import { createMetricThresholdRuleType } from './alerting/metric_threshold'; import { LOG_STREAM_EMBEDDABLE } from './components/log_stream/log_stream_embeddable'; import { LogStreamEmbeddableFactoryDefinition } from './components/log_stream/log_stream_embeddable_factory'; import { createMetricsFetchData, createMetricsHasData } from './metrics_overview_fetchers'; @@ -26,15 +29,11 @@ import { getLogsHasDataFetcher, getLogsOverviewDataFetcher } from './utils/logs_ export class Plugin implements InfraClientPluginClass { constructor(_context: PluginInitializerContext) {} - async setup(core: InfraClientCoreSetup, pluginsSetup: InfraClientSetupDeps) { + setup(core: InfraClientCoreSetup, pluginsSetup: InfraClientSetupDeps) { if (pluginsSetup.home) { registerFeatures(pluginsSetup.home); } - const { createInventoryMetricRuleType } = await import('./alerting/inventory'); - const { createLogThresholdRuleType } = await import('./alerting/log_threshold'); - const { createMetricThresholdRuleType } = await import('./alerting/metric_threshold'); - pluginsSetup.observability.observabilityRuleTypeRegistry.register( createInventoryMetricRuleType() ); diff --git a/x-pack/plugins/infra/server/features.ts b/x-pack/plugins/infra/server/features.ts index 361565c3672c54..3e7ede11f7e9d3 100644 --- a/x-pack/plugins/infra/server/features.ts +++ b/x-pack/plugins/infra/server/features.ts @@ -6,10 +6,12 @@ */ import { i18n } from '@kbn/i18n'; -import { LOG_DOCUMENT_COUNT_RULE_TYPE_ID } from '../common/alerting/logs/log_threshold/types'; -import { METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID } from './lib/alerting/inventory_metric_threshold/types'; -import { METRIC_THRESHOLD_ALERT_TYPE_ID } from './lib/alerting/metric_threshold/types'; import { DEFAULT_APP_CATEGORIES } from '../../../../src/core/server'; +import { LOG_DOCUMENT_COUNT_RULE_TYPE_ID } from '../common/alerting/logs/log_threshold/types'; +import { + METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, + METRIC_THRESHOLD_ALERT_TYPE_ID, +} from '../common/alerting/metrics'; import { LOGS_FEATURE_ID, METRICS_FEATURE_ID } from '../common/constants'; export const METRICS_FEATURE = { diff --git a/x-pack/plugins/infra/server/lib/alerting/common/messages.ts b/x-pack/plugins/infra/server/lib/alerting/common/messages.ts index 0a3b41190a0883..d92670a4eb5f61 100644 --- a/x-pack/plugins/infra/server/lib/alerting/common/messages.ts +++ b/x-pack/plugins/infra/server/lib/alerting/common/messages.ts @@ -6,7 +6,7 @@ */ import { i18n } from '@kbn/i18n'; -import { Comparator, AlertStates } from './types'; +import { AlertStates, Comparator } from '../../../../common/alerting/metrics'; export const DOCUMENT_COUNT_I18N = i18n.translate( 'xpack.infra.metrics.alerting.threshold.documentCount', diff --git a/x-pack/plugins/infra/server/lib/alerting/common/types.ts b/x-pack/plugins/infra/server/lib/alerting/common/types.ts deleted file mode 100644 index 1d038cace14fe1..00000000000000 --- a/x-pack/plugins/infra/server/lib/alerting/common/types.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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. - */ - -export enum Comparator { - GT = '>', - LT = '<', - GT_OR_EQ = '>=', - LT_OR_EQ = '<=', - BETWEEN = 'between', - OUTSIDE_RANGE = 'outside', -} - -export enum Aggregators { - COUNT = 'count', - AVERAGE = 'avg', - SUM = 'sum', - MIN = 'min', - MAX = 'max', - RATE = 'rate', - CARDINALITY = 'cardinality', - P95 = 'p95', - P99 = 'p99', -} - -export enum AlertStates { - OK, - ALERT, - WARNING, - NO_DATA, - ERROR, -} diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts index b78c5eb291adb1..b4c4cb1e495d58 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts @@ -5,25 +5,26 @@ * 2.0. */ -import { mapValues, last, first } from 'lodash'; -import moment from 'moment'; import { ElasticsearchClient } from 'kibana/server'; +import { first, last, mapValues } from 'lodash'; +import moment from 'moment'; import { + Comparator, + InventoryMetricConditions, isTooManyBucketsPreviewException, TOO_MANY_BUCKETS_PREVIEW_EXCEPTION, } from '../../../../common/alerting/metrics'; -import { InfraDatabaseSearchResponse, CallWithRequestParams } from '../../adapters/framework'; -import { Comparator, InventoryMetricConditions } from './types'; -import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; import { InfraTimerangeInput, - SnapshotRequest, SnapshotCustomMetricInput, + SnapshotRequest, } from '../../../../common/http_api'; -import { InfraSource } from '../../sources'; -import { UNGROUPED_FACTORY_KEY } from '../common/utils'; +import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; import { getNodes } from '../../../routes/snapshot/lib/get_nodes'; import { LogQueryFields } from '../../../services/log_queries/get_log_query_fields'; +import { CallWithRequestParams, InfraDatabaseSearchResponse } from '../../adapters/framework'; +import { InfraSource } from '../../sources'; +import { UNGROUPED_FACTORY_KEY } from '../common/utils'; type ConditionResult = InventoryMetricConditions & { shouldFire: boolean[]; diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts index 9776d1ab669154..ac68a0df706fb4 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/register_inventory_metric_threshold_rule_type.ts @@ -5,37 +5,40 @@ * 2.0. */ -import { schema, Type } from '@kbn/config-schema'; import { Unit } from '@elastic/datemath'; +import { schema, Type } from '@kbn/config-schema'; import { i18n } from '@kbn/i18n'; import { PluginSetupContract } from '../../../../../alerting/server'; import { - createInventoryMetricThresholdExecutor, - FIRED_ACTIONS, - FIRED_ACTIONS_ID, - WARNING_ACTIONS, -} from './inventory_metric_threshold_executor'; -import { METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, Comparator } from './types'; + Comparator, + METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, +} from '../../../../common/alerting/metrics'; +import { + SnapshotCustomAggregation, + SNAPSHOT_CUSTOM_AGGREGATIONS, +} from '../../../../common/http_api/snapshot_api'; +import { + InventoryItemType, + SnapshotMetricType, + SnapshotMetricTypeKeys, +} from '../../../../common/inventory_models/types'; import { InfraBackendLibs } from '../../infra_types'; -import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils'; import { - groupActionVariableDescription, alertStateActionVariableDescription, + groupActionVariableDescription, + metricActionVariableDescription, reasonActionVariableDescription, + thresholdActionVariableDescription, timestampActionVariableDescription, valueActionVariableDescription, - metricActionVariableDescription, - thresholdActionVariableDescription, } from '../common/messages'; +import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils'; import { - SnapshotMetricTypeKeys, - SnapshotMetricType, - InventoryItemType, -} from '../../../../common/inventory_models/types'; -import { - SNAPSHOT_CUSTOM_AGGREGATIONS, - SnapshotCustomAggregation, -} from '../../../../common/http_api/snapshot_api'; + createInventoryMetricThresholdExecutor, + FIRED_ACTIONS, + FIRED_ACTIONS_ID, + WARNING_ACTIONS, +} from './inventory_metric_threshold_executor'; const condition = schema.object({ threshold: schema.arrayOf(schema.number()), diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts deleted file mode 100644 index 829f34d42ee039..00000000000000 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/types.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 { Unit } from '@elastic/datemath'; -import { SnapshotCustomMetricInput } from '../../../../common/http_api/snapshot_api'; -import { SnapshotMetricType } from '../../../../common/inventory_models/types'; -import { Comparator, AlertStates, Aggregators } from '../common/types'; - -export { Comparator, AlertStates, Aggregators }; - -export const METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID = 'metrics.alert.inventory.threshold'; - -export interface InventoryMetricConditions { - metric: SnapshotMetricType; - timeSize: number; - timeUnit: Unit; - sourceId?: string; - threshold: number[]; - comparator: Comparator; - customMetric?: SnapshotCustomMetricInput; - warningThreshold?: number[]; - warningComparator?: Comparator; -} diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts b/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts index a0eac87ed161eb..f762d694a59e70 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_anomaly/metric_anomaly_executor.ts @@ -6,27 +6,26 @@ */ import { i18n } from '@kbn/i18n'; +import { KibanaRequest } from 'kibana/server'; import { first } from 'lodash'; import moment from 'moment'; -import { KibanaRequest } from 'kibana/server'; -import { stateToAlertMessage } from '../common/messages'; -import { MetricAnomalyParams } from '../../../../common/alerting/metrics'; -import { MappedAnomalyHit } from '../../infra_ml'; -import { AlertStates } from '../common/types'; import { ActionGroup, AlertInstanceContext as AlertContext, AlertInstanceState as AlertState, } from '../../../../../alerting/common'; import { AlertExecutorOptions as RuleExecutorOptions } from '../../../../../alerting/server'; -import { getIntervalInSeconds } from '../../../utils/get_interval_in_seconds'; -import { MetricAnomalyAllowedActionGroups } from './register_metric_anomaly_rule_type'; import { MlPluginSetup } from '../../../../../ml/server'; +import { AlertStates, MetricAnomalyParams } from '../../../../common/alerting/metrics'; +import { getIntervalInSeconds } from '../../../utils/get_interval_in_seconds'; +import { MappedAnomalyHit } from '../../infra_ml'; import { InfraBackendLibs } from '../../infra_types'; +import { stateToAlertMessage } from '../common/messages'; import { evaluateCondition } from './evaluate_condition'; +import { MetricAnomalyAllowedActionGroups } from './register_metric_anomaly_rule_type'; export const createMetricAnomalyExecutor = - (libs: InfraBackendLibs, ml?: MlPluginSetup) => + (_libs: InfraBackendLibs, ml?: MlPluginSetup) => async ({ services, params, diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts index 101be1f77b9d0a..c8e20b5b35b9c0 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts @@ -6,22 +6,14 @@ */ import { Unit } from '@elastic/datemath'; -import { Comparator, AlertStates } from '../common/types'; +import { + Aggregators, + AlertStates, + Comparator, + METRIC_THRESHOLD_ALERT_TYPE_ID, +} from '../../../../common/alerting/metrics'; -export { Comparator, AlertStates }; -export const METRIC_THRESHOLD_ALERT_TYPE_ID = 'metrics.alert.threshold'; - -export enum Aggregators { - COUNT = 'count', - AVERAGE = 'avg', - SUM = 'sum', - MIN = 'min', - MAX = 'max', - RATE = 'rate', - CARDINALITY = 'cardinality', - P95 = 'p95', - P99 = 'p99', -} +export { Comparator, AlertStates, METRIC_THRESHOLD_ALERT_TYPE_ID }; interface BaseMetricExpressionParams { timeSize: number; From 51bb2103c37c1663a120aa8d5d41b21009ba808b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Tue, 18 Jan 2022 18:42:58 +0000 Subject: [PATCH 2/7] Redirect more forbidden imports --- .../infra/common/alerting/metrics/types.ts | 28 ++++++++++++-- .../threshold_annotations.test.tsx | 10 ++--- .../threshold_annotations.tsx | 10 ++--- .../alerting/inventory/components/metric.tsx | 26 ++++++------- .../expression_editor/threshold.tsx | 19 +++++----- .../metric_anomaly/components/expression.tsx | 33 +++++++--------- .../metric_anomaly/components/validation.tsx | 3 +- .../components/expression_chart.test.tsx | 11 +++--- .../components/expression_row.tsx | 31 +++++++-------- .../components/validation.tsx | 9 +---- .../public/alerting/metric_threshold/index.ts | 9 ++--- .../public/alerting/metric_threshold/types.ts | 7 +--- .../metric_threshold/lib/evaluate_rule.ts | 14 ++++--- .../metric_threshold/lib/metric_query.test.ts | 4 +- .../metric_threshold/lib/metric_query.ts | 4 +- .../metric_threshold_executor.test.ts | 24 ++++++------ .../lib/alerting/metric_threshold/types.ts | 38 ------------------- 17 files changed, 118 insertions(+), 162 deletions(-) delete mode 100644 x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts diff --git a/x-pack/plugins/infra/common/alerting/metrics/types.ts b/x-pack/plugins/infra/common/alerting/metrics/types.ts index 0ad7ca1c2aba58..37bc14431e41ad 100644 --- a/x-pack/plugins/infra/common/alerting/metrics/types.ts +++ b/x-pack/plugins/infra/common/alerting/metrics/types.ts @@ -4,14 +4,12 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import * as rt from 'io-ts'; import { Unit } from '@elastic/datemath'; +import * as rt from 'io-ts'; +import { SnapshotCustomMetricInput } from '../../http_api'; import { ANOMALY_THRESHOLD } from '../../infra_ml'; import { InventoryItemType, SnapshotMetricType } from '../../inventory_models/types'; -import { SnapshotCustomMetricInput } from '../../http_api'; -// TODO: Have threshold and inventory alerts import these types from this file instead of from their -// local directories export const METRIC_THRESHOLD_ALERT_TYPE_ID = 'metrics.alert.threshold'; export const METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID = 'metrics.alert.inventory.threshold'; export const METRIC_ANOMALY_ALERT_TYPE_ID = 'metrics.alert.anomaly'; @@ -88,3 +86,25 @@ export interface InventoryMetricThresholdParams { sourceId?: string; alertOnNoData?: boolean; } + +interface BaseMetricExpressionParams { + timeSize: number; + timeUnit: Unit; + sourceId?: string; + threshold: number[]; + comparator: Comparator; + warningComparator?: Comparator; + warningThreshold?: number[]; +} + +export interface NonCountMetricExpressionParams extends BaseMetricExpressionParams { + aggType: Exclude; + metric: string; +} + +export interface CountMetricExpressionParams extends BaseMetricExpressionParams { + aggType: Aggregators.COUNT; + metric: never; +} + +export type MetricExpressionParams = NonCountMetricExpressionParams | CountMetricExpressionParams; diff --git a/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.test.tsx b/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.test.tsx index 306a623eed984d..be8e474b601142 100644 --- a/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.test.tsx +++ b/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.test.tsx @@ -4,15 +4,11 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ +import { shallow } from 'enzyme'; import React from 'react'; -import { ThresholdAnnotations } from './threshold_annotations'; -import { - Comparator, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../server/lib/alerting/metric_threshold/types'; -// import { Color } from 'x-pack/plugins/infra/common/color_palette'; +import { Comparator } from '../../../../common/alerting/metrics'; import { Color } from '../../../../common/color_palette'; -import { shallow } from 'enzyme'; +import { ThresholdAnnotations } from './threshold_annotations'; jest.mock('@elastic/charts', () => { const original = jest.requireActual('@elastic/charts'); diff --git a/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.tsx b/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.tsx index 397d355eaeb5a9..9400537bb9d7c7 100644 --- a/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.tsx +++ b/x-pack/plugins/infra/public/alerting/common/criterion_preview_chart/threshold_annotations.tsx @@ -4,14 +4,10 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React from 'react'; +import { AnnotationDomainType, LineAnnotation, RectAnnotation } from '@elastic/charts'; import { first, last } from 'lodash'; -import { RectAnnotation, AnnotationDomainType, LineAnnotation } from '@elastic/charts'; - -import { - Comparator, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../server/lib/alerting/metric_threshold/types'; +import React from 'react'; +import { Comparator } from '../../../../common/alerting/metrics'; import { Color, colorTransformer } from '../../../../common/color_palette'; interface ThresholdAnnotationsProps { diff --git a/x-pack/plugins/infra/public/alerting/inventory/components/metric.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/metric.tsx index 9319272833c141..ba5316cbcfebd2 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/components/metric.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/metric.tsx @@ -4,33 +4,33 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React, { useState, useCallback, useMemo } from 'react'; -import { i18n } from '@kbn/i18n'; -import { FormattedMessage } from '@kbn/i18n-react'; import { + EuiButtonGroup, + EuiButtonIcon, + EuiComboBox, EuiExpression, - EuiPopover, + EuiFieldText, EuiFlexGroup, EuiFlexItem, EuiFormRow, - EuiComboBox, - EuiButtonGroup, - EuiSpacer, + EuiPopover, + EuiPopoverTitle, EuiSelect, + EuiSpacer, EuiText, - EuiFieldText, } from '@elastic/eui'; -import { EuiPopoverTitle, EuiButtonIcon } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n-react'; import { debounce } from 'lodash'; +import React, { useCallback, useMemo, useState } from 'react'; +import { IErrorObject } from '../../../../../triggers_actions_ui/public'; import { getCustomMetricLabel } from '../../../../common/formatters/get_custom_metric_label'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { IErrorObject } from '../../../../../triggers_actions_ui/public/types'; import { + SnapshotCustomAggregation, + SnapshotCustomAggregationRT, SnapshotCustomMetricInput, SnapshotCustomMetricInputRT, - SnapshotCustomAggregation, SNAPSHOT_CUSTOM_AGGREGATIONS, - SnapshotCustomAggregationRT, } from '../../../../common/http_api/snapshot_api'; import { DerivedIndexPattern } from '../../../containers/metrics_source'; diff --git a/x-pack/plugins/infra/public/alerting/log_threshold/components/expression_editor/threshold.tsx b/x-pack/plugins/infra/public/alerting/log_threshold/components/expression_editor/threshold.tsx index fdc60daceb7156..6b3023123cd8c1 100644 --- a/x-pack/plugins/infra/public/alerting/log_threshold/components/expression_editor/threshold.tsx +++ b/x-pack/plugins/infra/public/alerting/log_threshold/components/expression_editor/threshold.tsx @@ -5,21 +5,20 @@ * 2.0. */ -import React, { useState } from 'react'; -import { i18n } from '@kbn/i18n'; -import { isNumber, isFinite } from 'lodash'; import { - EuiPopoverTitle, - EuiFlexItem, + EuiExpression, + EuiFieldNumber, EuiFlexGroup, + EuiFlexItem, + EuiFormRow, EuiPopover, + EuiPopoverTitle, EuiSelect, - EuiFieldNumber, - EuiExpression, - EuiFormRow, } from '@elastic/eui'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { IErrorObject } from '../../../../../../triggers_actions_ui/public/types'; +import { i18n } from '@kbn/i18n'; +import { isFinite, isNumber } from 'lodash'; +import React, { useState } from 'react'; +import { IErrorObject } from '../../../../../../triggers_actions_ui/public'; import { Comparator, ComparatorToi18nMap, diff --git a/x-pack/plugins/infra/public/alerting/metric_anomaly/components/expression.tsx b/x-pack/plugins/infra/public/alerting/metric_anomaly/components/expression.tsx index 5a0060f229795f..51629b656a7e73 100644 --- a/x-pack/plugins/infra/public/alerting/metric_anomaly/components/expression.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_anomaly/components/expression.tsx @@ -5,34 +5,29 @@ * 2.0. */ -import React, { useCallback, useState, useMemo, useEffect } from 'react'; -import { EuiFlexGroup, EuiSpacer, EuiText, EuiLoadingContent } from '@elastic/eui'; -import { FormattedMessage } from '@kbn/i18n-react'; +import { EuiFlexGroup, EuiLoadingContent, EuiSpacer, EuiText } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; -import { useInfraMLCapabilities } from '../../../containers/ml/infra_ml_capabilities'; -import { SubscriptionSplashPrompt } from '../../../components/subscription_splash_content'; -import { MetricAnomalyParams } from '../../../../common/alerting/metrics'; +import { FormattedMessage } from '@kbn/i18n-react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { euiStyled, EuiThemeProvider } from '../../../../../../../src/plugins/kibana_react/common'; -import { - WhenExpression, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../triggers_actions_ui/public/common'; import { RuleTypeParams, RuleTypeParamsExpressionProps, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../../triggers_actions_ui/public/types'; -import { useSourceViaHttp } from '../../../containers/metrics_source/use_source_via_http'; + WhenExpression, +} from '../../../../../triggers_actions_ui/public'; +import { MetricAnomalyParams } from '../../../../common/alerting/metrics'; +import { ANOMALY_THRESHOLD } from '../../../../common/infra_ml'; import { findInventoryModel } from '../../../../common/inventory_models'; import { InventoryItemType, SnapshotMetricType } from '../../../../common/inventory_models/types'; -import { NodeTypeExpression } from './node_type'; -import { SeverityThresholdExpression } from './severity_threshold'; -import { InfraWaffleMapOptions } from '../../../lib/lib'; -import { ANOMALY_THRESHOLD } from '../../../../common/infra_ml'; - -import { InfluencerFilter } from './influencer_filter'; +import { SubscriptionSplashPrompt } from '../../../components/subscription_splash_content'; +import { useSourceViaHttp } from '../../../containers/metrics_source/use_source_via_http'; +import { useInfraMLCapabilities } from '../../../containers/ml/infra_ml_capabilities'; import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; import { useActiveKibanaSpace } from '../../../hooks/use_kibana_space'; +import { InfraWaffleMapOptions } from '../../../lib/lib'; +import { InfluencerFilter } from './influencer_filter'; +import { NodeTypeExpression } from './node_type'; +import { SeverityThresholdExpression } from './severity_threshold'; export interface AlertContextMeta { metric?: InfraWaffleMapOptions['metric']; diff --git a/x-pack/plugins/infra/public/alerting/metric_anomaly/components/validation.tsx b/x-pack/plugins/infra/public/alerting/metric_anomaly/components/validation.tsx index 8e254fb2b67a8b..a4eda632b2fdd8 100644 --- a/x-pack/plugins/infra/public/alerting/metric_anomaly/components/validation.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_anomaly/components/validation.tsx @@ -6,8 +6,7 @@ */ import { i18n } from '@kbn/i18n'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { ValidationResult } from '../../../../../triggers_actions_ui/public/types'; +import type { ValidationResult } from '../../../../../triggers_actions_ui/public'; export function validateMetricAnomaly({ hasInfraMLCapabilities, diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx index c2c1fa719bb95b..f7e3201bbf2c92 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_chart.test.tsx @@ -5,17 +5,16 @@ * 2.0. */ +import { DataViewBase } from '@kbn/es-query'; import { mountWithIntl, nextTick } from '@kbn/test/jest'; +import React from 'react'; +import { act } from 'react-dom/test-utils'; // We are using this inside a `jest.mock` call. Jest requires dynamic dependencies to be prefixed with `mock` import { coreMock as mockCoreMock } from 'src/core/public/mocks'; -import { MetricExpression } from '../types'; -import { DataViewBase } from '@kbn/es-query'; +import { Aggregators, Comparator } from '../../../../common/alerting/metrics'; import { MetricsSourceConfiguration } from '../../../../common/metrics_sources'; -import React from 'react'; +import { MetricExpression } from '../types'; import { ExpressionChart } from './expression_chart'; -import { act } from 'react-dom/test-utils'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { Aggregators, Comparator } from '../../../../server/lib/alerting/metric_threshold/types'; const mockStartServices = mockCoreMock.createStart(); jest.mock('../../../hooks/use_kibana', () => ({ diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.tsx index cc5a58aee13037..6f1d1ed6e12c01 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.tsx @@ -4,35 +4,32 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import React, { useCallback, useState, useMemo } from 'react'; -import { i18n } from '@kbn/i18n'; -import { FormattedMessage } from '@kbn/i18n-react'; import { + EuiButtonEmpty, + EuiButtonIcon, EuiFlexGroup, EuiFlexItem, - EuiButtonIcon, + EuiHealth, + EuiLink, EuiSpacer, EuiText, - EuiLink, - EuiHealth, - EuiButtonEmpty, } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n-react'; import { omit } from 'lodash'; -import { pctToDecimal, decimalToPct } from '../../../../common/utils/corrected_percent_convert'; +import React, { useCallback, useMemo, useState } from 'react'; +import { euiStyled } from '../../../../../../../src/plugins/kibana_react/common'; import { - WhenExpression, + builtInComparators, + IErrorObject, OfExpression, ThresholdExpression, + WhenExpression, } from '../../../../../triggers_actions_ui/public'; -import { euiStyled } from '../../../../../../../src/plugins/kibana_react/common'; -import { IErrorObject } from '../../../../../triggers_actions_ui/public'; -import { MetricExpression, AGGREGATION_TYPES } from '../types'; -import { - Comparator, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../server/lib/alerting/metric_threshold/types'; -import { builtInComparators } from '../../../../../triggers_actions_ui/public'; +import { Comparator } from '../../../../common/alerting/metrics'; +import { decimalToPct, pctToDecimal } from '../../../../common/utils/corrected_percent_convert'; import { DerivedIndexPattern } from '../../../containers/metrics_source'; +import { AGGREGATION_TYPES, MetricExpression } from '../types'; const customComparators = { ...builtInComparators, diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx index 8df313aa1627a4..e7aaf950e82cbf 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx @@ -6,13 +6,8 @@ */ import { i18n } from '@kbn/i18n'; -import { - MetricExpressionParams, - Comparator, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../../server/lib/alerting/metric_threshold/types'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { ValidationResult } from '../../../../../triggers_actions_ui/public/types'; +import { ValidationResult } from '../../../../../triggers_actions_ui/public'; +import { Comparator, MetricExpressionParams } from '../../../../common/alerting/metrics'; import { QUERY_INVALID } from './expression'; export function validateMetricThreshold({ diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts b/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts index cb938597ab432b..e67ece78365951 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/index.ts @@ -7,15 +7,14 @@ import { i18n } from '@kbn/i18n'; import React from 'react'; -import { ObservabilityRuleTypeModel } from '../../../../observability/public'; -import { validateMetricThreshold } from './components/validation'; -import { formatReason } from './rule_data_formatters'; import { AlertTypeParams as RuleTypeParams } from '../../../../alerting/common'; +import { ObservabilityRuleTypeModel } from '../../../../observability/public'; import { MetricExpressionParams, METRIC_THRESHOLD_ALERT_TYPE_ID, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../server/lib/alerting/metric_threshold/types'; +} from '../../../common/alerting/metrics'; +import { validateMetricThreshold } from './components/validation'; +import { formatReason } from './rule_data_formatters'; interface MetricThresholdRuleTypeParams extends RuleTypeParams { criteria: MetricExpressionParams[]; diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts b/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts index 0d1c85087f33d9..4fae8bfacca353 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts @@ -5,12 +5,9 @@ * 2.0. */ -import { - MetricExpressionParams, - // eslint-disable-next-line @kbn/eslint/no-restricted-paths -} from '../../../server/lib/alerting/metric_threshold/types'; -import { MetricsExplorerOptions } from '../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options'; +import { MetricExpressionParams } from '../../../common/alerting/metrics'; import { MetricsExplorerSeries } from '../../../common/http_api/metrics_explorer'; +import { MetricsExplorerOptions } from '../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options'; export interface AlertContextMeta { currentOptions?: Partial; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule.ts index d1d4f4e9560b52..3e7f2b3c6cf0f4 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule.ts @@ -5,23 +5,25 @@ * 2.0. */ -import moment from 'moment'; import { ElasticsearchClient } from 'kibana/server'; -import { mapValues, first, last, isNaN, isNumber, isObject, has } from 'lodash'; +import { first, has, isNaN, isNumber, isObject, last, mapValues } from 'lodash'; +import moment from 'moment'; import { + Aggregators, + Comparator, isTooManyBucketsPreviewException, + MetricExpressionParams, TOO_MANY_BUCKETS_PREVIEW_EXCEPTION, } from '../../../../../common/alerting/metrics'; -import { getIntervalInSeconds } from '../../../../utils/get_interval_in_seconds'; import { InfraSource } from '../../../../../common/source_configuration/source_configuration'; -import { InfraDatabaseSearchResponse } from '../../../adapters/framework/adapter_types'; import { createAfterKeyHandler } from '../../../../utils/create_afterkey_handler'; import { getAllCompositeData } from '../../../../utils/get_all_composite_data'; +import { getIntervalInSeconds } from '../../../../utils/get_interval_in_seconds'; +import { InfraDatabaseSearchResponse } from '../../../adapters/framework/adapter_types'; import { DOCUMENT_COUNT_I18N } from '../../common/messages'; import { UNGROUPED_FACTORY_KEY } from '../../common/utils'; -import { MetricExpressionParams, Comparator, Aggregators } from '../types'; -import { getElasticsearchMetricQuery } from './metric_query'; import { createTimerange } from './create_timerange'; +import { getElasticsearchMetricQuery } from './metric_query'; interface AggregationWithoutIntervals { aggregatedValue: { value: number; values?: Array<{ key: number; value: number }> }; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.test.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.test.ts index 463365b6449d0f..4fbdf72a7429cf 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.test.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.test.ts @@ -5,9 +5,9 @@ * 2.0. */ -import { MetricExpressionParams } from '../types'; -import { getElasticsearchMetricQuery } from './metric_query'; import moment from 'moment'; +import { MetricExpressionParams } from '../../../../../common/alerting/metrics'; +import { getElasticsearchMetricQuery } from './metric_query'; describe("The Metric Threshold Alert's getElasticsearchMetricQuery", () => { const expressionParams = { diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.ts index 588b77250e6a64..56c9d5d14d0141 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query.ts @@ -5,11 +5,11 @@ * 2.0. */ +import { Aggregators, MetricExpressionParams } from '../../../../../common/alerting/metrics'; import { TIMESTAMP_FIELD } from '../../../../../common/constants'; import { networkTraffic } from '../../../../../common/inventory_models/shared/metrics/snapshot/network_traffic'; -import { MetricExpressionParams, Aggregators } from '../types'; -import { createPercentileAggregation } from './create_percentile_aggregation'; import { calculateDateHistogramOffset } from '../../../metrics/lib/calculate_date_histogram_offset'; +import { createPercentileAggregation } from './create_percentile_aggregation'; const COMPOSITE_RESULTS_PER_PAGE = 100; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts index 5a75b18e47590c..b59ca5245db982 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.test.ts @@ -5,30 +5,30 @@ * 2.0. */ -import { createMetricThresholdExecutor, FIRED_ACTIONS } from './metric_threshold_executor'; -import * as mocks from './test_mocks'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks'; +import { + AlertInstanceContext as AlertContext, + AlertInstanceState as AlertState, +} from '../../../../../alerting/server'; // import { RecoveredActionGroup } from '../../../../../alerting/common'; import { - alertsMock, - AlertServicesMock, AlertInstanceMock, + AlertServicesMock, + alertsMock, } from '../../../../../alerting/server/mocks'; import { LifecycleAlertServices } from '../../../../../rule_registry/server'; import { ruleRegistryMocks } from '../../../../../rule_registry/server/mocks'; import { createLifecycleRuleExecutorMock } from '../../../../../rule_registry/server/utils/create_lifecycle_rule_executor_mock'; -import { InfraSources } from '../../sources'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks'; -import { - AlertInstanceContext as AlertContext, - AlertInstanceState as AlertState, -} from '../../../../../alerting/server'; import { Aggregators, Comparator, CountMetricExpressionParams, NonCountMetricExpressionParams, -} from './types'; +} from '../../../../common/alerting/metrics'; +import { InfraSources } from '../../sources'; +import { createMetricThresholdExecutor, FIRED_ACTIONS } from './metric_threshold_executor'; +import * as mocks from './test_mocks'; interface AlertTestInstance { instance: AlertInstanceMock; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts deleted file mode 100644 index c8e20b5b35b9c0..00000000000000 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/types.ts +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 { Unit } from '@elastic/datemath'; -import { - Aggregators, - AlertStates, - Comparator, - METRIC_THRESHOLD_ALERT_TYPE_ID, -} from '../../../../common/alerting/metrics'; - -export { Comparator, AlertStates, METRIC_THRESHOLD_ALERT_TYPE_ID }; - -interface BaseMetricExpressionParams { - timeSize: number; - timeUnit: Unit; - sourceId?: string; - threshold: number[]; - comparator: Comparator; - warningComparator?: Comparator; - warningThreshold?: number[]; -} - -export interface NonCountMetricExpressionParams extends BaseMetricExpressionParams { - aggType: Exclude; - metric: string; -} - -export interface CountMetricExpressionParams extends BaseMetricExpressionParams { - aggType: Aggregators.COUNT; - metric: never; -} - -export type MetricExpressionParams = NonCountMetricExpressionParams | CountMetricExpressionParams; From 17d5f47454253bc0b68ed1128f74d72ef4cc8f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Tue, 18 Jan 2022 18:54:55 +0000 Subject: [PATCH 3/7] Fix imports broken due to deleted intermediate files --- .../components/expression.test.tsx | 7 +++--- .../components/expression_row.test.tsx | 7 +++--- .../inventory_metric_threshold_executor.ts | 17 +++++++------- .../lib/create_percentile_aggregation.ts | 3 ++- .../lib/create_timerange.test.ts | 4 ++-- .../metric_threshold/lib/create_timerange.ts | 2 +- .../metric_threshold_executor.ts | 18 +++++++-------- .../register_metric_threshold_rule_type.ts | 22 +++++++++---------- 8 files changed, 39 insertions(+), 41 deletions(-) diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.test.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.test.tsx index f5df605316e245..94192f9f911c54 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.test.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.test.tsx @@ -6,14 +6,13 @@ */ import { mountWithIntl, nextTick } from '@kbn/test/jest'; +import React from 'react'; +import { act } from 'react-dom/test-utils'; // We are using this inside a `jest.mock` call. Jest requires dynamic dependencies to be prefixed with `mock` import { coreMock as mockCoreMock } from 'src/core/public/mocks'; +import { Comparator } from '../../../../common/alerting/metrics'; import { MetricsExplorerMetric } from '../../../../common/http_api/metrics_explorer'; -import React from 'react'; import { Expressions } from './expression'; -import { act } from 'react-dom/test-utils'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { Comparator } from '../../../../server/lib/alerting/metric_threshold/types'; jest.mock('../../../containers/metrics_source/use_source_via_http', () => ({ useSourceViaHttp: () => ({ diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.test.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.test.tsx index b4321dbfda3209..1d8c6f63398785 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.test.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression_row.test.tsx @@ -6,12 +6,11 @@ */ import { mountWithIntl, nextTick } from '@kbn/test/jest'; -import { MetricExpression } from '../types'; import React from 'react'; -import { ExpressionRow } from './expression_row'; import { act } from 'react-dom/test-utils'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { Comparator } from '../../../../server/lib/alerting/metric_threshold/types'; +import { Comparator } from '../../../../common/alerting/metrics'; +import { MetricExpression } from '../types'; +import { ExpressionRow } from './expression_row'; jest.mock('../../../containers/metrics_source/use_source_via_http', () => ({ useSourceViaHttp: () => ({ diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts index 9ffde130b93179..87bfe122f679d9 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/inventory_metric_threshold_executor.ts @@ -7,14 +7,11 @@ import { i18n } from '@kbn/i18n'; import { ALERT_REASON, ALERT_RULE_PARAMETERS } from '@kbn/rule-data-utils'; -import moment from 'moment'; import { first, get, last } from 'lodash'; -import { getCustomMetricLabel } from '../../../../common/formatters/get_custom_metric_label'; -import { toMetricOpt } from '../../../../common/snapshot_metric_i18n'; -import { AlertStates } from './types'; +import moment from 'moment'; import { - ActionGroupIdsOf, ActionGroup, + ActionGroupIdsOf, AlertInstanceContext as AlertContext, AlertInstanceState as AlertState, RecoveredActionGroup, @@ -23,18 +20,20 @@ import { AlertInstance as Alert, AlertTypeState as RuleTypeState, } from '../../../../../alerting/server'; +import { AlertStates, InventoryMetricThresholdParams } from '../../../../common/alerting/metrics'; +import { createFormatter } from '../../../../common/formatters'; +import { getCustomMetricLabel } from '../../../../common/formatters/get_custom_metric_label'; +import { METRIC_FORMATTERS } from '../../../../common/formatters/snapshot_metric_formats'; import { SnapshotMetricType } from '../../../../common/inventory_models/types'; +import { toMetricOpt } from '../../../../common/snapshot_metric_i18n'; import { InfraBackendLibs } from '../../infra_types'; -import { METRIC_FORMATTERS } from '../../../../common/formatters/snapshot_metric_formats'; -import { createFormatter } from '../../../../common/formatters'; -import { InventoryMetricThresholdParams } from '../../../../common/alerting/metrics'; import { buildErrorAlertReason, buildFiredAlertReason, + buildInvalidQueryAlertReason, buildNoDataAlertReason, // buildRecoveredAlertReason, stateToAlertMessage, - buildInvalidQueryAlertReason, } from '../common/messages'; import { evaluateCondition } from './evaluate_condition'; diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_percentile_aggregation.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_percentile_aggregation.ts index 92219e733da5d9..35111c1a69b2f1 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_percentile_aggregation.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_percentile_aggregation.ts @@ -5,7 +5,8 @@ * 2.0. */ -import { Aggregators } from '../types'; +import { Aggregators } from '../../../../../common/alerting/metrics'; + export const createPercentileAggregation = ( type: Aggregators.P95 | Aggregators.P99, field: string diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.test.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.test.ts index 5640a1d9284360..bf365d7e89bcac 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.test.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.test.ts @@ -5,9 +5,9 @@ * 2.0. */ -import { createTimerange } from './create_timerange'; -import { Aggregators } from '../types'; import moment from 'moment'; +import { Aggregators } from '../../../../../common/alerting/metrics'; +import { createTimerange } from './create_timerange'; describe('createTimerange(interval, aggType, timeframe)', () => { describe('without timeframe', () => { diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.ts index cca63aca14d097..03c407e8afe377 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/create_timerange.ts @@ -6,7 +6,7 @@ */ import moment from 'moment'; -import { Aggregators } from '../types'; +import { Aggregators } from '../../../../../common/alerting/metrics'; export const createTimerange = ( interval: number, diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts index 810055fc1771a6..371e75451c5792 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/metric_threshold_executor.ts @@ -5,33 +5,33 @@ * 2.0. */ -import { first, last, isEqual } from 'lodash'; import { i18n } from '@kbn/i18n'; -import moment from 'moment'; import { ALERT_REASON } from '@kbn/rule-data-utils'; +import { first, isEqual, last } from 'lodash'; +import moment from 'moment'; import { ActionGroupIdsOf, - RecoveredActionGroup, - AlertInstanceState as AlertState, AlertInstanceContext as AlertContext, + AlertInstanceState as AlertState, + RecoveredActionGroup, } from '../../../../../alerting/common'; import { - AlertTypeState as RuleTypeState, AlertInstance as Alert, + AlertTypeState as RuleTypeState, } from '../../../../../alerting/server'; +import { AlertStates, Comparator } from '../../../../common/alerting/metrics'; +import { createFormatter } from '../../../../common/formatters'; import { InfraBackendLibs } from '../../infra_types'; import { buildErrorAlertReason, buildFiredAlertReason, + buildInvalidQueryAlertReason, buildNoDataAlertReason, // buildRecoveredAlertReason, stateToAlertMessage, - buildInvalidQueryAlertReason, } from '../common/messages'; import { UNGROUPED_FACTORY_KEY } from '../common/utils'; -import { createFormatter } from '../../../../common/formatters'; -import { AlertStates, Comparator } from './types'; -import { evaluateRule, EvaluatedRuleParams } from './lib/evaluate_rule'; +import { EvaluatedRuleParams, evaluateRule } from './lib/evaluate_rule'; export type MetricThresholdRuleParams = Record; export type MetricThresholdRuleTypeState = RuleTypeState & { diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts index 6142e7083e0d24..d5d819ba7902e8 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/register_metric_threshold_rule_type.ts @@ -8,25 +8,25 @@ import { schema } from '@kbn/config-schema'; import { i18n } from '@kbn/i18n'; import { ActionGroupIdsOf } from '../../../../../alerting/common'; -import { RuleType, PluginSetupContract } from '../../../../../alerting/server'; +import { PluginSetupContract, RuleType } from '../../../../../alerting/server'; +import { Comparator, METRIC_THRESHOLD_ALERT_TYPE_ID } from '../../../../common/alerting/metrics'; import { METRIC_EXPLORER_AGGREGATIONS } from '../../../../common/http_api'; -import { - createMetricThresholdExecutor, - FIRED_ACTIONS, - WARNING_ACTIONS, -} from './metric_threshold_executor'; -import { METRIC_THRESHOLD_ALERT_TYPE_ID, Comparator } from './types'; import { InfraBackendLibs } from '../../infra_types'; -import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils'; import { - groupActionVariableDescription, alertStateActionVariableDescription, + groupActionVariableDescription, + metricActionVariableDescription, reasonActionVariableDescription, + thresholdActionVariableDescription, timestampActionVariableDescription, valueActionVariableDescription, - metricActionVariableDescription, - thresholdActionVariableDescription, } from '../common/messages'; +import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils'; +import { + createMetricThresholdExecutor, + FIRED_ACTIONS, + WARNING_ACTIONS, +} from './metric_threshold_executor'; type MetricThresholdAllowedActionGroups = ActionGroupIdsOf< typeof FIRED_ACTIONS | typeof WARNING_ACTIONS From a44734109a056a8fe3ea0c73b5a7eff8d9319021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Tue, 18 Jan 2022 19:10:16 +0000 Subject: [PATCH 4/7] Invert import direction to invert chunk dependency --- .../components/expression.tsx | 35 +++++++++---------- .../components/validation.tsx | 5 +-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx index f553d4b9dadf70..b03b90f6b028b6 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx @@ -6,43 +6,42 @@ */ import { Unit } from '@elastic/datemath'; -import React, { ChangeEvent, useCallback, useMemo, useEffect, useState } from 'react'; import { - EuiSpacer, - EuiText, - EuiFormRow, + EuiAccordion, EuiButtonEmpty, EuiCheckbox, - EuiToolTip, - EuiIcon, EuiFieldSearch, - EuiAccordion, - EuiPanel, + EuiFormRow, + EuiIcon, EuiLink, + EuiPanel, + EuiSpacer, + EuiText, + EuiToolTip, } from '@elastic/eui'; -import { FormattedMessage } from '@kbn/i18n-react'; import { i18n } from '@kbn/i18n'; +import { FormattedMessage } from '@kbn/i18n-react'; import { debounce } from 'lodash'; -import { Comparator, Aggregators } from '../../../../common/alerting/metrics'; -import { ForLastExpression } from '../../../../../triggers_actions_ui/public'; +import React, { ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react'; import { + ForLastExpression, IErrorObject, RuleTypeParams, RuleTypeParamsExpressionProps, } from '../../../../../triggers_actions_ui/public'; +import { Aggregators, Comparator } from '../../../../common/alerting/metrics'; +import { useSourceViaHttp } from '../../../containers/metrics_source/use_source_via_http'; +import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; +import { MetricsExplorerGroupBy } from '../../../pages/metrics/metrics_explorer/components/group_by'; import { MetricsExplorerKueryBar } from '../../../pages/metrics/metrics_explorer/components/kuery_bar'; import { MetricsExplorerOptions } from '../../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options'; -import { MetricsExplorerGroupBy } from '../../../pages/metrics/metrics_explorer/components/group_by'; -import { useSourceViaHttp } from '../../../containers/metrics_source/use_source_via_http'; import { convertKueryToElasticSearchQuery } from '../../../utils/kuery'; - -import { ExpressionRow } from './expression_row'; -import { MetricExpression, AlertParams, AlertContextMeta } from '../types'; +import { AlertContextMeta, AlertParams, MetricExpression } from '../types'; import { ExpressionChart } from './expression_chart'; -import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; +import { ExpressionRow } from './expression_row'; +import { QUERY_INVALID } from './validation'; const FILTER_TYPING_DEBOUNCE_MS = 500; -export const QUERY_INVALID = Symbol('QUERY_INVALID'); type Props = Omit< RuleTypeParamsExpressionProps, diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx index e7aaf950e82cbf..ff0f2fbb5a01db 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx @@ -8,14 +8,15 @@ import { i18n } from '@kbn/i18n'; import { ValidationResult } from '../../../../../triggers_actions_ui/public'; import { Comparator, MetricExpressionParams } from '../../../../common/alerting/metrics'; -import { QUERY_INVALID } from './expression'; + +export const QUERY_INVALID = Symbol('QUERY_INVALID'); export function validateMetricThreshold({ criteria, filterQuery, }: { criteria: MetricExpressionParams[]; - filterQuery?: string | symbol; + filterQuery?: string | typeof QUERY_INVALID; }): ValidationResult { const validationResult = { errors: {} }; const errors: { From e4615a48796c9637926ee86918ee444b4d50915d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Tue, 18 Jan 2022 19:33:54 +0000 Subject: [PATCH 5/7] Deduplicate invalid query symbol --- x-pack/plugins/infra/common/alerting/metrics/types.ts | 4 ++++ .../alerting/inventory/components/expression.tsx | 10 +++++++--- .../alerting/inventory/components/validation.tsx | 11 +++++++---- .../metric_threshold/components/expression.tsx | 3 +-- .../metric_threshold/components/validation.tsx | 11 +++++++---- .../infra/public/alerting/metric_threshold/types.ts | 4 ++-- 6 files changed, 28 insertions(+), 15 deletions(-) diff --git a/x-pack/plugins/infra/common/alerting/metrics/types.ts b/x-pack/plugins/infra/common/alerting/metrics/types.ts index 37bc14431e41ad..0216f63b8f85de 100644 --- a/x-pack/plugins/infra/common/alerting/metrics/types.ts +++ b/x-pack/plugins/infra/common/alerting/metrics/types.ts @@ -108,3 +108,7 @@ export interface CountMetricExpressionParams extends BaseMetricExpressionParams } export type MetricExpressionParams = NonCountMetricExpressionParams | CountMetricExpressionParams; + +export const QUERY_INVALID: unique symbol = Symbol('QUERY_INVALID'); + +export type FilterQuery = string | typeof QUERY_INVALID; diff --git a/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx index 58721f097dc525..f7d52a1aa95f0f 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/expression.tsx @@ -31,7 +31,12 @@ import { RuleTypeParamsExpressionProps, ThresholdExpression, } from '../../../../../triggers_actions_ui/public'; -import { Comparator, InventoryMetricConditions } from '../../../../common/alerting/metrics'; +import { + Comparator, + FilterQuery, + InventoryMetricConditions, + QUERY_INVALID, +} from '../../../../common/alerting/metrics'; import { SnapshotCustomMetricInput, SnapshotCustomMetricInputRT, @@ -59,7 +64,6 @@ import { convertKueryToElasticSearchQuery } from '../../../utils/kuery'; import { ExpressionChart } from './expression_chart'; import { MetricExpression } from './metric'; import { NodeTypeExpression } from './node_type'; -import { QUERY_INVALID } from './validation'; const FILTER_TYPING_DEBOUNCE_MS = 500; @@ -76,7 +80,7 @@ type Props = Omit< { criteria: Criteria; nodeType: InventoryItemType; - filterQuery?: string | symbol; + filterQuery?: FilterQuery; filterQueryText?: string; sourceId: string; alertOnNoData?: boolean; diff --git a/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx b/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx index 85782a7fa3dab3..e093ea789cf795 100644 --- a/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx +++ b/x-pack/plugins/infra/public/alerting/inventory/components/validation.tsx @@ -7,16 +7,19 @@ import { i18n } from '@kbn/i18n'; import type { ValidationResult } from '../../../../../triggers_actions_ui/public'; -import { Comparator, InventoryMetricConditions } from '../../../../common/alerting/metrics'; - -export const QUERY_INVALID = Symbol('QUERY_INVALID'); +import { + Comparator, + FilterQuery, + InventoryMetricConditions, + QUERY_INVALID, +} from '../../../../common/alerting/metrics'; export function validateMetricThreshold({ criteria, filterQuery, }: { criteria: InventoryMetricConditions[]; - filterQuery?: string | typeof QUERY_INVALID; + filterQuery?: FilterQuery; }): ValidationResult { const validationResult = { errors: {} }; const errors: { diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx index b03b90f6b028b6..e0039c4590069b 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.tsx @@ -29,7 +29,7 @@ import { RuleTypeParams, RuleTypeParamsExpressionProps, } from '../../../../../triggers_actions_ui/public'; -import { Aggregators, Comparator } from '../../../../common/alerting/metrics'; +import { Aggregators, Comparator, QUERY_INVALID } from '../../../../common/alerting/metrics'; import { useSourceViaHttp } from '../../../containers/metrics_source/use_source_via_http'; import { useKibanaContextForPlugin } from '../../../hooks/use_kibana'; import { MetricsExplorerGroupBy } from '../../../pages/metrics/metrics_explorer/components/group_by'; @@ -39,7 +39,6 @@ import { convertKueryToElasticSearchQuery } from '../../../utils/kuery'; import { AlertContextMeta, AlertParams, MetricExpression } from '../types'; import { ExpressionChart } from './expression_chart'; import { ExpressionRow } from './expression_row'; -import { QUERY_INVALID } from './validation'; const FILTER_TYPING_DEBOUNCE_MS = 500; diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx b/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx index ff0f2fbb5a01db..8634c3686087ff 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/components/validation.tsx @@ -7,16 +7,19 @@ import { i18n } from '@kbn/i18n'; import { ValidationResult } from '../../../../../triggers_actions_ui/public'; -import { Comparator, MetricExpressionParams } from '../../../../common/alerting/metrics'; - -export const QUERY_INVALID = Symbol('QUERY_INVALID'); +import { + Comparator, + FilterQuery, + MetricExpressionParams, + QUERY_INVALID, +} from '../../../../common/alerting/metrics'; export function validateMetricThreshold({ criteria, filterQuery, }: { criteria: MetricExpressionParams[]; - filterQuery?: string | typeof QUERY_INVALID; + filterQuery?: FilterQuery; }): ValidationResult { const validationResult = { errors: {} }; const errors: { diff --git a/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts b/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts index 4fae8bfacca353..a88dd1d4548b8d 100644 --- a/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts +++ b/x-pack/plugins/infra/public/alerting/metric_threshold/types.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { MetricExpressionParams } from '../../../common/alerting/metrics'; +import { FilterQuery, MetricExpressionParams } from '../../../common/alerting/metrics'; import { MetricsExplorerSeries } from '../../../common/http_api/metrics_explorer'; import { MetricsExplorerOptions } from '../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options'; @@ -54,7 +54,7 @@ export interface ExpressionChartData { export interface AlertParams { criteria: MetricExpression[]; groupBy?: string | string[]; - filterQuery?: string | symbol; + filterQuery?: FilterQuery; sourceId: string; filterQueryText?: string; alertOnNoData?: boolean; From cce4394c2032772bacdaaaf35cb16a47ec3a8b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Tue, 18 Jan 2022 20:47:18 +0000 Subject: [PATCH 6/7] Fix imports in api integration tests --- .../apis/metrics_ui/inventory_threshold_alert.ts | 6 +++--- .../apis/metrics_ui/metric_threshold_alert.ts | 16 ++++++++-------- .../apis/metrics_ui/metrics_alerting.ts | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/x-pack/test/api_integration/apis/metrics_ui/inventory_threshold_alert.ts b/x-pack/test/api_integration/apis/metrics_ui/inventory_threshold_alert.ts index a6e0ce1bc628f7..18fa4507eaf4f0 100644 --- a/x-pack/test/api_integration/apis/metrics_ui/inventory_threshold_alert.ts +++ b/x-pack/test/api_integration/apis/metrics_ui/inventory_threshold_alert.ts @@ -10,12 +10,12 @@ import { convertToKibanaClient } from '@kbn/test'; import { Comparator, InventoryMetricConditions, -} from '../../../../plugins/infra/server/lib/alerting/inventory_metric_threshold/types'; +} from '../../../../plugins/infra/common/alerting/metrics'; +import { InventoryItemType } from '../../../../plugins/infra/common/inventory_models/types'; +import { evaluateCondition } from '../../../../plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition'; import { InfraSource } from '../../../../plugins/infra/server/lib/sources'; import { FtrProviderContext } from '../../ftr_provider_context'; import { DATES } from './constants'; -import { evaluateCondition } from '../../../../plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition'; -import { InventoryItemType } from '../../../../plugins/infra/common/inventory_models/types'; export default function ({ getService }: FtrProviderContext) { const esArchiver = getService('esArchiver'); diff --git a/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts b/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts index ecefef2fe930c4..337d31a0a51b04 100644 --- a/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts +++ b/x-pack/test/api_integration/apis/metrics_ui/metric_threshold_alert.ts @@ -7,18 +7,18 @@ import expect from '@kbn/expect'; import { convertToKibanaClient } from '@kbn/test'; -import { InfraSource } from '../../../../plugins/infra/common/source_configuration/source_configuration'; -import { FtrProviderContext } from '../../ftr_provider_context'; -import { - evaluateRule, - EvaluatedRuleParams, -} from '../../../../plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule'; import { Aggregators, + Comparator, CountMetricExpressionParams, NonCountMetricExpressionParams, -} from '../../../../plugins/infra/server/lib/alerting/metric_threshold/types'; -import { Comparator } from '../../../../plugins/infra/server/lib/alerting/common/types'; +} from '../../../../plugins/infra/common/alerting/metrics'; +import { InfraSource } from '../../../../plugins/infra/common/source_configuration/source_configuration'; +import { + EvaluatedRuleParams, + evaluateRule, +} from '../../../../plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_rule'; +import { FtrProviderContext } from '../../ftr_provider_context'; import { DATES } from './constants'; const { gauge, rate } = DATES['alert-test-data']; diff --git a/x-pack/test/api_integration/apis/metrics_ui/metrics_alerting.ts b/x-pack/test/api_integration/apis/metrics_ui/metrics_alerting.ts index eb8888a613dc3c..d441c59b24a20f 100644 --- a/x-pack/test/api_integration/apis/metrics_ui/metrics_alerting.ts +++ b/x-pack/test/api_integration/apis/metrics_ui/metrics_alerting.ts @@ -7,10 +7,10 @@ import expect from '@kbn/expect'; import moment from 'moment'; +import { MetricExpressionParams } from '../../../../plugins/infra/common/alerting/metrics'; import { getElasticsearchMetricQuery } from '../../../../plugins/infra/server/lib/alerting/metric_threshold/lib/metric_query'; -import { MetricExpressionParams } from '../../../../plugins/infra/server/lib/alerting/metric_threshold/types'; - import { FtrProviderContext } from '../../ftr_provider_context'; + export default function ({ getService }: FtrProviderContext) { const client = getService('es'); const index = 'test-index'; From 7d5bb90330c97cf9c6e0f10f1ae8e14c0db11994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Tue, 25 Jan 2022 10:51:07 +0000 Subject: [PATCH 7/7] Fix newly added import --- .../lib/calculate_from_based_on_metric.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/lib/calculate_from_based_on_metric.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/lib/calculate_from_based_on_metric.ts index 7c6031dffd57dd..5adaa441309290 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/lib/calculate_from_based_on_metric.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/lib/calculate_from_based_on_metric.ts @@ -6,14 +6,14 @@ */ import { Moment } from 'moment'; +import { InventoryMetricConditions } from '../../../../../common/alerting/metrics'; import { SnapshotCustomMetricInput } from '../../../../../common/http_api'; +import { findInventoryModel } from '../../../../../common/inventory_models'; import { InventoryItemType, SnapshotMetricType, } from '../../../../../common/inventory_models/types'; -import { InventoryMetricConditions } from '../types'; import { isRate } from './is_rate'; -import { findInventoryModel } from '../../../../../common/inventory_models'; export const calcualteFromBasedOnMetric = ( to: Moment,