-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Make the alerts plugin support generics #72716
Changes from 10 commits
7ea56a0
cfaef3a
25e433f
6f32aa4
1b53ddf
03f0777
e36f3f2
dbad87e
5b9e64b
ac688c2
665d2b6
b92d847
5c63c94
dbfcea5
17503fe
0b9bdea
8d4dde2
44dafc0
67cd17a
f6b1272
2981fd1
2cf9ea0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,26 @@ import { | |
AlertInstanceState, | ||
RawAlertInstance, | ||
rawAlertInstance, | ||
AlertInstanceContext, | ||
} from '../../common'; | ||
|
||
import { State, Context } from '../types'; | ||
import { parseDuration } from '../lib'; | ||
|
||
interface ScheduledExecutionOptions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure, for this one here I had to move it within the |
||
actionGroup: string; | ||
context: Context; | ||
state: State; | ||
} | ||
export type AlertInstances = Record<string, AlertInstance>; | ||
export class AlertInstance { | ||
private scheduledExecutionOptions?: ScheduledExecutionOptions; | ||
export class AlertInstance< | ||
State extends AlertInstanceState = AlertInstanceState, | ||
Context extends AlertInstanceContext = AlertInstanceContext | ||
> { | ||
private scheduledExecutionOptions?: { | ||
actionGroup: string; | ||
context: Context; | ||
state: State; | ||
}; | ||
private meta: AlertInstanceMeta; | ||
private state: AlertInstanceState; | ||
private state: State; | ||
|
||
constructor({ state = {}, meta = {} }: RawAlertInstance = {}) { | ||
this.state = state; | ||
constructor({ state, meta = {} }: RawAlertInstance = {}) { | ||
this.state = (state || {}) as State; | ||
this.meta = meta; | ||
} | ||
|
||
|
@@ -62,11 +64,15 @@ export class AlertInstance { | |
return this.state; | ||
} | ||
|
||
scheduleActions(actionGroup: string, context: Context = {}) { | ||
scheduleActions(actionGroup: string, context?: Context) { | ||
if (this.hasScheduledActions()) { | ||
throw new Error('Alert instance execution has already been scheduled, cannot schedule twice'); | ||
} | ||
this.scheduledExecutionOptions = { actionGroup, context, state: this.state }; | ||
this.scheduledExecutionOptions = { | ||
actionGroup, | ||
context: (context || {}) as Context, | ||
state: this.state, | ||
}; | ||
return this; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,13 @@ import { schema } from '@kbn/config-schema'; | |
import typeDetect from 'type-detect'; | ||
import { RunContext, TaskManagerSetupContract } from '../../task_manager/server'; | ||
import { TaskRunnerFactory } from './task_runner'; | ||
import { AlertType } from './types'; | ||
import { | ||
AlertType, | ||
AlertTypeParams, | ||
AlertTypeState, | ||
AlertInstanceState, | ||
AlertInstanceContext, | ||
} from './types'; | ||
|
||
interface ConstructorOptions { | ||
taskManager: TaskManagerSetupContract; | ||
|
@@ -59,7 +65,12 @@ export class AlertTypeRegistry { | |
return this.alertTypes.has(id); | ||
} | ||
|
||
public register(alertType: AlertType) { | ||
public register< | ||
Params extends AlertTypeParams = AlertTypeParams, | ||
State extends AlertTypeState = AlertTypeState, | ||
AlertInstanceStateType extends AlertInstanceState = AlertInstanceState, | ||
AlertInstanceContextType extends AlertInstanceContext = AlertInstanceContext | ||
>(alertType: AlertType<Params, State, AlertInstanceStateType, AlertInstanceContextType>) { | ||
if (this.has(alertType.id)) { | ||
throw new Error( | ||
i18n.translate('xpack.alerts.alertTypeRegistry.register.duplicateAlertTypeError', { | ||
|
@@ -71,18 +82,23 @@ export class AlertTypeRegistry { | |
); | ||
} | ||
alertType.actionVariables = normalizedActionVariables(alertType.actionVariables); | ||
this.alertTypes.set(alertIdSchema.validate(alertType.id), { ...alertType }); | ||
this.alertTypes.set(alertIdSchema.validate(alertType.id), { ...alertType } as AlertType); | ||
this.taskManager.registerTaskDefinitions({ | ||
[`alerting:${alertType.id}`]: { | ||
title: alertType.name, | ||
type: `alerting:${alertType.id}`, | ||
createTaskRunner: (context: RunContext) => | ||
this.taskRunnerFactory.create(alertType, context), | ||
this.taskRunnerFactory.create({ ...alertType } as AlertType, context), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
}); | ||
} | ||
|
||
public get(id: string): AlertType { | ||
public get< | ||
Params extends AlertTypeParams = AlertTypeParams, | ||
State extends AlertTypeState = AlertTypeState, | ||
AlertInstanceStateType extends AlertInstanceState = AlertInstanceState, | ||
AlertInstanceContextType extends AlertInstanceContext = AlertInstanceContext | ||
>(id: string): AlertType<Params, State, AlertInstanceStateType, AlertInstanceContextType> { | ||
if (!this.has(id)) { | ||
throw Boom.badRequest( | ||
i18n.translate('xpack.alerts.alertTypeRegistry.get.missingAlertTypeError', { | ||
|
@@ -93,7 +109,12 @@ export class AlertTypeRegistry { | |
}) | ||
); | ||
} | ||
return this.alertTypes.get(id)!; | ||
return this.alertTypes.get(id)! as AlertType< | ||
Params, | ||
State, | ||
AlertInstanceStateType, | ||
AlertInstanceContextType | ||
>; | ||
} | ||
|
||
public list(): Set<RegistryAlertType> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be a generic type with a default of
unknown
instead ofany
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting these types to
Record<string, unknown>
caused over 130 type check failures. Most of these were outside of the alerting team's code and would cause us to change a lot of external code. I'm thinking we enforce this at some other time and have created #74897 to track this.