diff --git a/src/plugins/dashboard/kibana.jsonc b/src/plugins/dashboard/kibana.jsonc index 0f8601cb96c5d..022e0b1bca5d8 100644 --- a/src/plugins/dashboard/kibana.jsonc +++ b/src/plugins/dashboard/kibana.jsonc @@ -25,7 +25,7 @@ "urlForwarding", "presentationUtil", "visualizations", - "unifiedSearch" + "unifiedSearch", ], "optionalPlugins": [ "home", @@ -35,7 +35,8 @@ "usageCollection", "taskManager", "serverless", - "noDataPage" + "noDataPage", + "lens" ], "requiredBundles": ["kibanaReact", "kibanaUtils", "presentationUtil"] } diff --git a/src/plugins/dashboard/server/plugin.ts b/src/plugins/dashboard/server/plugin.ts index e1626c2e72108..99a2ccd179988 100644 --- a/src/plugins/dashboard/server/plugin.ts +++ b/src/plugins/dashboard/server/plugin.ts @@ -15,6 +15,7 @@ import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; import { ContentManagementServerSetup } from '@kbn/content-management-plugin/server'; import { PluginInitializerContext, CoreSetup, CoreStart, Plugin, Logger } from '@kbn/core/server'; +import { LensServerPluginSetup } from '@kbn/lens-plugin/server'; import { initializeDashboardTelemetryTask, scheduleDashboardTelemetry, @@ -28,12 +29,14 @@ import { createDashboardSavedObjectType } from './dashboard_saved_object'; import { CONTENT_ID, LATEST_VERSION } from '../common/content_management'; import { registerDashboardUsageCollector } from './usage/register_collector'; import { dashboardPersistableStateServiceFactory } from './dashboard_container/dashboard_container_embeddable_factory'; +import { setupQueryExtractionRoute } from './query_extraction/query_extraction_route'; interface SetupDeps { embeddable: EmbeddableSetup; usageCollection: UsageCollectionSetup; taskManager: TaskManagerSetupContract; contentManagement: ContentManagementServerSetup; + lens: LensServerPluginSetup; } interface StartDeps { @@ -60,6 +63,8 @@ export class DashboardPlugin }) ); + setupQueryExtractionRoute(core, plugins.lens.extractQueries); + plugins.contentManagement.register({ id: CONTENT_ID, storage: new DashboardStorage({ diff --git a/src/plugins/dashboard/server/query_extraction/query_extraction_route.ts b/src/plugins/dashboard/server/query_extraction/query_extraction_route.ts new file mode 100644 index 0000000000000..e2b10a2330b57 --- /dev/null +++ b/src/plugins/dashboard/server/query_extraction/query_extraction_route.ts @@ -0,0 +1,103 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { getKbnServerError, reportServerError } from '@kbn/kibana-utils-plugin/server'; +import type { CoreSetup } from '@kbn/core/server'; +import { schema } from '@kbn/config-schema'; +import type { LensByValueInput } from '@kbn/lens-plugin/public/embeddable/embeddable'; +import type { LensServerPluginSetup } from '@kbn/lens-plugin/server'; +import type { SavedDashboardPanel } from '../../common/content_management'; +import { convertSavedDashboardPanelToPanelState, type DashboardAttributes } from '../../common'; + +export const setupQueryExtractionRoute = ( + { http, getStartServices }: CoreSetup, + extractQueries: LensServerPluginSetup['extractQueries'] +) => { + const router = http.createRouter(); + + router.versioned + .get({ + access: 'public', + path: '/api/dashboards/dashboard/{id}/getQueries', + options: { + authRequired: false, + }, + }) + .addVersion( + { + version: '2023-10-31', + validate: { + request: { + params: schema.object( + { + id: schema.string({ + minLength: 1, + maxLength: 1_000, + }), + }, + { unknowns: 'allow' } + ), + }, + response: schema.any(), + }, + }, + async (context, request, response) => { + try { + const [coreStart] = await getStartServices(); + const client = coreStart.savedObjects.getScopedClient(request); + + const SO = await client.get('dashboard', request.params.id); + + const panels = JSON.parse( + (SO.attributes as DashboardAttributes).panelsJSON + ) as SavedDashboardPanel[]; + + const supportedPanels = panels.filter(({ type }) => type === 'lens'); + + const queriesByPanel: Array<{ title?: string; queries: object[] }> = []; + + for (const panel of supportedPanels) { + const panelState = convertSavedDashboardPanelToPanelState(panel); + + // TODO not sure if this is type safe + const attributes = panelState.explicitInput.attributes!; + + // copied from initializeSavedVis in Lens embeddable + const savedVis = { + ...attributes, + type: 'lens', + // savedObjectId: (input as LensByReferenceInput)?.savedObjectId, + }; + + const queries = await extractQueries( + savedVis, + { + elasticsearch: (await context.core).elasticsearch.client.asCurrentUser, + savedObjects: client, + }, + request + ); + + queriesByPanel.push({ + title: panel.title, + queries, + }); + } + + return response.ok({ + body: { + panels: queriesByPanel, + }, + }); + } catch (e) { + const kbnErr = getKbnServerError(e); + return reportServerError(response, kbnErr); + } + } + ); +}; diff --git a/src/plugins/data/common/index.ts b/src/plugins/data/common/index.ts index a7a5263229ddc..ca83014e69b82 100644 --- a/src/plugins/data/common/index.ts +++ b/src/plugins/data/common/index.ts @@ -46,6 +46,8 @@ export type { } from './types'; export { KBN_FIELD_TYPES, ES_FIELD_TYPES } from './types'; +export { NowProvider } from './now_provider'; + export { createEscapeValue, tableHasFormulas, diff --git a/src/plugins/data/public/now_provider/index.ts b/src/plugins/data/common/now_provider/index.ts similarity index 100% rename from src/plugins/data/public/now_provider/index.ts rename to src/plugins/data/common/now_provider/index.ts diff --git a/src/plugins/data/public/now_provider/lib/get_force_now_from_url.test.ts b/src/plugins/data/common/now_provider/lib/get_force_now_from_url.test.ts similarity index 100% rename from src/plugins/data/public/now_provider/lib/get_force_now_from_url.test.ts rename to src/plugins/data/common/now_provider/lib/get_force_now_from_url.test.ts diff --git a/src/plugins/data/public/now_provider/lib/get_force_now_from_url.ts b/src/plugins/data/common/now_provider/lib/get_force_now_from_url.ts similarity index 100% rename from src/plugins/data/public/now_provider/lib/get_force_now_from_url.ts rename to src/plugins/data/common/now_provider/lib/get_force_now_from_url.ts diff --git a/src/plugins/data/public/now_provider/lib/index.ts b/src/plugins/data/common/now_provider/lib/index.ts similarity index 100% rename from src/plugins/data/public/now_provider/lib/index.ts rename to src/plugins/data/common/now_provider/lib/index.ts diff --git a/src/plugins/data/public/now_provider/mocks.ts b/src/plugins/data/common/now_provider/mocks.ts similarity index 100% rename from src/plugins/data/public/now_provider/mocks.ts rename to src/plugins/data/common/now_provider/mocks.ts diff --git a/src/plugins/data/public/now_provider/now_provider.test.ts b/src/plugins/data/common/now_provider/now_provider.test.ts similarity index 100% rename from src/plugins/data/public/now_provider/now_provider.test.ts rename to src/plugins/data/common/now_provider/now_provider.test.ts diff --git a/src/plugins/data/public/now_provider/now_provider.ts b/src/plugins/data/common/now_provider/now_provider.ts similarity index 100% rename from src/plugins/data/public/now_provider/now_provider.ts rename to src/plugins/data/common/now_provider/now_provider.ts diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index de2504dec2d99..0aeb827a8060d 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -244,8 +244,11 @@ export { QueryService, } from './query'; -export { NowProvider } from './now_provider'; -export type { NowProviderInternalContract, NowProviderPublicContract } from './now_provider'; +export { NowProvider } from '../common/now_provider'; +export type { + NowProviderInternalContract, + NowProviderPublicContract, +} from '../common/now_provider'; export type { QueryState, diff --git a/src/plugins/data/public/mocks.ts b/src/plugins/data/public/mocks.ts index e26bb07268b6d..8f49d54794a08 100644 --- a/src/plugins/data/public/mocks.ts +++ b/src/plugins/data/public/mocks.ts @@ -11,7 +11,7 @@ import { createDatatableUtilitiesMock } from '../common/mocks'; import { DataPlugin } from '.'; import { searchServiceMock } from './search/mocks'; import { queryServiceMock } from './query/mocks'; -import { createNowProviderMock } from './now_provider/mocks'; +import { createNowProviderMock } from '../common/now_provider/mocks'; import { dataViewPluginMocks } from './data_views/mocks'; export type Setup = jest.Mocked>; diff --git a/src/plugins/data/public/plugin.ts b/src/plugins/data/public/plugin.ts index 32e321bd98a43..37446fb81ed10 100644 --- a/src/plugins/data/public/plugin.ts +++ b/src/plugins/data/public/plugin.ts @@ -40,8 +40,8 @@ import { } from './actions'; import { applyFilterTrigger } from './triggers'; import { getTableViewDescription } from './utils/table_inspector_view'; -import { NowProvider, NowProviderInternalContract } from './now_provider'; -import { getAggsFormats, DatatableUtilitiesService } from '../common'; +import type { NowProviderInternalContract } from '../common/now_provider'; +import { NowProvider, getAggsFormats, DatatableUtilitiesService } from '../common'; export class DataPublicPlugin implements diff --git a/src/plugins/data/public/query/query_service.test.ts b/src/plugins/data/public/query/query_service.test.ts index 6ddde2d18f74f..3b8176f653354 100644 --- a/src/plugins/data/public/query/query_service.test.ts +++ b/src/plugins/data/public/query/query_service.test.ts @@ -16,7 +16,7 @@ import { Storage } from '@kbn/kibana-utils-plugin/public'; import { QueryService, QueryStart } from './query_service'; import { StubBrowserStorage } from '@kbn/test-jest-helpers'; import { TimefilterContract } from './timefilter'; -import { createNowProviderMock } from '../now_provider/mocks'; +import { createNowProviderMock } from '../../common/now_provider/mocks'; const setupMock = coreMock.createSetup(); const startMock = coreMock.createStart(); diff --git a/src/plugins/data/public/query/query_service.ts b/src/plugins/data/public/query/query_service.ts index 894db102aa041..3d5a0590a9f42 100644 --- a/src/plugins/data/public/query/query_service.ts +++ b/src/plugins/data/public/query/query_service.ts @@ -26,7 +26,7 @@ import type { QueryStringContract } from './query_string'; import { QueryStringManager } from './query_string'; import { getEsQueryConfig } from '../../common'; import { getUiSettings } from '../services'; -import { NowProviderInternalContract } from '../now_provider'; +import { NowProviderInternalContract } from '../../common/now_provider'; import { extract, getAllMigrations, diff --git a/src/plugins/data/public/query/state_sync/connect_to_query_state.test.ts b/src/plugins/data/public/query/state_sync/connect_to_query_state.test.ts index 515cc38783cbd..2ad6dc2388900 100644 --- a/src/plugins/data/public/query/state_sync/connect_to_query_state.test.ts +++ b/src/plugins/data/public/query/state_sync/connect_to_query_state.test.ts @@ -18,7 +18,7 @@ import { StubBrowserStorage } from '@kbn/test-jest-helpers'; import { connectToQueryState } from './connect_to_query_state'; import { TimefilterContract } from '../timefilter'; import { QueryState } from '../query_state'; -import { createNowProviderMock } from '../../now_provider/mocks'; +import { createNowProviderMock } from '../../../common/now_provider/mocks'; const connectToQueryGlobalState = (query: QueryStart, state: BaseStateContainer) => connectToQueryState(query, state, { diff --git a/src/plugins/data/public/query/state_sync/sync_state_with_url.test.ts b/src/plugins/data/public/query/state_sync/sync_state_with_url.test.ts index 10cbe1bd25c5b..527856ed820d2 100644 --- a/src/plugins/data/public/query/state_sync/sync_state_with_url.test.ts +++ b/src/plugins/data/public/query/state_sync/sync_state_with_url.test.ts @@ -23,7 +23,7 @@ import { StubBrowserStorage } from '@kbn/test-jest-helpers'; import { TimefilterContract } from '../timefilter'; import { syncQueryStateWithUrl } from './sync_state_with_url'; import { GlobalQueryStateFromUrl } from './types'; -import { createNowProviderMock } from '../../now_provider/mocks'; +import { createNowProviderMock } from '../../../common/now_provider/mocks'; const setupMock = coreMock.createSetup(); const startMock = coreMock.createStart(); diff --git a/src/plugins/data/public/query/timefilter/timefilter.test.ts b/src/plugins/data/public/query/timefilter/timefilter.test.ts index 1a4023644ed43..128fe43d5306b 100644 --- a/src/plugins/data/public/query/timefilter/timefilter.test.ts +++ b/src/plugins/data/public/query/timefilter/timefilter.test.ts @@ -14,7 +14,7 @@ import { TimeRange } from '@kbn/es-query'; import { AutoRefreshDoneFn, Timefilter } from './timefilter'; import { Subscription } from 'rxjs'; import { RefreshInterval } from '../../../common'; -import { createNowProviderMock } from '../../now_provider/mocks'; +import { createNowProviderMock } from '../../../common/now_provider/mocks'; import { timefilterServiceMock } from './timefilter_service.mock'; const timefilterSetupMock = timefilterServiceMock.createSetupContract(); diff --git a/src/plugins/data/public/query/timefilter/timefilter.ts b/src/plugins/data/public/query/timefilter/timefilter.ts index 11c44bd296640..dc549800cdcb9 100644 --- a/src/plugins/data/public/query/timefilter/timefilter.ts +++ b/src/plugins/data/public/query/timefilter/timefilter.ts @@ -14,7 +14,7 @@ import { TimeRange } from '@kbn/es-query'; import type { DataView } from '@kbn/data-views-plugin/common'; import { areRefreshIntervalsDifferent, areTimeRangesDifferent } from './lib/diff_time_picker_vals'; import type { TimefilterConfig, InputTimeRange, TimeRangeBounds } from './types'; -import { NowProviderInternalContract } from '../../now_provider'; +import { NowProviderInternalContract } from '../../../common/now_provider'; import { calculateBounds, getAbsoluteTimeRange, diff --git a/src/plugins/data/public/query/timefilter/timefilter_service.ts b/src/plugins/data/public/query/timefilter/timefilter_service.ts index a8c9b7a759e9e..cf44931b3bb22 100644 --- a/src/plugins/data/public/query/timefilter/timefilter_service.ts +++ b/src/plugins/data/public/query/timefilter/timefilter_service.ts @@ -10,7 +10,7 @@ import { IUiSettingsClient } from '@kbn/core/public'; import { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import { TimeHistory, Timefilter, TimeHistoryContract, TimefilterContract } from '.'; import { UI_SETTINGS } from '../../../common'; -import { NowProviderInternalContract } from '../../now_provider'; +import { NowProviderInternalContract } from '../../../common/now_provider'; /** * Filter Service diff --git a/src/plugins/data/public/search/aggs/aggs_service.test.ts b/src/plugins/data/public/search/aggs/aggs_service.test.ts index cbd0cbc40eb03..7ee7a33659453 100644 --- a/src/plugins/data/public/search/aggs/aggs_service.test.ts +++ b/src/plugins/data/public/search/aggs/aggs_service.test.ts @@ -20,7 +20,7 @@ import { AggsStartDependencies, createGetConfig, } from './aggs_service'; -import { createNowProviderMock } from '../../now_provider/mocks'; +import { createNowProviderMock } from '../../../common/now_provider/mocks'; const { uiSettings } = coreMock.createSetup(); diff --git a/src/plugins/data/public/search/aggs/aggs_service.ts b/src/plugins/data/public/search/aggs/aggs_service.ts index 5a830b6cc154c..9b438f774da39 100644 --- a/src/plugins/data/public/search/aggs/aggs_service.ts +++ b/src/plugins/data/public/search/aggs/aggs_service.ts @@ -19,7 +19,7 @@ import { } from '../../../common/search/aggs'; import { calculateBounds, TimeRange } from '../../../common'; import type { AggsSetup, AggsStart } from './types'; -import type { NowProviderInternalContract } from '../../now_provider'; +import type { NowProviderInternalContract } from '../../../common/now_provider'; /** * Aggs needs synchronous access to specific uiSettings. Since settings can change diff --git a/src/plugins/data/public/search/search_service.ts b/src/plugins/data/public/search/search_service.ts index f336096468606..584dbaf2ed4f7 100644 --- a/src/plugins/data/public/search/search_service.ts +++ b/src/plugins/data/public/search/search_service.ts @@ -62,7 +62,7 @@ import { } from '../../common/search/aggs/buckets/shard_delay'; import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn'; import { ConfigSchema } from '../../config'; -import { NowProviderInternalContract } from '../now_provider'; +import { NowProviderInternalContract } from '../../common/now_provider'; import { DataPublicPluginStart, DataStartDependencies } from '../types'; import { AggsService } from './aggs'; import { createUsageCollector, SearchUsageCollector } from './collectors'; diff --git a/src/plugins/data/public/search/session/session_helpers.test.ts b/src/plugins/data/public/search/session/session_helpers.test.ts index 96187bdfa1c75..449746be6a46a 100644 --- a/src/plugins/data/public/search/session/session_helpers.test.ts +++ b/src/plugins/data/public/search/session/session_helpers.test.ts @@ -11,9 +11,9 @@ import { ISessionService, SessionService } from './session_service'; import { BehaviorSubject } from 'rxjs'; import { fakeSchedulers } from 'rxjs-marbles/jest'; import { SearchSessionState } from './search_session_state'; -import { NowProviderInternalContract } from '../../now_provider'; +import { NowProviderInternalContract } from '../../../common/now_provider'; import { coreMock } from '@kbn/core/public/mocks'; -import { createNowProviderMock } from '../../now_provider/mocks'; +import { createNowProviderMock } from '../../../common/now_provider/mocks'; import { SEARCH_SESSIONS_MANAGEMENT_ID } from './constants'; import { getSessionsClientMock } from './mocks'; diff --git a/src/plugins/data/public/search/session/session_service.test.ts b/src/plugins/data/public/search/session/session_service.test.ts index d74d4d9d93664..01b3d5b346a55 100644 --- a/src/plugins/data/public/search/session/session_service.test.ts +++ b/src/plugins/data/public/search/session/session_service.test.ts @@ -12,8 +12,8 @@ import { first, take, toArray } from 'rxjs/operators'; import { getSessionsClientMock } from './mocks'; import { BehaviorSubject } from 'rxjs'; import { SearchSessionState } from './search_session_state'; -import { createNowProviderMock } from '../../now_provider/mocks'; -import { NowProviderInternalContract } from '../../now_provider'; +import { createNowProviderMock } from '../../../common/now_provider/mocks'; +import { NowProviderInternalContract } from '../../../common/now_provider'; import { SEARCH_SESSIONS_MANAGEMENT_ID } from './constants'; import type { ISessionsClient, SearchSessionSavedObject } from './sessions_client'; import { CoreStart } from '@kbn/core/public'; diff --git a/src/plugins/data/public/search/session/session_service.ts b/src/plugins/data/public/search/session/session_service.ts index 70c343651e611..a9557dba8c906 100644 --- a/src/plugins/data/public/search/session/session_service.ts +++ b/src/plugins/data/public/search/session/session_service.ts @@ -51,7 +51,7 @@ import { } from './search_session_state'; import { ISessionsClient } from './sessions_client'; import { ISearchOptions } from '../../../common'; -import { NowProviderInternalContract } from '../../now_provider'; +import { NowProviderInternalContract } from '../../../common/now_provider'; import { SEARCH_SESSIONS_MANAGEMENT_ID } from './constants'; import { formatSessionName } from './lib/session_name_formatter'; diff --git a/src/plugins/data/public/types.ts b/src/plugins/data/public/types.ts index 538f0412f2f8c..1421b4751c9d5 100644 --- a/src/plugins/data/public/types.ts +++ b/src/plugins/data/public/types.ts @@ -28,7 +28,7 @@ import { import type { ISearchSetup, ISearchStart } from './search'; import { QuerySetup, QueryStart } from './query'; import { DataViewsContract } from './data_views'; -import { NowProviderPublicContract } from './now_provider'; +import { NowProviderPublicContract } from '../common/now_provider'; export interface DataSetupDependencies { bfetch: BfetchPublicSetup; diff --git a/src/plugins/event_annotation/common/event_annotation_group/index.ts b/src/plugins/event_annotation/common/event_annotation_group/index.ts index 1eae918657185..08b1c719d6452 100644 --- a/src/plugins/event_annotation/common/event_annotation_group/index.ts +++ b/src/plugins/event_annotation/common/event_annotation_group/index.ts @@ -8,8 +8,13 @@ import type { ExpressionFunctionDefinition } from '@kbn/expressions-plugin/common'; import { i18n } from '@kbn/i18n'; -import { IndexPatternExpressionType } from '@kbn/data-views-plugin/common'; +import { + DataViewPersistableStateService, + IndexPatternExpressionType, +} from '@kbn/data-views-plugin/common'; +import { EventAnnotationGroupConfig } from '@kbn/event-annotation-common'; import type { EventAnnotationOutput } from '../types'; +import { EventAnnotationGroupSavedObject } from '../content_management'; export interface EventAnnotationGroupOutput { type: 'event_annotation_group'; @@ -79,3 +84,26 @@ export function eventAnnotationGroup(): ExpressionFunctionDefinition< }, }; } + +export const mapSavedObjectToGroupConfig = ( + savedObject: EventAnnotationGroupSavedObject +): EventAnnotationGroupConfig => { + const adHocDataViewSpec = savedObject.attributes.dataViewSpec + ? DataViewPersistableStateService.inject( + savedObject.attributes.dataViewSpec, + savedObject.references + ) + : undefined; + + return { + title: savedObject.attributes.title, + description: savedObject.attributes.description, + tags: savedObject.references.filter((ref) => ref.type === 'tag').map(({ id }) => id), + ignoreGlobalFilters: savedObject.attributes.ignoreGlobalFilters, + indexPatternId: adHocDataViewSpec + ? adHocDataViewSpec.id! + : savedObject.references.find((ref) => ref.type === 'index-pattern')?.id!, + annotations: savedObject.attributes.annotations, + dataViewSpec: adHocDataViewSpec, + }; +}; diff --git a/src/plugins/event_annotation/common/index.ts b/src/plugins/event_annotation/common/index.ts index f163108976ef6..d3ffc80d14058 100644 --- a/src/plugins/event_annotation/common/index.ts +++ b/src/plugins/event_annotation/common/index.ts @@ -20,7 +20,7 @@ export type { } from './query_point_event_annotation/types'; export { manualPointEventAnnotation, manualRangeEventAnnotation } from './manual_event_annotation'; export { queryPointEventAnnotation } from './query_point_event_annotation'; -export { eventAnnotationGroup } from './event_annotation_group'; +export { eventAnnotationGroup, mapSavedObjectToGroupConfig } from './event_annotation_group'; export type { EventAnnotationGroupArgs } from './event_annotation_group'; export type { FetchEventAnnotationsArgs } from './fetch_event_annotations/types'; diff --git a/src/plugins/event_annotation/public/event_annotation_service/service.tsx b/src/plugins/event_annotation/public/event_annotation_service/service.tsx index 4003ea524b291..84f33b37c8cc7 100644 --- a/src/plugins/event_annotation/public/event_annotation_service/service.tsx +++ b/src/plugins/event_annotation/public/event_annotation_service/service.tsx @@ -24,6 +24,7 @@ import { type EventAnnotationConfig, type EventAnnotationGroupConfig, } from '@kbn/event-annotation-common'; +import { mapSavedObjectToGroupConfig } from '../../common'; import { EventAnnotationGroupSavedObjectFinder } from '../components/event_annotation_group_saved_object_finder'; import { CONTENT_ID } from '../../common/content_management'; import type { @@ -51,29 +52,6 @@ export function getEventAnnotationService( ): EventAnnotationServiceType { const client = contentManagement.client; - const mapSavedObjectToGroupConfig = ( - savedObject: EventAnnotationGroupSavedObject - ): EventAnnotationGroupConfig => { - const adHocDataViewSpec = savedObject.attributes.dataViewSpec - ? DataViewPersistableStateService.inject( - savedObject.attributes.dataViewSpec, - savedObject.references - ) - : undefined; - - return { - title: savedObject.attributes.title, - description: savedObject.attributes.description, - tags: savedObject.references.filter((ref) => ref.type === 'tag').map(({ id }) => id), - ignoreGlobalFilters: savedObject.attributes.ignoreGlobalFilters, - indexPatternId: adHocDataViewSpec - ? adHocDataViewSpec.id! - : savedObject.references.find((ref) => ref.type === 'index-pattern')?.id!, - annotations: savedObject.attributes.annotations, - dataViewSpec: adHocDataViewSpec, - }; - }; - const mapSavedObjectToGroupContent = ( savedObject: EventAnnotationGroupSavedObject ): EventAnnotationGroupContent => { diff --git a/x-pack/plugins/lens/public/data_views_service/loader.test.ts b/x-pack/plugins/lens/common/data_views_service/loader.test.ts similarity index 100% rename from x-pack/plugins/lens/public/data_views_service/loader.test.ts rename to x-pack/plugins/lens/common/data_views_service/loader.test.ts diff --git a/x-pack/plugins/lens/public/data_views_service/loader.ts b/x-pack/plugins/lens/common/data_views_service/loader.ts similarity index 93% rename from x-pack/plugins/lens/public/data_views_service/loader.ts rename to x-pack/plugins/lens/common/data_views_service/loader.ts index 784c97d832e34..9dfd8501059f6 100644 --- a/x-pack/plugins/lens/public/data_views_service/loader.ts +++ b/x-pack/plugins/lens/common/data_views_service/loader.ts @@ -5,16 +5,26 @@ * 2.0. */ -import { isFieldLensCompatible } from '@kbn/visualization-ui-components'; +// import { isFieldLensCompatible } from '@kbn/visualization-ui-components'; +const isFieldLensCompatible = () => true; import type { DataViewsContract, DataView, DataViewSpec } from '@kbn/data-views-plugin/public'; import { keyBy } from 'lodash'; -import { IndexPattern, IndexPatternField, IndexPatternMap, IndexPatternRef } from '../types'; -import { documentField } from '../datasources/form_based/document_field'; -import { sortDataViewRefs } from '../utils'; +import type { + IndexPattern, + IndexPatternField, + IndexPatternMap, + IndexPatternRef, +} from '../../public/types'; +import { documentField } from '../document_field'; type ErrorHandler = (err: Error) => void; type MinimalDataViewsContract = Pick; +export const sortDataViewRefs = (dataViewRefs: IndexPatternRef[]) => + dataViewRefs.sort((a, b) => { + return a.title.localeCompare(b.title); + }); + /** * All these functions will be used by the Embeddable instance too, * therefore keep all these functions pretty raw here and do not use the IndexPatternService diff --git a/x-pack/plugins/lens/public/data_views_service/mocks.ts b/x-pack/plugins/lens/common/data_views_service/mocks.ts similarity index 100% rename from x-pack/plugins/lens/public/data_views_service/mocks.ts rename to x-pack/plugins/lens/common/data_views_service/mocks.ts diff --git a/x-pack/plugins/lens/public/data_views_service/service.ts b/x-pack/plugins/lens/common/data_views_service/service.ts similarity index 84% rename from x-pack/plugins/lens/public/data_views_service/service.ts rename to x-pack/plugins/lens/common/data_views_service/service.ts index 217d621b0ee98..8c2853ccc9016 100644 --- a/x-pack/plugins/lens/public/data_views_service/service.ts +++ b/x-pack/plugins/lens/common/data_views_service/service.ts @@ -8,14 +8,14 @@ import type { DataViewsContract, DataView, DataViewSpec } from '@kbn/data-views-plugin/public'; import type { CoreStart } from '@kbn/core/public'; import { i18n } from '@kbn/i18n'; -import { ActionExecutionContext, UiActionsStart } from '@kbn/ui-actions-plugin/public'; -import { - UPDATE_FILTER_REFERENCES_ACTION, - UPDATE_FILTER_REFERENCES_TRIGGER, -} from '@kbn/unified-search-plugin/public'; -import type { IndexPattern, IndexPatternMap } from '../types'; +import type { UiActionsStart } from '@kbn/ui-actions-plugin/public'; +// import { +// UPDATE_FILTER_REFERENCES_ACTION, +// UPDATE_FILTER_REFERENCES_TRIGGER, +// } from '@kbn/unified-search-plugin/public'; +import type { DataViewsState } from '../../public/state_management'; +import type { IndexPattern, IndexPatternMap } from '../../public/types'; import { ensureIndexPattern, loadIndexPatterns } from './loader'; -import type { DataViewsState } from '../state_management'; import { generateId } from '../id_generator'; export interface IndexPatternServiceProps { @@ -114,15 +114,15 @@ export const createIndexPatternService = ({ replaceIndexPattern(loadedPatterns[newDataView.id!], dataView.id!, { applyImmediately: true, }); - const trigger = uiActions.getTrigger(UPDATE_FILTER_REFERENCES_TRIGGER); - const action = uiActions.getAction(UPDATE_FILTER_REFERENCES_ACTION); + // const trigger = uiActions.getTrigger(UPDATE_FILTER_REFERENCES_TRIGGER); + // const action = uiActions.getAction(UPDATE_FILTER_REFERENCES_ACTION); - action?.execute({ - trigger, - fromDataView: dataView.id, - toDataView: newDataView.id, - usedDataViews: [], - } as ActionExecutionContext); + // action?.execute({ + // trigger, + // fromDataView: dataView.id, + // toDataView: newDataView.id, + // usedDataViews: [], + // } as ActionExecutionContext); }, ensureIndexPattern: (args) => ensureIndexPattern({ onError: showLoadingDataViewError, dataViews, ...args }), diff --git a/x-pack/plugins/lens/public/datasources/form_based/dedupe_aggs.test.ts b/x-pack/plugins/lens/common/datasources/form_based/dedupe_aggs.test.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/dedupe_aggs.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/dedupe_aggs.test.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/dedupe_aggs.ts b/x-pack/plugins/lens/common/datasources/form_based/dedupe_aggs.ts similarity index 88% rename from x-pack/plugins/lens/public/datasources/form_based/dedupe_aggs.ts rename to x-pack/plugins/lens/common/datasources/form_based/dedupe_aggs.ts index 575ff93d36311..5ff7ef8b9713f 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dedupe_aggs.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/dedupe_aggs.ts @@ -5,14 +5,17 @@ * 2.0. */ -import { AggFunctionsMapping } from '@kbn/data-plugin/public'; +import { AggFunctionsMapping } from '@kbn/data-plugin/common'; import { ExpressionAstExpressionBuilder, ExpressionAstFunctionBuilder, } from '@kbn/expressions-plugin/common'; -import { GenericOperationDefinition } from './operations'; -import { groupByKey } from './operations/definitions/get_group_by_key'; -import { extractAggId, OriginalColumn } from './to_expression'; +import type { GenericOperationDefinition } from './operations'; +import { groupByKey } from './get_group_by_key'; +import type { OriginalColumn } from './to_expression'; + +// esAggs column ID manipulation functions +export const extractAggId = (id: string) => id.split('.')[0].split('-')[2]; /** * Consolidates duplicate agg expression builders to increase performance diff --git a/x-pack/plugins/lens/common/datasources/form_based/form_based.ts b/x-pack/plugins/lens/common/datasources/form_based/form_based.ts new file mode 100644 index 0000000000000..ab9e59ccbf12b --- /dev/null +++ b/x-pack/plugins/lens/common/datasources/form_based/form_based.ts @@ -0,0 +1,59 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { SavedObjectReference } from '@kbn/core-saved-objects-common'; +import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; +import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public'; +import type { VisualizeEditorContext } from '../../../public/types'; +import type { + FormBasedPersistedState, + FormBasedPrivateState, +} from '../../../public/datasources/form_based/types'; +import type { DatasourceCommon, IndexPattern, IndexPatternRef } from '../../types'; +import { loadInitialState } from './loader'; +import { toExpression } from './to_expression'; + +export function getCommonFormBasedDatasource({ + defaultIndexPatternId, + storage, +}: { + defaultIndexPatternId?: string; + storage?: IStorageWrapper; +}) { + const DATASOURCE_ID = 'formBased'; + + const formBasedDatasource: DatasourceCommon = { + id: DATASOURCE_ID, + + initialize( + persistedState?: FormBasedPersistedState, + references?: SavedObjectReference[], + initialContext?: VisualizeFieldContext | VisualizeEditorContext, + indexPatternRefs?: IndexPatternRef[], + indexPatterns?: Record + ) { + return loadInitialState({ + persistedState, + references, + defaultIndexPatternId, + storage, + initialContext, + indexPatternRefs, + indexPatterns, + }); + }, + + getLayers(state: FormBasedPrivateState) { + return Object.keys(state?.layers); + }, + + toExpression: (state, layerId, indexPatterns, dateRange, nowInstant, searchSessionId) => + toExpression(state, layerId, indexPatterns, dateRange, nowInstant, searchSessionId), + }; + + return formBasedDatasource; +} diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/get_group_by_key.ts b/x-pack/plugins/lens/common/datasources/form_based/get_group_by_key.ts similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/get_group_by_key.ts rename to x-pack/plugins/lens/common/datasources/form_based/get_group_by_key.ts index 059edca36f58b..3383d7acb9856 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/get_group_by_key.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/get_group_by_key.ts @@ -5,17 +5,17 @@ * 2.0. */ -import { +import type { AggFunctionsMapping, ExpressionFunctionKql, ExpressionFunctionLucene, -} from '@kbn/data-plugin/public'; -import { +} from '@kbn/data-plugin/common'; +import type { AnyExpressionFunctionDefinition, ExpressionAstExpressionBuilder, ExpressionAstFunctionBuilder, } from '@kbn/expressions-plugin/common'; -import { Primitive } from 'utility-types'; +import type { Primitive } from 'utility-types'; export function groupByKey( items: T[], diff --git a/x-pack/plugins/lens/common/datasources/form_based/loader.ts b/x-pack/plugins/lens/common/datasources/form_based/loader.ts new file mode 100644 index 0000000000000..11d48fdfd091b --- /dev/null +++ b/x-pack/plugins/lens/common/datasources/form_based/loader.ts @@ -0,0 +1,159 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { SavedObjectReference } from '@kbn/core-saved-objects-common'; +import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; +import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public'; +import { difference, uniq } from 'lodash'; +import type { FormBasedPersistedState } from '../../../public'; +import type { + FormBasedLayer, + FormBasedPrivateState, +} from '../../../public/datasources/form_based/types'; +import type { VisualizeEditorContext } from '../../../public/types'; +import { readFromStorage, writeToStorage } from '../../settings_storage'; +import type { IndexPattern, IndexPatternRef } from '../../types'; + +export function loadInitialState({ + persistedState, + references, + defaultIndexPatternId, + storage, + initialContext, + indexPatternRefs = [], + indexPatterns = {}, +}: { + persistedState?: FormBasedPersistedState; + references?: SavedObjectReference[]; + defaultIndexPatternId?: string; + storage?: IStorageWrapper; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + indexPatternRefs?: IndexPatternRef[]; + indexPatterns?: Record; +}): FormBasedPrivateState { + const state = createStateFromPersisted({ persistedState, references }); + const { usedPatterns, allIndexPatternIds: indexPatternIds } = getUsedIndexPatterns({ + state, + defaultIndexPatternId, + storage, + initialContext, + indexPatternRefs, + }); + + const availableIndexPatterns = new Set(indexPatternRefs.map(({ id }: IndexPatternRef) => id)); + + const notUsedPatterns: string[] = difference([...availableIndexPatterns], usedPatterns); + + // Priority list: + // * start with the indexPattern in context + // * then fallback to the used ones + // * then as last resort use a first one from not used refs + const currentIndexPatternId = [...indexPatternIds, ...usedPatterns, ...notUsedPatterns].find( + (id) => id != null && availableIndexPatterns.has(id) && indexPatterns[id] + ); + + if (currentIndexPatternId) { + setLastUsedIndexPatternId(currentIndexPatternId, storage); + } + + return { + layers: {}, + ...state, + currentIndexPatternId, + }; +} + +function createStateFromPersisted({ + persistedState, + references, +}: { + persistedState?: FormBasedPersistedState; + references?: SavedObjectReference[]; +}) { + return persistedState && references ? injectReferences(persistedState, references) : undefined; +} + +export function injectReferences( + state: FormBasedPersistedState, + references: SavedObjectReference[] +) { + const layers: Record = {}; + Object.entries(state.layers).forEach(([layerId, persistedLayer]) => { + const indexPatternId = references.find( + ({ name }) => name === getLayerReferenceName(layerId) + )?.id; + + if (indexPatternId) { + layers[layerId] = { + ...persistedLayer, + indexPatternId, + }; + } + }); + return { + layers, + }; +} + +export function getLayerReferenceName(layerId: string) { + return `indexpattern-datasource-layer-${layerId}`; +} + +const getLastUsedIndexPatternId = ( + indexPatternRefs: IndexPatternRef[], + storage?: IStorageWrapper +) => { + if (!storage) { + return undefined; + } + const indexPattern = readFromStorage(storage, 'indexPatternId'); + return indexPattern && indexPatternRefs.find((i) => i.id === indexPattern)?.id; +}; + +export const setLastUsedIndexPatternId = (value: string, storage?: IStorageWrapper) => { + if (!storage) return; + writeToStorage(storage, 'indexPatternId', value); +}; + +function getUsedIndexPatterns({ + state, + indexPatternRefs, + storage, + initialContext, + defaultIndexPatternId, +}: { + state?: { + layers: Record; + }; + defaultIndexPatternId?: string; + storage?: IStorageWrapper; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + indexPatternRefs: IndexPatternRef[]; +}) { + const lastUsedIndexPatternId = getLastUsedIndexPatternId(indexPatternRefs, storage); + const fallbackId = lastUsedIndexPatternId || defaultIndexPatternId || indexPatternRefs[0]?.id; + const indexPatternIds = []; + if (initialContext) { + if ('isVisualizeAction' in initialContext) { + indexPatternIds.push(...initialContext.indexPatternIds); + } else { + indexPatternIds.push(initialContext.dataViewSpec.id!); + } + } + const usedPatterns = ( + initialContext + ? indexPatternIds + : uniq(state ? Object.values(state.layers).map((l) => l.indexPatternId) : [fallbackId]) + ) + // take out the undefined from the list + .filter(Boolean); + + return { + usedPatterns, + allIndexPatternIds: indexPatternIds, + }; +} diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/__mocks__/index.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/__mocks__/index.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/__mocks__/index.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/__mocks__/index.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions.test.ts similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions.test.ts index 3dc58b7f1ef6c..55df563eb9226 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions.test.ts @@ -16,10 +16,10 @@ import { AvgIndexPatternColumn, DerivativeIndexPatternColumn, } from './definitions'; -import { getFieldByNameFactory } from '../pure_helpers'; +import { getFieldByNameFactory } from '../../../../public/datasources/form_based/pure_helpers'; import { documentField } from '../document_field'; -import { FormBasedLayer } from '../types'; -import { IndexPattern, IndexPatternField } from '../../../types'; +import { FormBasedLayer } from '../../../../public/datasources/form_based/types'; +import { IndexPattern, IndexPatternField } from '../../../../public/types'; import { GenericIndexPatternColumn } from '.'; import { DateHistogramIndexPatternColumn } from './definitions/date_histogram'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/__snapshots__/percentile.test.tsx.snap b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/__snapshots__/percentile.test.tsx.snap similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/__snapshots__/percentile.test.tsx.snap rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/__snapshots__/percentile.test.tsx.snap diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/counter_rate.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/counter_rate.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/counter_rate.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/counter_rate.tsx index a35669c01ca33..46358fe60a05f 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/counter_rate.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/counter_rate.tsx @@ -7,7 +7,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types'; -import { FormBasedLayer } from '../../../types'; +import { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { buildLabelFunction, getErrorsForDateReference, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/cumulative_sum.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/cumulative_sum.tsx similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/cumulative_sum.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/cumulative_sum.tsx index 916ecd6f965e6..fe47de3aa3a56 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/cumulative_sum.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/cumulative_sum.tsx @@ -7,7 +7,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types'; -import { FormBasedLayer } from '../../../types'; +import { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { checkForDateHistogram, getErrorsForDateReference, @@ -18,7 +18,7 @@ import { } from './utils'; import { OperationDefinition } from '..'; import { getFormatFromPreviousColumn, getFilter } from '../helpers'; -import { DOCUMENT_FIELD_NAME } from '../../../../../../common/constants'; +import { DOCUMENT_FIELD_NAME } from '../../../../../constants'; const ofName = buildLabelFunction((name?: string) => { return i18n.translate('xpack.lens.indexPattern.cumulativeSumOf', { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/differences.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/differences.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/differences.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/differences.tsx index 9f592be860d77..07d746f25015a 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/differences.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/differences.tsx @@ -7,7 +7,7 @@ import { i18n } from '@kbn/i18n'; import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types'; -import { FormBasedLayer } from '../../../types'; +import { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { buildLabelFunction, checkForDateHistogram, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/index.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/index.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/index.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/index.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/moving_average.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/moving_average.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/moving_average.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/moving_average.tsx index 2594cc558f026..9b7ae39020d4c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/moving_average.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/moving_average.tsx @@ -9,9 +9,9 @@ import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n-react'; import React, { useState } from 'react'; import { EuiFieldNumber, EuiFormRow } from '@elastic/eui'; -import { useDebounceWithOptions } from '../../../../../shared_components'; +import { useDebounceWithOptions } from '../../../../../../public/shared_components'; import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types'; -import { FormBasedLayer } from '../../../types'; +import { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { buildLabelFunction, checkForDateHistogram, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/overall_metric.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/overall_metric.test.ts similarity index 87% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/overall_metric.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/overall_metric.test.ts index 901600e90f034..6abbf17929868 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/overall_metric.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/overall_metric.test.ts @@ -5,8 +5,8 @@ * 2.0. */ -import { createMockedIndexPattern } from '../../../mocks'; -import type { FormBasedLayer } from '../../../types'; +import { createMockedIndexPattern } from '../../../../../../public/datasources/form_based/mocks'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { overallAverageOperation, overallMaxOperation, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/overall_metric.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/overall_metric.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/overall_metric.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/overall_metric.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/time_scale.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/time_scale.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/time_scale.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/time_scale.tsx index 5915941938737..755fba8164339 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/time_scale.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/time_scale.tsx @@ -13,7 +13,7 @@ import type { import { getErrorsForDateReference } from './utils'; import type { OperationDefinition } from '..'; import { combineErrorMessages, getFormatFromPreviousColumn } from '../helpers'; -import { FormBasedLayer } from '../../../types'; +import { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; type OverallMetricIndexPatternColumn = FormattedIndexPatternColumn & ReferenceBasedIndexPatternColumn & { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/utils.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/utils.test.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/utils.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/utils.test.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/utils.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/utils.ts similarity index 94% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/utils.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/utils.ts index 99173e5064cb5..21ae92249ad10 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/calculations/utils.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/calculations/utils.ts @@ -9,16 +9,16 @@ import { i18n } from '@kbn/i18n'; import type { AstFunction } from '@kbn/interpreter'; import memoizeOne from 'memoize-one'; import { LayerTypes } from '@kbn/expression-xy-plugin/public'; -import type { IndexPattern } from '../../../../../types'; -import { LayerType } from '../../../../../../common/types'; -import type { TimeScaleUnit } from '../../../../../../common/expressions'; -import type { FormBasedLayer } from '../../../types'; +import type { IndexPattern } from '../../../../../../public/types'; +import { LayerType } from '../../../../../types'; +import type { TimeScaleUnit } from '../../../../../expressions'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { adjustTimeScaleLabelSuffix } from '../../time_scale_utils'; import type { ReferenceBasedIndexPatternColumn } from '../column_types'; import { getManagedColumnsFrom, isColumnValidAsReference } from '../../layer_helpers'; import { operationDefinitionMap } from '..'; -import { FieldBasedIndexPatternColumn } from '../../../types'; -import { IndexPatternField } from '../../../../../types'; +import { FieldBasedIndexPatternColumn } from '../../../../../../public/datasources/form_based/types'; +import { IndexPatternField } from '../../../../../../public/types'; export const buildLabelFunction = (ofName: (name?: string) => string) => diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/cardinality.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/cardinality.test.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/cardinality.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/cardinality.test.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/cardinality.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/cardinality.tsx similarity index 97% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/cardinality.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/cardinality.tsx index 172daafed06ac..f2d51f7f5c9b8 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/cardinality.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/cardinality.tsx @@ -24,8 +24,8 @@ import { } from './helpers'; import { adjustTimeScaleLabelSuffix } from '../time_scale_utils'; import { updateColumnParam } from '../layer_helpers'; -import { getColumnReducedTimeRangeError } from '../../reduced_time_range_utils'; -import { getGroupByKey } from './get_group_by_key'; +import { getColumnReducedTimeRangeError } from '../../../../../public/datasources/form_based/reduced_time_range_utils'; +import { getGroupByKey } from '../../get_group_by_key'; const supportedTypes = new Set([ 'string', diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/column_types.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/column_types.ts similarity index 92% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/column_types.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/column_types.ts index 971f952c73dc6..81d482f224528 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/column_types.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/column_types.ts @@ -6,8 +6,8 @@ */ import type { Query } from '@kbn/es-query'; -import type { Operation } from '../../../../types'; -import type { TimeScaleUnit } from '../../../../../common/expressions'; +import type { Operation } from '../../../../../public/types'; +import type { TimeScaleUnit } from '../../../../expressions'; import type { OperationType } from '.'; export interface BaseIndexPatternColumn extends Operation { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/count.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/count.test.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/count.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/count.test.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/count.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/count.tsx similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/count.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/count.tsx index 142e38a144dbb..8be68f53f426d 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/count.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/count.tsx @@ -11,10 +11,10 @@ import { euiThemeVars } from '@kbn/ui-theme'; import { EuiSwitch, EuiText } from '@elastic/eui'; import { AggFunctionsMapping } from '@kbn/data-plugin/public'; import { buildExpressionFunction } from '@kbn/expressions-plugin/public'; -import { TimeScaleUnit } from '../../../../../common/expressions'; +import { TimeScaleUnit } from '../../../../expressions'; import { OperationDefinition, ParamEditorProps } from '.'; import { FieldBasedIndexPatternColumn, ValueFormatConfig } from './column_types'; -import type { IndexPatternField } from '../../../../types'; +import type { IndexPatternField } from '../../../../../public/types'; import { getInvalidFieldMessage, getFilter, @@ -24,8 +24,8 @@ import { } from './helpers'; import { adjustTimeScaleLabelSuffix } from '../time_scale_utils'; import { updateColumnParam } from '../layer_helpers'; -import { getColumnReducedTimeRangeError } from '../../reduced_time_range_utils'; -import { getGroupByKey } from './get_group_by_key'; +import { getColumnReducedTimeRangeError } from '../../../../../public/datasources/form_based/reduced_time_range_utils'; +import { getGroupByKey } from '../../get_group_by_key'; const countLabel = i18n.translate('xpack.lens.indexPattern.countOf', { defaultMessage: 'Count of records', diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/date_histogram.test.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/date_histogram.test.tsx index fb1c95f57a8f3..7d95450f5dcc7 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/date_histogram.test.tsx @@ -17,10 +17,10 @@ import type { IUiSettingsClient, HttpSetup } from '@kbn/core/public'; import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import { UI_SETTINGS } from '@kbn/data-plugin/public'; import { dataPluginMock, getCalculateAutoTimeExpression } from '@kbn/data-plugin/public/mocks'; -import { createMockedIndexPattern } from '../../mocks'; -import type { FormBasedLayer } from '../../types'; -import type { IndexPattern } from '../../../../types'; -import { getFieldByNameFactory } from '../../pure_helpers'; +import { createMockedIndexPattern } from '../../../../../public/datasources/form_based/mocks'; +import type { FormBasedLayer } from '../../../../../public/datasources/form_based/types'; +import type { IndexPattern } from '../../../../../public/types'; +import { getFieldByNameFactory } from '../../../../../public/datasources/form_based/pure_helpers'; import { act } from 'react-dom/test-utils'; const dataStart = dataPluginMock.createStartContract(); diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/date_histogram.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/date_histogram.tsx index bdc88a7cef452..7be02dec481e9 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/date_histogram.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/date_histogram.tsx @@ -34,7 +34,7 @@ import { updateColumnParam } from '../layer_helpers'; import { OperationDefinition, ParamEditorProps } from '.'; import { FieldBasedIndexPatternColumn } from './column_types'; import { getInvalidFieldMessage, getSafeName } from './helpers'; -import { FormBasedLayer } from '../../types'; +import { FormBasedLayer } from '../../../../../public/datasources/form_based/types'; const { isValidInterval } = search.aggs; const autoInterval = 'auto'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.scss b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.scss similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.scss rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.scss diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.test.tsx similarity index 97% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.test.tsx index 8e7fb9e310f66..a5598040ce68b 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.test.tsx @@ -9,7 +9,7 @@ import React from 'react'; import { shallow, mount } from 'enzyme'; import { act } from 'react-dom/test-utils'; import { EuiPopover, EuiLink } from '@elastic/eui'; -import { createMockedIndexPattern } from '../../../mocks'; +import { createMockedIndexPattern } from '../../../../../../public/datasources/form_based/mocks'; import { FilterPopover } from './filter_popover'; import { LabelInput } from '../shared_components'; import { QueryStringInput } from '@kbn/unified-search-plugin/public'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.tsx similarity index 93% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.tsx index 4e3fe6336be9e..22a8e4e90f06c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filter_popover.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filter_popover.tsx @@ -14,11 +14,11 @@ import type { Query } from '@kbn/es-query'; // import { QueryInput } from '../../../../shared_components/query_input'; import { isQueryValid, QueryInput } from '@kbn/visualization-ui-components'; import { useKibana } from '@kbn/kibana-react-plugin/public'; -import { LENS_APP_NAME } from '../../../../../../common/constants'; -import { IndexPattern } from '../../../../../types'; +import { LENS_APP_NAME } from '../../../../../constants'; +import { IndexPattern } from '../../../../../../public/types'; import { FilterValue, defaultLabel } from '.'; import { LabelInput } from '../shared_components'; -import { LensAppServices } from '../../../../../app_plugin/types'; +import { LensAppServices } from '../../../../../../public/app_plugin/types'; export const FilterPopover = ({ filter, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.scss b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.scss similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.scss rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.scss diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.test.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.test.tsx index ea71550959399..3a0850c813ee6 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.test.tsx @@ -16,8 +16,8 @@ import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; import type { FiltersIndexPatternColumn } from '.'; import { filtersOperation } from '..'; -import type { FormBasedLayer } from '../../../types'; -import { createMockedIndexPattern } from '../../../mocks'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; +import { createMockedIndexPattern } from '../../../../../../public/datasources/form_based/mocks'; import { FilterPopover } from './filter_popover'; const uiSettingsMock = {} as IUiSettingsClient; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.tsx index e514e9d6c08d4..d45bf49d02925 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/filters.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/filters.tsx @@ -20,7 +20,7 @@ import { DraggableBucketContainer, isQueryValid, } from '@kbn/visualization-ui-components'; -import { IndexPattern } from '../../../../../types'; +import { IndexPattern } from '../../../../../../public/types'; import { updateColumnParam } from '../../layer_helpers'; import type { OperationDefinition } from '..'; import type { BaseIndexPatternColumn } from '../column_types'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/index.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/index.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/filters/index.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/filters/index.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/__snapshots__/formula.test.tsx.snap b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/__snapshots__/formula.test.tsx.snap similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/__snapshots__/formula.test.tsx.snap rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/__snapshots__/formula.test.tsx.snap diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/context_variables.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/context_variables.test.ts similarity index 97% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/context_variables.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/context_variables.test.ts index 407f458a11e3e..b2833989210f9 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/context_variables.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/context_variables.test.ts @@ -5,8 +5,8 @@ * 2.0. */ -import type { FormBasedLayer } from '../../../../..'; -import { createMockedIndexPattern } from '../../../mocks'; +import type { FormBasedLayer } from '../../../../../../public'; +import { createMockedIndexPattern } from '../../../../../../public/datasources/form_based/mocks'; import { DateHistogramIndexPatternColumn } from '../date_histogram'; import { ConstantsIndexPatternColumn, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/context_variables.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/context_variables.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/context_variables.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/context_variables.tsx index 4b7b5b94b493b..ed086b1da5e32 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/context_variables.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/context_variables.tsx @@ -13,11 +13,11 @@ import type { DateHistogramIndexPatternColumn, FormBasedLayer, GenericIndexPatternColumn, -} from '../../../../..'; -import type { DateRange } from '../../../../../../common/types'; +} from '../../../../../../public'; +import type { DateRange } from '../../../../../types'; import type { GenericOperationDefinition, OperationDefinition } from '..'; import type { ReferenceBasedIndexPatternColumn } from '../column_types'; -import { IndexPattern } from '../../../../../types'; +import { IndexPattern } from '../../../../../../public/types'; // copied over from layer_helpers // TODO: split layer_helpers util into pure/non-pure functions to avoid issues with tests diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula.scss b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula.scss similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula.scss rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula.scss diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula_editor.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula_editor.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula_editor.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula_editor.tsx index ef726c282a4b9..a58311deddf80 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula_editor.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula_editor.tsx @@ -32,7 +32,7 @@ import classNames from 'classnames'; import { CodeEditor } from '@kbn/kibana-react-plugin/public'; import type { CodeEditorProps } from '@kbn/kibana-react-plugin/public'; import { UI_SETTINGS } from '@kbn/data-plugin/public'; -import { useDebounceWithOptions } from '../../../../../../shared_components'; +import { useDebounceWithOptions } from '../../../../../../../public/shared_components'; import { ParamEditorProps } from '../..'; import { getManagedColumnsFrom } from '../../../layer_helpers'; import { ErrorWrapper, runASTValidation, tryToParse } from '../validation'; @@ -57,7 +57,7 @@ import { insertOrReplaceFormulaColumn } from '../parse'; import { filterByVisibleOperation } from '../util'; import { getColumnTimeShiftWarnings, getDateHistogramInterval } from '../../../../time_shift_utils'; import { getDocumentationSections } from './formula_help'; -import { nonNullable } from '../../../../../../utils'; +import { nonNullable } from '../../../../../../../public/utils'; function tableHasData( activeData: ParamEditorProps['activeData'], diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula_help.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula_help.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula_help.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula_help.tsx index 0911f648077c6..5afe7b850050c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/formula_help.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/formula_help.tsx @@ -9,7 +9,7 @@ import React from 'react'; import { i18n } from '@kbn/i18n'; import { Markdown } from '@kbn/kibana-react-plugin/public'; import { groupBy } from 'lodash'; -import type { IndexPattern } from '../../../../../../types'; +import type { IndexPattern } from '../../../../../../../public/types'; import { tinymathFunctions } from '../util'; import { getPossibleFunctions } from './math_completion'; import { hasFunctionFieldArgument } from '../validation'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/index.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/index.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/index.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/index.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_completion.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_completion.test.ts similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_completion.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_completion.test.ts index 419208ec48d80..e74464def7c96 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_completion.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_completion.test.ts @@ -9,9 +9,9 @@ import { parse } from '@kbn/tinymath'; import { monaco } from '@kbn/monaco'; import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; -import { createMockedIndexPattern } from '../../../../mocks'; +import { createMockedIndexPattern } from '../../../../../../../public/datasources/form_based/mocks'; import { GenericOperationDefinition } from '../..'; -import type { OperationMetadata, IndexPatternField } from '../../../../../../types'; +import type { OperationMetadata, IndexPatternField } from '../../../../../../../public/types'; import { tinymathFunctions } from '../util'; import { getSignatureHelp, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_completion.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_completion.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_completion.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_completion.ts index 2213391235847..83873b2dbc65d 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_completion.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_completion.ts @@ -23,9 +23,9 @@ import type { import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; import { parseTimeShift } from '@kbn/data-plugin/common'; import moment from 'moment'; -import { nonNullable } from '../../../../../../utils'; -import { DateRange } from '../../../../../../../common/types'; -import type { IndexPattern } from '../../../../../../types'; +import { nonNullable } from '../../../../../../../public/utils'; +import { DateRange } from '../../../../../../types'; +import type { IndexPattern } from '../../../../../../../public/types'; import { memoizedGetAvailableOperationsByMetadata } from '../../../operations'; import { tinymathFunctions, groupArgsByType, unquotedStringRegex } from '../util'; import type { GenericOperationDefinition } from '../..'; @@ -35,7 +35,7 @@ import { timeShiftOptions, timeShiftOptionOrder } from '../../../../time_shift_u import { reducedTimeRangeOptionOrder, reducedTimeRangeOptions, -} from '../../../../reduced_time_range_utils'; +} from '../../../../../../../public/datasources/form_based/reduced_time_range_utils'; export enum SUGGESTION_TYPE { FIELD = 'field', diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_tokenization.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_tokenization.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/editor/math_tokenization.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/editor/math_tokenization.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula.test.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula.test.tsx index ea77bc3e429ab..e6393e7029f00 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula.test.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import { createMockedIndexPattern } from '../../../mocks'; +import { createMockedIndexPattern } from '../../../../../../public/datasources/form_based/mocks'; import { formulaOperation, type GenericOperationDefinition, @@ -13,8 +13,8 @@ import { } from '..'; import type { FormulaIndexPatternColumn } from './formula'; import { insertOrReplaceFormulaColumn } from './parse'; -import type { FormBasedLayer } from '../../../types'; -import { IndexPattern } from '../../../../../types'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; +import { IndexPattern } from '../../../../../../public/types'; import { tinymathFunctions } from './util'; import { TermsIndexPatternColumn } from '../terms'; import { MovingAverageIndexPatternColumn } from '../calculations'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula.tsx index 4444c9ee0fbba..60bb5277db63b 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula.tsx @@ -7,14 +7,14 @@ import { i18n } from '@kbn/i18n'; import { uniqBy } from 'lodash'; -import { nonNullable } from '../../../../../utils'; +import { nonNullable } from '../../../../../../public/utils'; import type { BaseIndexPatternColumn, FieldBasedOperationErrorMessage, OperationDefinition, } from '..'; import type { ReferenceBasedIndexPatternColumn } from '../column_types'; -import type { IndexPattern } from '../../../../../types'; +import type { IndexPattern } from '../../../../../../public/types'; import { runASTValidation, tryToParse } from './validation'; import { WrappedFormulaEditor } from './editor'; import { insertOrReplaceFormulaColumn } from './parse'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula_public_api.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula_public_api.test.ts similarity index 97% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula_public_api.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula_public_api.test.ts index 5290056470297..2bf555ff18f62 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula_public_api.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula_public_api.test.ts @@ -9,7 +9,10 @@ import { insertOrReplaceFormulaColumn } from './parse'; import { createFormulaPublicApi, FormulaPublicApi } from './formula_public_api'; import type { DataView } from '@kbn/data-views-plugin/public'; -import type { DateHistogramIndexPatternColumn, PersistedIndexPatternLayer } from '../../../types'; +import type { + DateHistogramIndexPatternColumn, + PersistedIndexPatternLayer, +} from '../../../../../../public/datasources/form_based/types'; import { convertDataViewIntoLensIndexPattern } from '../../../../../data_views_service/loader'; import moment from 'moment'; import type { FormulaIndexPatternColumn } from './formula'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula_public_api.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula_public_api.ts similarity index 90% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula_public_api.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula_public_api.ts index 571a7e890eb08..7567368e3f8dd 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/formula_public_api.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/formula_public_api.ts @@ -7,11 +7,11 @@ import type { DataView } from '@kbn/data-views-plugin/public'; import { Query } from '@kbn/es-query'; -import type { DateRange } from '../../../../../../common/types'; +import type { DateRange } from '../../../../../types'; import { convertDataViewIntoLensIndexPattern } from '../../../../../data_views_service/loader'; -import type { IndexPattern } from '../../../../../types'; -import type { PersistedIndexPatternLayer } from '../../../types'; -import type { TimeScaleUnit } from '../../../../../../common/expressions'; +import type { IndexPattern } from '../../../../../../public/types'; +import type { PersistedIndexPatternLayer } from '../../../../../../public/datasources/form_based/types'; +import type { TimeScaleUnit } from '../../../../../expressions'; import { insertOrReplaceFormulaColumn } from './parse'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/generate.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/generate.ts similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/generate.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/generate.ts index a2f94fb437062..a21c4312b164d 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/generate.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/generate.ts @@ -6,14 +6,14 @@ */ import { isObject } from 'lodash'; -import { DOCUMENT_FIELD_NAME } from '../../../../../../common/constants'; +import { DOCUMENT_FIELD_NAME } from '../../../../../constants'; import { FieldBasedIndexPatternColumn, GenericOperationDefinition, GenericIndexPatternColumn, } from '..'; import { BaseIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types'; -import { FormBasedLayer } from '../../../types'; +import { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { unquotedStringRegex } from './util'; import { isColumnOfType } from '../helpers'; import { StaticValueIndexPatternColumn } from '../static_value'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/index.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/index.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/index.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/index.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math.test.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math.test.ts index 5ca43cb125cc8..ea3c33fe1b8fe 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math.test.ts @@ -6,8 +6,8 @@ */ import { TinymathAST } from '@kbn/tinymath'; -import { IndexPattern } from '../../../../../types'; -import { FormBasedLayer } from '../../../types'; +import { IndexPattern } from '../../../../../../public/types'; +import { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { MathIndexPatternColumn, mathOperation } from './math'; function createLayerWithMathColumn(tinymathAst: string | TinymathAST): FormBasedLayer { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math.tsx index cf8116d66447a..1088faf4435e2 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math.tsx @@ -8,7 +8,7 @@ import type { TinymathAST } from '@kbn/tinymath'; import { OperationDefinition } from '..'; import { ValueFormatConfig, ReferenceBasedIndexPatternColumn } from '../column_types'; -import { IndexPattern } from '../../../../../types'; +import { IndexPattern } from '../../../../../../public/types'; export interface MathIndexPatternColumn extends ReferenceBasedIndexPatternColumn { operationType: 'math'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math_examples.md b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math_examples.md similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/math_examples.md rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/math_examples.md diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/mocks/operation_mocks.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/parse.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/parse.ts similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/parse.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/parse.ts index be98b086ff52c..893b1db6ea29c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/parse.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/parse.ts @@ -8,18 +8,18 @@ import { i18n } from '@kbn/i18n'; import { isObject } from 'lodash'; import type { TinymathAST, TinymathVariable, TinymathLocation } from '@kbn/tinymath'; -import { nonNullable } from '../../../../../utils'; -import type { DateRange } from '../../../../../../common/types'; -import type { IndexPattern } from '../../../../../types'; +import { nonNullable } from '../../../../../../public/utils'; +import type { DateRange } from '../../../../../types'; +import type { IndexPattern } from '../../../../../../public/types'; import { OperationDefinition, GenericOperationDefinition, GenericIndexPatternColumn, operationDefinitionMap, } from '..'; -import type { FormBasedLayer } from '../../../types'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { mathOperation } from './math'; -import { documentField } from '../../../document_field'; +import { documentField } from '../../../../../document_field'; import { runASTValidation, shouldHaveFieldArgument, tryToParse } from './validation'; import { filterByVisibleOperation, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/types.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/types.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/types.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/types.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/util.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/util.ts similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/util.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/util.ts index 6dcc727ec0e70..6cafb36fd7527 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/util.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/util.ts @@ -14,7 +14,7 @@ import type { TinymathVariable, } from '@kbn/tinymath'; import type { Query } from '@kbn/es-query'; -import { nonNullable } from '../../../../../utils'; +import { nonNullable } from '../../../../../../public/utils'; import type { OperationDefinition, GenericIndexPatternColumn, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/validation.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/validation.ts similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/validation.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/validation.ts index 95eb8a84e6849..ae41c00f4a454 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/formula/validation.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/formula/validation.ts @@ -18,8 +18,8 @@ import { REASON_ID_TYPES, validateAbsoluteTimeShift, } from '@kbn/data-plugin/common'; -import { nonNullable } from '../../../../../utils'; -import { DateRange } from '../../../../../../common/types'; +import { nonNullable } from '../../../../../../public/utils'; +import { DateRange } from '../../../../../types'; import { findMathNodes, findVariables, @@ -36,8 +36,8 @@ import type { GenericIndexPatternColumn, GenericOperationDefinition, } from '..'; -import type { FormBasedLayer } from '../../../types'; -import type { IndexPattern } from '../../../../../types'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; +import type { IndexPattern } from '../../../../../../public/types'; import type { TinymathNodeTypes } from './types'; interface ValidationErrors { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/helpers.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/helpers.test.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/helpers.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/helpers.test.ts index 560c1e26c59ae..ed53c39b2a1fc 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/helpers.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/helpers.test.ts @@ -5,8 +5,8 @@ * 2.0. */ -import { createMockedIndexPattern } from '../../mocks'; -import type { FormBasedLayer } from '../../types'; +import { createMockedIndexPattern } from '../../../../../public/datasources/form_based/mocks'; +import type { FormBasedLayer } from '../../../../../public/datasources/form_based/types'; import type { GenericIndexPatternColumn } from './column_types'; import { getInvalidFieldMessage, isValidNumber } from './helpers'; import type { TermsIndexPatternColumn } from './terms'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/helpers.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/helpers.tsx similarity index 97% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/helpers.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/helpers.tsx index 11a4e16a39f44..c247f1b2a7231 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/helpers.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/helpers.tsx @@ -10,19 +10,22 @@ import React, { Fragment } from 'react'; import { FormattedMessage } from '@kbn/i18n-react'; import { isEqual } from 'lodash'; import { Query } from '@kbn/es-query'; -import type { IndexPattern, IndexPatternField } from '../../../../types'; +import { hasField } from '../../pure_utils'; +import { IndexPattern, IndexPatternField } from '../../../../types'; import { type FieldBasedOperationErrorMessage, type GenericIndexPatternColumn, operationDefinitionMap, } from '.'; -import { +import type { FieldBasedIndexPatternColumn, FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn, } from './column_types'; -import type { FormBasedLayer, LastValueIndexPatternColumn } from '../../types'; -import { hasField } from '../../pure_utils'; +import type { + FormBasedLayer, + LastValueIndexPatternColumn, +} from '../../../../../public/datasources/form_based/types'; export function getInvalidFieldMessage( layer: FormBasedLayer, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/index.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/index.ts similarity index 88% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/index.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/index.ts index 3c62f34cbc1f2..96fb0d6b9a113 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/index.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/index.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { IUiSettingsClient, HttpSetup, CoreStart } from '@kbn/core/public'; +import type { IUiSettingsClient, HttpSetup, CoreStart } from '@kbn/core/public'; import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import type { ExpressionAstExpressionBuilder, @@ -14,12 +14,12 @@ import type { import type { DataPublicPluginStart } from '@kbn/data-plugin/public'; import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public'; import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public'; -import { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; +import type { DataViewsService } from '@kbn/data-plugin/common'; import { termsOperation } from './terms'; -import { filtersOperation } from './filters'; -import { cardinalityOperation } from './cardinality'; -import { percentileOperation } from './percentile'; -import { percentileRanksOperation } from './percentile_ranks'; +// import { filtersOperation } from './filters'; +// import { cardinalityOperation } from './cardinality'; +// import { percentileOperation } from './percentile'; +// import { percentileRanksOperation } from './percentile_ranks'; import { minOperation, averageOperation, @@ -28,48 +28,52 @@ import { medianOperation, standardDeviationOperation, } from './metrics'; -import { dateHistogramOperation } from './date_histogram'; -import { - cumulativeSumOperation, - counterRateOperation, - derivativeOperation, - movingAverageOperation, - overallSumOperation, - overallMinOperation, - overallMaxOperation, - overallAverageOperation, - timeScaleOperation, -} from './calculations'; -import { countOperation } from './count'; -import { - mathOperation, - formulaOperation, - timeRangeOperation, - nowOperation, - intervalOperation, -} from './formula'; -import { staticValueOperation } from './static_value'; -import { lastValueOperation } from './last_value'; +// import { dateHistogramOperation } from './date_histogram'; +// import { +// cumulativeSumOperation, +// counterRateOperation, +// derivativeOperation, +// movingAverageOperation, +// overallSumOperation, +// overallMinOperation, +// overallMaxOperation, +// overallAverageOperation, +// timeScaleOperation, +// } from './calculations'; +// import { countOperation } from './count'; +// import { +// mathOperation, +// formulaOperation, +// timeRangeOperation, +// nowOperation, +// intervalOperation, +// } from './formula'; +// import { staticValueOperation } from './static_value'; +// import { lastValueOperation } from './last_value'; +// import { rangeOperation } from './ranges'; import type { FramePublicAPI, - IndexPattern, - IndexPatternField, OperationMetadata, ParamEditorCustomProps, UserMessage, -} from '../../../../types'; +} from '../../../../../public/types'; import type { BaseIndexPatternColumn, IncompleteColumn, GenericIndexPatternColumn, ReferenceBasedIndexPatternColumn, } from './column_types'; -import { DataViewDragDropOperation, FormBasedLayer } from '../../types'; -import { DateRange, LayerType } from '../../../../../common/types'; -import { rangeOperation } from './ranges'; -import { FormBasedDimensionEditorProps, OperationSupportMatrix } from '../../dimension_panel'; +import type { + DataViewDragDropOperation, + FormBasedLayer, +} from '../../../../../public/datasources/form_based/types'; +import type { DateRange, IndexPattern, IndexPatternField, LayerType } from '../../../../types'; +import type { + FormBasedDimensionEditorProps, + OperationSupportMatrix, +} from '../../../../../public/datasources/form_based/dimension_panel'; import type { OriginalColumn } from '../../to_expression'; -import { ReferenceEditorProps } from '../../dimension_panel/reference_editor'; +import type { ReferenceEditorProps } from '../../../../../public/datasources/form_based/dimension_panel/reference_editor'; export type { IncompleteColumn, @@ -119,66 +123,66 @@ export type { StaticValueIndexPatternColumn } from './static_value'; // If you want to implement a new operation, add the definition to this array and // the column type to the `GenericIndexPatternColumn` union type below. const internalOperationDefinitions = [ - filtersOperation, + // filtersOperation, termsOperation, - dateHistogramOperation, + // dateHistogramOperation, minOperation, maxOperation, averageOperation, - cardinalityOperation, + // cardinalityOperation, sumOperation, standardDeviationOperation, medianOperation, - percentileOperation, - percentileRanksOperation, - lastValueOperation, - countOperation, - rangeOperation, - cumulativeSumOperation, - counterRateOperation, - derivativeOperation, - movingAverageOperation, - mathOperation, - formulaOperation, - overallSumOperation, - overallMinOperation, - overallMaxOperation, - overallAverageOperation, - staticValueOperation, - timeScaleOperation, - timeRangeOperation, - nowOperation, - intervalOperation, + // percentileOperation, + // percentileRanksOperation, + // lastValueOperation, + // countOperation, + // rangeOperation, + // cumulativeSumOperation, + // counterRateOperation, + // derivativeOperation, + // movingAverageOperation, + // mathOperation, + // formulaOperation, + // overallSumOperation, + // overallMinOperation, + // overallMaxOperation, + // overallAverageOperation, + // staticValueOperation, + // timeScaleOperation, + // timeRangeOperation, + // nowOperation, + // intervalOperation, ]; -export { termsOperation } from './terms'; -export { rangeOperation } from './ranges'; -export { filtersOperation } from './filters'; -export { dateHistogramOperation } from './date_histogram'; -export { - minOperation, - averageOperation, - sumOperation, - maxOperation, - standardDeviationOperation, -} from './metrics'; -export { percentileOperation } from './percentile'; -export { percentileRanksOperation } from './percentile_ranks'; -export { countOperation } from './count'; -export { lastValueOperation } from './last_value'; -export { - cumulativeSumOperation, - counterRateOperation, - derivativeOperation, - movingAverageOperation, - overallSumOperation, - overallAverageOperation, - overallMaxOperation, - overallMinOperation, - timeScaleOperation, -} from './calculations'; -export { formulaOperation } from './formula/formula'; -export { staticValueOperation } from './static_value'; +// export { termsOperation } from './terms'; +// export { rangeOperation } from './ranges'; +// export { filtersOperation } from './filters'; +// export { dateHistogramOperation } from './date_histogram'; +// export { +// minOperation, +// averageOperation, +// sumOperation, +// maxOperation, +// standardDeviationOperation, +// } from './metrics'; +// export { percentileOperation } from './percentile'; +// export { percentileRanksOperation } from './percentile_ranks'; +// export { countOperation } from './count'; +// export { lastValueOperation } from './last_value'; +// export { +// cumulativeSumOperation, +// counterRateOperation, +// derivativeOperation, +// movingAverageOperation, +// overallSumOperation, +// overallAverageOperation, +// overallMaxOperation, +// overallMinOperation, +// timeScaleOperation, +// } from './calculations'; +// export { formulaOperation } from './formula/formula'; +// export { staticValueOperation } from './static_value'; /** * Properties passed to the operation-specific part of the popover editor @@ -203,7 +207,7 @@ export interface ParamEditorProps< data: DataPublicPluginStart; fieldFormats: FieldFormatsStart; unifiedSearch: UnifiedSearchPublicPluginStart; - dataViews: DataViewsPublicPluginStart; + dataViews: DataViewsService; activeData?: FormBasedDimensionEditorProps['activeData']; operationDefinitionMap: Record; paramEditorCustomProps?: ParamEditorCustomProps; @@ -513,8 +517,7 @@ interface FieldlessOperationDefinition column: C, columnId: string, indexPattern: IndexPattern, - layer: FormBasedLayer, - uiSettings: IUiSettingsClient + layer: FormBasedLayer ) => ExpressionAstFunction; } diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/last_value.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/last_value.test.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/last_value.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/last_value.test.tsx index a246aa5d95ef5..9508947c78b33 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/last_value.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/last_value.test.tsx @@ -14,10 +14,10 @@ import { IUiSettingsClient, HttpSetup } from '@kbn/core/public'; import { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; -import { createMockedIndexPattern } from '../../mocks'; +import { createMockedIndexPattern } from '../../../../../public/datasources/form_based/mocks'; import { LastValueIndexPatternColumn } from './last_value'; import { lastValueOperation } from '.'; -import type { FormBasedLayer } from '../../types'; +import type { FormBasedLayer } from '../../../../../public/datasources/form_based/types'; import { TermsIndexPatternColumn } from './terms'; import { EuiSwitch, EuiSwitchEvent } from '@elastic/eui'; import { buildExpression, parseExpression } from '@kbn/expressions-plugin/common'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/last_value.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/last_value.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/last_value.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/last_value.tsx index 6432577173af2..2efe03b756169 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/last_value.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/last_value.tsx @@ -20,8 +20,8 @@ import { buildExpressionFunction } from '@kbn/expressions-plugin/public'; import { FormattedMessage } from '@kbn/i18n-react'; import type { FieldBasedOperationErrorMessage, OperationDefinition } from '.'; import { FieldBasedIndexPatternColumn, ValueFormatConfig } from './column_types'; -import type { IndexPatternField, IndexPattern } from '../../../../types'; -import { DataType } from '../../../../types'; +import type { IndexPatternField, IndexPattern } from '../../../../../public/types'; +import { DataType } from '../../../../../public/types'; import { getFormatFromPreviousColumn, getInvalidFieldMessage, @@ -33,8 +33,8 @@ import { import { adjustTimeScaleLabelSuffix } from '../time_scale_utils'; import { isRuntimeField, isScriptedField } from './terms/helpers'; import { FormRow } from './shared_components/form_row'; -import { getColumnReducedTimeRangeError } from '../../reduced_time_range_utils'; -import { getGroupByKey } from './get_group_by_key'; +import { getColumnReducedTimeRangeError } from '../../../../../public/datasources/form_based/reduced_time_range_utils'; +import { getGroupByKey } from '../../get_group_by_key'; function ofName(name: string, timeShift: string | undefined, reducedTimeRange: string | undefined) { return adjustTimeScaleLabelSuffix( diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/metrics.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/metrics.test.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/metrics.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/metrics.test.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/metrics.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/metrics.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/metrics.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/metrics.tsx index 589b840c29f22..1b65a2974acb1 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/metrics.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/metrics.tsx @@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n'; import React from 'react'; import { EuiSwitch, EuiText } from '@elastic/eui'; import { euiThemeVars } from '@kbn/ui-theme'; -import { buildExpressionFunction } from '@kbn/expressions-plugin/public'; +import { buildExpressionFunction } from '@kbn/expressions-plugin/common'; import { LayerSettingsFeatures, OperationDefinition, ParamEditorProps } from '.'; import { getFormatFromPreviousColumn, @@ -26,8 +26,8 @@ import { } from './column_types'; import { adjustTimeScaleLabelSuffix } from '../time_scale_utils'; import { updateColumnParam } from '../layer_helpers'; -import { getColumnReducedTimeRangeError } from '../../reduced_time_range_utils'; -import { getGroupByKey } from './get_group_by_key'; +// import { getColumnReducedTimeRangeError } from '../../../../../public/datasources/form_based/reduced_time_range_utils'; +import { getGroupByKey } from '../../get_group_by_key'; type MetricColumn = FieldBasedIndexPatternColumn & { operationType: T; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile.test.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile.test.tsx index 3e999269c6d82..6474b5a4508e3 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile.test.tsx @@ -16,9 +16,9 @@ import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks import { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; -import { createMockedIndexPattern } from '../../mocks'; +import { createMockedIndexPattern } from '../../../../../public/datasources/form_based/mocks'; import { LastValueIndexPatternColumn, percentileOperation } from '.'; -import { FormBasedLayer } from '../../types'; +import { FormBasedLayer } from '../../../../../public/datasources/form_based/types'; import { PercentileIndexPatternColumn } from './percentile'; import { TermsIndexPatternColumn } from './terms'; import { @@ -28,7 +28,7 @@ import { parseExpression, } from '@kbn/expressions-plugin/public'; import type { OriginalColumn } from '../../to_expression'; -import { IndexPattern } from '../../../../types'; +import { IndexPattern } from '../../../../../public/types'; import faker from 'faker'; jest.mock('lodash', () => { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile.tsx index 3b9ecf0417313..2341b475104ff 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile.tsx @@ -29,8 +29,8 @@ import { import { FieldBasedIndexPatternColumn } from './column_types'; import { adjustTimeScaleLabelSuffix } from '../time_scale_utils'; import { FormRow } from './shared_components'; -import { getColumnReducedTimeRangeError } from '../../reduced_time_range_utils'; -import { getGroupByKey, groupByKey } from './get_group_by_key'; +import { getColumnReducedTimeRangeError } from '../../../../../public/datasources/form_based/reduced_time_range_utils'; +import { getGroupByKey, groupByKey } from '../../get_group_by_key'; export interface PercentileIndexPatternColumn extends FieldBasedIndexPatternColumn { operationType: 'percentile'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile_ranks.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile_ranks.test.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile_ranks.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile_ranks.test.tsx index 981beede0330f..e08a62dc8bda4 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile_ranks.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile_ranks.test.tsx @@ -16,12 +16,12 @@ import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks import { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; -import { createMockedIndexPattern } from '../../mocks'; +import { createMockedIndexPattern } from '../../../../../public/datasources/form_based/mocks'; import { percentileRanksOperation } from '.'; -import { FormBasedLayer } from '../../types'; +import { FormBasedLayer } from '../../../../../public/datasources/form_based/types'; import type { PercentileRanksIndexPatternColumn } from './percentile_ranks'; import { TermsIndexPatternColumn } from './terms'; -import { IndexPattern } from '../../../../types'; +import { IndexPattern } from '../../../../../public/types'; jest.mock('lodash', () => { const original = jest.requireActual('lodash'); diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile_ranks.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile_ranks.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile_ranks.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile_ranks.tsx index 54d6f379d9e5f..d68ff7474c445 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/percentile_ranks.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/percentile_ranks.tsx @@ -24,7 +24,7 @@ import { import { FieldBasedIndexPatternColumn } from './column_types'; import { adjustTimeScaleLabelSuffix } from '../time_scale_utils'; import { FormRow } from './shared_components'; -import { getColumnReducedTimeRangeError } from '../../reduced_time_range_utils'; +import { getColumnReducedTimeRangeError } from '../../../../../public/datasources/form_based/reduced_time_range_utils'; export interface PercentileRanksIndexPatternColumn extends FieldBasedIndexPatternColumn { operationType: 'percentile_rank'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/advanced_editor.scss b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/advanced_editor.scss similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/advanced_editor.scss rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/advanced_editor.scss diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/advanced_editor.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/advanced_editor.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/advanced_editor.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/advanced_editor.tsx index c196c3191bae3..5f3f6be4f89df 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/advanced_editor.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/advanced_editor.tsx @@ -28,7 +28,7 @@ import { DraggableBucketContainer, NewBucketButton, } from '@kbn/visualization-ui-components'; -import { useDebounceWithOptions } from '../../../../../shared_components'; +import { useDebounceWithOptions } from '../../../../../../public/shared_components'; import { RangeTypeLens, isValidRange } from './ranges'; import { FROM_PLACEHOLDER, TO_PLACEHOLDER, TYPING_DEBOUNCE_TIME } from './constants'; import { LabelInput } from '../shared_components'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/constants.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/constants.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/constants.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/constants.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/index.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/index.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/index.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/index.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/range_editor.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/range_editor.tsx similarity index 97% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/range_editor.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/range_editor.tsx index d6941417713e2..cb0e7fc6b1ad9 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/range_editor.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/range_editor.tsx @@ -26,8 +26,11 @@ import { UI_SETTINGS } from '@kbn/data-plugin/public'; import { RangeColumnParams, UpdateParamsFnType, MODES_TYPES } from './ranges'; import { AdvancedRangeEditor } from './advanced_editor'; import { TYPING_DEBOUNCE_TIME, MODES, MIN_HISTOGRAM_BARS } from './constants'; -import { useDebounceWithOptions } from '../../../../../shared_components'; -import { HelpPopover, HelpPopoverButton } from '../../../help_popover'; +import { useDebounceWithOptions } from '../../../../../../public/shared_components'; +import { + HelpPopover, + HelpPopoverButton, +} from '../../../../../../public/datasources/form_based/help_popover'; const GranularityHelpPopover = () => { const [isPopoverOpen, setIsPopoverOpen] = useState(false); diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/ranges.test.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/ranges.test.tsx index 8bf71089b21e0..95cc7b05e9e25 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/ranges.test.tsx @@ -15,7 +15,7 @@ import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; import { fieldFormatsServiceMock } from '@kbn/field-formats-plugin/public/mocks'; import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks'; -import type { FormBasedLayer } from '../../../types'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { rangeOperation } from '..'; import { RangeIndexPatternColumn } from './ranges'; import { @@ -27,8 +27,8 @@ import { } from './constants'; import { RangePopover } from './advanced_editor'; import { DragDropBuckets } from '@kbn/visualization-ui-components'; -import { getFieldByNameFactory } from '../../../pure_helpers'; -import { IndexPattern } from '../../../../../types'; +import { getFieldByNameFactory } from '../../../../../../public/datasources/form_based/pure_helpers'; +import { IndexPattern } from '../../../../../../public/types'; // mocking random id generator function jest.mock('@elastic/eui', () => { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/ranges.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/ranges.tsx index 3cc5569cb402c..6cc67acebcdef 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/ranges/ranges.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/ranges/ranges.tsx @@ -15,9 +15,9 @@ import { RangeEditor } from './range_editor'; import { OperationDefinition } from '..'; import { FieldBasedIndexPatternColumn } from '../column_types'; import { updateColumnParam } from '../../layer_helpers'; -import { supportedFormats } from '../../../../../../common/expressions/format_column/supported_formats'; +import { supportedFormats } from '../../../../../expressions/format_column/supported_formats'; import { MODES, AUTO_BARS, DEFAULT_INTERVAL, MIN_HISTOGRAM_BARS, SLICES } from './constants'; -import { IndexPattern, IndexPatternField } from '../../../../../types'; +import { IndexPattern, IndexPatternField } from '../../../../../../public/types'; import { getInvalidFieldMessage, isValidNumber } from '../helpers'; type RangeType = Omit; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/form_row.scss b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/form_row.scss similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/form_row.scss rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/form_row.scss diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/form_row.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/form_row.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/form_row.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/form_row.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/index.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/index.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/index.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/index.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/label_input.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/label_input.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/shared_components/label_input.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/shared_components/label_input.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/static_value.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/static_value.test.tsx similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/static_value.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/static_value.test.tsx index 91727f0d56ea7..da39ef0c8db81 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/static_value.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/static_value.test.tsx @@ -15,10 +15,10 @@ import { fieldFormatsServiceMock } from '@kbn/field-formats-plugin/public/mocks' import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; -import { createMockedIndexPattern } from '../../mocks'; +import { createMockedIndexPattern } from '../../../../../public/datasources/form_based/mocks'; import { staticValueOperation } from '.'; -import { FormBasedLayer } from '../../types'; -import { IndexPattern } from '../../../../types'; +import { FormBasedLayer } from '../../../../../public/datasources/form_based/types'; +import { IndexPattern } from '../../../../../public/types'; import { StaticValueIndexPatternColumn } from './static_value'; import { TermsIndexPatternColumn } from './terms'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/static_value.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/static_value.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/static_value.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/static_value.tsx index af00ba7cb5c69..ed3512fb42c6a 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/static_value.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/static_value.tsx @@ -14,7 +14,7 @@ import { GenericIndexPatternColumn, ValueFormatConfig, } from './column_types'; -import type { IndexPattern } from '../../../../types'; +import type { IndexPattern } from '../../../../../public/types'; import { getFormatFromPreviousColumn, isValidNumber } from './helpers'; import { getColumnOrder } from '../layer_helpers'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/constants.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/constants.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/constants.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/constants.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/field_inputs.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/field_inputs.tsx similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/field_inputs.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/field_inputs.tsx index 428747b6cda75..bc6efba6a3465 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/field_inputs.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/field_inputs.tsx @@ -15,10 +15,10 @@ import { NewBucketButton, DraggableBucketContainer, } from '@kbn/visualization-ui-components'; -import { IndexPattern } from '../../../../../types'; -import { FieldSelect } from '../../../dimension_panel/field_select'; +import { IndexPattern } from '../../../../../../public/types'; +import { FieldSelect } from '../../../../../../public/datasources/form_based/dimension_panel/field_select'; import type { TermsIndexPatternColumn } from './types'; -import type { OperationSupportMatrix } from '../../../dimension_panel'; +import type { OperationSupportMatrix } from '../../../../../../public/datasources/form_based/dimension_panel'; import { supportedTypes } from './constants'; const generateId = htmlIdGenerator(); diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/helpers.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/helpers.test.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/helpers.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/helpers.test.ts index 583f3ff5015c2..d9f90e68cb72a 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/helpers.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/helpers.test.ts @@ -7,11 +7,11 @@ import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { coreMock as corePluginMock } from '@kbn/core/public/mocks'; -import type { FramePublicAPI } from '../../../../../types'; +import type { FramePublicAPI } from '../../../../../../public/types'; import type { CountIndexPatternColumn } from '..'; import type { TermsIndexPatternColumn } from './types'; -import type { GenericIndexPatternColumn } from '../../../form_based'; -import { createMockedIndexPattern } from '../../../mocks'; +import type { GenericIndexPatternColumn } from '../../../../../../public/datasources/form_based/form_based'; +import { createMockedIndexPattern } from '../../../../../../public/datasources/form_based/mocks'; import { getDisallowedTermsMessage, getMultiTermsScriptedFieldErrorMessage, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/helpers.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/helpers.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/helpers.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/helpers.ts index cb3ed583d7cda..89e850e0ea41b 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/helpers.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/helpers.ts @@ -9,7 +9,7 @@ import { i18n } from '@kbn/i18n'; import { uniq } from 'lodash'; import type { CoreStart } from '@kbn/core/public'; import { buildEsQuery } from '@kbn/es-query'; -import { getEsQueryConfig, DataPublicPluginStart } from '@kbn/data-plugin/public'; +import { getEsQueryConfig, DataPublicPluginStart } from '@kbn/data-plugin/common'; import type { DataViewField } from '@kbn/data-views-plugin/common'; import { type FieldStatsResponse } from '@kbn/unified-field-list/src/types'; import { loadFieldStats } from '@kbn/unified-field-list/src/services/field_stats'; @@ -17,14 +17,18 @@ import { GenericIndexPatternColumn, operationDefinitionMap } from '..'; import { defaultLabel } from '../filters'; import { isReferenced } from '../../layer_helpers'; -import type { FramePublicAPI, IndexPattern, IndexPatternField } from '../../../../../types'; +import type { + FramePublicAPI, + IndexPattern, + IndexPatternField, +} from '../../../../../../public/types'; import type { FiltersIndexPatternColumn } from '..'; import type { TermsIndexPatternColumn } from './types'; import type { LastValueIndexPatternColumn } from '../last_value'; import type { PercentileRanksIndexPatternColumn } from '../percentile_ranks'; import type { PercentileIndexPatternColumn } from '../percentile'; -import type { FormBasedLayer } from '../../../types'; +import type { FormBasedLayer } from '../../../../../../public/datasources/form_based/types'; import { MULTI_KEY_VISUAL_SEPARATOR, supportedTypes, MAX_TERMS_OTHER_ENABLED } from './constants'; import { isColumnOfType } from '../helpers'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/include_exclude_options.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/include_exclude_options.test.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/include_exclude_options.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/include_exclude_options.test.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/include_exclude_options.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/include_exclude_options.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/include_exclude_options.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/include_exclude_options.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/index.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/index.tsx similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/index.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/index.tsx index 155fe24705276..1616be81582f8 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/index.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/index.tsx @@ -23,39 +23,39 @@ import { EuiTextColor, } from '@elastic/eui'; import { uniq } from 'lodash'; -import { AggFunctionsMapping } from '@kbn/data-plugin/public'; -import { buildExpressionFunction } from '@kbn/expressions-plugin/public'; -import { DOCUMENT_FIELD_NAME } from '../../../../../../common/constants'; +import type { AggFunctionsMapping } from '@kbn/data-plugin/common'; +import { buildExpressionFunction } from '@kbn/expressions-plugin/common'; +import { DOCUMENT_FIELD_NAME } from '../../../../../constants'; import { insertOrReplaceColumn, updateColumnParam, updateDefaultLabels } from '../../layer_helpers'; -import type { DataType, OperationMetadata } from '../../../../../types'; -import { OperationDefinition } from '..'; -import { GenericIndexPatternColumn, IncompleteColumn } from '../column_types'; -import { ValuesInput } from './values_input'; -import { getInvalidFieldMessage, isColumn } from '../helpers'; -import { FieldInputs, getInputFieldErrorMessage, MAX_MULTI_FIELDS_SIZE } from './field_inputs'; -import { - FieldInput as FieldInputBase, - getErrorMessage, -} from '../../../dimension_panel/field_input'; +import type { DataType, OperationMetadata } from '../../../../../../public/types'; +import type { OperationDefinition } from '..'; +import type { GenericIndexPatternColumn, IncompleteColumn } from '../column_types'; +// import { ValuesInput } from './values_input'; +// import { getInvalidFieldMessage, isColumn } from '../helpers'; +// import { FieldInputs, getInputFieldErrorMessage, MAX_MULTI_FIELDS_SIZE } from './field_inputs'; +// import { +// FieldInput as FieldInputBase, +// getErrorMessage, +// } from '../../../../../../public/datasources/form_based/dimension_panel/field_input'; import type { TermsIndexPatternColumn } from './types'; -import type { IndexPatternField } from '../../../../../types'; -import { - getDisallowedTermsMessage, - getMultiTermsScriptedFieldErrorMessage, - getFieldsByValidationState, - isSortableByColumn, - isPercentileRankSortable, - isPercentileSortable, - getOtherBucketSwitchDefault, -} from './helpers'; +import type { IndexPatternField } from '../../../../../../public/types'; +// import { +// getDisallowedTermsMessage, +// getMultiTermsScriptedFieldErrorMessage, +// getFieldsByValidationState, +// isSortableByColumn, +// isPercentileRankSortable, +// isPercentileSortable, +// getOtherBucketSwitchDefault, +// } from './helpers'; import { DEFAULT_MAX_DOC_COUNT, DEFAULT_SIZE, MAXIMUM_MAX_DOC_COUNT, supportedTypes, } from './constants'; -import { IncludeExcludeRow } from './include_exclude_options'; -import { shouldShowTimeSeriesOption } from '../../../pure_utils'; +import type { IncludeExcludeRow } from './include_exclude_options'; +// import { shouldShowTimeSeriesOption } from '../../../pure_utils'; export function supportsRarityRanking(field?: IndexPatternField) { // these es field types can't be sorted by rarity @@ -312,13 +312,13 @@ export const termsOperation: OperationDefinition< const orderColumn = layer.columns[column.params.orderBy.columnId]; orderBy = String(orderedColumnIds.indexOf(column.params.orderBy.columnId)); // percentile rank with non integer value should default to alphabetical order - if ( - !orderColumn || - !isPercentileRankSortable(orderColumn) || - !isPercentileSortable(orderColumn) - ) { - orderBy = '_key'; - } + // if ( + // !orderColumn || + // !isPercentileRankSortable(orderColumn) || + // !isPercentileSortable(orderColumn) + // ) { + // orderBy = '_key'; + // } } const orderAggColumn = column.params.orderAgg; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/terms.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/terms.test.tsx similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/terms.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/terms.test.tsx index a74872a46f907..e516ef53083ee 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/terms.test.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/terms.test.tsx @@ -23,7 +23,7 @@ import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; import { coreMock as corePluginMock } from '@kbn/core/public/mocks'; -import { createMockedIndexPattern } from '../../../mocks'; +import { createMockedIndexPattern } from '../../../../../../public/datasources/form_based/mocks'; import { ValuesInput } from './values_input'; import type { TermsIndexPatternColumn } from '.'; import { @@ -32,13 +32,16 @@ import { LastValueIndexPatternColumn, operationDefinitionMap, } from '..'; -import { FormBasedLayer, FormBasedPrivateState } from '../../../types'; -import { FramePublicAPI } from '../../../../../types'; +import { + FormBasedLayer, + FormBasedPrivateState, +} from '../../../../../../public/datasources/form_based/types'; +import { FramePublicAPI } from '../../../../../../public/types'; import { DateHistogramIndexPatternColumn } from '../date_histogram'; -import { getOperationSupportMatrix } from '../../../dimension_panel/operation_support'; -import { FieldSelect } from '../../../dimension_panel/field_select'; -import { ReferenceEditor } from '../../../dimension_panel/reference_editor'; -import { IndexPattern } from '../../../../../types'; +import { getOperationSupportMatrix } from '../../../../../../public/datasources/form_based/dimension_panel/operation_support'; +import { FieldSelect } from '../../../../../../public/datasources/form_based/dimension_panel/field_select'; +import { ReferenceEditor } from '../../../../../../public/datasources/form_based/dimension_panel/reference_editor'; +import { IndexPattern } from '../../../../../../public/types'; import { cloneDeep } from 'lodash'; import { IncludeExcludeRow } from './include_exclude_options'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/types.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/types.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/types.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/types.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/values_input.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/values_input.test.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/values_input.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/values_input.test.tsx diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/values_input.tsx b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/values_input.tsx similarity index 97% rename from x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/values_input.tsx rename to x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/values_input.tsx index a776227986d68..47448929b12e7 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/definitions/terms/values_input.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/definitions/terms/values_input.tsx @@ -8,7 +8,7 @@ import React, { useState } from 'react'; import { i18n } from '@kbn/i18n'; import { EuiFieldNumber, EuiFormRow } from '@elastic/eui'; -import { useDebounceWithOptions } from '../../../../../shared_components'; +import { useDebounceWithOptions } from '../../../../../../public/shared_components'; export const ValuesInput = ({ value, diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/index.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/index.ts similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/operations/index.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/index.ts diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/layer_helpers.test.ts similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/layer_helpers.test.ts index 11ea2a7b18414..9a3bf4ff282f1 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/layer_helpers.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { OperationMetadata } from '../../../types'; +import type { OperationMetadata } from '../../../../public/types'; import { copyColumn, insertNewColumn, @@ -23,9 +23,12 @@ import { operationDefinitionMap, OperationType } from '.'; import { TermsIndexPatternColumn } from './definitions/terms'; import { DateHistogramIndexPatternColumn } from './definitions/date_histogram'; import { AvgIndexPatternColumn } from './definitions/metrics'; -import type { FormBasedLayer, FormBasedPrivateState } from '../types'; +import type { + FormBasedLayer, + FormBasedPrivateState, +} from '../../../../public/datasources/form_based/types'; import { documentField } from '../document_field'; -import { getFieldByNameFactory } from '../pure_helpers'; +import { getFieldByNameFactory } from '../../../../public/datasources/form_based/pure_helpers'; import { generateId } from '../../../id_generator'; import { createMockedFullReference, createMockedManagedReference } from './mocks'; import { @@ -39,7 +42,7 @@ import { OperationDefinition, } from './definitions'; import { TinymathAST } from '@kbn/tinymath'; -import { IndexPattern } from '../../../types'; +import { IndexPattern } from '../../../../public/types'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { createCoreStartMock } from '@kbn/core-lifecycle-browser-mocks/src/core_start.mock'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/layer_helpers.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/layer_helpers.ts index 6ab10a097010d..d4f8a72850734 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/layer_helpers.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/layer_helpers.ts @@ -9,16 +9,15 @@ import { partition, mapValues, pickBy } from 'lodash'; import { CoreStart } from '@kbn/core/public'; import type { Query } from '@kbn/es-query'; import memoizeOne from 'memoize-one'; -import { DataPublicPluginStart, UI_SETTINGS } from '@kbn/data-plugin/public'; -import type { DateRange } from '../../../../common/types'; +import { UI_SETTINGS } from '@kbn/data-plugin/common'; +import type { DataPublicPluginStart } from '@kbn/data-plugin/public'; +import type { DateRange, IndexPattern, IndexPatternField } from '../../../types'; import type { DatasourceFixAction, FramePublicAPI, - IndexPattern, - IndexPatternField, OperationMetadata, VisualizationDimensionGroupConfig, -} from '../../../types'; +} from '../../../../public/types'; import { operationDefinitionMap, operationDefinitions, @@ -29,7 +28,11 @@ import { type TermsIndexPatternColumn, type FieldBasedOperationErrorMessage, } from './definitions'; -import type { DataViewDragDropOperation, FormBasedLayer, FormBasedPrivateState } from '../types'; +import type { + DataViewDragDropOperation, + FormBasedLayer, + FormBasedPrivateState, +} from '../../../../public/datasources/form_based/types'; import { getSortScoreByPriorityForField } from './operations'; import { generateId } from '../../../id_generator'; import { @@ -37,11 +40,11 @@ import { ReferenceBasedIndexPatternColumn, BaseIndexPatternColumn, } from './definitions/column_types'; -import { FormulaIndexPatternColumn, insertOrReplaceFormulaColumn } from './definitions/formula'; -import type { TimeScaleUnit } from '../../../../common/expressions'; -import { documentField } from '../document_field'; +// import { FormulaIndexPatternColumn, insertOrReplaceFormulaColumn } from './definitions/formula'; +import type { TimeScaleUnit } from '../../../expressions'; +import { documentField } from '../../../document_field'; import { isColumnOfType } from './definitions/helpers'; -import type { DataType } from '../../..'; +import type { DataType } from '../../../../public'; export interface ColumnAdvancedParams { filter?: Query | undefined; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/mocks.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/mocks.ts similarity index 96% rename from x-pack/plugins/lens/public/datasources/form_based/operations/mocks.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/mocks.ts index 3884bab02f200..f7ca11c7589df 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/mocks.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/mocks.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { OperationMetadata } from '../../../types'; +import type { OperationMetadata } from '../../../../public/types'; import type { OperationType } from './definitions'; export const createMockedFullReference = () => { diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/operations.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/operations.test.ts similarity index 99% rename from x-pack/plugins/lens/public/datasources/form_based/operations/operations.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/operations.test.ts index 1f6f041b4eee2..f37815eab45a3 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/operations.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/operations.test.ts @@ -6,7 +6,7 @@ */ import { getOperationTypesForField, getAvailableOperationsByMetadata } from '.'; -import { getFieldByNameFactory } from '../pure_helpers'; +import { getFieldByNameFactory } from '../../../../public/datasources/form_based/pure_helpers'; jest.mock('../loader'); diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/operations.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/operations.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/operations.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/operations.ts index cf7cbb48d4fd9..2224ff14d4f8a 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/operations.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/operations.ts @@ -6,7 +6,7 @@ */ import { memoize } from 'lodash'; -import type { IndexPattern, IndexPatternField, OperationMetadata } from '../../../types'; +import type { IndexPattern, IndexPatternField, OperationMetadata } from '../../../../public/types'; import { operationDefinitionMap, operationDefinitions, @@ -15,7 +15,7 @@ import { renameOperationsMapping, BaseIndexPatternColumn, } from './definitions'; -import { documentField } from '../document_field'; +import { documentField } from '../../../document_field'; import { hasField } from '../pure_utils'; export { operationDefinitionMap } from './definitions'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/time_scale_utils.test.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/time_scale_utils.test.ts similarity index 98% rename from x-pack/plugins/lens/public/datasources/form_based/operations/time_scale_utils.test.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/time_scale_utils.test.ts index e249be575aac2..949e4dd9fe249 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/time_scale_utils.test.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/time_scale_utils.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { TimeScaleUnit } from '../../../../common/expressions'; +import type { TimeScaleUnit } from '../../../expressions'; import { adjustTimeScaleLabelSuffix } from './time_scale_utils'; export const DEFAULT_TIME_SCALE = 's' as TimeScaleUnit; diff --git a/x-pack/plugins/lens/public/datasources/form_based/operations/time_scale_utils.ts b/x-pack/plugins/lens/common/datasources/form_based/operations/time_scale_utils.ts similarity index 92% rename from x-pack/plugins/lens/public/datasources/form_based/operations/time_scale_utils.ts rename to x-pack/plugins/lens/common/datasources/form_based/operations/time_scale_utils.ts index 08dfa147bf88e..c3639a941fcb7 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/operations/time_scale_utils.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/operations/time_scale_utils.ts @@ -6,8 +6,8 @@ */ import { i18n } from '@kbn/i18n'; -import { unitSuffixesLong } from '../../../../common/suffix_formatter'; -import type { TimeScaleUnit } from '../../../../common/expressions'; +import { unitSuffixesLong } from '../../../suffix_formatter'; +import type { TimeScaleUnit } from '../../../expressions'; export const DEFAULT_TIME_SCALE = 's' as TimeScaleUnit; diff --git a/x-pack/plugins/lens/public/datasources/form_based/pure_utils.ts b/x-pack/plugins/lens/common/datasources/form_based/pure_utils.ts similarity index 90% rename from x-pack/plugins/lens/public/datasources/form_based/pure_utils.ts rename to x-pack/plugins/lens/common/datasources/form_based/pure_utils.ts index 256c1de51f832..dd6d7438b5a5a 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/pure_utils.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/pure_utils.ts @@ -5,18 +5,14 @@ * 2.0. */ -import type { - DataType, - IndexPattern, - IndexPatternField, - VisualizationDimensionGroupConfig, -} from '../../types'; -import type { FormBasedLayer } from './types'; +import type { DataType, VisualizationDimensionGroupConfig } from '../../../public/types'; +import type { FormBasedLayer } from '../../../public/datasources/form_based/types'; import type { BaseIndexPatternColumn, FieldBasedIndexPatternColumn, GenericIndexPatternColumn, } from './operations/definitions/column_types'; +import type { IndexPattern, IndexPatternField } from '../../types'; /** * Normalizes the specified operation type. (e.g. document operations diff --git a/x-pack/plugins/lens/public/datasources/form_based/time_shift_utils.test.tsx b/x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.test.tsx similarity index 100% rename from x-pack/plugins/lens/public/datasources/form_based/time_shift_utils.test.tsx rename to x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.test.tsx diff --git a/x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.ts b/x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.ts new file mode 100644 index 0000000000000..6ccf2f3d90620 --- /dev/null +++ b/x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.ts @@ -0,0 +1,65 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { + calcAutoIntervalNear, + isAbsoluteTimeShift, + parseAbsoluteTimeShift, + parseTimeShift, +} from '@kbn/data-plugin/common'; +import moment from 'moment'; +import { DateRange } from '../../types'; + +export function parseTimeShiftWrapper(timeShiftString: string, dateRange: DateRange) { + if (isAbsoluteTimeShift(timeShiftString.trim())) { + return parseAbsoluteTimeShift(timeShiftString, { + from: dateRange.fromDate, + to: dateRange.toDate, + }).value; + } + return parseTimeShift(timeShiftString); +} + +function closestMultipleOfInterval(duration: number, interval: number) { + if (duration % interval === 0) { + return duration; + } + return Math.ceil(duration / interval) * interval; +} + +function roundAbsoluteInterval(timeShift: string, dateRange: DateRange, targetBars: number) { + // workout the interval (most probably matching the ES one) + const interval = calcAutoIntervalNear( + targetBars, + moment(dateRange.toDate).diff(moment(dateRange.fromDate)) + ); + const duration = parseTimeShiftWrapper(timeShift, dateRange); + if (typeof duration !== 'string') { + const roundingOffset = timeShift.startsWith('end') ? interval.asMilliseconds() : 0; + return `${ + (closestMultipleOfInterval(duration.asMilliseconds(), interval.asMilliseconds()) - + roundingOffset) / + 1000 + }s`; + } +} + +export function resolveTimeShift( + timeShift: string | undefined, + dateRange: DateRange, + targetBars: number, + hasDateHistogram: boolean = false +) { + if (timeShift && isAbsoluteTimeShift(timeShift)) { + return roundAbsoluteInterval(timeShift, dateRange, targetBars); + } + // Translate a relative "previous" shift into an absolute endAt() + if (timeShift && hasDateHistogram && timeShift === 'previous') { + return roundAbsoluteInterval(`endAt(${dateRange.fromDate})`, dateRange, targetBars); + } + return timeShift; +} diff --git a/x-pack/plugins/lens/public/datasources/form_based/time_shift_utils.tsx b/x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.tsx similarity index 88% rename from x-pack/plugins/lens/public/datasources/form_based/time_shift_utils.tsx rename to x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.tsx index c287a93675f64..76743f7d572f6 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/time_shift_utils.tsx +++ b/x-pack/plugins/lens/common/datasources/form_based/time_shift_utils.tsx @@ -10,27 +10,20 @@ import React from 'react'; import { FormattedMessage } from '@kbn/i18n-react'; import moment from 'moment'; import { Datatable } from '@kbn/expressions-plugin/common'; -import { search } from '@kbn/data-plugin/public'; import { calcAutoIntervalNear, DatatableUtilitiesService, isAbsoluteTimeShift, parseAbsoluteTimeShift, + parseInterval, parseTimeShift, } from '@kbn/data-plugin/common'; -import type { DateRange } from '../../../common/types'; -import type { FormBasedLayer, FormBasedPrivateState } from './types'; -import type { FramePublicAPI, IndexPattern } from '../../types'; - -export function parseTimeShiftWrapper(timeShiftString: string, dateRange: DateRange) { - if (isAbsoluteTimeShift(timeShiftString.trim())) { - return parseAbsoluteTimeShift(timeShiftString, { - from: dateRange.fromDate, - to: dateRange.toDate, - }).value; - } - return parseTimeShift(timeShiftString); -} +import type { DateRange, IndexPattern } from '../../types'; +import type { + FormBasedLayer, + FormBasedPrivateState, +} from '../../../public/datasources/form_based/types'; +import type { FramePublicAPI } from '../../../public/types'; export const timeShiftOptions = [ { @@ -133,7 +126,7 @@ export function getDateHistogramInterval( if (column) { const expression = datatableUtilities.getDateHistogramMeta(column)?.interval || ''; return { - interval: search.aggs.parseInterval(expression), + interval: parseInterval(expression), expression, canShift: true, hasDateHistogram: true, @@ -297,23 +290,6 @@ function closestMultipleOfInterval(duration: number, interval: number) { return Math.ceil(duration / interval) * interval; } -function roundAbsoluteInterval(timeShift: string, dateRange: DateRange, targetBars: number) { - // workout the interval (most probably matching the ES one) - const interval = calcAutoIntervalNear( - targetBars, - moment(dateRange.toDate).diff(moment(dateRange.fromDate)) - ); - const duration = parseTimeShiftWrapper(timeShift, dateRange); - if (typeof duration !== 'string') { - const roundingOffset = timeShift.startsWith('end') ? interval.asMilliseconds() : 0; - return `${ - (closestMultipleOfInterval(duration.asMilliseconds(), interval.asMilliseconds()) - - roundingOffset) / - 1000 - }s`; - } -} - export function resolveTimeShift( timeShift: string | undefined, dateRange: DateRange, diff --git a/x-pack/plugins/lens/public/datasources/form_based/to_expression.ts b/x-pack/plugins/lens/common/datasources/form_based/to_expression.ts similarity index 91% rename from x-pack/plugins/lens/public/datasources/form_based/to_expression.ts rename to x-pack/plugins/lens/common/datasources/form_based/to_expression.ts index feba325e5dfcf..f2099ad818060 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/to_expression.ts +++ b/x-pack/plugins/lens/common/datasources/form_based/to_expression.ts @@ -5,15 +5,13 @@ * 2.0. */ -import type { IUiSettingsClient } from '@kbn/core/public'; import { partition, uniq } from 'lodash'; import seedrandom from 'seedrandom'; -import { +import type { AggFunctionsMapping, EsaggsExpressionFunctionDefinition, IndexPatternLoadExpressionFunctionDefinition, - UI_SETTINGS, -} from '@kbn/data-plugin/public'; +} from '@kbn/data-plugin/common'; import { queryToAst } from '@kbn/data-plugin/common'; import { buildExpression, @@ -21,16 +19,22 @@ import { ExpressionAstExpression, ExpressionAstExpressionBuilder, ExpressionAstFunction, -} from '@kbn/expressions-plugin/public'; -import type { DateRange } from '../../../common/types'; -import { GenericIndexPatternColumn } from './form_based'; -import { operationDefinitionMap } from './operations'; -import { FormBasedPrivateState, FormBasedLayer } from './types'; -import { DateHistogramIndexPatternColumn, RangeIndexPatternColumn } from './operations/definitions'; +} from '@kbn/expressions-plugin/common'; +import type { IUiSettingsClient } from '@kbn/core-ui-settings-browser'; +import type { DateRange, IndexPattern, IndexPatternMap } from '../../types'; +import type { GenericIndexPatternColumn } from '../../../public/datasources/form_based/form_based'; +import { operationDefinitionMap } from './operations/definitions'; +import type { + FormBasedPrivateState, + FormBasedLayer, +} from '../../../public/datasources/form_based/types'; +import type { + DateHistogramIndexPatternColumn, + RangeIndexPatternColumn, +} from './operations/definitions'; import type { FormattedIndexPatternColumn } from './operations/definitions/column_types'; import { isColumnFormatted, isColumnOfType } from './operations/definitions/helpers'; -import type { IndexPattern, IndexPatternMap } from '../../types'; -import { dedupeAggs } from './dedupe_aggs'; +import { dedupeAggs, extractAggId } from './dedupe_aggs'; import { resolveTimeShift } from './time_shift_utils'; import { getSamplingValue } from './utils'; @@ -45,8 +49,6 @@ declare global { } } -// esAggs column ID manipulation functions -export const extractAggId = (id: string) => id.split('.')[0].split('-')[2]; // Need a more complex logic for decimals percentiles function getAggIdPostFixForPercentile(percentile: string, decimals?: string) { if (!percentile && !decimals) { @@ -67,7 +69,6 @@ const updatePositionIndex = (currentId: string, newIndex: number) => { function getExpressionForLayer( layer: FormBasedLayer, indexPattern: IndexPattern, - uiSettings: IUiSettingsClient, dateRange: DateRange, nowInstant: Date, searchSessionId?: string @@ -144,7 +145,7 @@ function getExpressionForLayer( if (referenceEntries.length || esAggEntries.length) { let aggs: ExpressionAstExpressionBuilder[] = []; const expressions: ExpressionAstFunction[] = []; - const histogramBarsTarget = uiSettings.get(UI_SETTINGS.HISTOGRAM_BAR_TARGET); + const histogramBarsTarget = 100; // TODO get from UI Settings sortedReferences(referenceEntries).forEach((colId) => { const col = columns[colId]; @@ -187,7 +188,7 @@ function getExpressionForLayer( wrapInFilter || wrapInTimeFilter ? `${aggId}-metric` : aggId, indexPattern, layer, - uiSettings, + { get: () => '' } as unknown as IUiSettingsClient, orderedColumnIds, operationDefinitionMap ); @@ -230,9 +231,10 @@ function getExpressionForLayer( }); aggs.push(expressionBuilder); - const esAggsId = window.ELASTIC_LENS_DELAY_SECONDS - ? `col-${index + (col.isBucketed ? 0 : 1)}-${aggId}` - : `col-${index}-${aggId}`; + // const esAggsId = window?.ELASTIC_LENS_DELAY_SECONDS + // ? `col-${index + (col.isBucketed ? 0 : 1)}-${aggId}` + // : `col-${index}-${aggId}`; + const esAggsId = `col-${index}-${aggId}`; esAggsIdMap[esAggsId] = [ { @@ -252,21 +254,21 @@ function getExpressionForLayer( } }); - if (window.ELASTIC_LENS_DELAY_SECONDS) { - aggs.push( - buildExpression({ - type: 'expression', - chain: [ - buildExpressionFunction('aggShardDelay', { - id: 'the-delay', - enabled: true, - schema: 'metric', - delay: `${window.ELASTIC_LENS_DELAY_SECONDS}s`, - }).toAst(), - ], - }) - ); - } + // if (window.ELASTIC_LENS_DELAY_SECONDS) { + // aggs.push( + // buildExpression({ + // type: 'expression', + // chain: [ + // buildExpressionFunction('aggShardDelay', { + // id: 'the-delay', + // enabled: true, + // schema: 'metric', + // delay: `${window.ELASTIC_LENS_DELAY_SECONDS}s`, + // }).toAst(), + // ], + // }) + // ); + // } const allOperations = uniq( esAggEntries.map(([_, column]) => operationDefinitionMap[column.operationType]) @@ -323,9 +325,10 @@ function getExpressionForLayer( matchingEsAggColumnIds.forEach((currentId) => { const currentColumn = esAggsIdMap[currentId][0]; - const aggIndex = window.ELASTIC_LENS_DELAY_SECONDS - ? counter + (currentColumn.isBucketed ? 0 : 1) - : counter; + // const aggIndex = window.ELASTIC_LENS_DELAY_SECONDS + // ? counter + (currentColumn.isBucketed ? 0 : 1) + // : counter; + const aggIndex = counter; const newId = updatePositionIndex(currentId, aggIndex); updatedEsAggsIdMap[newId] = esAggsIdMap[currentId]; @@ -517,7 +520,6 @@ export function toExpression( state: FormBasedPrivateState, layerId: string, indexPatterns: IndexPatternMap, - uiSettings: IUiSettingsClient, dateRange: DateRange, nowInstant: Date, searchSessionId?: string @@ -526,7 +528,6 @@ export function toExpression( return getExpressionForLayer( state.layers[layerId], indexPatterns[state.layers[layerId].indexPatternId], - uiSettings, dateRange, nowInstant, searchSessionId diff --git a/x-pack/plugins/lens/common/datasources/form_based/utils.ts b/x-pack/plugins/lens/common/datasources/form_based/utils.ts new file mode 100644 index 0000000000000..3d24764d5f8d6 --- /dev/null +++ b/x-pack/plugins/lens/common/datasources/form_based/utils.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { FormBasedLayer } from '../../../public'; +import type { + GenericIndexPatternColumn, + MaxIndexPatternColumn, + MinIndexPatternColumn, +} from './operations'; +import type { ReferenceBasedIndexPatternColumn } from './operations/definitions/column_types'; +import { isColumnOfType } from './operations/definitions/helpers'; + +function isMinOrMaxColumn( + column?: GenericIndexPatternColumn +): column is MaxIndexPatternColumn | MinIndexPatternColumn { + if (!column) { + return false; + } + return ( + isColumnOfType('max', column) || + isColumnOfType('min', column) + ); +} + +function isReferenceColumn( + column: GenericIndexPatternColumn +): column is ReferenceBasedIndexPatternColumn { + return 'references' in column; +} + +export function isSamplingValueEnabled(layer: FormBasedLayer) { + // Do not use columnOrder here as it needs to check also inside formulas columns + return !Object.values(layer.columns).some( + (column) => + isMinOrMaxColumn(column) || + (isReferenceColumn(column) && isMinOrMaxColumn(layer.columns[column.references[0]])) + ); +} + +/** + * Centralized logic to get the actual random sampling value for a layer + * @param layer + * @returns + */ +export function getSamplingValue(layer: FormBasedLayer) { + return isSamplingValueEnabled(layer) ? layer.sampling ?? 1 : 1; +} diff --git a/x-pack/plugins/lens/common/datasources/text_based/text_based_languages.ts b/x-pack/plugins/lens/common/datasources/text_based/text_based_languages.ts new file mode 100644 index 0000000000000..5d834265e4f73 --- /dev/null +++ b/x-pack/plugins/lens/common/datasources/text_based/text_based_languages.ts @@ -0,0 +1,52 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { toExpression } from './to_expression'; +import type { + TextBasedPrivateState, + TextBasedPersistedState, +} from '../../../public/datasources/text_based/types'; +import { DatasourceCommon } from '../../types'; + +export const TextBasedDatasourceCommon: DatasourceCommon< + TextBasedPrivateState, + TextBasedPersistedState +> = { + id: 'textBased', + + initialize( + state?: TextBasedPersistedState, + savedObjectReferences?, + context?, + indexPatternRefs?, + indexPatterns? + ) { + const patterns = indexPatterns ? Object.values(indexPatterns) : []; + const refs = patterns.map((p) => { + return { + id: p.id, + title: p.title, + timeField: p.timeFieldName, + }; + }); + + const initState = state || { layers: {} }; + return { + ...initState, + indexPatternRefs: refs, + initialContext: context, + }; + }, + + getLayers(state: TextBasedPrivateState) { + return state && state.layers ? Object.keys(state?.layers) : []; + }, + + toExpression: (state, layerId, indexPatterns, dateRange, searchSessionId) => { + return toExpression(state, layerId); + }, +}; diff --git a/x-pack/plugins/lens/public/datasources/text_based/to_expression.ts b/x-pack/plugins/lens/common/datasources/text_based/to_expression.ts similarity index 89% rename from x-pack/plugins/lens/public/datasources/text_based/to_expression.ts rename to x-pack/plugins/lens/common/datasources/text_based/to_expression.ts index e0c6889a59c79..951912104ca23 100644 --- a/x-pack/plugins/lens/public/datasources/text_based/to_expression.ts +++ b/x-pack/plugins/lens/common/datasources/text_based/to_expression.ts @@ -7,8 +7,12 @@ import { Ast } from '@kbn/interpreter'; import { textBasedQueryStateToExpressionAst } from '@kbn/data-plugin/common'; -import type { OriginalColumn } from '../../../common/types'; -import { TextBasedPrivateState, TextBasedLayer, IndexPatternRef } from './types'; +import type { OriginalColumn } from '../../types'; +import type { + TextBasedPrivateState, + TextBasedLayer, + IndexPatternRef, +} from '../../../public/datasources/text_based/types'; function getExpressionForLayer(layer: TextBasedLayer, refs: IndexPatternRef[]): Ast | null { if (!layer.columns || layer.columns?.length === 0) { diff --git a/x-pack/plugins/lens/common/doc_to_expression.ts b/x-pack/plugins/lens/common/doc_to_expression.ts new file mode 100644 index 0000000000000..85b1808a681aa --- /dev/null +++ b/x-pack/plugins/lens/common/doc_to_expression.ts @@ -0,0 +1,468 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { DEFAULT_COLOR_MAPPING_CONFIG } from '@kbn/coloring'; +import type { SavedObjectReference } from '@kbn/core-saved-objects-common'; +import { + DataViewPersistableStateService, + type DataViewSpec, + type DataViewsService, +} from '@kbn/data-views-plugin/common'; +import type { TimeRange } from '@kbn/es-query'; +import { + EventAnnotationGroupConfig, + EVENT_ANNOTATION_GROUP_TYPE, +} from '@kbn/event-annotation-common'; +import type { EventAnnotationServiceType } from '@kbn/event-annotation-components'; +import type { Ast } from '@kbn/interpreter'; +import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; +import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public'; +import { difference } from 'lodash'; +import type { Document } from '../public/persistence'; +import type { DatasourceStates, VisualizationState } from '../public/state_management'; +import type { + DatasourceMap, + InitializationOptions, + SuggestionRequest, + VisualizationMap, + VisualizeEditorContext, +} from '../public/types'; +import { COLOR_MAPPING_OFF_BY_DEFAULT } from './constants'; +import { + loadIndexPatternRefs, + loadIndexPatterns, + sortDataViewRefs, +} from './data_views_service/loader'; +import { buildExpression } from './expression_helpers'; +import { getDatasourceLayers } from './get_datasource_layers'; +import { readFromStorage } from './settings_storage'; +import type { DatasourceCommonMap, IndexPattern, IndexPatternMap, IndexPatternRef } from './types'; + +function getIndexPatterns( + annotationGroupDataviewIds: string[], + references?: SavedObjectReference[], + initialContext?: VisualizeFieldContext | VisualizeEditorContext, + initialId?: string, + adHocDataviews?: string[] +) { + const indexPatternIds = [...annotationGroupDataviewIds]; + + // use the initialId only when no context is passed over + if (!initialContext && initialId) { + indexPatternIds.push(initialId); + } + if (initialContext) { + if ('isVisualizeAction' in initialContext) { + indexPatternIds.push(...initialContext.indexPatternIds); + } else { + indexPatternIds.push(initialContext.dataViewSpec.id!); + } + } + + if (references) { + for (const reference of references) { + if (reference.type === 'index-pattern') { + indexPatternIds.push(reference.id); + } + } + } + if (adHocDataviews) { + indexPatternIds.push(...adHocDataviews); + } + return [...new Set(indexPatternIds)]; +} + +export interface DocumentToExpressionReturnType { + ast: Ast | null; + indexPatterns: IndexPatternMap; + indexPatternRefs: IndexPatternRef[]; + activeVisualizationState: unknown; +} + +// there are 2 ways of coloring, the color mapping where the user can map specific colors to +// specific terms, and the palette assignment where the colors are assinged automatically +// by a palette with rotating the colors +const COLORING_METHOD: SuggestionRequest['mainPalette'] = COLOR_MAPPING_OFF_BY_DEFAULT + ? { + type: 'legacyPalette', + value: { + name: 'default', + type: 'palette', + }, + } + : { type: 'colorMapping', value: { ...DEFAULT_COLOR_MAPPING_CONFIG } }; + +function initializeVisualization({ + visualizationMap, + visualizationState, + references, + annotationGroups, +}: { + visualizationState: VisualizationState; + visualizationMap: VisualizationMap; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + annotationGroups: Record; +}) { + if (visualizationState?.activeId) { + return ( + visualizationMap[visualizationState.activeId]?.initialize( + () => '', + visualizationState.state, + // initialize a new visualization with the color mapping off + COLORING_METHOD, + annotationGroups, + references + ) ?? visualizationState.state + ); + } + return visualizationState.state; +} + +export const initializeEventAnnotationGroups = async ( + loadAnnotationGroup: (id: string) => Promise, + references?: SavedObjectReference[] +) => { + const annotationGroups: Record = {}; + + await Promise.allSettled( + (references || []) + .filter((ref) => ref.type === EVENT_ANNOTATION_GROUP_TYPE) + .map(({ id }) => + loadAnnotationGroup(id).then((group) => { + annotationGroups[id] = group; + }) + ) + ); + + return annotationGroups; +}; + +const getLastUsedIndexPatternId = ( + storage: IStorageWrapper, + indexPatternRefs: IndexPatternRef[] +) => { + const indexPattern = readFromStorage(storage, 'indexPatternId'); + return indexPattern && indexPatternRefs.find((i) => i.id === indexPattern)?.id; +}; + +const getRefsForAdHocDataViewsFromContext = ( + indexPatternRefs: IndexPatternRef[], + usedIndexPatternsIds: string[], + indexPatterns: Record +) => { + const indexPatternIds = indexPatternRefs.map(({ id }) => id); + const adHocDataViewsIds = usedIndexPatternsIds.filter((id) => !indexPatternIds.includes(id)); + + const adHocDataViewsList = Object.values(indexPatterns).filter(({ id }) => + adHocDataViewsIds.includes(id) + ); + return adHocDataViewsList.map(({ id, title, name }) => ({ id, title, name })); +}; + +async function initializeDataViews( + { + dataViews, + datasourceMap, + datasourceStates, + storage, + defaultIndexPatternId, + references, + initialContext, + adHocDataViews: persistedAdHocDataViews, + annotationGroups, + }: { + dataViews: DataViewsService; + datasourceMap: DatasourceMap; + datasourceStates: DatasourceStates; + defaultIndexPatternId: string; + storage?: IStorageWrapper; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + adHocDataViews?: Record; + annotationGroups: Record; + }, + options?: InitializationOptions +) { + const adHocDataViews = Object.fromEntries( + Object.entries(persistedAdHocDataViews || {}).map(([id, persistedSpec]) => { + const spec = DataViewPersistableStateService.inject(persistedSpec, references || []); + return [id, spec]; + }) + ); + + const annotationGroupValues = Object.values(annotationGroups); + for (const group of annotationGroupValues) { + if (group.dataViewSpec?.id) { + adHocDataViews[group.dataViewSpec.id] = group.dataViewSpec; + } + } + + const { isFullEditor } = options ?? {}; + + // make it explicit or TS will infer never[] and break few lines down + const indexPatternRefs: IndexPatternRef[] = await (isFullEditor + ? loadIndexPatternRefs(dataViews) + : []); + + // if no state is available, use the fallbackId + const lastUsedIndexPatternId = storage + ? getLastUsedIndexPatternId(storage, indexPatternRefs) + : undefined; + const fallbackId = lastUsedIndexPatternId || defaultIndexPatternId || indexPatternRefs[0]?.id; + const initialId = + !initialContext && + Object.keys(datasourceMap).every((datasourceId) => !datasourceStates[datasourceId]?.state) + ? fallbackId + : undefined; + + const adHocDataviewsIds: string[] = Object.keys(adHocDataViews || {}); + + const usedIndexPatternsIds = getIndexPatterns( + annotationGroupValues.map((group) => group.indexPatternId), + references, + initialContext, + initialId, + adHocDataviewsIds + ); + + // load them + const availableIndexPatterns = new Set(indexPatternRefs.map(({ id }: IndexPatternRef) => id)); + + const notUsedPatterns: string[] = difference([...availableIndexPatterns], usedIndexPatternsIds); + + const indexPatterns = await loadIndexPatterns({ + dataViews, + patterns: usedIndexPatternsIds, + notUsedPatterns, + cache: {}, + adHocDataViews, + }); + + const adHocDataViewsRefs = getRefsForAdHocDataViewsFromContext( + indexPatternRefs, + usedIndexPatternsIds, + indexPatterns + ); + return { + indexPatternRefs: sortDataViewRefs([...indexPatternRefs, ...adHocDataViewsRefs]), + indexPatterns, + }; +} + +export function initializeDatasources({ + datasourceMap, + datasourceStates, + indexPatternRefs, + indexPatterns, + references, + initialContext, +}: { + datasourceMap: DatasourceCommonMap; + datasourceStates: DatasourceStates; + indexPatterns: Record; + indexPatternRefs: IndexPatternRef[]; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; +}) { + // init datasources + const states: DatasourceStates = {}; + for (const [datasourceId, datasource] of Object.entries(datasourceMap)) { + if (datasourceStates[datasourceId]) { + const state = datasource.initialize( + datasourceStates[datasourceId].state || undefined, + references, + initialContext, + indexPatternRefs, + indexPatterns + ); + states[datasourceId] = { isLoading: false, state }; + } + } + return states; +} + +export function getActiveDatasourceIdFromDoc(doc?: Document) { + if (!doc) { + return null; + } + + const [firstDatasourceFromDoc] = Object.keys(doc.state.datasourceStates); + return firstDatasourceFromDoc || null; +} + +/** + * This function composes both initializeDataViews & initializeDatasources into a single call + */ +export async function initializeSources( + { + dataViews, + eventAnnotationService, + datasourceMap, + visualizationMap, + visualizationState, + datasourceStates, + storage, + defaultIndexPatternId, + references, + initialContext, + adHocDataViews, + }: { + dataViews: DataViewsService; + eventAnnotationService: EventAnnotationServiceType; + datasourceMap: DatasourceMap; + visualizationMap: VisualizationMap; + visualizationState: VisualizationState; + datasourceStates: DatasourceStates; + defaultIndexPatternId: string; + storage?: IStorageWrapper; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + adHocDataViews?: Record; + }, + options?: InitializationOptions +) { + const annotationGroups = await initializeEventAnnotationGroups( + eventAnnotationService.loadAnnotationGroup, + references + ); + + const { indexPatternRefs, indexPatterns } = await initializeDataViews( + { + datasourceMap, + datasourceStates, + initialContext, + dataViews, + storage, + defaultIndexPatternId, + references, + adHocDataViews, + annotationGroups, + }, + options + ); + + return { + indexPatterns, + indexPatternRefs, + annotationGroups, + datasourceStates: initializeDatasources({ + datasourceMap, + datasourceStates, + initialContext, + indexPatternRefs, + indexPatterns, + references, + }), + visualizationState: initializeVisualization({ + visualizationMap, + visualizationState, + references, + initialContext, + annotationGroups, + }), + }; +} + +export async function docToExpression( + datasourceMap: DatasourceMap, + visualizations: VisualizationMap, + doc: Document, + defaultIndexPatternId: string, + currentTimeRange: TimeRange, + loadAnnotationGroup: (id: string) => Promise, + nowInstant: Date, + services: { + storage?: IStorageWrapper; + dataViews: DataViewsService; + } +): Promise { + const { + state: { + visualization: persistedVisualizationState, + datasourceStates: persistedDatasourceStates, + adHocDataViews, + internalReferences, + }, + visualizationType, + references, + title, + description, + } = doc; + if (!visualizationType) { + return { ast: null, indexPatterns: {}, indexPatternRefs: [], activeVisualizationState: null }; + } + + const annotationGroups = await initializeEventAnnotationGroups(loadAnnotationGroup, references); + + const visualization = visualizations[visualizationType!]; + const activeVisualizationState = initializeVisualization({ + visualizationMap: visualizations, + visualizationState: { + state: persistedVisualizationState, + activeId: visualizationType, + }, + annotationGroups, + references: [...references, ...(internalReferences || [])], + }); + const datasourceStatesFromSO = Object.fromEntries( + Object.entries(persistedDatasourceStates).map(([id, state]) => [ + id, + { isLoading: false, state }, + ]) + ); + const { indexPatterns, indexPatternRefs } = await initializeDataViews( + { + datasourceMap, + datasourceStates: datasourceStatesFromSO, + references, + dataViews: services.dataViews, + storage: services.storage, + defaultIndexPatternId, + adHocDataViews, + annotationGroups, + }, + { isFullEditor: false } + ); + const datasourceStates = initializeDatasources({ + datasourceMap, + datasourceStates: datasourceStatesFromSO, + references: [...references, ...(internalReferences || [])], + indexPatterns, + indexPatternRefs, + }); + + const datasourceLayers = getDatasourceLayers(datasourceStates, datasourceMap, indexPatterns); + + const datasourceId = getActiveDatasourceIdFromDoc(doc); + if (datasourceId == null) { + return { + ast: null, + indexPatterns, + indexPatternRefs, + activeVisualizationState, + }; + } + + return { + ast: buildExpression({ + title, + description, + visualization, + visualizationState: activeVisualizationState, + datasourceMap, + datasourceStates, + datasourceLayers, + indexPatterns, + dateRange: { fromDate: currentTimeRange.from, toDate: currentTimeRange.to }, + nowInstant, + }), + activeVisualizationState, + indexPatterns, + indexPatternRefs, + }; +} diff --git a/x-pack/plugins/lens/public/datasources/form_based/document_field.ts b/x-pack/plugins/lens/common/document_field.ts similarity index 85% rename from x-pack/plugins/lens/public/datasources/form_based/document_field.ts rename to x-pack/plugins/lens/common/document_field.ts index 1277f5b3d8014..a5c65fe6759b7 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/document_field.ts +++ b/x-pack/plugins/lens/common/document_field.ts @@ -6,8 +6,8 @@ */ import { i18n } from '@kbn/i18n'; -import { DOCUMENT_FIELD_NAME } from '../../../common/constants'; -import type { IndexPatternField } from '../../types'; +import type { IndexPatternField } from '../public/types'; +import { DOCUMENT_FIELD_NAME } from './constants'; const customLabel = i18n.translate('xpack.lens.indexPattern.records', { defaultMessage: 'Records', diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/expression_helpers.ts b/x-pack/plugins/lens/common/expression_helpers.ts similarity index 89% rename from x-pack/plugins/lens/public/editor_frame_service/editor_frame/expression_helpers.ts rename to x-pack/plugins/lens/common/expression_helpers.ts index 424e91d1a007d..327db95630ee0 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/expression_helpers.ts +++ b/x-pack/plugins/lens/common/expression_helpers.ts @@ -4,13 +4,13 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { Ast, fromExpression } from '@kbn/interpreter'; -import type { DateRange } from '../../../common/types'; -import { DatasourceStates } from '../../state_management'; -import type { Visualization, DatasourceMap, DatasourceLayers, IndexPatternMap } from '../../types'; +import { type Ast, fromExpression } from '@kbn/interpreter'; +import type { DatasourceCommonMap, DateRange, IndexPatternMap } from './types'; +import type { DatasourceStates } from '../public/state_management'; +import type { Visualization, DatasourceMap, DatasourceLayers } from '../public/types'; export function getDatasourceExpressionsByLayers( - datasourceMap: DatasourceMap, + datasourceMap: DatasourceCommonMap, datasourceStates: DatasourceStates, indexPatterns: IndexPatternMap, dateRange: DateRange, diff --git a/x-pack/plugins/lens/common/get_datasource_layers.ts b/x-pack/plugins/lens/common/get_datasource_layers.ts new file mode 100644 index 0000000000000..b7a9ad94fffd8 --- /dev/null +++ b/x-pack/plugins/lens/common/get_datasource_layers.ts @@ -0,0 +1,34 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import memoizeOne from 'memoize-one'; +import type { DatasourceStates, DataViewsState } from '../public/state_management'; +import type { DatasourceLayers, DatasourceMap } from '../public/types'; + +export const getDatasourceLayers = memoizeOne(function getDatasourceLayers( + datasourceStates: DatasourceStates, + datasourceMap: DatasourceMap, + indexPatterns: DataViewsState['indexPatterns'] +) { + const datasourceLayers: DatasourceLayers = {}; + Object.keys(datasourceMap) + .filter((id) => datasourceStates[id] && !datasourceStates[id].isLoading) + .forEach((id) => { + const datasourceState = datasourceStates[id].state; + const datasource = datasourceMap[id]; + + const layers = datasource.getLayers(datasourceState); + layers.forEach((layer) => { + datasourceLayers[layer] = datasourceMap[id].getPublicAPI({ + state: datasourceState, + layerId: layer, + indexPatterns, + }); + }); + }); + return datasourceLayers; +}); diff --git a/x-pack/plugins/lens/public/id_generator/id_generator.test.ts b/x-pack/plugins/lens/common/id_generator/id_generator.test.ts similarity index 100% rename from x-pack/plugins/lens/public/id_generator/id_generator.test.ts rename to x-pack/plugins/lens/common/id_generator/id_generator.test.ts diff --git a/x-pack/plugins/lens/public/id_generator/id_generator.ts b/x-pack/plugins/lens/common/id_generator/id_generator.ts similarity index 100% rename from x-pack/plugins/lens/public/id_generator/id_generator.ts rename to x-pack/plugins/lens/common/id_generator/id_generator.ts diff --git a/x-pack/plugins/lens/public/id_generator/index.ts b/x-pack/plugins/lens/common/id_generator/index.ts similarity index 100% rename from x-pack/plugins/lens/public/id_generator/index.ts rename to x-pack/plugins/lens/common/id_generator/index.ts diff --git a/x-pack/plugins/lens/common/index.ts b/x-pack/plugins/lens/common/index.ts index def0021c36bbd..f4c0d99f806a3 100644 --- a/x-pack/plugins/lens/common/index.ts +++ b/x-pack/plugins/lens/common/index.ts @@ -6,3 +6,4 @@ */ export { DOCUMENT_FIELD_NAME } from './constants'; export type { PersistableFilter, LegacyMetricState } from './types'; +export { docToExpression } from './doc_to_expression'; diff --git a/x-pack/plugins/lens/common/settings_storage.tsx b/x-pack/plugins/lens/common/settings_storage.tsx new file mode 100644 index 0000000000000..7a4aae49b85bc --- /dev/null +++ b/x-pack/plugins/lens/common/settings_storage.tsx @@ -0,0 +1,23 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; + +export interface LocalStorageLens { + indexPatternId?: string; + skipDeleteModal?: boolean; +} + +export const LOCAL_STORAGE_LENS_KEY = 'lens-settings'; + +export const readFromStorage = (storage: IStorageWrapper, key: string) => { + const data = storage.get(LOCAL_STORAGE_LENS_KEY); + return data && data[key]; +}; +export const writeToStorage = (storage: IStorageWrapper, key: string, value: string) => { + storage.set(LOCAL_STORAGE_LENS_KEY, { ...storage.get(LOCAL_STORAGE_LENS_KEY), [key]: value }); +}; diff --git a/x-pack/plugins/lens/common/state_helpers.ts b/x-pack/plugins/lens/common/state_helpers.ts new file mode 100644 index 0000000000000..871c1805709a5 --- /dev/null +++ b/x-pack/plugins/lens/common/state_helpers.ts @@ -0,0 +1,486 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { SavedObjectReference } from '@kbn/core/public'; +import type { Ast } from '@kbn/interpreter'; +import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public'; +import { difference } from 'lodash'; +import type { DataViewSpec } from '@kbn/data-views-plugin/public'; +import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; +import { DEFAULT_COLOR_MAPPING_CONFIG } from '@kbn/coloring'; +import { + DataViewPersistableStateService, + type DataViewsService, +} from '@kbn/data-views-plugin/common'; +import type { DataPublicPluginStart } from '@kbn/data-plugin/public'; +import type { EventAnnotationServiceType } from '@kbn/event-annotation-plugin/public'; +import { + type EventAnnotationGroupConfig, + EVENT_ANNOTATION_GROUP_TYPE, +} from '@kbn/event-annotation-common'; +import type { TimeRange } from '@kbn/es-query'; +import { COLOR_MAPPING_OFF_BY_DEFAULT } from './constants'; + +import type { + Datasource, + DatasourceMap, + InitializationOptions, + VisualizationMap, + VisualizeEditorContext, + SuggestionRequest, +} from '../public/types'; +import { buildExpression } from './expression_helpers'; +import type { Document } from '../public/persistence/saved_object_store'; +import type { + DatasourceState, + DatasourceStates, + VisualizationState, +} from '../public/state_management'; +import { getActiveDatasourceIdFromDoc } from './doc_to_expression'; +import { getDatasourceLayers } from './get_datasource_layers'; +import { + loadIndexPatternRefs, + loadIndexPatterns, + sortDataViewRefs, +} from './data_views_service/loader'; +import { readFromStorage } from './settings_storage'; +import { DatasourceCommonMap, IndexPattern, IndexPatternMap, IndexPatternRef } from './types'; + +// there are 2 ways of coloring, the color mapping where the user can map specific colors to +// specific terms, and the palette assignment where the colors are assinged automatically +// by a palette with rotating the colors +const COLORING_METHOD: SuggestionRequest['mainPalette'] = COLOR_MAPPING_OFF_BY_DEFAULT + ? { + type: 'legacyPalette', + value: { + name: 'default', + type: 'palette', + }, + } + : { type: 'colorMapping', value: { ...DEFAULT_COLOR_MAPPING_CONFIG } }; + +function getIndexPatterns( + annotationGroupDataviewIds: string[], + references?: SavedObjectReference[], + initialContext?: VisualizeFieldContext | VisualizeEditorContext, + initialId?: string, + adHocDataviews?: string[] +) { + const indexPatternIds = [...annotationGroupDataviewIds]; + + // use the initialId only when no context is passed over + if (!initialContext && initialId) { + indexPatternIds.push(initialId); + } + if (initialContext) { + if ('isVisualizeAction' in initialContext) { + indexPatternIds.push(...initialContext.indexPatternIds); + } else { + indexPatternIds.push(initialContext.dataViewSpec.id!); + } + } + + if (references) { + for (const reference of references) { + if (reference.type === 'index-pattern') { + indexPatternIds.push(reference.id); + } + } + } + if (adHocDataviews) { + indexPatternIds.push(...adHocDataviews); + } + return [...new Set(indexPatternIds)]; +} + +const getLastUsedIndexPatternId = ( + storage: IStorageWrapper, + indexPatternRefs: IndexPatternRef[] +) => { + const indexPattern = readFromStorage(storage, 'indexPatternId'); + return indexPattern && indexPatternRefs.find((i) => i.id === indexPattern)?.id; +}; + +const getRefsForAdHocDataViewsFromContext = ( + indexPatternRefs: IndexPatternRef[], + usedIndexPatternsIds: string[], + indexPatterns: Record +) => { + const indexPatternIds = indexPatternRefs.map(({ id }) => id); + const adHocDataViewsIds = usedIndexPatternsIds.filter((id) => !indexPatternIds.includes(id)); + + const adHocDataViewsList = Object.values(indexPatterns).filter(({ id }) => + adHocDataViewsIds.includes(id) + ); + return adHocDataViewsList.map(({ id, title, name }) => ({ id, title, name })); +}; + +export async function initializeDataViews( + { + dataViews, + datasourceMap, + datasourceStates, + storage, + defaultIndexPatternId, + references, + initialContext, + adHocDataViews: persistedAdHocDataViews, + annotationGroups, + }: { + dataViews: DataViewsService; + datasourceMap: DatasourceCommonMap; + datasourceStates: DatasourceStates; + defaultIndexPatternId: string; + storage?: IStorageWrapper; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + adHocDataViews?: Record; + annotationGroups: Record; + }, + options?: InitializationOptions +) { + const adHocDataViews = Object.fromEntries( + Object.entries(persistedAdHocDataViews || {}).map(([id, persistedSpec]) => { + const spec = DataViewPersistableStateService.inject(persistedSpec, references || []); + return [id, spec]; + }) + ); + + const annotationGroupValues = Object.values(annotationGroups); + for (const group of annotationGroupValues) { + if (group.dataViewSpec?.id) { + adHocDataViews[group.dataViewSpec.id] = group.dataViewSpec; + } + } + + const { isFullEditor } = options ?? {}; + + // make it explicit or TS will infer never[] and break few lines down + const indexPatternRefs: IndexPatternRef[] = await (isFullEditor + ? loadIndexPatternRefs(dataViews) + : []); + + // if no state is available, use the fallbackId + const lastUsedIndexPatternId = storage + ? getLastUsedIndexPatternId(storage, indexPatternRefs) + : undefined; + const fallbackId = lastUsedIndexPatternId || defaultIndexPatternId || indexPatternRefs[0]?.id; + const initialId = + !initialContext && + Object.keys(datasourceMap).every((datasourceId) => !datasourceStates[datasourceId]?.state) + ? fallbackId + : undefined; + + const adHocDataviewsIds: string[] = Object.keys(adHocDataViews || {}); + + const usedIndexPatternsIds = getIndexPatterns( + annotationGroupValues.map((group) => group.indexPatternId), + references, + initialContext, + initialId, + adHocDataviewsIds + ); + + // load them + const availableIndexPatterns = new Set(indexPatternRefs.map(({ id }: IndexPatternRef) => id)); + + const notUsedPatterns: string[] = difference([...availableIndexPatterns], usedIndexPatternsIds); + + const indexPatterns = await loadIndexPatterns({ + dataViews, + patterns: usedIndexPatternsIds, + notUsedPatterns, + cache: {}, + adHocDataViews, + }); + + const adHocDataViewsRefs = getRefsForAdHocDataViewsFromContext( + indexPatternRefs, + usedIndexPatternsIds, + indexPatterns + ); + return { + indexPatternRefs: sortDataViewRefs([...indexPatternRefs, ...adHocDataViewsRefs]), + indexPatterns, + }; +} + +const initializeEventAnnotationGroups = async ( + loadAnnotationGroup: (id: string) => Promise, + references?: SavedObjectReference[] +) => { + const annotationGroups: Record = {}; + + await Promise.allSettled( + (references || []) + .filter((ref) => ref.type === EVENT_ANNOTATION_GROUP_TYPE) + .map(({ id }) => + loadAnnotationGroup(id).then((group) => { + annotationGroups[id] = group; + }) + ) + ); + + return annotationGroups; +}; + +/** + * This function composes both initializeDataViews & initializeDatasources into a single call + */ +export async function initializeSources( + { + dataViews, + eventAnnotationService, + datasourceMap, + visualizationMap, + visualizationState, + datasourceStates, + storage, + defaultIndexPatternId, + references, + initialContext, + adHocDataViews, + }: { + dataViews: DataViewsService; + eventAnnotationService: EventAnnotationServiceType; + datasourceMap: DatasourceMap; + visualizationMap: VisualizationMap; + visualizationState: VisualizationState; + datasourceStates: DatasourceStates; + defaultIndexPatternId: string; + storage?: IStorageWrapper; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + adHocDataViews?: Record; + }, + options?: InitializationOptions +) { + const annotationGroups = await initializeEventAnnotationGroups( + eventAnnotationService.loadAnnotationGroup, + references + ); + + const { indexPatternRefs, indexPatterns } = await initializeDataViews( + { + datasourceMap, + datasourceStates, + initialContext, + dataViews, + storage, + defaultIndexPatternId, + references, + adHocDataViews, + annotationGroups, + }, + options + ); + + return { + indexPatterns, + indexPatternRefs, + annotationGroups, + datasourceStates: initializeDatasources({ + datasourceMap, + datasourceStates, + initialContext, + indexPatternRefs, + indexPatterns, + references, + }), + visualizationState: initializeVisualization({ + visualizationMap, + visualizationState, + references, + initialContext, + annotationGroups, + }), + }; +} + +export function initializeVisualization({ + visualizationMap, + visualizationState, + references, + annotationGroups, +}: { + visualizationState: VisualizationState; + visualizationMap: VisualizationMap; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; + annotationGroups: Record; +}) { + if (visualizationState?.activeId) { + return ( + visualizationMap[visualizationState.activeId]?.initialize( + () => '', + visualizationState.state, + // initialize a new visualization with the color mapping off + COLORING_METHOD, + annotationGroups, + references + ) ?? visualizationState.state + ); + } + return visualizationState.state; +} + +export function initializeDatasources({ + datasourceMap, + datasourceStates, + indexPatternRefs, + indexPatterns, + references, + initialContext, +}: { + datasourceMap: DatasourceMap; + datasourceStates: DatasourceStates; + indexPatterns: Record; + indexPatternRefs: IndexPatternRef[]; + references?: SavedObjectReference[]; + initialContext?: VisualizeFieldContext | VisualizeEditorContext; +}) { + // init datasources + const states: DatasourceStates = {}; + for (const [datasourceId, datasource] of Object.entries(datasourceMap)) { + if (datasourceStates[datasourceId]) { + const state = datasource.initialize( + datasourceStates[datasourceId].state || undefined, + references, + initialContext, + indexPatternRefs, + indexPatterns + ); + states[datasourceId] = { isLoading: false, state }; + } + } + return states; +} + +export interface DocumentToExpressionReturnType { + ast: Ast | null; + indexPatterns: IndexPatternMap; + indexPatternRefs: IndexPatternRef[]; + activeVisualizationState: unknown; +} + +export async function persistedStateToExpression( + datasourceMap: DatasourceMap, + visualizations: VisualizationMap, + doc: Document, + defaultIndexPatternId: string, + currentTimeRange: TimeRange, + loadAnnotationGroup: (id: string) => Promise, + services: { + storage?: IStorageWrapper; + dataViews: DataViewsService; + nowProvider: DataPublicPluginStart['nowProvider']; + } +): Promise { + const { + state: { + visualization: persistedVisualizationState, + datasourceStates: persistedDatasourceStates, + adHocDataViews, + internalReferences, + }, + visualizationType, + references, + title, + description, + } = doc; + if (!visualizationType) { + return { ast: null, indexPatterns: {}, indexPatternRefs: [], activeVisualizationState: null }; + } + + const annotationGroups = await initializeEventAnnotationGroups(loadAnnotationGroup, references); + + const visualization = visualizations[visualizationType!]; + const activeVisualizationState = initializeVisualization({ + visualizationMap: visualizations, + visualizationState: { + state: persistedVisualizationState, + activeId: visualizationType, + }, + annotationGroups, + references: [...references, ...(internalReferences || [])], + }); + const datasourceStatesFromSO = Object.fromEntries( + Object.entries(persistedDatasourceStates).map(([id, state]) => [ + id, + { isLoading: false, state }, + ]) + ); + const { indexPatterns, indexPatternRefs } = await initializeDataViews( + { + datasourceMap, + datasourceStates: datasourceStatesFromSO, + references, + dataViews: services.dataViews, + storage: services.storage, + defaultIndexPatternId, + adHocDataViews, + annotationGroups, + }, + { isFullEditor: false } + ); + const datasourceStates = initializeDatasources({ + datasourceMap, + datasourceStates: datasourceStatesFromSO, + references: [...references, ...(internalReferences || [])], + indexPatterns, + indexPatternRefs, + }); + + const datasourceLayers = getDatasourceLayers(datasourceStates, datasourceMap, indexPatterns); + + const datasourceId = getActiveDatasourceIdFromDoc(doc); + if (datasourceId == null) { + return { + ast: null, + indexPatterns, + indexPatternRefs, + activeVisualizationState, + }; + } + + return { + ast: buildExpression({ + title, + description, + visualization, + visualizationState: activeVisualizationState, + datasourceMap, + datasourceStates, + datasourceLayers, + indexPatterns, + dateRange: { fromDate: currentTimeRange.from, toDate: currentTimeRange.to }, + nowInstant: services.nowProvider.get(), + }), + activeVisualizationState, + indexPatterns, + indexPatternRefs, + }; +} + +export function getMissingIndexPattern( + currentDatasource: Datasource | null | undefined, + currentDatasourceState: DatasourceState | null, + indexPatterns: IndexPatternMap +) { + if ( + currentDatasourceState?.isLoading || + currentDatasourceState?.state == null || + currentDatasource == null + ) { + return []; + } + const missingIds = currentDatasource.checkIntegrity(currentDatasourceState.state, indexPatterns); + if (!missingIds.length) { + return []; + } + return missingIds; +} diff --git a/x-pack/plugins/lens/common/types.ts b/x-pack/plugins/lens/common/types.ts index 34baa25120e09..493da4279d59e 100644 --- a/x-pack/plugins/lens/common/types.ts +++ b/x-pack/plugins/lens/common/types.ts @@ -11,9 +11,16 @@ import type { $Values } from '@kbn/utility-types'; import { CustomPaletteParams, PaletteOutput, ColorMapping } from '@kbn/coloring'; import type { ColorMode } from '@kbn/charts-plugin/common'; import type { LegendSize } from '@kbn/visualizations-plugin/common'; -import { CategoryDisplay, LegendDisplay, NumberDisplay, PieChartTypes } from './constants'; -import { layerTypes } from './layer_types'; +import { SavedObjectReference } from '@kbn/core-saved-objects-common'; +import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public'; +import type { IndexPatternAggRestrictions } from '@kbn/data-plugin/public'; +import { DataViewSpec, FieldSpec } from '@kbn/data-views-plugin/common'; +import { FieldFormatParams } from '@kbn/field-formats-plugin/common'; +import { ExpressionAstExpression } from '@kbn/expressions-plugin/common'; +import type { VisualizeEditorContext } from '../public/types'; import { CollapseFunction } from './expressions'; +import { layerTypes } from './layer_types'; +import { CategoryDisplay, LegendDisplay, NumberDisplay, PieChartTypes } from './constants'; export type { OriginalColumn } from './expressions/map_to_columns'; export type { AllowedPartitionOverrides } from '@kbn/expression-partition-vis-plugin/common'; @@ -95,3 +102,74 @@ export interface LegacyMetricState { size?: string; textAlign?: 'left' | 'right' | 'center'; } + +export interface IndexPatternRef { + id: string; + title: string; + name?: string; +} + +export type IndexPatternField = FieldSpec & { + displayName: string; + aggregationRestrictions?: Partial; + /** + * Map of fields which can be used, but may fail partially (ranked lower than others) + */ + partiallyApplicableFunctions?: Partial>; + timeSeriesMetric?: 'histogram' | 'summary' | 'gauge' | 'counter' | 'position'; + timeSeriesRollup?: boolean; + meta?: boolean; + runtime?: boolean; +}; + +export interface IndexPattern { + id: string; + fields: IndexPatternField[]; + getFieldByName(name: string): IndexPatternField | undefined; + title: string; + name?: string; + timeFieldName?: string; + fieldFormatMap?: Record< + string, + { + id: string; + params: FieldFormatParams; + } + >; + hasRestrictions: boolean; + spec: DataViewSpec; + isPersisted: boolean; +} + +export type IndexPatternMap = Record; + +/** + * A subset of datasource methods used on both client and server + */ +export interface DatasourceCommon { + id: string; + + // For initializing, either from an empty state or from persisted state + // Because this will be called at runtime, state might have a type of `any` and + // datasources should validate their arguments + initialize: ( + state?: P, + savedObjectReferences?: SavedObjectReference[], + initialContext?: VisualizeFieldContext | VisualizeEditorContext, + indexPatternRefs?: IndexPatternRef[], + indexPatterns?: IndexPatternMap + ) => T; + + getLayers: (state: T) => string[]; + + toExpression: ( + state: T, + layerId: string, + indexPatterns: IndexPatternMap, + dateRange: DateRange, + nowInstant: Date, + searchSessionId?: string + ) => ExpressionAstExpression | string | null; +} + +export type DatasourceCommonMap = Record; diff --git a/x-pack/plugins/lens/kibana.jsonc b/x-pack/plugins/lens/kibana.jsonc index 1813264f7ca57..add1658514cd0 100644 --- a/x-pack/plugins/lens/kibana.jsonc +++ b/x-pack/plugins/lens/kibana.jsonc @@ -21,7 +21,6 @@ "navigation", "urlForwarding", "visualizations", - "dashboard", "uiActions", "uiActionsEnhanced", "embeddable", diff --git a/x-pack/plugins/lens/public/app_plugin/app.tsx b/x-pack/plugins/lens/public/app_plugin/app.tsx index 39a614b568799..b4de22afa96e1 100644 --- a/x-pack/plugins/lens/public/app_plugin/app.tsx +++ b/x-pack/plugins/lens/public/app_plugin/app.tsx @@ -38,7 +38,7 @@ import { isLensEqual } from './lens_document_equality'; import { type IndexPatternServiceAPI, createIndexPatternService, -} from '../data_views_service/service'; +} from '../../common/data_views_service/service'; import { replaceIndexpattern } from '../state_management/lens_slice'; import { useApplicationUserMessages } from './get_application_user_messages'; diff --git a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/get_edit_lens_configuration.tsx b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/get_edit_lens_configuration.tsx index 91b094c141161..4695d91d7fc53 100644 --- a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/get_edit_lens_configuration.tsx +++ b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/get_edit_lens_configuration.tsx @@ -23,7 +23,7 @@ import { initExisting, initEmpty, } from '../../../state_management'; -import { generateId } from '../../../id_generator'; +import { generateId } from '../../../../common/id_generator'; import type { DatasourceMap, VisualizationMap } from '../../../types'; import { LensEditConfigurationFlyout } from './lens_configuration_flyout'; import type { EditConfigPanelProps } from './types'; diff --git a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/layer_configuration_section.tsx b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/layer_configuration_section.tsx index 86f2cde79ee56..8dc4662271f98 100644 --- a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/layer_configuration_section.tsx +++ b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/layer_configuration_section.tsx @@ -10,7 +10,7 @@ import { EuiSpacer, useEuiTheme } from '@elastic/eui'; import { css } from '@emotion/react'; import { VisualizationToolbar } from '../../../editor_frame_service/editor_frame/workspace_panel'; import { ConfigPanelWrapper } from '../../../editor_frame_service/editor_frame/config_panel/config_panel'; -import { createIndexPatternService } from '../../../data_views_service/service'; +import { createIndexPatternService } from '../../../../common/data_views_service/service'; import { useLensDispatch, updateIndexPatterns } from '../../../state_management'; import { replaceIndexpattern } from '../../../state_management/lens_slice'; import type { LayerConfigurationProps } from './types'; diff --git a/x-pack/plugins/lens/public/async_services.ts b/x-pack/plugins/lens/public/async_services.ts index 7bbfaf415db03..2eb1072ef447a 100644 --- a/x-pack/plugins/lens/public/async_services.ts +++ b/x-pack/plugins/lens/public/async_services.ts @@ -34,7 +34,7 @@ export { getEditLensConfiguration } from './app_plugin/shared/edit_on_the_fly/ge export * from './datasources/form_based/form_based'; export { getTextBasedDatasource } from './datasources/text_based/text_based_languages'; -export { createFormulaPublicApi } from './datasources/form_based/operations/definitions/formula/formula_public_api'; +export { createFormulaPublicApi } from '../common/datasources/form_based/operations/definitions/formula/formula_public_api'; export * from './lens_suggestions_api'; export * from './datasources/text_based'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx index 01a31f98000f9..7bb19098330eb 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/datapanel.test.tsx @@ -28,7 +28,7 @@ import { fieldFormatsServiceMock } from '@kbn/field-formats-plugin/public/mocks' import { indexPatternFieldEditorPluginMock } from '@kbn/data-view-field-editor-plugin/public/mocks'; import { getFieldByNameFactory } from './pure_helpers'; import { uiActionsPluginMock } from '@kbn/ui-actions-plugin/public/mocks'; -import { TermsIndexPatternColumn } from './operations'; +import { TermsIndexPatternColumn } from '../../../common/datasources/form_based/operations'; import { DOCUMENT_FIELD_NAME } from '../../../common/constants'; import { createIndexPatternServiceMock } from '../../mocks/data_views_service_mock'; import { createMockFramePublicAPI } from '../../mocks'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/advanced_options.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/advanced_options.tsx index c8cbfb947a9e3..45b38cc0b0aaf 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/advanced_options.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/advanced_options.tsx @@ -9,7 +9,7 @@ import { EuiSpacer, EuiAccordion, EuiTextColor, EuiTitle, useEuiTheme } from '@e import { i18n } from '@kbn/i18n'; import React from 'react'; import { css } from '@emotion/react'; -import { AdvancedOption } from '../operations/definitions'; +import { AdvancedOption } from '../../../../common/datasources/form_based/operations/definitions'; export function AdvancedOptions(props: { options: AdvancedOption[] }) { const { euiTheme } = useEuiTheme(); diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/bucket_nesting_editor.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/bucket_nesting_editor.tsx index f8e7584662b0c..7dd5b122c1571 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/bucket_nesting_editor.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/bucket_nesting_editor.tsx @@ -9,8 +9,8 @@ import React from 'react'; import { i18n } from '@kbn/i18n'; import { EuiFormRow, EuiSwitch, EuiSelect, EuiSpacer, EuiText } from '@elastic/eui'; import { FormBasedLayer } from '../types'; -import { hasField } from '../pure_utils'; -import { GenericIndexPatternColumn } from '../operations'; +import { hasField } from '../../../../common/datasources/form_based/pure_utils'; +import { GenericIndexPatternColumn } from '../../../../common/datasources/form_based/operations'; import { IndexPatternField } from '../../../types'; function nestColumn(columnOrder: string[], outer: string, inner: string) { diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor.tsx index ca23cc220e3dd..e88b5ed881839 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor.tsx @@ -42,10 +42,14 @@ import { FieldBasedIndexPatternColumn, canTransition, adjustColumnReferencesForChangedColumn, -} from '../operations'; +} from '../../../../common/datasources/form_based/operations'; import { mergeLayer } from '../state_helpers'; -import { getReferencedField, hasField } from '../pure_utils'; -import { fieldIsInvalid, getSamplingValue, isSamplingValueEnabled } from '../utils'; +import { getReferencedField, hasField } from '../../../../common/datasources/form_based/pure_utils'; +import { + fieldIsInvalid, + getSamplingValue, + isSamplingValueEnabled, +} from '../../../../common/datasources/form_based/utils'; import { BucketNestingEditor } from './bucket_nesting_editor'; import type { FormBasedLayer } from '../types'; import { FormatSelector } from './format_selector'; @@ -71,12 +75,12 @@ import { } from './dimensions_editor_helpers'; import type { TemporaryState } from './dimensions_editor_helpers'; import { FieldInput } from './field_input'; -import { ParamEditorProps } from '../operations/definitions'; +import { ParamEditorProps } from '../../../../common/datasources/form_based/operations/definitions'; import { WrappingHelpPopover } from '../help_popover'; -import { isColumn } from '../operations/definitions/helpers'; +import { isColumn } from '../../../../common/datasources/form_based/operations/definitions/helpers'; import type { FieldChoiceWithOperationType } from './field_select'; import type { IndexPattern, IndexPatternField } from '../../../types'; -import { documentField } from '../document_field'; +import { documentField } from '../../../../common/document_field'; export interface DimensionEditorProps extends FormBasedDimensionEditorProps { selectedColumn?: GenericIndexPatternColumn; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor_helpers.test.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor_helpers.test.ts index 916733cc652ff..5743eee39f039 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor_helpers.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_editor_helpers.test.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { TermsIndexPatternColumn } from '../operations'; +import type { TermsIndexPatternColumn } from '../../../../common/datasources/form_based/operations'; import type { FormBasedLayer } from '../types'; import { isLayerChangingDueToOtherBucketChange } from './dimensions_editor_helpers'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx index 86ac619e9dcaa..ef5c3f625918f 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimension_panel.test.tsx @@ -29,7 +29,7 @@ import { mountWithIntl as mount } from '@kbn/test-jest-helpers'; import { IUiSettingsClient, HttpSetup, CoreStart, NotificationsStart } from '@kbn/core/public'; import { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import { useExistingFieldsReader } from '@kbn/unified-field-list/src/hooks/use_existing_fields'; -import { generateId } from '../../../id_generator'; +import { generateId } from '../../../../common/id_generator'; import { FormBasedPrivateState } from '../types'; import { LayerTypes } from '@kbn/expression-xy-plugin/public'; import { @@ -37,10 +37,10 @@ import { GenericIndexPatternColumn, replaceColumn, TermsIndexPatternColumn, -} from '../operations'; +} from '../../../../common/datasources/form_based/operations'; import { documentField } from '../document_field'; import { OperationMetadata } from '../../../types'; -import { DateHistogramIndexPatternColumn } from '../operations/definitions/date_histogram'; +import { DateHistogramIndexPatternColumn } from '../../../../common/datasources/form_based/operations/definitions/date_histogram'; import { getFieldByNameFactory } from '../pure_helpers'; import { Filtering, setFilter } from './filtering'; import { TimeShift } from './time_shift'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimensions_editor_helpers.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimensions_editor_helpers.tsx index 82cca9cea9b44..7c82c6459889c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimensions_editor_helpers.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/dimensions_editor_helpers.tsx @@ -22,10 +22,10 @@ import { type PercentileIndexPatternColumn, type PercentileRanksIndexPatternColumn, type TermsIndexPatternColumn, -} from '../operations'; -import { isColumnOfType } from '../operations/definitions/helpers'; +} from '../../../../common/datasources/form_based/operations'; +import { isColumnOfType } from '../../../../common/datasources/form_based/operations/definitions/helpers'; import { FormBasedLayer } from '../types'; -import { MAX_TERMS_OTHER_ENABLED } from '../operations/definitions/terms/constants'; +import { MAX_TERMS_OTHER_ENABLED } from '../../../../common/datasources/form_based/operations/definitions/terms/constants'; export const formulaOperationName = 'formula'; export const staticValueOperationName = 'static_value'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.test.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.test.ts index 21650cffed54a..aad34c1c247aa 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.test.ts @@ -6,7 +6,7 @@ */ import { DragDropOperation, OperationMetadata } from '../../../../types'; -import { TermsIndexPatternColumn } from '../../operations'; +import { TermsIndexPatternColumn } from '../../../../../common/datasources/form_based/operations'; import { getDropProps } from './get_drop_props'; import { mockDataViews, @@ -15,7 +15,7 @@ import { mockedDndOperations, mockedColumns, } from './mocks'; -import { generateId } from '../../../../id_generator'; +import { generateId } from '../../../../../common/id_generator'; const getDefaultProps = () => ({ indexPatterns: mockDataViews(), diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.ts index f144bdfa5107d..92446ad64e84f 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/get_drop_props.ts @@ -17,11 +17,11 @@ import { getCurrentFieldsForOperation, getOperationDisplay, hasOperationSupportForMultipleFields, -} from '../../operations'; +} from '../../../../../common/datasources/form_based/operations'; import { isDraggedDataViewField, isOperationFromTheSameGroup } from '../../../../utils'; -import { hasField } from '../../pure_utils'; +import { hasField } from '../../../../../common/datasources/form_based/pure_utils'; import { OperationMetadata, DraggedField } from '../../../../types'; -import { getOperationTypesForField } from '../../operations'; +import { getOperationTypesForField } from '../../../../../common/datasources/form_based/operations'; import { GenericIndexPatternColumn } from '../../form_based'; import { FormBasedPrivateState, DataViewDragDropOperation } from '../../types'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts index ceb16345f475c..e802d42c465ad 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/mocks.ts @@ -13,7 +13,7 @@ import { GenericIndexPatternColumn, StaticValueIndexPatternColumn, TermsIndexPatternColumn, -} from '../../operations'; +} from '../../../../../common/datasources/form_based/operations'; import { getFieldByNameFactory } from '../../pure_helpers'; jest.mock('../../../../id_generator'); diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.test.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.test.ts index 3c844ac8be12d..2f9c3ac4ab3bf 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.test.ts @@ -9,8 +9,11 @@ import { DropType } from '@kbn/dom-drag-drop'; import { onDrop } from './on_drop_handler'; import { FormBasedPrivateState } from '../../types'; import { OperationMetadata, DatasourceDimensionDropHandlerProps } from '../../../../types'; -import { FormulaIndexPatternColumn, MedianIndexPatternColumn } from '../../operations'; -import { generateId } from '../../../../id_generator'; +import { + FormulaIndexPatternColumn, + MedianIndexPatternColumn, +} from '../../../../../common/datasources/form_based/operations'; +import { generateId } from '../../../../../common/id_generator'; import { mockDataViews, mockedLayers, diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.ts index 62b11b071a12d..e399c67b58270 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/droppable/on_drop_handler.ts @@ -26,7 +26,7 @@ import { hasOperationSupportForMultipleFields, getOperationHelperForMultipleFields, replaceColumn, -} from '../../operations'; +} from '../../../../../common/datasources/form_based/operations'; import { mergeLayer, mergeLayers } from '../../state_helpers'; import { getNewOperation, getField } from './get_drop_props'; import { FormBasedPrivateState, DataViewDragDropOperation } from '../../types'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.test.tsx index d307e0eb094a2..23735ae141d2c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.test.tsx @@ -9,7 +9,7 @@ import React from 'react'; import { mount } from 'enzyme'; import { act } from 'react-dom/test-utils'; import { EuiComboBox } from '@elastic/eui'; -import { GenericOperationDefinition } from '../operations'; +import { GenericOperationDefinition } from '../../../../common/datasources/form_based/operations'; import { averageOperation, countOperation, @@ -18,12 +18,12 @@ import { termsOperation, staticValueOperation, minOperation, -} from '../operations/definitions'; +} from '../../../../common/datasources/form_based/operations/definitions'; import { FieldInput, getErrorMessage } from './field_input'; import { createMockedIndexPattern, createMockedIndexPatternWithAdditionalFields } from '../mocks'; import { getOperationSupportMatrix } from '.'; import { GenericIndexPatternColumn, FormBasedLayer, FormBasedPrivateState } from '../types'; -import { ReferenceBasedIndexPatternColumn } from '../operations/definitions/column_types'; +import { ReferenceBasedIndexPatternColumn } from '../../../../common/datasources/form_based/operations/definitions/column_types'; import { FieldSelect } from './field_select'; import { IndexPattern, VisualizationDimensionGroupConfig } from '../../../types'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.tsx index 5120c89c37b30..82bee2c31c13c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/field_input.tsx @@ -8,15 +8,15 @@ import { EuiFormRow } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import React from 'react'; -import { insertOrReplaceColumn } from '../operations/layer_helpers'; +import { insertOrReplaceColumn } from '../../../../common/datasources/form_based/operations/layer_helpers'; import { FieldSelect } from './field_select'; import type { FieldInputProps, OperationType, GenericIndexPatternColumn, -} from '../operations/definitions'; -import type { FieldBasedIndexPatternColumn } from '../operations/definitions/column_types'; -import { shouldShowTimeSeriesOption } from '../pure_utils'; +} from '../../../../common/datasources/form_based/operations/definitions'; +import type { FieldBasedIndexPatternColumn } from '../../../../common/datasources/form_based/operations/definitions/column_types'; +import { shouldShowTimeSeriesOption } from '../../../../common/datasources/form_based/pure_utils'; export function FieldInput({ layer, diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/filtering.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/filtering.tsx index e79ead1e5536a..216e2558def10 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/filtering.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/filtering.tsx @@ -10,7 +10,10 @@ import type { Query } from '@kbn/es-query'; import { validateQuery, FilterQueryInput } from '@kbn/visualization-ui-components'; import { useKibana } from '@kbn/kibana-react-plugin/public'; import { LENS_APP_NAME } from '../../../../common/constants'; -import { GenericIndexPatternColumn, operationDefinitionMap } from '../operations'; +import { + GenericIndexPatternColumn, + operationDefinitionMap, +} from '../../../../common/datasources/form_based/operations'; import type { FormBasedLayer } from '../types'; import type { IndexPattern } from '../../../types'; import { LensAppServices } from '../../../app_plugin/types'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/format_selector.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/format_selector.tsx index e049da2c30965..271d2d56f06d9 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/format_selector.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/format_selector.tsx @@ -29,8 +29,8 @@ import { css } from '@emotion/react'; import type { DocLinksStart } from '@kbn/core/public'; import { LensAppServices } from '../../../app_plugin/types'; import { GenericIndexPatternColumn } from '../form_based'; -import { isColumnFormatted } from '../operations/definitions/helpers'; -import { ValueFormatConfig } from '../operations/definitions/column_types'; +import { isColumnFormatted } from '../../../../common/datasources/form_based/operations/definitions/helpers'; +import { ValueFormatConfig } from '../../../../common/datasources/form_based/operations/definitions/column_types'; import { DurationRowInputs } from './formatting/duration_input'; import { Prepend, PrependWidthProvider } from '../../../shared_components/prepend_provider'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/operation_support.ts b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/operation_support.ts index 7f9aa024e18d6..0cde1483c68a9 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/operation_support.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/operation_support.ts @@ -8,7 +8,10 @@ import memoizeOne from 'memoize-one'; import { DatasourceDimensionProps, IndexPatternMap, OperationMetadata } from '../../../types'; import { OperationType } from '../form_based'; -import { memoizedGetAvailableOperationsByMetadata, OperationFieldTuple } from '../operations'; +import { + memoizedGetAvailableOperationsByMetadata, + OperationFieldTuple, +} from '../../../../common/datasources/form_based/operations'; import { FormBasedPrivateState } from '../types'; export interface OperationSupportMatrix { diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reduced_time_range.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reduced_time_range.tsx index 6f92b8dc75c17..ca16a20e07519 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reduced_time_range.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reduced_time_range.tsx @@ -16,7 +16,7 @@ import { adjustTimeScaleLabelSuffix, GenericIndexPatternColumn, operationDefinitionMap, -} from '../operations'; +} from '../../../../common/datasources/form_based/operations'; import type { FormBasedLayer } from '../types'; import type { IndexPattern } from '../../../types'; import { reducedTimeRangeOptions } from '../reduced_time_range_utils'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.test.tsx index a6a869720f3b3..b7088599a4053 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.test.tsx @@ -24,7 +24,7 @@ import { LastValueIndexPatternColumn, operationDefinitionMap, TermsIndexPatternColumn, -} from '../operations'; +} from '../../../../common/datasources/form_based/operations'; import { FieldSelect } from './field_select'; import { FormBasedLayer } from '../types'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.tsx index 66a01daae0581..6afc01ecc4fac 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/reference_editor.tsx @@ -25,13 +25,13 @@ import { RequiredReference, IncompleteColumn, GenericOperationDefinition, -} from '../operations'; +} from '../../../../common/datasources/form_based/operations'; import { FieldChoiceWithOperationType, FieldSelect } from './field_select'; -import { hasField } from '../pure_utils'; +import { hasField } from '../../../../common/datasources/form_based/pure_utils'; import type { FormBasedLayer } from '../types'; import type { IndexPattern, IndexPatternField, ParamEditorCustomProps } from '../../../types'; import type { FormBasedDimensionEditorProps } from './dimension_panel'; -import { FormRow } from '../operations/definitions/shared_components'; +import { FormRow } from '../../../../common/datasources/form_based/operations/definitions/shared_components'; const operationDisplay = getOperationDisplay(); diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_scaling.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_scaling.tsx index 003407649622e..02b551cf5649c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_scaling.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_scaling.tsx @@ -19,7 +19,7 @@ import { adjustTimeScaleLabelSuffix, GenericIndexPatternColumn, operationDefinitionMap, -} from '../operations'; +} from '../../../../common/datasources/form_based/operations'; import type { TimeScaleUnit } from '../../../../common/expressions'; import { unitSuffixesLong } from '../../../../common/suffix_formatter'; import type { FormBasedLayer } from '../types'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_shift.tsx b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_shift.tsx index 8bf26114416de..5c768ea40d7a0 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_shift.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/dimension_panel/time_shift.tsx @@ -14,7 +14,7 @@ import { adjustTimeScaleLabelSuffix, GenericIndexPatternColumn, operationDefinitionMap, -} from '../operations'; +} from '../../../../common/datasources/form_based/operations'; import type { FormBasedLayer } from '../types'; import type { FormBasedDimensionEditorProps } from './dimension_panel'; import { @@ -22,7 +22,7 @@ import { getLayerTimeShiftChecks, timeShiftOptions, getColumnTimeShiftWarnings, -} from '../time_shift_utils'; +} from '../../../../common/datasources/form_based/time_shift_utils'; import type { IndexPattern } from '../../../types'; export function setTimeShift( diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts index acc77bd34c39c..25b4f877db70c 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts @@ -44,8 +44,8 @@ import { SumIndexPatternColumn, AvgIndexPatternColumn, MedianIndexPatternColumn, -} from './operations'; -import { createMockedFullReference } from './operations/mocks'; +} from '../../../common/datasources/form_based/operations'; +import { createMockedFullReference } from '../../../common/datasources/form_based/operations/mocks'; import { cloneDeep } from 'lodash'; import { Datatable, DatatableColumn } from '@kbn/expressions-plugin/common'; import { filterAndSortUserMessages } from '../../app_plugin/get_application_user_messages'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx index 8099a787437ba..127602dc47c12 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx @@ -25,6 +25,12 @@ import type { SharePluginStart } from '@kbn/share-plugin/public'; import { type DraggingIdentifier } from '@kbn/dom-drag-drop'; import { DimensionTrigger } from '@kbn/visualization-ui-components'; import memoizeOne from 'memoize-one'; +import { + IndexPattern, + IndexPatternField, + IndexPatternMap, + IndexPatternRef, +} from '../../../common/types'; import type { DatasourceDimensionEditorProps, DatasourceDimensionTriggerProps, @@ -33,13 +39,9 @@ import type { PublicAPIProps, OperationDescriptor, FramePublicAPI, - IndexPatternField, - IndexPattern, - IndexPatternRef, DataSourceInfo, UserMessage, StateSetter, - IndexPatternMap, } from '../../types'; import { changeIndexPattern, @@ -51,7 +53,7 @@ import { renameIndexPattern, triggerActionOnIndexPatternChange, } from './loader'; -import { toExpression } from './to_expression'; +import { toExpression } from '../../../common/datasources/form_based/to_expression'; import { FormBasedDimensionEditor, getDropProps, onDrop } from './dimension_panel'; import { FormBasedDataPanel } from './datapanel'; import { @@ -69,9 +71,12 @@ import { cloneLayer, getNotifiableFeatures, getUnsupportedOperationsWarningMessage, -} from './utils'; +} from '../../../common/datasources/form_based/utils'; import { getUniqueLabelGenerator, isDraggedDataViewField, nonNullable } from '../../utils'; -import { hasField, normalizeOperationDataType } from './pure_utils'; +import { + hasField, + normalizeOperationDataType, +} from '../../../common/datasources/form_based/pure_utils'; import { LayerPanel } from './layerpanel'; import { DateHistogramIndexPatternColumn, @@ -81,27 +86,30 @@ import { insertNewColumn, operationDefinitionMap, TermsIndexPatternColumn, -} from './operations'; +} from '../../../common/datasources/form_based/operations'; import { copyColumn, getColumnOrder, getReferenceRoot, reorderByGroups, -} from './operations/layer_helpers'; +} from '../../../common/datasources/form_based/operations/layer_helpers'; import { FormBasedPrivateState, FormBasedPersistedState, DataViewDragDropOperation } from './types'; import { mergeLayer, mergeLayers } from './state_helpers'; import type { Datasource, VisualizeEditorContext } from '../../types'; -import { deleteColumn, isReferenced } from './operations'; +import { deleteColumn, isReferenced } from '../../../common/datasources/form_based/operations'; import { GeoFieldWorkspacePanel } from '../../editor_frame_service/editor_frame/workspace_panel/geo_field_workspace_panel'; -import { getStateTimeShiftWarningMessages } from './time_shift_utils'; -import { getPrecisionErrorWarningMessages } from './utils'; +import { getStateTimeShiftWarningMessages } from '../../../common/datasources/form_based/time_shift_utils'; +import { getPrecisionErrorWarningMessages } from '../../../common/datasources/form_based/utils'; import { DOCUMENT_FIELD_NAME } from '../../../common/constants'; -import { isColumnOfType } from './operations/definitions/helpers'; +import { isColumnOfType } from '../../../common/datasources/form_based/operations/definitions/helpers'; import { LayerSettingsPanel } from './layer_settings'; import { FormBasedLayer, LastValueIndexPatternColumn } from '../..'; import { filterAndSortUserMessages } from '../../app_plugin/get_application_user_messages'; -export type { OperationType, GenericIndexPatternColumn } from './operations'; -export { deleteColumn } from './operations'; +export type { + OperationType, + GenericIndexPatternColumn, +} from '../../../common/datasources/form_based/operations'; +export { deleteColumn } from '../../../common/datasources/form_based/operations'; function wrapOnDot(str?: string) { // u200B is a non-width white-space character, which allows diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx index 0a0c0dcc05eeb..499d7fc29b3af 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.test.tsx @@ -13,7 +13,7 @@ import type { CountColumn, } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { DatasourceSuggestion } from '../../types'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import type { FormBasedPrivateState } from './types'; import { getDatasourceSuggestionsForField, @@ -25,12 +25,15 @@ import { import { documentField } from './document_field'; import { getFieldByNameFactory } from './pure_helpers'; import { isEqual } from 'lodash'; -import { DateHistogramIndexPatternColumn, TermsIndexPatternColumn } from './operations'; +import { + DateHistogramIndexPatternColumn, + TermsIndexPatternColumn, +} from '../../../common/datasources/form_based/operations'; import { MathIndexPatternColumn, RangeIndexPatternColumn, StaticValueIndexPatternColumn, -} from './operations/definitions'; +} from '../../../common/datasources/form_based/operations/definitions'; jest.mock('./loader'); jest.mock('../../id_generator'); diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.ts b/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.ts index 4bb6e5cdf8ec3..91d642d5df6c6 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based_suggestions.ts @@ -14,7 +14,8 @@ import type { AnyColumnWithSourceField, TermsColumn, } from '@kbn/visualizations-plugin/common/convert_to_lens'; -import { generateId } from '../../id_generator'; +import { documentField } from '../../../common/document_field'; +import { generateId } from '../../../common/id_generator'; import type { DatasourceSuggestion, IndexPattern, @@ -39,12 +40,11 @@ import { FormulaIndexPatternColumn, updateColumnLabel, ColumnAdvancedParams, -} from './operations'; -import { hasField } from './pure_utils'; +} from '../../../common/datasources/form_based/operations'; +import { hasField } from '../../../common/datasources/form_based/pure_utils'; import type { FormBasedPrivateState, FormBasedLayer } from './types'; -import { documentField } from './document_field'; -import { OperationDefinition } from './operations/definitions'; -import { insertOrReplaceFormulaColumn } from './operations/definitions/formula'; +import { OperationDefinition } from '../../../common/datasources/form_based/operations/definitions'; +import { insertOrReplaceFormulaColumn } from '../../../common/datasources/form_based/operations/definitions/formula'; export type IndexPatternSuggestion = DatasourceSuggestion; diff --git a/x-pack/plugins/lens/public/datasources/form_based/info_badges.tsx b/x-pack/plugins/lens/public/datasources/form_based/info_badges.tsx index 4afcd4b8bbc5e..bf4b56b3de463 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/info_badges.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/info_badges.tsx @@ -11,7 +11,7 @@ import React from 'react'; import { FormBasedLayer } from '../..'; import { InfoBadge } from '../../shared_components/info_badges/info_badge'; import { FramePublicAPI, VisualizationInfo } from '../../types'; -import { getSamplingValue } from './utils'; +import { getSamplingValue } from '../../../common/datasources/form_based/utils'; export function ReducedSamplingSectionEntries({ layers, diff --git a/x-pack/plugins/lens/public/datasources/form_based/layer_settings.tsx b/x-pack/plugins/lens/public/datasources/form_based/layer_settings.tsx index 38f216f083d1d..e26a03151401e 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/layer_settings.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/layer_settings.tsx @@ -13,7 +13,7 @@ import { FormattedMessage } from '@kbn/i18n-react'; import { RandomSamplingSlider } from '@kbn/random-sampling'; import type { DatasourceLayerSettingsProps } from '../../types'; import type { FormBasedPrivateState } from './types'; -import { isSamplingValueEnabled } from './utils'; +import { isSamplingValueEnabled } from '../../../common/datasources/form_based/utils'; import { IgnoreGlobalFilterRowControl } from '../../shared_components/ignore_global_filter'; import { trackUiCounterEvents } from '../../lens_ui_telemetry'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx index eef44faaec8ff..cb09c2708665a 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/layerpanel.test.tsx @@ -14,7 +14,7 @@ import { EuiSelectable } from '@elastic/eui'; import { DataViewsList } from '@kbn/unified-search-plugin/public/dataview_picker/dataview_list'; import { ChangeIndexPattern } from '../../shared_components/dataview_picker/dataview_picker'; import { getFieldByNameFactory } from './pure_helpers'; -import { TermsIndexPatternColumn } from './operations'; +import { TermsIndexPatternColumn } from '../../../common/datasources/form_based/operations'; import { act } from 'react-dom/test-utils'; import { TriggerButton } from '../../shared_components/dataview_picker/trigger'; diff --git a/x-pack/plugins/lens/public/datasources/form_based/layerpanel.tsx b/x-pack/plugins/lens/public/datasources/form_based/layerpanel.tsx index 3945067311e9b..1fcb4be67acdc 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/layerpanel.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/layerpanel.tsx @@ -12,7 +12,7 @@ import { RandomSamplingIcon } from '@kbn/random-sampling'; import type { DatasourceLayerPanelProps } from '../../types'; import type { FormBasedPrivateState } from './types'; import { ChangeIndexPattern } from '../../shared_components/dataview_picker/dataview_picker'; -import { getSamplingValue } from './utils'; +import { getSamplingValue } from '../../../common/datasources/form_based/utils'; import { getIgnoreGlobalFilterIcon } from '../../shared_components/ignore_global_filter'; export interface FormBasedLayerPanelProps extends DatasourceLayerPanelProps { diff --git a/x-pack/plugins/lens/public/datasources/form_based/loader.test.ts b/x-pack/plugins/lens/public/datasources/form_based/loader.test.ts index 924110080329c..43a88a18d1cd4 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/loader.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/loader.test.ts @@ -13,7 +13,10 @@ import { injectReferences, } from './loader'; import { FormBasedPersistedState, FormBasedPrivateState } from './types'; -import { DateHistogramIndexPatternColumn, TermsIndexPatternColumn } from './operations'; +import { + DateHistogramIndexPatternColumn, + TermsIndexPatternColumn, +} from '../../../common/datasources/form_based/operations'; import { sampleIndexPatterns } from '../../data_views_service/mocks'; const createMockStorage = (lastData?: Record) => { diff --git a/x-pack/plugins/lens/public/datasources/form_based/loader.ts b/x-pack/plugins/lens/public/datasources/form_based/loader.ts index 56ec53b7664fd..9a5720b81b0e6 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/loader.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/loader.ts @@ -5,24 +5,25 @@ * 2.0. */ -import { uniq, mapValues, difference } from 'lodash'; +import { mapValues } from 'lodash'; import type { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; import type { SavedObjectReference } from '@kbn/core/public'; import { UPDATE_FILTER_REFERENCES_ACTION, UPDATE_FILTER_REFERENCES_TRIGGER, } from '@kbn/unified-search-plugin/public'; +import { ActionExecutionContext, UiActionsStart } from '@kbn/ui-actions-plugin/public'; import { - ActionExecutionContext, - UiActionsStart, - VisualizeFieldContext, -} from '@kbn/ui-actions-plugin/public'; -import type { VisualizeEditorContext } from '../../types'; -import { FormBasedPersistedState, FormBasedPrivateState, FormBasedLayer } from './types'; + getLayerReferenceName, + setLastUsedIndexPatternId, +} from '../../../common/datasources/form_based/loader'; +import { IndexPattern } from '../../../common/types'; +import { FormBasedPersistedState, FormBasedPrivateState } from './types'; -import { memoizedGetAvailableOperationsByMetadata, updateLayerIndexPattern } from './operations'; -import { readFromStorage, writeToStorage } from '../../settings_storage'; -import type { IndexPattern, IndexPatternRef } from '../../types'; +import { + memoizedGetAvailableOperationsByMetadata, + updateLayerIndexPattern, +} from '../../../common/datasources/form_based/operations'; export function onRefreshIndexPattern() { if (memoizedGetAvailableOperationsByMetadata.cache.clear) { @@ -31,22 +32,6 @@ export function onRefreshIndexPattern() { } } -const getLastUsedIndexPatternId = ( - storage: IStorageWrapper, - indexPatternRefs: IndexPatternRef[] -) => { - const indexPattern = readFromStorage(storage, 'indexPatternId'); - return indexPattern && indexPatternRefs.find((i) => i.id === indexPattern)?.id; -}; - -const setLastUsedIndexPatternId = (storage: IStorageWrapper, value: string) => { - writeToStorage(storage, 'indexPatternId', value); -}; - -function getLayerReferenceName(layerId: string) { - return `indexpattern-datasource-layer-${layerId}`; -} - export function extractReferences({ layers }: FormBasedPrivateState) { const savedObjectReferences: SavedObjectReference[] = []; const persistableState: FormBasedPersistedState = { @@ -63,126 +48,6 @@ export function extractReferences({ layers }: FormBasedPrivateState) { return { savedObjectReferences, state: persistableState }; } -export function injectReferences( - state: FormBasedPersistedState, - references: SavedObjectReference[] -) { - const layers: Record = {}; - Object.entries(state.layers).forEach(([layerId, persistedLayer]) => { - const indexPatternId = references.find( - ({ name }) => name === getLayerReferenceName(layerId) - )?.id; - - if (indexPatternId) { - layers[layerId] = { - ...persistedLayer, - indexPatternId, - }; - } - }); - return { - layers, - }; -} - -function createStateFromPersisted({ - persistedState, - references, -}: { - persistedState?: FormBasedPersistedState; - references?: SavedObjectReference[]; -}) { - return persistedState && references ? injectReferences(persistedState, references) : undefined; -} - -function getUsedIndexPatterns({ - state, - indexPatternRefs, - storage, - initialContext, - defaultIndexPatternId, -}: { - state?: { - layers: Record; - }; - defaultIndexPatternId?: string; - storage: IStorageWrapper; - initialContext?: VisualizeFieldContext | VisualizeEditorContext; - indexPatternRefs: IndexPatternRef[]; -}) { - const lastUsedIndexPatternId = getLastUsedIndexPatternId(storage, indexPatternRefs); - const fallbackId = lastUsedIndexPatternId || defaultIndexPatternId || indexPatternRefs[0]?.id; - const indexPatternIds = []; - if (initialContext) { - if ('isVisualizeAction' in initialContext) { - indexPatternIds.push(...initialContext.indexPatternIds); - } else { - indexPatternIds.push(initialContext.dataViewSpec.id!); - } - } - const usedPatterns = ( - initialContext - ? indexPatternIds - : uniq(state ? Object.values(state.layers).map((l) => l.indexPatternId) : [fallbackId]) - ) - // take out the undefined from the list - .filter(Boolean); - - return { - usedPatterns, - allIndexPatternIds: indexPatternIds, - }; -} - -export function loadInitialState({ - persistedState, - references, - defaultIndexPatternId, - storage, - initialContext, - indexPatternRefs = [], - indexPatterns = {}, -}: { - persistedState?: FormBasedPersistedState; - references?: SavedObjectReference[]; - defaultIndexPatternId?: string; - storage: IStorageWrapper; - initialContext?: VisualizeFieldContext | VisualizeEditorContext; - indexPatternRefs?: IndexPatternRef[]; - indexPatterns?: Record; -}): FormBasedPrivateState { - const state = createStateFromPersisted({ persistedState, references }); - const { usedPatterns, allIndexPatternIds: indexPatternIds } = getUsedIndexPatterns({ - state, - defaultIndexPatternId, - storage, - initialContext, - indexPatternRefs, - }); - - const availableIndexPatterns = new Set(indexPatternRefs.map(({ id }: IndexPatternRef) => id)); - - const notUsedPatterns: string[] = difference([...availableIndexPatterns], usedPatterns); - - // Priority list: - // * start with the indexPattern in context - // * then fallback to the used ones - // * then as last resort use a first one from not used refs - const currentIndexPatternId = [...indexPatternIds, ...usedPatterns, ...notUsedPatterns].find( - (id) => id != null && availableIndexPatterns.has(id) && indexPatterns[id] - ); - - if (currentIndexPatternId) { - setLastUsedIndexPatternId(storage, currentIndexPatternId); - } - - return { - layers: {}, - ...state, - currentIndexPatternId, - }; -} - export function changeIndexPattern({ indexPatternId, state, @@ -194,7 +59,7 @@ export function changeIndexPattern({ storage: IStorageWrapper; indexPatterns: Record; }) { - setLastUsedIndexPatternId(storage, indexPatternId); + setLastUsedIndexPatternId(indexPatternId, storage); return { ...state, layers: isSingleEmptyLayer(state.layers) @@ -269,7 +134,7 @@ export function changeLayerIndexPattern({ storage: IStorageWrapper; indexPatterns: Record; }) { - setLastUsedIndexPatternId(storage, indexPatternId); + setLastUsedIndexPatternId(indexPatternId, storage); const newLayers = { ...state.layers, diff --git a/x-pack/plugins/lens/public/datasources/form_based/reduced_time_range_utils.tsx b/x-pack/plugins/lens/public/datasources/form_based/reduced_time_range_utils.tsx index aaa5a446b514c..23fbc1fd1b40b 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/reduced_time_range_utils.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/reduced_time_range_utils.tsx @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n'; import type { FormBasedLayer } from './types'; import type { IndexPattern } from '../../types'; -import type { FieldBasedOperationErrorMessage } from './operations/definitions'; +import type { FieldBasedOperationErrorMessage } from '../../../common/datasources/form_based/operations/definitions'; export const reducedTimeRangeOptions = [ { diff --git a/x-pack/plugins/lens/public/datasources/form_based/types.ts b/x-pack/plugins/lens/public/datasources/form_based/types.ts index 5abec15c31d61..e498fe6c2ba07 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/types.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/types.ts @@ -5,7 +5,10 @@ * 2.0. */ import type { DragDropIdentifier } from '@kbn/dom-drag-drop'; -import type { IncompleteColumn, GenericIndexPatternColumn } from './operations'; +import type { + IncompleteColumn, + GenericIndexPatternColumn, +} from '../../../common/datasources/form_based/operations'; import type { IndexPattern, IndexPatternField, DragDropOperation } from '../../types'; export type { @@ -37,9 +40,9 @@ export type { OverallSumIndexPatternColumn, StaticValueIndexPatternColumn, TimeScaleIndexPatternColumn, -} from './operations'; +} from '../../../common/datasources/form_based/operations'; -export type { FormulaPublicApi } from './operations/definitions/formula/formula_public_api'; +export type { FormulaPublicApi } from '../../../common/datasources/form_based/operations/definitions/formula/formula_public_api'; export type DraggedField = DragDropIdentifier & { field: IndexPatternField; diff --git a/x-pack/plugins/lens/public/datasources/form_based/utils.tsx b/x-pack/plugins/lens/public/datasources/form_based/utils.tsx index 16c4dbdc047fb..f0166a7674bcd 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/utils.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/utils.tsx @@ -26,17 +26,12 @@ import { import { estypes } from '@elastic/elasticsearch'; import { isQueryValid } from '@kbn/visualization-ui-components'; -import type { DateRange } from '../../../common/types'; -import type { - FramePublicAPI, - IndexPattern, - StateSetter, - UserMessage, - VisualizationInfo, -} from '../../types'; +import { getSamplingValue } from '../../../common/datasources/form_based/utils'; +import type { DateRange, IndexPattern } from '../../../common/types'; +import type { FramePublicAPI, StateSetter, UserMessage, VisualizationInfo } from '../../types'; import { renewIDs } from '../../utils'; import type { FormBasedLayer, FormBasedPersistedState, FormBasedPrivateState } from './types'; -import type { ReferenceBasedIndexPatternColumn } from './operations/definitions/column_types'; +import type { ReferenceBasedIndexPatternColumn } from '../../../common/datasources/form_based/operations/definitions/column_types'; import { operationDefinitionMap, @@ -49,58 +44,23 @@ import { type RangeIndexPatternColumn, type FormulaIndexPatternColumn, type DateHistogramIndexPatternColumn, - type MaxIndexPatternColumn, - type MinIndexPatternColumn, type GenericOperationDefinition, type FieldBasedIndexPatternColumn, -} from './operations'; +} from '../../../common/datasources/form_based/operations'; -import { getInvalidFieldMessage, isColumnOfType } from './operations/definitions/helpers'; -import { FiltersIndexPatternColumn } from './operations/definitions/filters'; -import { hasField } from './pure_utils'; +import { + getInvalidFieldMessage, + isColumnOfType, +} from '../../../common/datasources/form_based/operations/definitions/helpers'; +import { FiltersIndexPatternColumn } from '../../../common/datasources/form_based/operations/definitions/filters'; +import { hasField } from '../../../common/datasources/form_based/pure_utils'; import { mergeLayer } from './state_helpers'; -import { supportsRarityRanking } from './operations/definitions/terms'; -import { DEFAULT_MAX_DOC_COUNT } from './operations/definitions/terms/constants'; +import { supportsRarityRanking } from '../../../common/datasources/form_based/operations/definitions/terms'; +import { DEFAULT_MAX_DOC_COUNT } from '../../../common/datasources/form_based/operations/definitions/terms/constants'; import { getOriginalId } from '../../../common/expressions/datatable/transpose_helpers'; import { ReducedSamplingSectionEntries } from './info_badges'; import { IgnoredGlobalFiltersEntries } from '../../shared_components/ignore_global_filter'; -function isMinOrMaxColumn( - column?: GenericIndexPatternColumn -): column is MaxIndexPatternColumn | MinIndexPatternColumn { - if (!column) { - return false; - } - return ( - isColumnOfType('max', column) || - isColumnOfType('min', column) - ); -} - -function isReferenceColumn( - column: GenericIndexPatternColumn -): column is ReferenceBasedIndexPatternColumn { - return 'references' in column; -} - -export function isSamplingValueEnabled(layer: FormBasedLayer) { - // Do not use columnOrder here as it needs to check also inside formulas columns - return !Object.values(layer.columns).some( - (column) => - isMinOrMaxColumn(column) || - (isReferenceColumn(column) && isMinOrMaxColumn(layer.columns[column.references[0]])) - ); -} - -/** - * Centralized logic to get the actual random sampling value for a layer - * @param layer - * @returns - */ -export function getSamplingValue(layer: FormBasedLayer) { - return isSamplingValueEnabled(layer) ? layer.sampling ?? 1 : 1; -} - export function isColumnInvalid( layer: FormBasedLayer, columnId: string, diff --git a/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts b/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts index 604e45f72ec84..a07e7f9c715f4 100644 --- a/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts +++ b/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.test.ts @@ -12,7 +12,7 @@ import { TextBasedPersistedState, TextBasedPrivateState } from './types'; import { dataPluginMock } from '@kbn/data-plugin/public/mocks'; import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; import { getTextBasedDatasource } from './text_based_languages'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { DatasourcePublicAPI, Datasource, FramePublicAPI } from '../../types'; jest.mock('../../id_generator'); diff --git a/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.tsx b/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.tsx index bc46f0b4076d9..a4f4ba01985c2 100644 --- a/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.tsx +++ b/x-pack/plugins/lens/public/datasources/text_based/text_based_languages.tsx @@ -20,8 +20,8 @@ import { euiThemeVars } from '@kbn/ui-theme'; import { DimensionTrigger } from '@kbn/visualization-ui-components'; import memoizeOne from 'memoize-one'; import { isEqual } from 'lodash'; +import { TextBasedDatasourceCommon } from '../../../common/datasources/text_based/text_based_languages'; import { TextBasedDataPanel } from './datapanel'; -import { toExpression } from './to_expression'; import { DatasourceDimensionEditorProps, DatasourceDataPanelProps, @@ -33,7 +33,7 @@ import { DataSourceInfo, UserMessage, } from '../../types'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import type { TextBasedPrivateState, TextBasedPersistedState, @@ -200,7 +200,7 @@ export function getTextBasedDatasource({ return []; }; const TextBasedDatasource: Datasource = { - id: 'textBased', + ...TextBasedDatasourceCommon, checkIntegrity: () => { return []; @@ -224,30 +224,6 @@ export function getTextBasedDatasource({ return message; }); }, - initialize( - state?: TextBasedPersistedState, - savedObjectReferences?, - context?, - indexPatternRefs?, - indexPatterns? - ) { - const patterns = indexPatterns ? Object.values(indexPatterns) : []; - const refs = patterns.map((p) => { - return { - id: p.id, - title: p.title, - timeField: p.timeFieldName, - }; - }); - - const initState = state || { layers: {} }; - return { - ...initState, - indexPatternRefs: refs, - initialContext: context, - }; - }, - syncColumns({ state }) { // TODO implement this for real return state; @@ -333,10 +309,6 @@ export function getTextBasedDatasource({ }, }; }, - - getLayers(state: TextBasedPrivateState) { - return state && state.layers ? Object.keys(state?.layers) : []; - }, // there are cases where a query can return a big amount of columns // at this case we don't suggest all columns in a table but the first // MAX_NUM_OF_COLUMNS @@ -364,9 +336,6 @@ export function getTextBasedDatasource({ removeColumn, - toExpression: (state, layerId, indexPatterns, dateRange, searchSessionId) => { - return toExpression(state, layerId); - }, getSelectedFields(state) { return getSelectedFieldsFromColumns( Object.values(state?.layers)?.flatMap((l) => Object.values(l.columns)) diff --git a/x-pack/plugins/lens/public/datasources/text_based/utils.ts b/x-pack/plugins/lens/public/datasources/text_based/utils.ts index ecf4fbcd12ff2..de771ec6fb2c7 100644 --- a/x-pack/plugins/lens/public/datasources/text_based/utils.ts +++ b/x-pack/plugins/lens/public/datasources/text_based/utils.ts @@ -14,7 +14,7 @@ import { getIndexPatternFromESQLQuery, } from '@kbn/es-query'; import type { DatatableColumn } from '@kbn/expressions-plugin/public'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { fetchDataFromAggregateQuery } from './fetch_data_from_aggregate_query'; import type { IndexPatternRef, TextBasedPrivateState, TextBasedLayerColumn } from './types'; diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/buttons/empty_dimension_button.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/buttons/empty_dimension_button.tsx index 0c6195d51cfe4..95cd2251cb854 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/buttons/empty_dimension_button.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/buttons/empty_dimension_button.tsx @@ -19,7 +19,7 @@ import { EmptyDimensionButton as EmptyDimensionButtonInner } from '@kbn/visualiz import { css } from '@emotion/react'; import { euiThemeVars } from '@kbn/ui-theme'; import { isDraggedField } from '../../../../utils'; -import { generateId } from '../../../../id_generator'; +import { generateId } from '../../../../../common/id_generator'; import { Datasource, diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.test.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.test.tsx index b0729cb489ba7..6009e0016b75c 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.test.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.test.tsx @@ -23,7 +23,7 @@ import { LayerPanel } from './layer_panel'; import { coreMock } from '@kbn/core/public/mocks'; import { UiActionsStart } from '@kbn/ui-actions-plugin/public'; import { uiActionsPluginMock } from '@kbn/ui-actions-plugin/public/mocks'; -import { generateId } from '../../../id_generator'; +import { generateId } from '../../../../common/id_generator'; import { mountWithProvider } from '../../../mocks'; import { LayerTypes } from '@kbn/expression-xy-plugin/public'; import { ReactWrapper } from 'enzyme'; diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.tsx index 8c31fb2f9d800..c0fdcca65dc7f 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/config_panel.tsx @@ -22,7 +22,7 @@ import { } from '../../../state_management/lens_slice'; import { AddLayerFunction, DragDropOperation, Visualization } from '../../../types'; import { LayerPanel } from './layer_panel'; -import { generateId } from '../../../id_generator'; +import { generateId } from '../../../../common/id_generator'; import { ConfigPanelWrapperProps } from './types'; import { useFocusUpdate } from './use_focus_update'; import { diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.test.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.test.tsx index cef598de31af0..11ec25606165f 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.test.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.test.tsx @@ -13,7 +13,7 @@ import { FramePublicAPI, Visualization, VisualizationConfigProps } from '../../. import { LayerPanel } from './layer_panel'; import { LayerActions } from './layer_actions'; import { coreMock } from '@kbn/core/public/mocks'; -import { generateId } from '../../../id_generator'; +import { generateId } from '../../../../common/id_generator'; import { createMockVisualization, createMockFramePublicAPI, diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/state_helpers.ts b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/state_helpers.ts index 466773ec1c6b2..9d7c5363ca3e3 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/state_helpers.ts +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/state_helpers.ts @@ -5,459 +5,8 @@ * 2.0. */ -import { IUiSettingsClient, SavedObjectReference } from '@kbn/core/public'; -import { Ast } from '@kbn/interpreter'; -import { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public'; -import { difference } from 'lodash'; -import type { DataViewsContract, DataViewSpec } from '@kbn/data-views-plugin/public'; -import { IStorageWrapper } from '@kbn/kibana-utils-plugin/public'; -import { DEFAULT_COLOR_MAPPING_CONFIG } from '@kbn/coloring'; -import { DataViewPersistableStateService } from '@kbn/data-views-plugin/common'; -import type { DataPublicPluginStart, TimefilterContract } from '@kbn/data-plugin/public'; -import { EventAnnotationServiceType } from '@kbn/event-annotation-plugin/public'; -import { - type EventAnnotationGroupConfig, - EVENT_ANNOTATION_GROUP_TYPE, -} from '@kbn/event-annotation-common'; -import { COLOR_MAPPING_OFF_BY_DEFAULT } from '../../../common/constants'; - -import type { - Datasource, - DatasourceMap, - IndexPattern, - IndexPatternMap, - IndexPatternRef, - InitializationOptions, - VisualizationMap, - VisualizeEditorContext, - SuggestionRequest, -} from '../../types'; -import { buildExpression } from './expression_helpers'; -import { Document } from '../../persistence/saved_object_store'; -import { getActiveDatasourceIdFromDoc, sortDataViewRefs } from '../../utils'; -import type { DatasourceState, DatasourceStates, VisualizationState } from '../../state_management'; -import { readFromStorage } from '../../settings_storage'; -import { loadIndexPatternRefs, loadIndexPatterns } from '../../data_views_service/loader'; -import { getDatasourceLayers } from '../../state_management/utils'; - -// there are 2 ways of coloring, the color mapping where the user can map specific colors to -// specific terms, and the palette assignment where the colors are assinged automatically -// by a palette with rotating the colors -const COLORING_METHOD: SuggestionRequest['mainPalette'] = COLOR_MAPPING_OFF_BY_DEFAULT - ? { - type: 'legacyPalette', - value: { - name: 'default', - type: 'palette', - }, - } - : { type: 'colorMapping', value: { ...DEFAULT_COLOR_MAPPING_CONFIG } }; - -function getIndexPatterns( - annotationGroupDataviewIds: string[], - references?: SavedObjectReference[], - initialContext?: VisualizeFieldContext | VisualizeEditorContext, - initialId?: string, - adHocDataviews?: string[] -) { - const indexPatternIds = [...annotationGroupDataviewIds]; - - // use the initialId only when no context is passed over - if (!initialContext && initialId) { - indexPatternIds.push(initialId); - } - if (initialContext) { - if ('isVisualizeAction' in initialContext) { - indexPatternIds.push(...initialContext.indexPatternIds); - } else { - indexPatternIds.push(initialContext.dataViewSpec.id!); - } - } - - if (references) { - for (const reference of references) { - if (reference.type === 'index-pattern') { - indexPatternIds.push(reference.id); - } - } - } - if (adHocDataviews) { - indexPatternIds.push(...adHocDataviews); - } - return [...new Set(indexPatternIds)]; -} - -const getLastUsedIndexPatternId = ( - storage: IStorageWrapper, - indexPatternRefs: IndexPatternRef[] -) => { - const indexPattern = readFromStorage(storage, 'indexPatternId'); - return indexPattern && indexPatternRefs.find((i) => i.id === indexPattern)?.id; -}; - -const getRefsForAdHocDataViewsFromContext = ( - indexPatternRefs: IndexPatternRef[], - usedIndexPatternsIds: string[], - indexPatterns: Record -) => { - const indexPatternIds = indexPatternRefs.map(({ id }) => id); - const adHocDataViewsIds = usedIndexPatternsIds.filter((id) => !indexPatternIds.includes(id)); - - const adHocDataViewsList = Object.values(indexPatterns).filter(({ id }) => - adHocDataViewsIds.includes(id) - ); - return adHocDataViewsList.map(({ id, title, name }) => ({ id, title, name })); -}; - -export async function initializeDataViews( - { - dataViews, - datasourceMap, - datasourceStates, - storage, - defaultIndexPatternId, - references, - initialContext, - adHocDataViews: persistedAdHocDataViews, - annotationGroups, - }: { - dataViews: DataViewsContract; - datasourceMap: DatasourceMap; - datasourceStates: DatasourceStates; - defaultIndexPatternId: string; - storage: IStorageWrapper; - references?: SavedObjectReference[]; - initialContext?: VisualizeFieldContext | VisualizeEditorContext; - adHocDataViews?: Record; - annotationGroups: Record; - }, - options?: InitializationOptions -) { - const adHocDataViews = Object.fromEntries( - Object.entries(persistedAdHocDataViews || {}).map(([id, persistedSpec]) => { - const spec = DataViewPersistableStateService.inject(persistedSpec, references || []); - return [id, spec]; - }) - ); - - const annotationGroupValues = Object.values(annotationGroups); - for (const group of annotationGroupValues) { - if (group.dataViewSpec?.id) { - adHocDataViews[group.dataViewSpec.id] = group.dataViewSpec; - } - } - - const { isFullEditor } = options ?? {}; - - // make it explicit or TS will infer never[] and break few lines down - const indexPatternRefs: IndexPatternRef[] = await (isFullEditor - ? loadIndexPatternRefs(dataViews) - : []); - - // if no state is available, use the fallbackId - const lastUsedIndexPatternId = getLastUsedIndexPatternId(storage, indexPatternRefs); - const fallbackId = lastUsedIndexPatternId || defaultIndexPatternId || indexPatternRefs[0]?.id; - const initialId = - !initialContext && - Object.keys(datasourceMap).every((datasourceId) => !datasourceStates[datasourceId]?.state) - ? fallbackId - : undefined; - - const adHocDataviewsIds: string[] = Object.keys(adHocDataViews || {}); - - const usedIndexPatternsIds = getIndexPatterns( - annotationGroupValues.map((group) => group.indexPatternId), - references, - initialContext, - initialId, - adHocDataviewsIds - ); - - // load them - const availableIndexPatterns = new Set(indexPatternRefs.map(({ id }: IndexPatternRef) => id)); - - const notUsedPatterns: string[] = difference([...availableIndexPatterns], usedIndexPatternsIds); - - const indexPatterns = await loadIndexPatterns({ - dataViews, - patterns: usedIndexPatternsIds, - notUsedPatterns, - cache: {}, - adHocDataViews, - }); - - const adHocDataViewsRefs = getRefsForAdHocDataViewsFromContext( - indexPatternRefs, - usedIndexPatternsIds, - indexPatterns - ); - return { - indexPatternRefs: sortDataViewRefs([...indexPatternRefs, ...adHocDataViewsRefs]), - indexPatterns, - }; -} - -const initializeEventAnnotationGroups = async ( - eventAnnotationService: EventAnnotationServiceType, - references?: SavedObjectReference[] -) => { - const annotationGroups: Record = {}; - - await Promise.allSettled( - (references || []) - .filter((ref) => ref.type === EVENT_ANNOTATION_GROUP_TYPE) - .map(({ id }) => - eventAnnotationService.loadAnnotationGroup(id).then((group) => { - annotationGroups[id] = group; - }) - ) - ); - - return annotationGroups; -}; - -/** - * This function composes both initializeDataViews & initializeDatasources into a single call - */ -export async function initializeSources( - { - dataViews, - eventAnnotationService, - datasourceMap, - visualizationMap, - visualizationState, - datasourceStates, - storage, - defaultIndexPatternId, - references, - initialContext, - adHocDataViews, - }: { - dataViews: DataViewsContract; - eventAnnotationService: EventAnnotationServiceType; - datasourceMap: DatasourceMap; - visualizationMap: VisualizationMap; - visualizationState: VisualizationState; - datasourceStates: DatasourceStates; - defaultIndexPatternId: string; - storage: IStorageWrapper; - references?: SavedObjectReference[]; - initialContext?: VisualizeFieldContext | VisualizeEditorContext; - adHocDataViews?: Record; - }, - options?: InitializationOptions -) { - const annotationGroups = await initializeEventAnnotationGroups( - eventAnnotationService, - references - ); - - const { indexPatternRefs, indexPatterns } = await initializeDataViews( - { - datasourceMap, - datasourceStates, - initialContext, - dataViews, - storage, - defaultIndexPatternId, - references, - adHocDataViews, - annotationGroups, - }, - options - ); - - return { - indexPatterns, - indexPatternRefs, - annotationGroups, - datasourceStates: initializeDatasources({ - datasourceMap, - datasourceStates, - initialContext, - indexPatternRefs, - indexPatterns, - references, - }), - visualizationState: initializeVisualization({ - visualizationMap, - visualizationState, - references, - initialContext, - annotationGroups, - }), - }; -} - -export function initializeVisualization({ - visualizationMap, - visualizationState, - references, - annotationGroups, -}: { - visualizationState: VisualizationState; - visualizationMap: VisualizationMap; - references?: SavedObjectReference[]; - initialContext?: VisualizeFieldContext | VisualizeEditorContext; - annotationGroups: Record; -}) { - if (visualizationState?.activeId) { - return ( - visualizationMap[visualizationState.activeId]?.initialize( - () => '', - visualizationState.state, - // initialize a new visualization with the color mapping off - COLORING_METHOD, - annotationGroups, - references - ) ?? visualizationState.state - ); - } - return visualizationState.state; -} - -export function initializeDatasources({ - datasourceMap, - datasourceStates, - indexPatternRefs, - indexPatterns, - references, - initialContext, -}: { - datasourceMap: DatasourceMap; - datasourceStates: DatasourceStates; - indexPatterns: Record; - indexPatternRefs: IndexPatternRef[]; - references?: SavedObjectReference[]; - initialContext?: VisualizeFieldContext | VisualizeEditorContext; -}) { - // init datasources - const states: DatasourceStates = {}; - for (const [datasourceId, datasource] of Object.entries(datasourceMap)) { - if (datasourceStates[datasourceId]) { - const state = datasource.initialize( - datasourceStates[datasourceId].state || undefined, - references, - initialContext, - indexPatternRefs, - indexPatterns - ); - states[datasourceId] = { isLoading: false, state }; - } - } - return states; -} - -export interface DocumentToExpressionReturnType { - ast: Ast | null; - indexPatterns: IndexPatternMap; - indexPatternRefs: IndexPatternRef[]; - activeVisualizationState: unknown; -} - -export async function persistedStateToExpression( - datasourceMap: DatasourceMap, - visualizations: VisualizationMap, - doc: Document, - services: { - uiSettings: IUiSettingsClient; - storage: IStorageWrapper; - dataViews: DataViewsContract; - timefilter: TimefilterContract; - nowProvider: DataPublicPluginStart['nowProvider']; - eventAnnotationService: EventAnnotationServiceType; - } -): Promise { - const { - state: { - visualization: persistedVisualizationState, - datasourceStates: persistedDatasourceStates, - adHocDataViews, - internalReferences, - }, - visualizationType, - references, - title, - description, - } = doc; - if (!visualizationType) { - return { ast: null, indexPatterns: {}, indexPatternRefs: [], activeVisualizationState: null }; - } - - const annotationGroups = await initializeEventAnnotationGroups( - services.eventAnnotationService, - references - ); - - const visualization = visualizations[visualizationType!]; - const activeVisualizationState = initializeVisualization({ - visualizationMap: visualizations, - visualizationState: { - state: persistedVisualizationState, - activeId: visualizationType, - }, - annotationGroups, - references: [...references, ...(internalReferences || [])], - }); - const datasourceStatesFromSO = Object.fromEntries( - Object.entries(persistedDatasourceStates).map(([id, state]) => [ - id, - { isLoading: false, state }, - ]) - ); - const { indexPatterns, indexPatternRefs } = await initializeDataViews( - { - datasourceMap, - datasourceStates: datasourceStatesFromSO, - references, - dataViews: services.dataViews, - storage: services.storage, - defaultIndexPatternId: services.uiSettings.get('defaultIndex'), - adHocDataViews, - annotationGroups, - }, - { isFullEditor: false } - ); - const datasourceStates = initializeDatasources({ - datasourceMap, - datasourceStates: datasourceStatesFromSO, - references: [...references, ...(internalReferences || [])], - indexPatterns, - indexPatternRefs, - }); - - const datasourceLayers = getDatasourceLayers(datasourceStates, datasourceMap, indexPatterns); - - const datasourceId = getActiveDatasourceIdFromDoc(doc); - if (datasourceId == null) { - return { - ast: null, - indexPatterns, - indexPatternRefs, - activeVisualizationState, - }; - } - - const currentTimeRange = services.timefilter.getAbsoluteTime(); - - return { - ast: buildExpression({ - title, - description, - visualization, - visualizationState: activeVisualizationState, - datasourceMap, - datasourceStates, - datasourceLayers, - indexPatterns, - dateRange: { fromDate: currentTimeRange.from, toDate: currentTimeRange.to }, - nowInstant: services.nowProvider.get(), - }), - activeVisualizationState, - indexPatterns, - indexPatternRefs, - }; -} +import { DatasourceState } from '../../state_management'; +import { Datasource, IndexPatternMap } from '../../types'; export function getMissingIndexPattern( currentDatasource: Datasource | null | undefined, diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.tsx index 3f94d11d731e9..2a04fbab45ecb 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/suggestion_panel.tsx @@ -47,7 +47,7 @@ import { UserMessagesGetter, } from '../../types'; import { getSuggestions, switchToSuggestion } from './suggestion_helpers'; -import { getDatasourceExpressionsByLayers } from './expression_helpers'; +import { getDatasourceExpressionsByLayers } from '../../../common/expression_helpers'; import { showMemoizedErrorNotification } from '../../lens_ui_errors/memoized_error_notification'; import { getMissingIndexPattern } from './state_helpers'; import { diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx index c40c81285dc15..0a93e282041ac 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/chart_switch.tsx @@ -42,7 +42,7 @@ import { selectVisualization, selectDatasourceStates, } from '../../../state_management'; -import { generateId } from '../../../id_generator/id_generator'; +import { generateId } from '../../../../common/id_generator/id_generator'; interface VisualizationSelection { visualizationId: string; diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx index 73a4ef853390d..8ee6470075b28 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx @@ -55,7 +55,7 @@ import { isMessageRemovable, } from '../../../types'; import { switchToSuggestion } from '../suggestion_helpers'; -import { buildExpression } from '../expression_helpers'; +import { buildExpression } from '../../../../common/expression_helpers'; import { WorkspacePanelWrapper } from './workspace_panel_wrapper'; import applyChangesIllustrationDark from '../../../assets/render_dark@2x.png'; import applyChangesIllustrationLight from '../../../assets/render_light@2x.png'; diff --git a/x-pack/plugins/lens/public/editor_frame_service/service.tsx b/x-pack/plugins/lens/public/editor_frame_service/service.tsx index a6d0a48addf6b..c4a78a80a320e 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/service.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/service.tsx @@ -103,7 +103,15 @@ export class EditorFrameService { const { persistedStateToExpression } = await import('../async_services'); - return persistedStateToExpression(resolvedDatasources, resolvedVisualizations, doc, services); + return persistedStateToExpression( + resolvedDatasources, + resolvedVisualizations, + doc, + services.uiSettings.get('defaultIndex'), + services.timefilter.getAbsoluteTime(), + services.eventAnnotationService.loadAnnotationGroup, + services + ); }; public setup(): EditorFrameSetup { diff --git a/x-pack/plugins/lens/public/id_generator.ts b/x-pack/plugins/lens/public/id_generator.ts new file mode 100644 index 0000000000000..78136b3cf167f --- /dev/null +++ b/x-pack/plugins/lens/public/id_generator.ts @@ -0,0 +1,8 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export { generateId } from '../common/id_generator'; diff --git a/x-pack/plugins/lens/public/state_management/lens_slice.ts b/x-pack/plugins/lens/public/state_management/lens_slice.ts index df3564958111b..164e1507d4393 100644 --- a/x-pack/plugins/lens/public/state_management/lens_slice.ts +++ b/x-pack/plugins/lens/public/state_management/lens_slice.ts @@ -26,7 +26,7 @@ import type { import { getInitialDatasourceId, getResolvedDateRange, getRemoveOperation } from '../utils'; import type { DataViewsState, LensAppState, LensStoreDeps, VisualizationState } from './types'; import type { Datasource, Visualization } from '../types'; -import { generateId } from '../id_generator'; +import { generateId } from '../../common/id_generator'; import type { DateRange, LayerType } from '../../common/types'; import { getVisualizeFieldSuggestions } from '../editor_frame_service/editor_frame/suggestion_helpers'; import type { FramePublicAPI, LensEditContextMapping, LensEditEvent } from '../types'; diff --git a/x-pack/plugins/lens/public/types.ts b/x-pack/plugins/lens/public/types.ts index e5c9fad96d6ca..700844504c0be 100644 --- a/x-pack/plugins/lens/public/types.ts +++ b/x-pack/plugins/lens/public/types.ts @@ -29,10 +29,8 @@ import type { BrushTriggerEvent, MultiClickTriggerEvent, } from '@kbn/charts-plugin/public'; -import type { IndexPatternAggRestrictions } from '@kbn/data-plugin/public'; -import type { FieldSpec, DataViewSpec, DataView } from '@kbn/data-views-plugin/common'; +import type { DataViewSpec, DataView } from '@kbn/data-views-plugin/common'; import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; -import type { FieldFormatParams } from '@kbn/field-formats-plugin/common'; import type { SearchResponseWarning } from '@kbn/search-response-warnings'; import type { EuiButtonIconProps } from '@elastic/eui'; import { SearchRequest } from '@kbn/data-plugin/public'; @@ -42,7 +40,15 @@ import { CellValueContext } from '@kbn/embeddable-plugin/public'; import { EventAnnotationGroupConfig } from '@kbn/event-annotation-common'; import type { DraggingIdentifier, DragDropIdentifier, DropType } from '@kbn/dom-drag-drop'; import type { AccessorConfig } from '@kbn/visualization-ui-components'; -import type { DateRange, LayerType, SortingHint } from '../common/types'; +import type { + DatasourceCommon, + DateRange, + IndexPattern, + IndexPatternField, + IndexPatternMap, + LayerType, + SortingHint, +} from '../common/types'; import type { LensSortActionData, LensResizeActionData, @@ -58,47 +64,9 @@ import { } from './visualizations/datatable/components/constants'; import type { LensInspector } from './lens_inspector_service'; import type { DataViewsState } from './state_management/types'; -import type { IndexPatternServiceAPI } from './data_views_service/service'; +import type { IndexPatternServiceAPI } from '../common/data_views_service/service'; import type { Document } from './persistence/saved_object_store'; -export interface IndexPatternRef { - id: string; - title: string; - name?: string; -} - -export interface IndexPattern { - id: string; - fields: IndexPatternField[]; - getFieldByName(name: string): IndexPatternField | undefined; - title: string; - name?: string; - timeFieldName?: string; - fieldFormatMap?: Record< - string, - { - id: string; - params: FieldFormatParams; - } - >; - hasRestrictions: boolean; - spec: DataViewSpec; - isPersisted: boolean; -} - -export type IndexPatternField = FieldSpec & { - displayName: string; - aggregationRestrictions?: Partial; - /** - * Map of fields which can be used, but may fail partially (ranked lower than others) - */ - partiallyApplicableFunctions?: Partial>; - timeSeriesMetric?: 'histogram' | 'summary' | 'gauge' | 'counter' | 'position'; - timeSeriesRollup?: boolean; - meta?: boolean; - runtime?: boolean; -}; - export interface PublicAPIProps { state: T; layerId: string; @@ -115,7 +83,6 @@ export interface EditorFrameProps { export type VisualizationMap = Record; export type DatasourceMap = Record; -export type IndexPatternMap = Record; export interface EditorFrameInstance { EditorFrameContainer: (props: EditorFrameProps) => React.ReactElement; @@ -313,21 +280,9 @@ export function isMessageRemovable(message: UserMessage): message is RemovableUs /** * Interface for the datasource registry */ -export interface Datasource { - id: string; +export interface Datasource extends DatasourceCommon { alias?: string[]; - // For initializing, either from an empty state or from persisted state - // Because this will be called at runtime, state might have a type of `any` and - // datasources should validate their arguments - initialize: ( - state?: P, - savedObjectReferences?: SavedObjectReference[], - initialContext?: VisualizeFieldContext | VisualizeEditorContext, - indexPatternRefs?: IndexPatternRef[], - indexPatterns?: IndexPatternMap - ) => T; - // Given the current state, which parts should be saved? getPersistableState: (state: T) => { state: P; savedObjectReferences: SavedObjectReference[] }; @@ -341,7 +296,6 @@ export interface Datasource { newLayerId: string, getNewId: (id: string) => string ) => T; - getLayers: (state: T) => string[]; removeColumn: (props: { prevState: T; layerId: string; @@ -413,15 +367,6 @@ export interface Datasource { onRefreshIndexPattern: () => void; - toExpression: ( - state: T, - layerId: string, - indexPatterns: IndexPatternMap, - dateRange: DateRange, - nowInstant: Date, - searchSessionId?: string - ) => ExpressionAstExpression | string | null; - getDatasourceSuggestionsForField: ( state: T, field: unknown, diff --git a/x-pack/plugins/lens/public/utils.ts b/x-pack/plugins/lens/public/utils.ts index 86c769444c69c..1e409659c8695 100644 --- a/x-pack/plugins/lens/public/utils.ts +++ b/x-pack/plugins/lens/public/utils.ts @@ -35,7 +35,7 @@ import { isLensFilterEvent, } from './types'; import type { DatasourceStates, VisualizationState } from './state_management'; -import type { IndexPatternServiceAPI } from './data_views_service/service'; +import type { IndexPatternServiceAPI } from '../common/data_views_service/service'; import { COLOR_MAPPING_OFF_BY_DEFAULT } from '../common/constants'; export function getVisualizeGeoFieldMessage(fieldType: string) { @@ -66,14 +66,6 @@ export function getTimeZone(uiSettings: IUiSettingsClient) { return configuredTimeZone; } -export function getActiveDatasourceIdFromDoc(doc?: Document) { - if (!doc) { - return null; - } - - const [firstDatasourceFromDoc] = Object.keys(doc.state.datasourceStates); - return firstDatasourceFromDoc || null; -} export function getActiveVisualizationIdFromDoc(doc?: Document) { if (!doc) { @@ -337,11 +329,6 @@ export const isOperationFromTheSameGroup = (op1?: DraggingIdentifier, op2?: Drag ); }; -export const sortDataViewRefs = (dataViewRefs: IndexPatternRef[]) => - dataViewRefs.sort((a, b) => { - return a.title.localeCompare(b.title); - }); - export const getSearchWarningMessages = ( adapter: RequestAdapter, datasource: Datasource, diff --git a/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx b/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx index ac78d37ca28e9..72caf8b227115 100644 --- a/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx +++ b/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx @@ -36,7 +36,7 @@ import { GROUP_ID, LENS_GAUGE_ID, GaugeVisualizationState } from './constants'; import { GaugeToolbar } from './toolbar_component'; import { applyPaletteParams } from '../../shared_components'; import { GaugeDimensionEditor } from './dimension_editor'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { getAccessorsFromState } from './utils'; const groupLabelForGauge = i18n.translate('xpack.lens.metric.groupLabel', { diff --git a/x-pack/plugins/lens/public/visualizations/legacy_metric/visualization.test.ts b/x-pack/plugins/lens/public/visualizations/legacy_metric/visualization.test.ts index d6a70ddbe7114..6d2eb5fb12d3b 100644 --- a/x-pack/plugins/lens/public/visualizations/legacy_metric/visualization.test.ts +++ b/x-pack/plugins/lens/public/visualizations/legacy_metric/visualization.test.ts @@ -9,7 +9,7 @@ import { getLegacyMetricVisualization } from './visualization'; import { LayerTypes } from '@kbn/expression-xy-plugin/public'; import type { LegacyMetricState } from '../../../common/types'; import { createMockDatasource, createMockFramePublicAPI } from '../../mocks'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { DatasourcePublicAPI, FramePublicAPI } from '../../types'; import { chartPluginMock } from '@kbn/charts-plugin/public/mocks'; import { ColorMode } from '@kbn/charts-plugin/common'; diff --git a/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx b/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx index 00b931714c1be..a275e63042d43 100644 --- a/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx +++ b/x-pack/plugins/lens/public/visualizations/metric/visualization.tsx @@ -31,7 +31,7 @@ import { import { GROUP_ID, LENS_METRIC_ID } from './constants'; import { DimensionEditor, DimensionEditorAdditionalSection } from './dimension_editor'; import { Toolbar } from './toolbar'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { toExpression } from './to_expression'; import { nonNullable } from '../../utils'; diff --git a/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx b/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx index 0a7df7f63d5ef..56334eee3c985 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx +++ b/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx @@ -27,7 +27,7 @@ import { getDataLayers, isAnnotationsLayer, } from '../visualization_helpers'; -import { generateId } from '../../../id_generator'; +import { generateId } from '../../../../common/id_generator'; const MAX_DATE = 8640000000000000; const MIN_DATE = -8640000000000000; diff --git a/x-pack/plugins/lens/public/visualizations/xy/reference_line_helpers.tsx b/x-pack/plugins/lens/public/visualizations/xy/reference_line_helpers.tsx index e23a407bc26c6..34f6c0dd6b5f5 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/reference_line_helpers.tsx +++ b/x-pack/plugins/lens/public/visualizations/xy/reference_line_helpers.tsx @@ -29,7 +29,7 @@ import { isNumericMetric, isReferenceLayer, } from './visualization_helpers'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { defaultReferenceLineColor } from './color_assignment'; export interface ReferenceLineBase { diff --git a/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx b/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx index d3bb805a0a381..8065dbbc96fec 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx +++ b/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx @@ -27,7 +27,7 @@ import { type AccessorConfig, DimensionTrigger } from '@kbn/visualization-ui-com import { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; import { getColorsFromMapping } from '@kbn/coloring'; import useObservable from 'react-use/lib/useObservable'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { isDraggedDataViewField, isOperationFromCompatibleGroup, diff --git a/x-pack/plugins/lens/public/visualizations/xy/xy_suggestions.test.ts b/x-pack/plugins/lens/public/visualizations/xy/xy_suggestions.test.ts index 82368c2414c9a..de691908be1bf 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/xy_suggestions.test.ts +++ b/x-pack/plugins/lens/public/visualizations/xy/xy_suggestions.test.ts @@ -14,7 +14,7 @@ import { XYAnnotationLayerConfig, XYDataLayerConfig, } from './types'; -import { generateId } from '../../id_generator'; +import { generateId } from '../../../common/id_generator'; import { getXyVisualization } from './xy_visualization'; import { chartPluginMock } from '@kbn/charts-plugin/public/mocks'; import { eventAnnotationServiceMock } from '@kbn/event-annotation-plugin/public/mocks'; diff --git a/x-pack/plugins/lens/server/plugin.ts b/x-pack/plugins/lens/server/plugin.ts new file mode 100644 index 0000000000000..bd877423fa82c --- /dev/null +++ b/x-pack/plugins/lens/server/plugin.ts @@ -0,0 +1,260 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { + Plugin, + CoreSetup, + CoreStart, + PluginInitializerContext, + SavedObjectsClientContract, + ElasticsearchClient, + KibanaRequest, +} from '@kbn/core/server'; +import { + DataViewsServerPluginSetup, + PluginStart as DataViewsServerPluginStart, +} from '@kbn/data-views-plugin/server'; +import { + PluginStart as DataPluginStart, + PluginSetup as DataPluginSetup, +} from '@kbn/data-plugin/server'; +import { ExpressionsServerSetup, ExpressionsServerStart } from '@kbn/expressions-plugin/server'; +import { FieldFormatsStart } from '@kbn/field-formats-plugin/server'; +import type { MigrateFunctionsObject } from '@kbn/kibana-utils-plugin/common'; +import { ContentManagementServerSetup } from '@kbn/content-management-plugin/server'; + +import { + TaskManagerSetupContract, + TaskManagerStartContract, +} from '@kbn/task-manager-plugin/server'; +import { EmbeddableSetup } from '@kbn/embeddable-plugin/server'; +import { DataViewPersistableStateService } from '@kbn/data-views-plugin/common'; +import { SharePluginSetup } from '@kbn/share-plugin/server'; +import type { EventAnnotationGroupSavedObjectAttributes } from '@kbn/event-annotation-plugin/common'; +import { mapSavedObjectToGroupConfig } from '@kbn/event-annotation-plugin/common'; +import { RequestAdapter } from '@kbn/inspector-plugin/common'; +import { lastValueFrom } from 'rxjs'; +import { setupSavedObjects } from './saved_objects'; +import { setupExpressions } from './expressions'; +import { makeLensEmbeddableFactory } from './embeddable/make_lens_embeddable_factory'; +import type { CustomVisualizationMigrations } from './migrations/types'; +import { LensAppLocatorDefinition } from '../common/locator/locator'; +import { CONTENT_ID, LATEST_VERSION } from '../common/content_management'; +import { LensStorage } from './content_management'; +import type { Document } from '../public/persistence'; +import type { DatasourceCommonMap } from '../common/types'; +import { initializeDataViews } from '../common/state_helpers'; +import { + initializeDatasources, + initializeEventAnnotationGroups, +} from '../common/doc_to_expression'; +import { getDatasourceExpressionsByLayers } from '../common/expression_helpers'; +import { TextBasedDatasourceCommon } from '../common/datasources/text_based/text_based_languages'; +import { getCommonFormBasedDatasource } from '../common/datasources/form_based/form_based'; + +export interface PluginSetupContract { + taskManager?: TaskManagerSetupContract; + embeddable: EmbeddableSetup; + expressions: ExpressionsServerSetup; + data: DataPluginSetup; + share?: SharePluginSetup; + contentManagement: ContentManagementServerSetup; + dataViews: DataViewsServerPluginSetup; +} + +export interface PluginStartContract { + taskManager?: TaskManagerStartContract; + fieldFormats: FieldFormatsStart; + data: DataPluginStart; + dataViews: DataViewsServerPluginStart; + expressions: ExpressionsServerStart; +} + +export interface LensServerPluginSetup { + /** + * Server side embeddable definition which provides migrations to run if Lens state is embedded into another saved object somewhere + */ + lensEmbeddableFactory: ReturnType; + /** + * Register custom migration functions for custom third party Lens visualizations + */ + registerVisualizationMigration: ( + id: string, + migrationsGetter: () => MigrateFunctionsObject + ) => void; + /** + * Converts a Lens document to an expression + */ + extractQueries: ( + doc: Document, + clients: { savedObjects: SavedObjectsClientContract; elasticsearch: ElasticsearchClient }, + request: KibanaRequest + ) => Promise; +} + +export class LensServerPlugin implements Plugin { + private customVisualizationMigrations: CustomVisualizationMigrations = {}; + + constructor(private initializerContext: PluginInitializerContext) {} + + setup(core: CoreSetup, plugins: PluginSetupContract) { + const getFilterMigrations = plugins.data.query.filterManager.getAllMigrations.bind( + plugins.data.query.filterManager + ); + setupSavedObjects(core, getFilterMigrations, this.customVisualizationMigrations); + setupExpressions(core, plugins.expressions); + + if (plugins.share) { + plugins.share.url.locators.create(new LensAppLocatorDefinition()); + } + + plugins.contentManagement.register({ + id: CONTENT_ID, + storage: new LensStorage({ + throwOnResultValidationError: this.initializerContext.env.mode.dev, + logger: this.initializerContext.logger.get('storage'), + }), + version: { + latest: LATEST_VERSION, + }, + }); + + const lensEmbeddableFactory = makeLensEmbeddableFactory( + getFilterMigrations, + DataViewPersistableStateService.getAllMigrations.bind(DataViewPersistableStateService), + this.customVisualizationMigrations + ); + plugins.embeddable.registerEmbeddableFactory(lensEmbeddableFactory()); + + const extractQueries = async ( + doc: Document, + clients: { savedObjects: SavedObjectsClientContract; elasticsearch: ElasticsearchClient }, + request: KibanaRequest + ) => { + const [, { dataViews, expressions }] = await core.getStartServices(); + + const dataViewsService = await dataViews.dataViewsServiceFactory( + clients.savedObjects, + clients.elasticsearch + ); + + const loadAnnotationGroup = async (id: string) => { + const savedObject = + await clients.savedObjects.get( + 'annotation', + id + ); + return mapSavedObjectToGroupConfig(savedObject); + }; + + const { + state: { datasourceStates: persistedDatasourceStates, adHocDataViews, internalReferences }, + references, + } = doc; + + const annotationGroups = await initializeEventAnnotationGroups( + loadAnnotationGroup, + references + ); + + const datasources = [TextBasedDatasourceCommon, getCommonFormBasedDatasource({})]; + + const datasourceMap: DatasourceCommonMap = {}; + + datasources.forEach((datasource) => { + datasourceMap[datasource.id] = datasource; + }); + + const datasourceStatesFromSO = Object.fromEntries( + Object.entries(persistedDatasourceStates).map(([id, state]) => [ + id, + { isLoading: false, state }, + ]) + ); + + const { indexPatterns, indexPatternRefs } = await initializeDataViews( + { + datasourceMap, + datasourceStates: datasourceStatesFromSO, + references, + dataViews: dataViewsService, + defaultIndexPatternId: '', + adHocDataViews, + annotationGroups, + }, + { isFullEditor: false } + ); + + const datasourceStates = initializeDatasources({ + datasourceMap, + datasourceStates: datasourceStatesFromSO, + references: [...references, ...(internalReferences || [])], + indexPatterns, + indexPatternRefs, + }); + + const datasourceExpressionsByLayers = getDatasourceExpressionsByLayers( + datasourceMap, + datasourceStates, + indexPatterns, + { fromDate: '', toDate: '' }, + new Date() + ); + + if (!datasourceExpressionsByLayers) { + return []; + } + + const requestAdapter = new RequestAdapter(); + + const asts = Object.values(datasourceExpressionsByLayers); + + const observables = asts.map((ast) => + expressions.run(ast, undefined, { + kibanaRequest: request, + inspectorAdapters: { + requests: requestAdapter, + }, + }) + ); + + const requests: Array = []; + + observables.map((obs) => + obs.subscribe(() => { + requests.push(...requestAdapter.getRequests().map(({ json }) => json)); + }) + ); + + const promises = observables.map((obs) => lastValueFrom(obs)); + + await Promise.all(promises); + + return requests.filter(Boolean) as object[]; + }; + + return { + lensEmbeddableFactory, + registerVisualizationMigration: ( + id: string, + migrationsGetter: () => MigrateFunctionsObject + ) => { + if (this.customVisualizationMigrations[id]) { + throw new Error(`Migrations object for visualization ${id} registered already`); + } + this.customVisualizationMigrations[id] = migrationsGetter; + }, + extractQueries, + }; + } + + start(_core: CoreStart, _plugins: PluginStartContract) { + return {}; + } + + stop() {} +} diff --git a/x-pack/plugins/lens/server/plugin.tsx b/x-pack/plugins/lens/server/plugin.tsx deleted file mode 100644 index c7584474dfc2b..0000000000000 --- a/x-pack/plugins/lens/server/plugin.tsx +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { Plugin, CoreSetup, CoreStart, PluginInitializerContext } from '@kbn/core/server'; -import { PluginStart as DataViewsServerPluginStart } from '@kbn/data-views-plugin/server'; -import { - PluginStart as DataPluginStart, - PluginSetup as DataPluginSetup, -} from '@kbn/data-plugin/server'; -import { ExpressionsServerSetup } from '@kbn/expressions-plugin/server'; -import { FieldFormatsStart } from '@kbn/field-formats-plugin/server'; -import type { MigrateFunctionsObject } from '@kbn/kibana-utils-plugin/common'; -import { ContentManagementServerSetup } from '@kbn/content-management-plugin/server'; - -import { - TaskManagerSetupContract, - TaskManagerStartContract, -} from '@kbn/task-manager-plugin/server'; -import { EmbeddableSetup } from '@kbn/embeddable-plugin/server'; -import { DataViewPersistableStateService } from '@kbn/data-views-plugin/common'; -import { SharePluginSetup } from '@kbn/share-plugin/server'; -import { setupSavedObjects } from './saved_objects'; -import { setupExpressions } from './expressions'; -import { makeLensEmbeddableFactory } from './embeddable/make_lens_embeddable_factory'; -import type { CustomVisualizationMigrations } from './migrations/types'; -import { LensAppLocatorDefinition } from '../common/locator/locator'; -import { CONTENT_ID, LATEST_VERSION } from '../common/content_management'; -import { LensStorage } from './content_management'; - -export interface PluginSetupContract { - taskManager?: TaskManagerSetupContract; - embeddable: EmbeddableSetup; - expressions: ExpressionsServerSetup; - data: DataPluginSetup; - share?: SharePluginSetup; - contentManagement: ContentManagementServerSetup; -} - -export interface PluginStartContract { - taskManager?: TaskManagerStartContract; - fieldFormats: FieldFormatsStart; - data: DataPluginStart; - dataViews: DataViewsServerPluginStart; -} - -export interface LensServerPluginSetup { - /** - * Server side embeddable definition which provides migrations to run if Lens state is embedded into another saved object somewhere - */ - lensEmbeddableFactory: ReturnType; - /** - * Register custom migration functions for custom third party Lens visualizations - */ - registerVisualizationMigration: ( - id: string, - migrationsGetter: () => MigrateFunctionsObject - ) => void; -} - -export class LensServerPlugin implements Plugin { - private customVisualizationMigrations: CustomVisualizationMigrations = {}; - - constructor(private initializerContext: PluginInitializerContext) {} - - setup(core: CoreSetup, plugins: PluginSetupContract) { - const getFilterMigrations = plugins.data.query.filterManager.getAllMigrations.bind( - plugins.data.query.filterManager - ); - setupSavedObjects(core, getFilterMigrations, this.customVisualizationMigrations); - setupExpressions(core, plugins.expressions); - - if (plugins.share) { - plugins.share.url.locators.create(new LensAppLocatorDefinition()); - } - - plugins.contentManagement.register({ - id: CONTENT_ID, - storage: new LensStorage({ - throwOnResultValidationError: this.initializerContext.env.mode.dev, - logger: this.initializerContext.logger.get('storage'), - }), - version: { - latest: LATEST_VERSION, - }, - }); - - const lensEmbeddableFactory = makeLensEmbeddableFactory( - getFilterMigrations, - DataViewPersistableStateService.getAllMigrations.bind(DataViewPersistableStateService), - this.customVisualizationMigrations - ); - plugins.embeddable.registerEmbeddableFactory(lensEmbeddableFactory()); - return { - lensEmbeddableFactory, - registerVisualizationMigration: ( - id: string, - migrationsGetter: () => MigrateFunctionsObject - ) => { - if (this.customVisualizationMigrations[id]) { - throw new Error(`Migrations object for visualization ${id} registered already`); - } - this.customVisualizationMigrations[id] = migrationsGetter; - }, - }; - } - - start(core: CoreStart, plugins: PluginStartContract) { - return {}; - } - - stop() {} -}