Skip to content

Commit

Permalink
Showing alerts as data in table with reason formatter for index thres…
Browse files Browse the repository at this point in the history
…hold
  • Loading branch information
ymao1 committed Apr 21, 2021
1 parent 26da715 commit 118c4a4
Show file tree
Hide file tree
Showing 22 changed files with 566 additions and 180 deletions.
3 changes: 2 additions & 1 deletion x-pack/plugins/stack_alerts/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* 2.0.
*/

export * from './config';
export * from './render_rule_params';
export { ComparatorFns, ComparatorFnNames, getHumanReadableComparator } from './comparator_types';
export const STACK_ALERTS_FEATURE_ID = 'stackAlerts';
18 changes: 18 additions & 0 deletions x-pack/plugins/stack_alerts/common/render_rule_params.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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getHumanReadableComparator } from './comparator_types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function renderIndexThresholdParams(params: any) {
const agg = params.aggField ? `${params.aggType}(${params.aggField})` : `${params.aggType}`;
const conditions = `${agg} is ${getHumanReadableComparator(
params.thresholdComparator
)} ${params.threshold.join(' and ')}`;
const window = `${params.timeWindowSize}${params.timeWindowUnit}`;
return `${conditions} over ${window}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n';
import { validateExpression } from './validation';
import { IndexThresholdAlertParams } from './types';
import { AlertTypeModel } from '../../../../triggers_actions_ui/public';
import { renderIndexThresholdParams } from '../../../common';

export function getAlertType(): AlertTypeModel<IndexThresholdAlertParams> {
return {
Expand All @@ -21,6 +22,7 @@ export function getAlertType(): AlertTypeModel<IndexThresholdAlertParams> {
documentationUrl: (docLinks) => docLinks.links.alerting.indexThreshold,
alertParamsExpression: lazy(() => import('./expression')),
validate: validateExpression,
formatter: renderIndexThresholdParams,
defaultActionMessage: i18n.translate(
'xpack.stackAlerts.threshold.ui.alertType.defaultActionMessage',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';
import { schema, TypeOf } from '@kbn/config-schema';
import { ComparatorFnNames } from '../lib';
import { ComparatorFnNames } from '../../../common';
import { validateTimeWindowUnits } from '../../../../triggers_actions_ui/server';
import { AlertTypeState } from '../../../../alerting/server';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import { i18n } from '@kbn/i18n';
import type { estypes } from '@elastic/elasticsearch';
import { RegisterAlertTypesParams } from '..';
import { createThresholdRuleType } from '../../types';
import { STACK_ALERTS_FEATURE_ID } from '../../../common';
import { ComparatorFns, getHumanReadableComparator } from '../lib';
import {
STACK_ALERTS_FEATURE_ID,
ComparatorFns,
getHumanReadableComparator,
} from '../../../common';
import * as EsQuery from './alert_type';
import {
getSearchParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';
import { Params } from './alert_type_params';
import { AlertInstanceContext } from '../../../../alerting/server';
import { renderIndexThresholdParams } from '../../../common';

// alert type context provided to actions

Expand All @@ -26,8 +27,6 @@ export interface BaseActionContext extends AlertInstanceContext {
date: string;
// the value that met the threshold
value: number;
// threshold conditions
conditions: string;
}

export function addMessages(
Expand All @@ -43,21 +42,19 @@ export function addMessages(
},
});

const window = `${params.timeWindowSize}${params.timeWindowUnit}`;
const message = i18n.translate(
'xpack.stackAlerts.indexThreshold.alertTypeContextMessageDescription',
{
defaultMessage: `alert '{name}' is active for group '{group}':
- Value: {value}
- Conditions Met: {conditions} over {window}
- Conditions Met: {conditionsMet}
- Timestamp: {date}`,
values: {
name: alertName,
group: baseContext.group,
value: baseContext.value,
conditions: baseContext.conditions,
window,
conditionsMet: renderIndexThresholdParams(params),
date: baseContext.date,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { i18n } from '@kbn/i18n';
import { schema, TypeOf } from '@kbn/config-schema';
import { ComparatorFnNames } from '../lib';
import { ComparatorFnNames } from '../../../common';
import {
CoreQueryParamsSchemaProperties,
validateCoreQueryBody,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { i18n } from '@kbn/i18n';
import { RegisterAlertTypesParams } from '..';
import { createThresholdRuleType } from '../../types';
import * as IndexThreshold from './alert_type';
import { STACK_ALERTS_FEATURE_ID } from '../../../common';
import { STACK_ALERTS_FEATURE_ID, ComparatorFns } from '../../../common';
import { ParamsSchema } from './alert_type_params';
import { ComparatorFns, getHumanReadableComparator } from '../lib';
import { TimeSeriesQuery } from '../../../../triggers_actions_ui/server';
import { BaseActionContext, addMessages } from './action_context';

Expand Down Expand Up @@ -121,18 +120,10 @@ export function register(registerParams: RegisterAlertTypesParams) {

if (!met) continue;

const agg = params.aggField
? `${params.aggType}(${params.aggField})`
: `${params.aggType}`;
const humanFn = `${agg} is ${getHumanReadableComparator(
params.thresholdComparator
)} ${params.threshold.join(' and ')}`;

const baseContext: BaseActionContext = {
date,
group: instanceId,
value,
conditions: humanFn,
};
const actionContext = addMessages(rule.name, baseContext, params);
writeRuleAlert({
Expand Down
8 changes: 0 additions & 8 deletions x-pack/plugins/stack_alerts/server/alert_types/lib/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion x-pack/plugins/stack_alerts/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { PluginConfigDescriptor, PluginInitializerContext } from 'src/core/server';
import { AlertingBuiltinsPlugin } from './plugin';
import { configSchema, Config } from '../common/config';
import { configSchema, Config } from './config';
export { ID as INDEX_THRESHOLD_ID } from './alert_types/index_threshold/alert_type';

export const config: PluginConfigDescriptor<Config> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ const TriggersActionsUIHome = lazy(async () => import('./home'));
const AlertDetailsRoute = lazy(
() => import('./sections/alert_details/components/alert_details_route')
);
const AlertDataRoute = lazy(() => import('./sections/alert_details/components/alert_data_route'));

export interface TriggersAndActionsUiServices extends CoreStart {
data: DataPublicPluginStart;
charts: ChartsPluginStart;
Expand Down Expand Up @@ -94,7 +92,7 @@ export const AppWithoutRouter = ({ sectionsRegex }: { sectionsRegex: string }) =
<Route
exact
path={routeToAlertData}
component={suspendedComponentWithProps(AlertDataRoute, 'xl')}
component={suspendedComponentWithProps(AlertDetailsRoute, 'xl')}
/>
<Route
exact
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 { HttpSetup } from 'kibana/public';

export interface AlertData {
'rule.id': string;
'kibana.rac.alert.value': number;
'rule.name': string;
'kibana.rac.alert.duration.us': number;
'kibana.rac.alert.end': string;
'kibana.rac.alert.threshold': number;
'kibana.rac.alert.status': string;
'kibana.rac.alert.uuid': string;
'rule.uuid': string;
'event.action': string;
'@timestamp': string;
'kibana.rac.alert.id': string;
'kibana.rac.alert.start': string;
'kibana.rac.producer': string;
'event.kind': string;
'rule.category': string;
}

export async function loadAlertData({
http,
alertId,
}: {
http: HttpSetup;
alertId: string;
}): Promise<AlertData[]> {
return await http.get(`/internal/stack_alerts/rule/${alertId}/_alert_data`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { disableAlert, disableAlerts } from './disable';
export { enableAlert, enableAlerts } from './enable';
export { loadAlert } from './get_rule';
export { loadAlertInstanceSummary } from './alert_summary';
export { AlertData, loadAlertData } from './alert_data';
export { muteAlertInstance } from './mute_alert';
export { muteAlert, muteAlerts } from './mute';
export { loadAlertTypes } from './rule_types';
Expand Down

This file was deleted.

Loading

0 comments on commit 118c4a4

Please sign in to comment.