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

Onboard alerting tasks to use stateSchemaByVersion for task state validation of the framework fields #162425

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a3ee8a8
Initial commit
mikecote Jul 24, 2023
aa82946
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Jul 24, 2023
c1674dc
Keep hasFieldsForAAD
mikecote Jul 24, 2023
629e118
Merge branch 'alerting/onboard-task-types-state-validation' of github…
mikecote Jul 24, 2023
35c9f16
Fix code merge
mikecote Jul 24, 2023
88deca5
Merge branch 'main' of github.com:elastic/kibana into alerting/onboar…
mikecote Jul 24, 2023
532a3dd
Fix typecheck failures
mikecote Jul 24, 2023
613542b
Merge branch 'main' of github.com:elastic/kibana into alerting/onboar…
mikecote Jul 27, 2023
b6f9c7f
Undo failing test
mikecote Jul 27, 2023
7967331
Fix test failure
mikecote Jul 27, 2023
35bfc3b
Merge types and schemas into a single source
mikecote Jul 28, 2023
8fe518e
Attempt reducing bundle size for alerting plugin
mikecote Jul 28, 2023
dcdd31e
allow undefined
mikecote Jul 28, 2023
c0c9b30
Merge branch 'main' of github.com:elastic/kibana into alerting/onboar…
mikecote Jul 31, 2023
2d8af0b
Add migrations
mikecote Jul 31, 2023
78b5716
Merge branch 'main' of github.com:elastic/kibana into alerting/onboar…
mikecote Jul 31, 2023
1b38ed8
Add unit tests and fix type check
mikecote Jul 31, 2023
667e8b1
Add back accidentally removed code
mikecote Jul 31, 2023
2484511
Merge branch 'main' of github.com:elastic/kibana into alerting/onboar…
mikecote Jul 31, 2023
ca0b27a
Add back the comments
mikecote Jul 31, 2023
eaf3fee
Make code more readable
mikecote Jul 31, 2023
26a781e
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Jul 31, 2023
d4dfbf0
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 1, 2023
1decfbc
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 8, 2023
51aa13c
Add comment linking to GitHub issue
mikecote Aug 9, 2023
475cb5c
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 9, 2023
78030fc
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 14, 2023
f227556
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 16, 2023
4d9fd20
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 22, 2023
c9a97b6
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 25, 2023
95108ca
Remove some outdated code comments
mikecote Aug 25, 2023
b5af1cd
Merge branch 'main' into alerting/onboard-task-types-state-validation
kibanamachine Aug 28, 2023
c054572
Add back code that only keeps alertStates in the state
mikecote Aug 28, 2023
8259214
Modify code to make tests pass
mikecote Aug 28, 2023
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
28 changes: 15 additions & 13 deletions x-pack/packages/kbn-alerting-state-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
* 2.0.
*/

export type {
ThrottledActions,
LastScheduledActions,
AlertInstanceMeta,
AlertInstanceState,
AlertInstanceContext,
RawAlertInstance,
} from './src/alert_instance';
export { rawAlertInstance } from './src/alert_instance';

export { DateFromString } from './src/date_from_string';
export type { AlertInstanceContext } from './src/alert_instance';

export type { TrackedLifecycleAlertState, WrappedLifecycleRuleState } from './src/lifecycle_state';
export { wrappedStateRt } from './src/lifecycle_state';

export type { RuleTaskState, RuleTaskParams } from './src/rule_task_instance';
export { ActionsCompletion, ruleStateSchema, ruleParamsSchema } from './src/rule_task_instance';
export type { RuleTaskParams } from './src/rule_task_instance';
export { ActionsCompletion, ruleParamsSchema } from './src/rule_task_instance';

export type {
LatestTaskStateSchema as RuleTaskState,
MutableLatestTaskStateSchema as MutableRuleTaskState,
LatestRawAlertInstanceSchema as RawAlertInstance,
LatestAlertInstanceMetaSchema as AlertInstanceMeta,
MutableLatestAlertInstanceMetaSchema as MutableAlertInstanceMeta,
LatestAlertInstanceStateSchema as AlertInstanceState,
LatestThrottledActionSchema as ThrottledActions,
LatestLastScheduledActionsSchema as LastScheduledActions,
} from './src/task_state';
export { stateSchemaByVersion, emptyState as emptyTaskState } from './src/task_state';
44 changes: 0 additions & 44 deletions x-pack/packages/kbn-alerting-state-types/src/alert_instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,6 @@
*/

import * as t from 'io-ts';
import { DateFromString } from './date_from_string';

const actionSchema = t.type({
date: DateFromString,
});

export const throttledActionSchema = t.record(t.string, actionSchema);
export type ThrottledActions = t.TypeOf<typeof throttledActionSchema>;

const lastScheduledActionsSchema = t.intersection([
t.partial({
subgroup: t.string,
}),
t.type({
group: t.string,
date: DateFromString,
}),
t.partial({ actions: throttledActionSchema }),
]);

export type LastScheduledActions = t.TypeOf<typeof lastScheduledActionsSchema>;

const metaSchema = t.partial({
lastScheduledActions: lastScheduledActionsSchema,
// an array used to track changes in alert state, the order is based on the rule executions (oldest to most recent)
// true - alert has changed from active/recovered
// false - the status has remained either active or recovered
flappingHistory: t.array(t.boolean),
// flapping flag that indicates whether the alert is flapping
flapping: t.boolean,
maintenanceWindowIds: t.array(t.string),
pendingRecoveredCount: t.number,
uuid: t.string,
});
export type AlertInstanceMeta = t.TypeOf<typeof metaSchema>;

const stateSchema = t.record(t.string, t.unknown);
export type AlertInstanceState = t.TypeOf<typeof stateSchema>;

const contextSchema = t.record(t.string, t.unknown);
export type AlertInstanceContext = t.TypeOf<typeof contextSchema>;

export const rawAlertInstance = t.partial({
state: stateSchema,
meta: metaSchema,
});
export type RawAlertInstance = t.TypeOf<typeof rawAlertInstance>;

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@
*/

import * as t from 'io-ts';
import { throttledActionSchema, rawAlertInstance } from './alert_instance';
import { DateFromString } from './date_from_string';

export enum ActionsCompletion {
COMPLETE = 'complete',
PARTIAL = 'partial',
}

export const ruleStateSchema = t.partial({
alertTypeState: t.record(t.string, t.unknown),
// tracks the active alerts
alertInstances: t.record(t.string, rawAlertInstance),
// tracks the recovered alerts for flapping purposes
alertRecoveredInstances: t.record(t.string, rawAlertInstance),
previousStartedAt: t.union([t.null, DateFromString]),
summaryActions: throttledActionSchema,
});

// This is serialized in the rule task document
export type RuleTaskState = t.TypeOf<typeof ruleStateSchema>;

export const ruleParamsSchema = t.intersection([
t.type({
alertId: t.string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 { type TypeOf } from '@kbn/config-schema';
import * as v1 from './v1';

export const stateSchemaByVersion = {
1: v1.versionDefinition,
};

const latest = v1;
/**
* WARNING: Do not modify the code below when doing a new version.
* Update the "latest" variable instead.
*/
const latestTaskStateSchema = latest.versionDefinition.schema;
export type LatestTaskStateSchema = TypeOf<typeof latestTaskStateSchema>;
export type LatestRawAlertInstanceSchema = TypeOf<typeof latest.rawAlertInstanceSchema>;
export type LatestAlertInstanceMetaSchema = TypeOf<typeof latest.metaSchema>;
export type LatestAlertInstanceStateSchema = TypeOf<typeof latest.alertStateSchema>;
export type LatestThrottledActionSchema = TypeOf<typeof latest.throttledActionSchema>;
export type LatestLastScheduledActionsSchema = TypeOf<typeof latest.lastScheduledActionsSchema>;

export const emptyState: LatestTaskStateSchema = {
alertTypeState: {},
alertInstances: {},
alertRecoveredInstances: {},
previousStartedAt: null,
summaryActions: {},
};

type Mutable<T> = {
-readonly [k in keyof T]: Mutable<T[k]>;
};
export type MutableLatestTaskStateSchema = Mutable<LatestTaskStateSchema>;
export type MutableLatestAlertInstanceMetaSchema = Mutable<LatestAlertInstanceMetaSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 { isPlainObject } from 'lodash';

export function isJSONObject(obj: unknown): obj is Record<string, unknown> {
return isPlainObject(obj);
}

export function isString(value: unknown): value is string {
return typeof value === 'string';
}

export function isBoolean(value: unknown): value is boolean {
return typeof value === 'boolean';
}

export function isNumber(value: unknown): value is number {
return typeof value === 'number';
}

export function isStringArray(value: unknown): value is string[] {
return Array.isArray(value) && value.every((item) => typeof item === 'string');
}

export function isBooleanArray(value: unknown): value is boolean[] {
return Array.isArray(value) && value.every((item) => typeof item === 'boolean');
}
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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { upMigration } from './migration';
import { versionSchema } from './schema';

export {
versionSchema,
throttledActionSchema,
rawAlertInstanceSchema,
metaSchema,
alertStateSchema,
lastScheduledActionsSchema,
} from './schema';

export const versionDefinition = {
up: upMigration,
schema: versionSchema,
};
Loading
Loading