Skip to content

Commit

Permalink
resolve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuellr committed Aug 12, 2020
1 parent ab427f5 commit fb3a7a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/alerts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ Params:

|Property|Description|Type|
|---|---|---|
|id|The id of the alert whose state you're trying to get.|string|
|id|The id of the alert whose status you're trying to get.|string|

Query:

Expand Down
58 changes: 28 additions & 30 deletions x-pack/plugins/alerts/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,7 @@ export class AlertsClient {
}

public async getAlertStatus({ id, dateStart }: GetAlertStatusParams): Promise<AlertStatus> {
const eventLogClient = await this.getEventLogClient();

this.logger.debug('getAlertStatus(): getting the alert');
this.logger.debug(`getAlertStatus(): getting alert ${id}`);
const alert = await this.get({ id });
await this.authorization.ensureAuthorized(
alert.alertTypeId,
Expand All @@ -301,7 +299,9 @@ export class AlertsClient {
const defaultDateStart = new Date(dateNow.valueOf() - durationMillis);
const parsedDateStart = parseDate(dateStart, 'dateStart', defaultDateStart);

this.logger.debug('getAlertStatus(): search the event log');
const eventLogClient = await this.getEventLogClient();

this.logger.debug(`getAlertStatus(): search the event log for alert ${id}`);
let events: IEvent[];
try {
const queryResults = await eventLogClient.findEventsBySavedObject('alert', id, {
Expand All @@ -313,7 +313,9 @@ export class AlertsClient {
});
events = queryResults.data;
} catch (err) {
this.logger.debug(`alertsClient.getAlertStatus(): error searching event log: ${err.message}`);
this.logger.debug(
`alertsClient.getAlertStatus(): error searching event log for alert ${id}: ${err.message}`
);
events = [];
}

Expand All @@ -323,31 +325,6 @@ export class AlertsClient {
dateStart: parsedDateStart.toISOString(),
dateEnd: dateNow.toISOString(),
});

function parseDate(
dateString: string | undefined,
propertyName: string,
defaultValue: Date
): Date {
if (dateString === undefined) {
return defaultValue;
}

const parsedDate = parseIsoOrRelativeDate(dateString);
if (parsedDate === undefined) {
throw Boom.badRequest(
i18n.translate('xpack.alerts.alertsClient.getAlertStatus.invalidDate', {
defaultMessage: 'Invalid date for parameter {field}: "{dateValue}"',
values: {
field: propertyName,
dateValue: dateString,
},
})
);
}

return parsedDate;
}
}

public async find({
Expand Down Expand Up @@ -966,3 +943,24 @@ export class AlertsClient {
return truncate(`Alerting: ${alertTypeId}/${alertName}`, { length: 256 });
}
}

function parseDate(dateString: string | undefined, propertyName: string, defaultValue: Date): Date {
if (dateString === undefined) {
return defaultValue;
}

const parsedDate = parseIsoOrRelativeDate(dateString);
if (parsedDate === undefined) {
throw Boom.badRequest(
i18n.translate('xpack.alerts.alertsClient.getAlertStatus.invalidDate', {
defaultMessage: 'Invalid date for parameter {field}: "{dateValue}"',
values: {
field: propertyName,
dateValue: dateString,
},
})
);
}

return parsedDate;
}
9 changes: 7 additions & 2 deletions x-pack/plugins/alerts/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,13 @@ interface GenerateNewAndResolvedInstanceEventsParams {
}

function generateNewAndResolvedInstanceEvents(params: GenerateNewAndResolvedInstanceEventsParams) {
const { eventLogger, alertId, namespace } = params;
const { currentAlertInstanceIds, originalAlertInstanceIds } = params;
const {
eventLogger,
alertId,
namespace,
currentAlertInstanceIds,
originalAlertInstanceIds,
} = params;

const newIds = without(currentAlertInstanceIds, ...originalAlertInstanceIds);
const resolvedIds = without(originalAlertInstanceIds, ...currentAlertInstanceIds);
Expand Down

0 comments on commit fb3a7a9

Please sign in to comment.