Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Infra UI] Avoid eager async imports in metric alert registrations #123285

Merged
merged 11 commits into from
Jan 26, 2022
40 changes: 36 additions & 4 deletions x-pack/plugins/infra/common/alerting/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -37,6 +35,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'),
Expand Down Expand Up @@ -80,3 +86,29 @@ 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<Aggregators, Aggregators.COUNT>;
metric: string;
}

export interface CountMetricExpressionParams extends BaseMetricExpressionParams {
aggType: Aggregators.COUNT;
metric: never;
}

export type MetricExpressionParams = NonCountMetricExpressionParams | CountMetricExpressionParams;

export const QUERY_INVALID: unique symbol = Symbol('QUERY_INVALID');

export type FilterQuery = string | typeof QUERY_INVALID;
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,66 @@
*/

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,
FilterQuery,
InventoryMetricConditions,
QUERY_INVALID,
} 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';

const FILTER_TYPING_DEBOUNCE_MS = 500;
export const QUERY_INVALID = Symbol('QUERY_INVALID');

export interface AlertContextMeta {
options?: Partial<InfraWaffleMapOptions>;
Expand All @@ -85,7 +80,7 @@ type Props = Omit<
{
criteria: Criteria;
nodeType: InventoryItemType;
filterQuery?: string | symbol;
filterQuery?: FilterQuery;
filterQueryText?: string;
sourceId: string;
alertOnNoData?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
*/

import { i18n } from '@kbn/i18n';
import type { ValidationResult } from '../../../../../triggers_actions_ui/public';
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';
FilterQuery,
InventoryMetricConditions,
QUERY_INVALID,
} from '../../../../common/alerting/metrics';

export function validateMetricThreshold({
criteria,
filterQuery,
}: {
criteria: InventoryMetricConditions[];
filterQuery?: string | symbol;
filterQuery?: FilterQuery;
}): ValidationResult {
const validationResult = { errors: {} };
const errors: {
Expand Down
9 changes: 3 additions & 6 deletions x-pack/plugins/infra/public/alerting/inventory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
Loading