Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable datemath in from/to canvas timelion args #52159

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions x-pack/legacy/plugins/canvas/public/functions/timelion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/

import { flatten } from 'lodash';
import moment from 'moment-timezone';
import chrome from 'ui/chrome';
import { npStart } from 'ui/new_platform';
import { TimeRange } from 'src/plugins/data/common';
import { ExpressionFunction, DatatableRow } from 'src/plugins/expressions/public';
import { fetch } from '../../common/lib/fetch';
// @ts-ignore untyped local
Expand All @@ -21,6 +24,26 @@ interface Arguments {
timezone: string;
}

/**
* This function parses a given time range containing date math
* and returns ISO dates. Parsing is done respecting the given time zone.
* @param timeRange time range to parse
* @param timeZone time zone to do the parsing in
*/
function parseDateMath(timeRange: TimeRange, timeZone: string) {
// the datemath plugin always parses dates by using the current default moment time zone.
// to use the configured time zone, we are switching just for the bounds calculation.
const defaultTimezone = moment().zoneName();
moment.tz.setDefault(timeZone);

const parsedRange = npStart.plugins.data.query.timefilter.timefilter.calculateBounds(timeRange);

// reset default moment timezone
moment.tz.setDefault(defaultTimezone);

return parsedRange;
}

export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Promise<Datatable>> {
const { help, args: argHelp } = getFunctionHelp().timelion;

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

const body = {
extended: {
Expand All @@ -79,8 +102,8 @@ export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Pr
},
sheet: [args.query],
time: {
from: range.from,
to: range.to,
from: range.min,
to: range.max,
interval: args.interval,
timezone: args.timezone,
},
Expand Down