Skip to content

Commit

Permalink
fix for quickrange to use datemath to parse datetime strings
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sebastian <[email protected]>
  • Loading branch information
paulstn committed May 8, 2024
1 parent bc59492 commit 79c7962
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/plugins/data/common/data_frames/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 79c7962

Please sign in to comment.