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

[Metrics UI] Prefill alerts from the global dropdown #68967

Merged
merged 20 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { useState } from 'react';
import { SnapshotMetricInput } from '../../../../common/http_api/snapshot_api';
import { InventoryItemType } from '../../../../common/inventory_models/types';

export const useInventoryAlertPrefill = () => {
const [nodeType, setNodeType] = useState<InventoryItemType>('host');
const [filterQuery, setFilterQuery] = useState<string | undefined>();
const [metric, setMetric] = useState<SnapshotMetricInput>({ type: 'cpu' });

return {
nodeType,
filterQuery,
metric,
setNodeType,
setFilterQuery,
setMetric,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
import React, { useState, useCallback, useMemo } from 'react';
import { EuiPopover, EuiButtonEmpty, EuiContextMenuItem, EuiContextMenuPanel } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { AlertFlyout } from './alert_flyout';
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';
import { useAlertPrefillContext } from '../../use_alert_prefill';
import { AlertFlyout } from './alert_flyout';

export const MetricsAlertDropdown = () => {
const [popoverOpen, setPopoverOpen] = useState(false);
const [flyoutVisible, setFlyoutVisible] = useState(false);
const kibana = useKibana();

const { metricThresholdPrefill } = useAlertPrefillContext();
const { groupBy, filterQuery, metrics } = metricThresholdPrefill;

const closePopover = useCallback(() => {
setPopoverOpen(false);
}, [setPopoverOpen]);
Expand Down Expand Up @@ -57,7 +61,11 @@ export const MetricsAlertDropdown = () => {
>
<EuiContextMenuPanel items={menuItems} />
</EuiPopover>
<AlertFlyout setVisible={setFlyoutVisible} visible={flyoutVisible} />
<AlertFlyout
setVisible={setFlyoutVisible}
visible={flyoutVisible}
options={{ groupBy, filterQuery, metrics }}
/>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,15 @@ import { useSourceViaHttp } from '../../../containers/source/use_source_via_http
import { convertKueryToElasticSearchQuery } from '../../../utils/kuery';

import { ExpressionRow } from './expression_row';
import { AlertContextMeta, TimeUnit, MetricExpression } from '../types';
import { AlertContextMeta, TimeUnit, MetricExpression, AlertParams } from '../types';
import { ExpressionChart } from './expression_chart';
import { validateMetricThreshold } from './validation';

const FILTER_TYPING_DEBOUNCE_MS = 500;

interface Props {
errors: IErrorObject[];
alertParams: {
criteria: MetricExpression[];
groupBy?: string;
filterQuery?: string;
sourceId?: string;
filterQueryText?: string;
alertOnNoData?: boolean;
};
alertParams: AlertParams;
alertsContext: AlertsContextValue<AlertContextMeta>;
alertInterval: string;
setAlertParams(key: string, value: any): void;
Expand Down Expand Up @@ -260,6 +253,13 @@ export const Expressions: React.FC<Props> = (props) => {
}
}, [alertsContext.metadata, derivedIndexPattern, setAlertParams]);

const preFillAlertGroupBy = useCallback(() => {
const md = alertsContext.metadata;
if (md && md.currentOptions?.groupBy && !md.series) {
setAlertParams('groupBy', md.currentOptions.groupBy);
}
}, [alertsContext.metadata, setAlertParams]);

Zacqary marked this conversation as resolved.
Show resolved Hide resolved
const onSelectPreviewLookbackInterval = useCallback((e) => {
setPreviewLookbackInterval(e.target.value);
setPreviewResult(null);
Expand Down Expand Up @@ -299,6 +299,10 @@ export const Expressions: React.FC<Props> = (props) => {
preFillAlertFilter();
}

if (!alertParams.groupBy) {
preFillAlertGroupBy();
}

if (!alertParams.sourceId) {
setAlertParams('sourceId', source?.id || 'default');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { useState } from 'react';
import { MetricsExplorerMetric } from '../../../../common/http_api/metrics_explorer';

export const useMetricThresholdAlertPrefill = () => {
const [groupBy, setGroupBy] = useState<string | string[] | undefined>();
const [filterQuery, setFilterQuery] = useState<string | undefined>();
const [metrics, setMetrics] = useState<MetricsExplorerMetric[]>([]);

return {
groupBy,
filterQuery,
metrics,
setGroupBy,
setFilterQuery,
setMetrics,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ export interface ExpressionChartData {
id: string;
series: ExpressionChartSeries;
}

export interface AlertParams {
criteria: MetricExpression[];
groupBy?: string;
filterQuery?: string;
sourceId?: string;
filterQueryText?: string;
alertOnNoData?: boolean;
}
18 changes: 18 additions & 0 deletions x-pack/plugins/infra/public/alerting/use_alert_prefill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import createContainer from 'constate';
import { useMetricThresholdAlertPrefill } from './metric_threshold/hooks/use_metric_threshold_alert_prefill';
import { useInventoryAlertPrefill } from './inventory/hooks/use_inventory_alert_prefill';

const useAlertPrefill = () => {
const metricThresholdPrefill = useMetricThresholdAlertPrefill();
const inventoryPrefill = useInventoryAlertPrefill();

return { metricThresholdPrefill, inventoryPrefill };
};

export const [AlertPrefillProvider, useAlertPrefillContext] = createContainer(useAlertPrefill);
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import React, { useState, useCallback, useMemo } from 'react';
import { EuiPopover, EuiButtonEmpty, EuiContextMenuItem, EuiContextMenuPanel } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import { useAlertPrefillContext } from '../../../alerting/use_alert_prefill';
import { AlertFlyout } from './alert_flyout';
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';

Expand All @@ -15,6 +16,9 @@ export const InventoryAlertDropdown = () => {
const [flyoutVisible, setFlyoutVisible] = useState(false);
const kibana = useKibana();

const { inventoryPrefill } = useAlertPrefillContext();
const { nodeType, metric, filterQuery } = inventoryPrefill;

const closePopover = useCallback(() => {
setPopoverOpen(false);
}, [setPopoverOpen]);
Expand Down Expand Up @@ -57,7 +61,13 @@ export const InventoryAlertDropdown = () => {
>
<EuiContextMenuPanel items={menuItems} />
</EuiPopover>
<AlertFlyout setVisible={setFlyoutVisible} visible={flyoutVisible} />
<AlertFlyout
setVisible={setFlyoutVisible}
visible={flyoutVisible}
nodeType={nodeType}
options={{ metric }}
filter={filterQuery}
/>
</>
);
};
Loading