Skip to content

Commit

Permalink
[alerting] replace internal legacy API calls with new APIs (#121048)
Browse files Browse the repository at this point in the history
resolves #116939

Removes the remaining calls to the legacy HTTP alerting endpoints by internal Kibana code.
  • Loading branch information
pmuellr authored Dec 16, 2021
1 parent 825e35d commit b4c44a1
Show file tree
Hide file tree
Showing 11 changed files with 699 additions and 116 deletions.
25 changes: 25 additions & 0 deletions x-pack/examples/alerting_example/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/

// We don't have types defined for the response objects from our HTTP APIs,
// so defining what we need here.

export interface Rule<T = never> {
id: string;
name: string;
rule_type_id: string;
schedule: {
interval: string;
};
params: T;
}

export interface RuleTaskState {
alerts: Array<{
state: Record<string, never>;
}>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function registerNavigation(alerting: AlertingSetup) {
// register default navigation
alerting.registerDefaultNavigation(
ALERTING_EXAMPLE_APP_ID,
(alert: SanitizedAlert) => `/alert/${alert.id}`
(alert: SanitizedAlert) => `/rule/${alert.id}`
);

registerPeopleInSpaceNavigation(alerting);
Expand Down
19 changes: 10 additions & 9 deletions x-pack/examples/alerting_example/public/components/view_alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ import { CoreStart } from 'kibana/public';
import { isEmpty } from 'lodash';
import { ALERTING_EXAMPLE_APP_ID } from '../../common/constants';
import {
Alert,
RuleTaskState,
LEGACY_BASE_ALERT_API_PATH,
BASE_ALERTING_API_PATH,
INTERNAL_BASE_ALERTING_API_PATH,
} from '../../../../plugins/alerting/common';
import { Rule, RuleTaskState } from '../../common/types';

type Props = RouteComponentProps & {
http: CoreStart['http'];
id: string;
};

export const ViewAlertPage = withRouter(({ http, id }: Props) => {
const [alert, setAlert] = useState<Alert | null>(null);
const [alert, setAlert] = useState<Rule | null>(null);
const [alertState, setAlertState] = useState<RuleTaskState | null>(null);

useEffect(() => {
if (!alert) {
http.get<Alert | null>(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}`).then(setAlert);
http.get<Rule | null>(`${BASE_ALERTING_API_PATH}/rule/${id}`).then(setAlert);
}
if (!alertState) {
http
.get<RuleTaskState | null>(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`)
.get<RuleTaskState | null>(`${INTERNAL_BASE_ALERTING_API_PATH}/rule/${id}/state`)
.then(setAlertState);
}
}, [alert, alertState, http, id]);
Expand All @@ -60,7 +61,7 @@ export const ViewAlertPage = withRouter(({ http, id }: Props) => {
Rule, whose ID is <EuiTextColor color="accent">{`${alert.id}`}</EuiTextColor>.
</p>
<p>
Its RuleType is <EuiTextColor color="accent">{`${alert.alertTypeId}`}</EuiTextColor> and
Its RuleType is <EuiTextColor color="accent">{`${alert.rule_type_id}`}</EuiTextColor> and
its scheduled to run at an interval of
<EuiTextColor color="accent"> {`${alert.schedule.interval}`}</EuiTextColor>.
</p>
Expand All @@ -69,7 +70,7 @@ export const ViewAlertPage = withRouter(({ http, id }: Props) => {
<EuiText>
<h2>Alerts</h2>
</EuiText>
{isEmpty(alertState.alertInstances) ? (
{isEmpty(alertState.alerts) ? (
<EuiCallOut title="No Alerts!" color="warning" iconType="help">
<p>This Rule doesn&apos;t have any active alerts at the moment.</p>
</EuiCallOut>
Expand All @@ -84,7 +85,7 @@ export const ViewAlertPage = withRouter(({ http, id }: Props) => {
</EuiCallOut>
<EuiSpacer size="l" />
<EuiDescriptionList compressed>
{Object.entries(alertState.alertInstances ?? {}).map(([instance, { state }]) => (
{Object.entries(alertState.alerts ?? {}).map(([instance, { state }]) => (
<Fragment>
<EuiDescriptionListTitle>{instance}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import { CoreStart } from 'kibana/public';
import { isEmpty } from 'lodash';
import { ALERTING_EXAMPLE_APP_ID, AlwaysFiringParams } from '../../common/constants';
import {
Alert,
RuleTaskState,
LEGACY_BASE_ALERT_API_PATH,
BASE_ALERTING_API_PATH,
INTERNAL_BASE_ALERTING_API_PATH,
} from '../../../../plugins/alerting/common';
import { Rule, RuleTaskState } from '../../common/types';

type Props = RouteComponentProps & {
http: CoreStart['http'];
Expand All @@ -39,18 +39,18 @@ function hasCraft(state: any): state is { craft: string } {
return state && state.craft;
}
export const ViewPeopleInSpaceAlertPage = withRouter(({ http, id }: Props) => {
const [alert, setAlert] = useState<Alert<AlwaysFiringParams> | null>(null);
const [alert, setAlert] = useState<Rule<AlwaysFiringParams> | null>(null);
const [alertState, setAlertState] = useState<RuleTaskState | null>(null);

useEffect(() => {
if (!alert) {
http
.get<Alert<AlwaysFiringParams> | null>(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}`)
.get<Rule<AlwaysFiringParams> | null>(`${BASE_ALERTING_API_PATH}/rule/${id}`)
.then(setAlert);
}
if (!alertState) {
http
.get<RuleTaskState | null>(`${LEGACY_BASE_ALERT_API_PATH}/alert/${id}/state`)
.get<RuleTaskState | null>(`${INTERNAL_BASE_ALERTING_API_PATH}/rule/${id}/state`)
.then(setAlertState);
}
}, [alert, alertState, http, id]);
Expand All @@ -69,7 +69,7 @@ export const ViewPeopleInSpaceAlertPage = withRouter(({ http, id }: Props) => {
<EuiText>
<h2>Alerts</h2>
</EuiText>
{isEmpty(alertState.alertInstances) ? (
{isEmpty(alertState.alerts) ? (
<EuiCallOut title="No Alerts!" color="warning" iconType="help">
<p>
The people in {alert.params.craft} at the moment <b>are not</b> {alert.params.op}{' '}
Expand All @@ -89,23 +89,21 @@ export const ViewPeopleInSpaceAlertPage = withRouter(({ http, id }: Props) => {
<EuiFlexGroup>
<EuiFlexItem grow={false}>
<EuiStat
title={Object.keys(alertState.alertInstances ?? {}).length}
title={Object.keys(alertState.alerts ?? {}).length}
description={`People in ${alert.params.craft}`}
titleColor="primary"
/>
</EuiFlexItem>
<EuiFlexItem>
<EuiDescriptionList compressed>
{Object.entries(alertState.alertInstances ?? {}).map(
([instance, { state }], index) => (
<Fragment key={index}>
<EuiDescriptionListTitle>{instance}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
{hasCraft(state) ? state.craft : 'Unknown Craft'}
</EuiDescriptionListDescription>
</Fragment>
)
)}
{Object.entries(alertState.alerts ?? {}).map(([instance, { state }], index) => (
<Fragment key={index}>
<EuiDescriptionListTitle>{instance}</EuiDescriptionListTitle>
<EuiDescriptionListDescription>
{hasCraft(state) ? state.craft : 'Unknown Craft'}
</EuiDescriptionListDescription>
</Fragment>
))}
</EuiDescriptionList>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
17 changes: 16 additions & 1 deletion x-pack/plugins/alerting/common/rule_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
import { LicenseType } from '../../licensing/common/types';
import { RecoveredActionGroupId, DefaultActionGroupId } from './builtin_action_groups';

interface ConsumerPrivileges {
read: boolean;
all: boolean;
}

interface ActionVariable {
name: string;
description: string;
}
export interface RuleType<
ActionGroupIds extends Exclude<string, RecoveredActionGroupId> = DefaultActionGroupId,
RecoveryActionGroupId extends string = RecoveredActionGroupId
Expand All @@ -16,14 +25,20 @@ export interface RuleType<
name: string;
actionGroups: Array<ActionGroup<ActionGroupIds>>;
recoveryActionGroup: ActionGroup<RecoveryActionGroupId>;
actionVariables: string[];
actionVariables: {
context: ActionVariable[];
state: ActionVariable[];
params: ActionVariable[];
};
defaultActionGroupId: ActionGroupIds;
producer: string;
minimumLicenseRequired: LicenseType;
isExportable: boolean;
ruleTaskTimeout?: string;
defaultScheduleInterval?: string;
minimumScheduleInterval?: string;
enabledInLicense: boolean;
authorizedConsumers: Record<string, ConsumerPrivileges>;
}

export interface ActionGroup<ActionGroupIds extends string> {
Expand Down
Loading

0 comments on commit b4c44a1

Please sign in to comment.