Skip to content

Commit 489b39c

Browse files
authored
Re-enable datemath in from/to canvas timelion args (#52159)
1 parent 0cd62ca commit 489b39c

File tree

1 file changed

+27
-4
lines changed
  • x-pack/legacy/plugins/canvas/public/functions

1 file changed

+27
-4
lines changed

x-pack/legacy/plugins/canvas/public/functions/timelion.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66

77
import { flatten } from 'lodash';
8+
import moment from 'moment-timezone';
89
import chrome from 'ui/chrome';
10+
import { npStart } from 'ui/new_platform';
11+
import { TimeRange } from 'src/plugins/data/common';
912
import { ExpressionFunction, DatatableRow } from 'src/plugins/expressions/public';
1013
import { fetch } from '../../common/lib/fetch';
1114
// @ts-ignore untyped local
@@ -21,6 +24,26 @@ interface Arguments {
2124
timezone: string;
2225
}
2326

27+
/**
28+
* This function parses a given time range containing date math
29+
* and returns ISO dates. Parsing is done respecting the given time zone.
30+
* @param timeRange time range to parse
31+
* @param timeZone time zone to do the parsing in
32+
*/
33+
function parseDateMath(timeRange: TimeRange, timeZone: string) {
34+
// the datemath plugin always parses dates by using the current default moment time zone.
35+
// to use the configured time zone, we are switching just for the bounds calculation.
36+
const defaultTimezone = moment().zoneName();
37+
moment.tz.setDefault(timeZone);
38+
39+
const parsedRange = npStart.plugins.data.query.timefilter.timefilter.calculateBounds(timeRange);
40+
41+
// reset default moment timezone
42+
moment.tz.setDefault(defaultTimezone);
43+
44+
return parsedRange;
45+
}
46+
2447
export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Promise<Datatable>> {
2548
const { help, args: argHelp } = getFunctionHelp().timelion;
2649

@@ -64,8 +87,8 @@ export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Pr
6487
// workpad, if it exists. Otherwise fall back on the function args.
6588
const timeFilter = context.and.find(and => and.type === 'time');
6689
const range = timeFilter
67-
? { from: timeFilter.from, to: timeFilter.to }
68-
: { from: args.from, to: args.to };
90+
? { min: timeFilter.from, max: timeFilter.to }
91+
: parseDateMath({ from: args.from, to: args.to }, args.timezone);
6992

7093
const body = {
7194
extended: {
@@ -79,8 +102,8 @@ export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Pr
79102
},
80103
sheet: [args.query],
81104
time: {
82-
from: range.from,
83-
to: range.to,
105+
from: range.min,
106+
to: range.max,
84107
interval: args.interval,
85108
timezone: args.timezone,
86109
},

0 commit comments

Comments
 (0)