From 0ad40f8768493e56021e80051dc7c8fd235e2e8c Mon Sep 17 00:00:00 2001 From: Anton Dosov Date: Thu, 27 Aug 2020 12:46:26 +0200 Subject: [PATCH] improve --- .../drilldowns/url_drilldown/url_drilldown_scope.tsx | 10 +++++----- .../url_drilldown/url_drilldown_scope.test.ts | 8 ++++---- .../drilldowns/url_drilldown/url_drilldown_scope.ts | 10 +++++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/embeddable_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.tsx b/x-pack/plugins/embeddable_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.tsx index 161ccc6444cdb..e12604791f085 100644 --- a/x-pack/plugins/embeddable_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.tsx +++ b/x-pack/plugins/embeddable_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.tsx @@ -122,7 +122,7 @@ interface ValueClickTriggerEventScope { negate: boolean; } interface RangeSelectTriggerEventScope { - key?: string; + key: string; from?: string | number; to?: string | number; } @@ -147,7 +147,7 @@ async function getEventScopeFromRangeSelectTriggerContext( const { table, column: columnIndex, range } = eventScopeInput.data; const column = table.columns[columnIndex]; return cleanEmptyKeys({ - key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field) as string | undefined, + key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field) as string, from: toPrimitiveOrUndefined(range[0]) as string | number | undefined, to: toPrimitiveOrUndefined(range[range.length - 1]) as string | number | undefined, }); @@ -180,14 +180,14 @@ async function getEventScopeFromValueClickTriggerContext( export function getMockEventScope([trigger]: UrlTrigger[]): UrlDrilldownEventScope { if (trigger === SELECT_RANGE_TRIGGER) { return { - key: '__testKey__', + key: 'event.key', from: new Date(Date.now() - 15 * 60 * 1000).toISOString(), // 15 minutes ago to: new Date().toISOString(), }; } else { return { - key: '__testKey__', - value: '__testValue__', + key: 'event.key', + value: 'event.value', negate: false, }; } diff --git a/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.test.ts b/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.test.ts index 9d3df521ab8e5..f95fc5e70ae00 100644 --- a/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.test.ts +++ b/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.test.ts @@ -41,12 +41,12 @@ test('buildScopeSuggestions', () => { ) ).toMatchInlineSnapshot(` Array [ - "kibanaUrl", - "context.filters", - "context.query.query", - "context.query.language", "event.key", "event.value", + "context.filters", + "context.query.language", + "context.query.query", + "kibanaUrl", ] `); }); diff --git a/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.ts b/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.ts index ba792c868919c..d499812a9d5ae 100644 --- a/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.ts +++ b/x-pack/plugins/ui_actions_enhanced/public/drilldowns/url_drilldown/url_drilldown_scope.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import partition from 'lodash/partition'; import { UrlDrilldownGlobalScope, UrlDrilldownScope } from './types'; import { getFlattenedObject } from '../../../../../../src/core/public'; @@ -26,6 +27,13 @@ export function buildScope< }; } +/** + * Builds list of variables for suggestion from scope + * keys sorted alphabetically, except {{event.$}} variables are pulled to the top + * @param scope + */ export function buildScopeSuggestions(scope: UrlDrilldownGlobalScope): string[] { - return Object.keys(getFlattenedObject(scope)); + const allKeys = Object.keys(getFlattenedObject(scope)).sort(); + const [eventKeys, otherKeys] = partition(allKeys, (key) => key.startsWith('event')); + return [...eventKeys, ...otherKeys]; }