kibana/x-pack/plugins/alerts/server/lib/iso_or_relative_date.ts
Patrick Mueller 67e28ac8b4
[EventLog] Populate alert instances view with event log data (#68437)
resolves https://github.com/elastic/kibana/issues/57446

Adds a new API (AlertClient and HTTP endpoint) `getAlertStatus()` which returns
alert data calculated from the event log.
2020-08-14 08:34:26 -04:00

27 lines
848 B
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { parseDuration } from '../../common/parse_duration';
/**
* Parse an ISO date or NNx duration string as a Date
*
* @param dateString an ISO date or NNx "duration" string representing now-duration
* @returns a Date or undefined if the dateString was not valid
*/
export function parseIsoOrRelativeDate(dateString: string): Date | undefined {
const epochMillis = Date.parse(dateString);
if (!isNaN(epochMillis)) return new Date(epochMillis);
let millis: number;
try {
millis = parseDuration(dateString);
} catch (err) {
return;
}
return new Date(Date.now() - millis);
}