Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { DATA_VIEW_SAVED_OBJECT_TYPE } from '@kbn/data-views-plugin/common';
import type { Reference } from '@kbn/content-management-utils';
import type {
FieldStatisticsTableEmbeddableState,
StoredFieldStatisticsTableEmbeddableState,
} from './types';
import { FIELD_STATS_DATA_VIEW_REF_NAME } from './constants';

export function transformIn(state: FieldStatisticsTableEmbeddableState): {
state: StoredFieldStatisticsTableEmbeddableState;
references: Reference[];
} {
const { dataViewId, ...rest } = state;
return {
state: rest,
references: dataViewId
? [
{
type: DATA_VIEW_SAVED_OBJECT_TYPE,
name: FIELD_STATS_DATA_VIEW_REF_NAME,
id: dataViewId,
},
]
: [],
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { Reference } from '@kbn/content-management-utils';
import type {
FieldStatisticsTableEmbeddableState,
StoredFieldStatisticsTableEmbeddableState,
} from './types';
import { FIELD_STATS_DATA_VIEW_REF_NAME } from './constants';

export function transformOut(
state: StoredFieldStatisticsTableEmbeddableState,
references?: Reference[]
): FieldStatisticsTableEmbeddableState {
const dataViewIdRef = references?.find((ref) => ref.name === FIELD_STATS_DATA_VIEW_REF_NAME);
return {
...state,
dataViewId: dataViewIdRef?.id ?? '',
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 { AggregateQuery } from '@kbn/es-query';
import type { SerializedTimeRange } from '@kbn/presentation-publishing';
import type { SerializedTitles } from '@kbn/presentation-publishing-schemas';

export enum FieldStatsInitializerViewType {
DATA_VIEW = 'dataview',
ESQL = 'esql',
}

export interface FieldStatsInitialState {
dataViewId?: string;
viewType?: FieldStatsInitializerViewType;
query?: AggregateQuery;
showDistributions?: boolean;
}
export type FieldStatisticsTableEmbeddableState = FieldStatsInitialState &
SerializedTitles &
SerializedTimeRange & {};

export type StoredFieldStatisticsTableEmbeddableState = Omit<
FieldStatisticsTableEmbeddableState,
'dataViewId'
>;
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"cloud"
],
"requiredBundles": [
"dataViews",
"kibanaReact",
"kibanaUtils",
"maps",
Expand Down
1 change: 1 addition & 0 deletions x-pack/platform/plugins/private/data_visualizer/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependsOn:
- '@kbn/file-upload'
- '@kbn/charts-theme'
- '@kbn/presentation-util'
- '@kbn/presentation-publishing-schemas'
tags:
- plugin
- prod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { Reference } from '@kbn/content-management-utils';

import type { StartServicesAccessor } from '@kbn/core-lifecycle-browser';
import {
APPLY_FILTER_TRIGGER,
generateFilters,
type DataPublicPluginStart,
} from '@kbn/data-plugin/public';
import type { DataViewField } from '@kbn/data-views-plugin/common';
import { DATA_VIEW_SAVED_OBJECT_TYPE } from '@kbn/data-views-plugin/common';
import type { EmbeddableFactory } from '@kbn/embeddable-plugin/public';
import { i18n } from '@kbn/i18n';
import {
type SerializedPanelState,
apiHasExecutionContext,
fetch$,
initializeTimeRangeManager,
Expand All @@ -27,7 +25,6 @@ import {
timeRangeComparators,
} from '@kbn/presentation-publishing';
import { initializeUnsavedChanges } from '@kbn/presentation-containers';
import { cloneDeep } from 'lodash';
import React, { useEffect } from 'react';
import useObservable from 'react-use/lib/useObservable';
import {
Expand Down Expand Up @@ -56,12 +53,12 @@ import type { DataVisualizerTableState } from '../../../../../common/types';
import type { DataVisualizerPluginStart } from '../../../../plugin';
import type { FieldStatisticsTableEmbeddableState } from '../grid_embeddable/types';
import { FieldStatsInitializerViewType } from '../grid_embeddable/types';
import { FIELD_STATS_EMBEDDABLE_TYPE, FIELD_STATS_DATA_VIEW_REF_NAME } from './constants';
import { initializeFieldStatsControls } from './initialize_field_stats_controls';
import type { DataVisualizerStartDependencies } from '../../../common/types/data_visualizer_plugin';
import type { FieldStatisticsTableEmbeddableApi } from './types';
import { isESQLQuery } from '../../search_strategy/requests/esql_utils';
import { FieldStatsComponentType } from '../../constants/field_stats_component_type';
import { FIELD_STATS_EMBEDDABLE_TYPE } from '../../../../../common/embeddables/constants';

export interface EmbeddableFieldStatsChartStartServices {
data: DataPublicPluginStart;
Expand Down Expand Up @@ -134,22 +131,7 @@ export const getFieldStatsChartEmbeddableFactory = (
const timeRangeManager = initializeTimeRangeManager(initialState.rawState);
const titleManager = initializeTitleManager(initialState.rawState);

const deserializeState = (
state: SerializedPanelState<FieldStatisticsTableEmbeddableState>
) => {
const serializedState = cloneDeep(state.rawState);
// inject the reference
const dataViewIdRef = state.references?.find(
(ref) => ref.name === FIELD_STATS_DATA_VIEW_REF_NAME
);
// if the serializedState already contains a dataViewId, we don't want to overwrite it. (Unsaved state can cause this)
if (dataViewIdRef && serializedState && !serializedState.dataViewId) {
serializedState.dataViewId = dataViewIdRef?.id;
}
return serializedState;
};

const state = deserializeState(initialState);
const state = initialState.rawState;

const {
fieldStatsControlsApi,
Expand Down Expand Up @@ -213,23 +195,13 @@ export const getFieldStatsChartEmbeddableFactory = (
const { toasts } = deps.notifications;

const serializeState = () => {
const dataViewId = fieldStatsControlsApi.dataViewId$?.getValue();
const references: Reference[] = dataViewId
? [
{
type: DATA_VIEW_SAVED_OBJECT_TYPE,
name: FIELD_STATS_DATA_VIEW_REF_NAME,
id: dataViewId,
},
]
: [];
return {
rawState: {
...titleManager.getLatestState(),
...timeRangeManager.getLatestState(),
...serializeFieldStatsChartState(),
},
references,
references: [],
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
*/
import type { EmbeddableSetup } from '@kbn/embeddable-plugin/public';
import type { DataVisualizerCoreSetup } from '../../../../plugin';
import { FIELD_STATS_EMBEDDABLE_TYPE } from './constants';
import { FIELD_STATS_EMBEDDABLE_TYPE } from '../../../../../common/embeddables/constants';

export const registerEmbeddables = (embeddable: EmbeddableSetup, core: DataVisualizerCoreSetup) => {
embeddable.registerReactEmbeddableFactory(FIELD_STATS_EMBEDDABLE_TYPE, async () => {
const { getFieldStatsChartEmbeddableFactory } = await import('./field_stats_factory');
return getFieldStatsChartEmbeddableFactory(core.getStartServices);
});
embeddable.registerLegacyURLTransform(FIELD_STATS_EMBEDDABLE_TYPE, async () => {
const { transformOut } = await import('../../../../../common/embeddables/transform_out');
return transformOut;
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { Query } from '@kbn/es-query';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/public';
import type { SavedSearch } from '@kbn/saved-search-plugin/public';
import type { BehaviorSubject, Observable } from 'rxjs';
import type { SerializedTimeRange, SerializedTitles } from '@kbn/presentation-publishing';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { DataVisualizerTableState } from '../../../../../common/types';
import type { SamplingOption } from '../../../../../common/types/field_stats';
Expand All @@ -20,6 +19,13 @@ import type { DataVisualizerStartDependencies } from '../../../common/types/data
import type { ESQLQuery } from '../../search_strategy/requests/esql_utils';
import type { DataVisualizerTableItem } from '../../../common/components/stats_table/types';
import type { FieldStatsComponentType } from '../../constants/field_stats_component_type';
import type { FieldStatisticsTableEmbeddableState } from '../../../../../common/embeddables/types';

export { FieldStatsInitializerViewType } from '../../../../../common/embeddables/types';
export type {
FieldStatsInitialState,
FieldStatisticsTableEmbeddableState,
} from '../../../../../common/embeddables/types';

export interface FieldStatisticTableEmbeddableProps {
/**
Expand Down Expand Up @@ -110,21 +116,6 @@ export type ESQLDataVisualizerGridEmbeddableState = Omit<
'query'
> & { query?: ESQLQuery };

export enum FieldStatsInitializerViewType {
DATA_VIEW = 'dataview',
ESQL = 'esql',
}

export interface FieldStatsInitialState {
dataViewId?: string;
viewType?: FieldStatsInitializerViewType;
query?: AggregateQuery;
showDistributions?: boolean;
}
export type FieldStatisticsTableEmbeddableState = FieldStatsInitialState &
SerializedTitles &
SerializedTimeRange & {};

export type OnAddFilter = (field: DataViewField | string, value: string, type: '+' | '-') => void;
export interface FieldStatisticsTableEmbeddableParentApi {
executionContext?: { value: string };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import type { ConfigSchema } from '@kbn/file-upload-common';
import type { StartDeps, SetupDeps } from './types';
import { registerWithCustomIntegrations } from './register_custom_integration';
import { routes } from './routes';
import { FIELD_STATS_EMBEDDABLE_TYPE } from '../common/embeddables/constants';
import { transformIn } from '../common/embeddables/transform_in';
import { transformOut } from '../common/embeddables/transform_out';

export class DataVisualizerPlugin implements Plugin<void, void, SetupDeps, StartDeps> {
private readonly _logger: Logger;
Expand All @@ -29,6 +32,12 @@ export class DataVisualizerPlugin implements Plugin<void, void, SetupDeps, Start
registerWithCustomIntegrations(plugins.customIntegrations);
}
routes(coreSetup, this._logger);

plugins.embeddable.registerTransforms(FIELD_STATS_EMBEDDABLE_TYPE, {
transformOutInjectsReferences: true,
transformIn,
transformOut,
});
}

start(core: CoreStart) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
PluginSetup as DataPluginSetup,
PluginStart as DataPluginStart,
} from '@kbn/data-plugin/server';
import type { EmbeddableSetup } from '@kbn/embeddable-plugin/server';

export interface StartDeps {
security?: SecurityPluginStart;
Expand All @@ -23,4 +24,5 @@ export interface SetupDeps {
customIntegrations?: CustomIntegrationsPluginSetup;
home?: HomeServerPluginSetup;
data: DataPluginSetup;
embeddable: EmbeddableSetup;
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@kbn/file-upload",
"@kbn/charts-theme",
"@kbn/presentation-util",
"@kbn/presentation-publishing-schemas",
],
"exclude": [
"target/**/*",
Expand Down