From 79c79629997bfc4439ab4dff5e6b9f9de6d7a297 Mon Sep 17 00:00:00 2001 From: Paul Sebastian <paulstn@amazon.com> Date: Wed, 8 May 2024 11:49:47 -0700 Subject: [PATCH] fix for quickrange to use datemath to parse datetime strings Signed-off-by: Paul Sebastian <paulstn@amazon.com> --- src/plugins/data/common/data_frames/utils.ts | 22 +++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/plugins/data/common/data_frames/utils.ts b/src/plugins/data/common/data_frames/utils.ts index c3c55c5f227c..5a4e5a77f03e 100644 --- a/src/plugins/data/common/data_frames/utils.ts +++ b/src/plugins/data/common/data_frames/utils.ts @@ -17,7 +17,7 @@ import { import { IFieldType } from './fields'; import { IndexPatternFieldMap, IndexPatternSpec } from '../index_patterns'; import { IOpenSearchDashboardsSearchRequest } from '../search'; -import { GetAggTypeFn, GetDataFrameAggQsFn } from '../types'; +import { GetAggTypeFn, GetDataFrameAggQsFn, TimeRange } from '../types'; /** * Returns the raw data frame from the search request. @@ -290,6 +290,26 @@ export const getTimeField = ( : fields.find((field) => field.type === 'date'); }; +/** + * Parses timepicker datetimes using datemath package. Will attempt to parse strings such as + * "now - 15m" + * + * @param dateRange - of type TimeRange + * @returns object with fromDate and toDate, both of which will be in utc time and formatted to + * 'YYYY-MM-DD HH:mm:ss.SSS' + */ +export const formatTimePickerDate = (dateRange: TimeRange) => { + const dateMathParse = (date: string) => { + const parsedDate = datemath.parse(date); + return parsedDate ? parsedDate.utc().format('YYYY-MM-DD HH:mm:ss.SSS') : ''; + }; + + const fromDate = dateMathParse(dateRange.from); + const toDate = dateMathParse(dateRange.to); + + return { fromDate, toDate }; +}; + /** * Checks if the value is a GeoPoint. Expects an object with lat and lon properties. *