From 330797f73600ed0bbccf6c46fe905ae1052bcd12 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Mon, 8 Feb 2021 18:14:46 +0300 Subject: [PATCH] [Timelion] Communicate the index pattern to the dashboard Closes #86418 --- .eslintignore | 2 +- .../{public => common}/_generated_/chain.js | 0 src/plugins/vis_type_timelion/common/types.ts | 16 +++++++++++++++ .../timelion_expression_input_helpers.ts | 2 +- .../public/timelion_vis_type.tsx | 20 ++++++++++++++++++- .../server/handlers/lib/parse_sheet.js | 11 +++------- tasks/config/peg.js | 4 ++-- 7 files changed, 42 insertions(+), 13 deletions(-) rename src/plugins/vis_type_timelion/{public => common}/_generated_/chain.js (100%) diff --git a/.eslintignore b/.eslintignore index 5513ad1320232c0..ea8ab55ad772697 100644 --- a/.eslintignore +++ b/.eslintignore @@ -22,7 +22,7 @@ snapshots.js /src/core/lib/kbn_internal_native_observable /src/legacy/plugin_discovery/plugin_pack/__tests__/fixtures/plugins/broken /src/plugins/data/common/es_query/kuery/ast/_generated_/** -/src/plugins/vis_type_timelion/public/_generated_/** +/src/plugins/vis_type_timelion/common/_generated_/** /x-pack/legacy/plugins/**/__tests__/fixtures/** /x-pack/plugins/apm/e2e/tmp/* /x-pack/plugins/canvas/canvas_plugin diff --git a/src/plugins/vis_type_timelion/public/_generated_/chain.js b/src/plugins/vis_type_timelion/common/_generated_/chain.js similarity index 100% rename from src/plugins/vis_type_timelion/public/_generated_/chain.js rename to src/plugins/vis_type_timelion/common/_generated_/chain.js diff --git a/src/plugins/vis_type_timelion/common/types.ts b/src/plugins/vis_type_timelion/common/types.ts index f3f2a74a711a91c..808097650d6e9ce 100644 --- a/src/plugins/vis_type_timelion/common/types.ts +++ b/src/plugins/vis_type_timelion/common/types.ts @@ -33,3 +33,19 @@ export interface ITimelionFunction { [key: string]: TimelionFunctionArgs[]; }; } + +export interface TimelionExpressionNamedArgument { + function: string; + type: 'namedArg'; + location: { + min: number; + max: number; + }; + name: string; + text: string; + value: { + type: string; + value: string; + text: string; + }; +} diff --git a/src/plugins/vis_type_timelion/public/components/timelion_expression_input_helpers.ts b/src/plugins/vis_type_timelion/public/components/timelion_expression_input_helpers.ts index a428bc946364b06..bfcdceea005286a 100644 --- a/src/plugins/vis_type_timelion/public/components/timelion_expression_input_helpers.ts +++ b/src/plugins/vis_type_timelion/public/components/timelion_expression_input_helpers.ts @@ -13,7 +13,7 @@ import { monaco } from '@kbn/monaco'; import { Parser } from 'pegjs'; // @ts-ignore -import { parse } from '../_generated_/chain'; +import { parse } from '../../common/_generated_/chain'; import { ArgValueSuggestions, FunctionArg, Location } from '../helpers/arg_value_suggestions'; import { ITimelionFunction, TimelionFunctionArgs } from '../../common/types'; diff --git a/src/plugins/vis_type_timelion/public/timelion_vis_type.tsx b/src/plugins/vis_type_timelion/public/timelion_vis_type.tsx index b41bea96de302ab..17c546ccc43a779 100644 --- a/src/plugins/vis_type_timelion/public/timelion_vis_type.tsx +++ b/src/plugins/vis_type_timelion/public/timelion_vis_type.tsx @@ -13,8 +13,13 @@ import { DefaultEditorSize } from '../../vis_default_editor/public'; import { TimelionOptionsProps } from './timelion_options'; import { TimelionVisDependencies } from './plugin'; import { toExpressionAst } from './to_ast'; +import { getIndexPatterns } from './helpers/plugin_services'; -import { VIS_EVENT_TO_TRIGGER } from '../../visualizations/public'; +// @ts-ignore +import { parse } from '../common/_generated_/chain'; + +import { VIS_EVENT_TO_TRIGGER, VisParams } from '../../visualizations/public'; +import { TimelionExpressionNamedArgument } from '../common/types'; const TimelionOptions = lazy(() => import('./timelion_options')); @@ -47,6 +52,19 @@ export function getTimelionVisDefinition(dependencies: TimelionVisDependencies) getSupportedTriggers: () => { return [VIS_EVENT_TO_TRIGGER.applyFilter]; }, + getUsedIndexPattern: (params: VisParams) => { + try { + const args: TimelionExpressionNamedArgument[] = parse(params.expression)?.args ?? []; + const indexArg = args.find((arg) => arg.function === 'es' && arg.name === 'index'); + + if (indexArg?.value.text) { + return getIndexPatterns().find(indexArg?.value.text); + } + } catch { + // timelion expression is invalid + } + return []; + }, options: { showIndexSelection: false, showQueryBar: false, diff --git a/src/plugins/vis_type_timelion/server/handlers/lib/parse_sheet.js b/src/plugins/vis_type_timelion/server/handlers/lib/parse_sheet.js index 67bde9d7e6daa8a..0c75ce62c6b0ba3 100644 --- a/src/plugins/vis_type_timelion/server/handlers/lib/parse_sheet.js +++ b/src/plugins/vis_type_timelion/server/handlers/lib/parse_sheet.js @@ -7,17 +7,12 @@ */ import { i18n } from '@kbn/i18n'; -import fs from 'fs'; -import path from 'path'; -import _ from 'lodash'; -const grammar = fs.readFileSync(path.resolve(__dirname, '../../../common/chain.peg'), 'utf8'); -import PEG from 'pegjs'; -const Parser = PEG.generate(grammar); +import { parse } from '../../../common/_generated_/chain'; export default function parseSheet(sheet) { - return _.map(sheet, function (plot) { + return sheet.map(function (plot) { try { - return Parser.parse(plot).tree; + return parse(plot).tree; } catch (e) { if (e.expected) { throw new Error( diff --git a/tasks/config/peg.js b/tasks/config/peg.js index 117af5909f23e4a..09da1ed81c2229d 100644 --- a/tasks/config/peg.js +++ b/tasks/config/peg.js @@ -15,7 +15,7 @@ module.exports = { }, }, timelion_chain: { - src: 'src/plugins/vis_type_timelion/public/chain.peg', - dest: 'src/plugins/vis_type_timelion/public/_generated_/chain.js', + src: 'src/plugins/vis_type_timelion/common/chain.peg', + dest: 'src/plugins/vis_type_timelion/common/_generated_/chain.js', }, };