Skip to content

Commit

Permalink
Revert "[Alerting] Enforces typing of Alert's ActionGroups (#86761)"
Browse files Browse the repository at this point in the history
This reverts commit 76b8c49.
  • Loading branch information
jbudz committed Jan 5, 2021
1 parent 7ed089c commit 051be28
Show file tree
Hide file tree
Showing 69 changed files with 328 additions and 1,006 deletions.
13 changes: 6 additions & 7 deletions x-pack/examples/alerting_example/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ export const ALERTING_EXAMPLE_APP_ID = 'AlertingExample';

// always firing
export const DEFAULT_INSTANCES_TO_GENERATE = 5;
export interface AlwaysFiringThresholds {
small?: number;
medium?: number;
large?: number;
}
export interface AlwaysFiringParams extends AlertTypeParams {
instances?: number;
thresholds?: AlwaysFiringThresholds;
thresholds?: {
small?: number;
medium?: number;
large?: number;
};
}
export type AlwaysFiringActionGroupIds = keyof AlwaysFiringThresholds;
export type AlwaysFiringActionGroupIds = keyof AlwaysFiringParams['thresholds'];

// Astros
export enum Craft {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,8 @@ export const AlwaysFiringExpression: React.FunctionComponent<
};

interface TShirtSelectorProps {
actionGroup?: ActionGroupWithCondition<number, AlwaysFiringActionGroupIds>;
setTShirtThreshold: (
actionGroup: ActionGroupWithCondition<number, AlwaysFiringActionGroupIds>
) => void;
actionGroup?: ActionGroupWithCondition<number>;
setTShirtThreshold: (actionGroup: ActionGroupWithCondition<number>) => void;
}
const TShirtSelector = ({ actionGroup, setTShirtThreshold }: TShirtSelectorProps) => {
const [isOpen, setIsOpen] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
DEFAULT_INSTANCES_TO_GENERATE,
ALERTING_EXAMPLE_APP_ID,
AlwaysFiringParams,
AlwaysFiringActionGroupIds,
} from '../../common/constants';

type ActionGroups = 'small' | 'medium' | 'large';
Expand Down Expand Up @@ -40,8 +39,7 @@ export const alertType: AlertType<
AlwaysFiringParams,
{ count?: number },
{ triggerdOnCycle: number },
never,
AlwaysFiringActionGroupIds
never
> = {
id: 'example.always-firing',
name: 'Always firing',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ function getCraftFilter(craft: string) {
export const alertType: AlertType<
{ outerSpaceCapacity: number; craft: string; op: string },
{ peopleInSpace: number },
{ craft: string },
never,
'default',
'hasLandedBackOnEarth'
{ craft: string }
> = {
id: 'example.people-in-space',
name: 'People In Space Right Now',
Expand Down
41 changes: 3 additions & 38 deletions x-pack/plugins/alerts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,41 +142,8 @@ This example receives server and threshold as parameters. It will read the CPU u

```typescript
import { schema } from '@kbn/config-schema';
import {
Alert,
AlertTypeParams,
AlertTypeState,
AlertInstanceState,
AlertInstanceContext
} from 'x-pack/plugins/alerts/common';
...
interface MyAlertTypeParams extends AlertTypeParams {
server: string;
threshold: number;
}

interface MyAlertTypeState extends AlertTypeState {
lastChecked: number;
}

interface MyAlertTypeInstanceState extends AlertInstanceState {
cpuUsage: number;
}

interface MyAlertTypeInstanceContext extends AlertInstanceContext {
server: string;
hasCpuUsageIncreased: boolean;
}

type MyAlertTypeActionGroups = 'default' | 'warning';

const myAlertType: AlertType<
MyAlertTypeParams,
MyAlertTypeState,
MyAlertTypeInstanceState,
MyAlertTypeInstanceContext,
MyAlertTypeActionGroups
> = {
server.newPlatform.setup.plugins.alerts.registerType({
id: 'my-alert-type',
name: 'My alert type',
validate: {
Expand Down Expand Up @@ -213,7 +180,7 @@ const myAlertType: AlertType<
services,
params,
state,
}: AlertExecutorOptions<MyAlertTypeParams, MyAlertTypeState, MyAlertTypeInstanceState, MyAlertTypeInstanceContext, MyAlertTypeActionGroups>) {
}: AlertExecutorOptions) {
// Let's assume params is { server: 'server_1', threshold: 0.8 }
const { server, threshold } = params;

Expand Down Expand Up @@ -252,9 +219,7 @@ const myAlertType: AlertType<
};
},
producer: 'alerting',
};

server.newPlatform.setup.plugins.alerts.registerType(myAlertType);
});
```

This example only receives threshold as a parameter. It will read the CPU usage of all the servers and schedule individual actions if the reading for a server is greater than the threshold. This is a better implementation than above as only one query is performed for all the servers instead of one query per server.
Expand Down
22 changes: 6 additions & 16 deletions x-pack/plugins/alerts/common/alert_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,19 @@
*/

import { LicenseType } from '../../licensing/common/types';
import { RecoveredActionGroupId, DefaultActionGroupId } from './builtin_action_groups';

export interface AlertType<
ActionGroupIds extends Exclude<string, RecoveredActionGroupId> = DefaultActionGroupId,
RecoveryActionGroupId extends string = RecoveredActionGroupId
> {
export interface AlertType {
id: string;
name: string;
actionGroups: Array<ActionGroup<ActionGroupIds>>;
recoveryActionGroup: ActionGroup<RecoveryActionGroupId>;
actionGroups: ActionGroup[];
recoveryActionGroup: ActionGroup;
actionVariables: string[];
defaultActionGroupId: ActionGroupIds;
defaultActionGroupId: ActionGroup['id'];
producer: string;
minimumLicenseRequired: LicenseType;
}

export interface ActionGroup<ActionGroupIds extends string> {
id: ActionGroupIds;
export interface ActionGroup {
id: string;
name: string;
}

export type ActionGroupIdsOf<T> = T extends ActionGroup<infer groups>
? groups
: T extends Readonly<ActionGroup<infer groups>>
? groups
: never;
22 changes: 4 additions & 18 deletions x-pack/plugins/alerts/common/builtin_action_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,13 @@
import { i18n } from '@kbn/i18n';
import { ActionGroup } from './alert_type';

export type DefaultActionGroupId = 'default';

export type RecoveredActionGroupId = typeof RecoveredActionGroup['id'];
export const RecoveredActionGroup: Readonly<ActionGroup<'recovered'>> = Object.freeze({
export const RecoveredActionGroup: Readonly<ActionGroup> = {
id: 'recovered',
name: i18n.translate('xpack.alerts.builtinActionGroups.recovered', {
defaultMessage: 'Recovered',
}),
});

export type ReservedActionGroups<RecoveryActionGroupId extends string> =
| RecoveryActionGroupId
| RecoveredActionGroupId;

export type WithoutReservedActionGroups<
ActionGroupIds extends string,
RecoveryActionGroupId extends string
> = ActionGroupIds extends ReservedActionGroups<RecoveryActionGroupId> ? never : ActionGroupIds;
};

export function getBuiltinActionGroups<RecoveryActionGroupId extends string>(
customRecoveryGroup?: ActionGroup<RecoveryActionGroupId>
): [ActionGroup<ReservedActionGroups<RecoveryActionGroupId>>] {
return [customRecoveryGroup ?? RecoveredActionGroup];
export function getBuiltinActionGroups(customRecoveryGroup?: ActionGroup): ActionGroup[] {
return [customRecoveryGroup ?? Object.freeze(RecoveredActionGroup)];
}
Loading

0 comments on commit 051be28

Please sign in to comment.