Skip to content

Commit

Permalink
[canvas] Handle Timelion errors gracefully. (elastic#109761) (elastic…
Browse files Browse the repository at this point in the history
…#110302)

Co-authored-by: Clint Andrew Hall <clint.hall@elastic.co>
  • Loading branch information
kibanamachine and clintandrewhall authored Aug 26, 2021
1 parent 1022cad commit 6f7562b
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions x-pack/plugins/canvas/public/functions/timelion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import { flatten } from 'lodash';
import moment from 'moment-timezone';
import { i18n } from '@kbn/i18n';

import { TimeRange } from 'src/plugins/data/common';
import { ExpressionFunctionDefinition, DatatableRow } from 'src/plugins/expressions/public';
import { fetch } from '../../common/lib/fetch';
Expand All @@ -16,6 +18,15 @@ import { Datatable, ExpressionValueFilter } from '../../types';
import { getFunctionHelp } from '../../i18n';
import { InitializeArguments } from './';

const errors = {
timelionError: () =>
new Error(
i18n.translate('xpack.canvas.functions.timelion.executionError', {
defaultMessage:
'There was an error executing the Timelion query. Check your syntax and try again.',
})
),
};
export interface Arguments {
query: string;
interval: string;
Expand Down Expand Up @@ -92,7 +103,7 @@ export function timelionFunctionFactory(initialize: InitializeArguments): () =>
default: 'UTC',
},
},
fn: (input, args): Promise<Datatable> => {
fn: async (input, args) => {
// Timelion requires a time range. Use the time range from the timefilter element in the
// workpad, if it exists. Otherwise fall back on the function args.
const timeFilter = input.and.find((and) => and.filterType === 'time');
Expand All @@ -118,35 +129,41 @@ export function timelionFunctionFactory(initialize: InitializeArguments): () =>
},
};

return fetch(initialize.prependBasePath(`/api/timelion/run`), {
method: 'POST',
responseType: 'json',
data: body,
}).then((resp) => {
const seriesList = resp.data.sheet[0].list;
const rows = flatten(
seriesList.map((series: { data: any[]; label: string }) =>
series.data.map((row) => ({
'@timestamp': row[0],
value: row[1],
label: series.label,
}))
)
) as DatatableRow[];
let result: any;

return {
type: 'datatable',
meta: {
source: 'timelion',
},
columns: [
{ id: '@timestamp', name: '@timestamp', meta: { type: 'date' } },
{ id: 'value', name: 'value', meta: { type: 'number' } },
{ id: 'label', name: 'label', meta: { type: 'string' } },
],
rows,
};
});
try {
result = await fetch(initialize.prependBasePath(`/api/timelion/run`), {
method: 'POST',
responseType: 'json',
data: body,
});
} catch (e) {
throw errors.timelionError();
}

const seriesList = result.data.sheet[0].list;
const rows = flatten(
seriesList.map((series: { data: any[]; label: string }) =>
series.data.map((row) => ({
'@timestamp': row[0],
value: row[1],
label: series.label,
}))
)
) as DatatableRow[];

return {
type: 'datatable',
meta: {
source: 'timelion',
},
columns: [
{ id: '@timestamp', name: '@timestamp', meta: { type: 'date' } },
{ id: 'value', name: 'value', meta: { type: 'number' } },
{ id: 'label', name: 'label', meta: { type: 'string' } },
],
rows,
};
},
};
};
Expand Down

0 comments on commit 6f7562b

Please sign in to comment.