Skip to content

Commit

Permalink
Add support of Data View switching for Agg-Based visualizations (#132184
Browse files Browse the repository at this point in the history
)

* Add support of Data View switching for Agg-Based visualizations

* fix CI

* add use_date_view_updates

* implement sync with state

* cleanup

* cleanup

* cleanup

* Update index.ts

* fix PR comments

* Update use_data_view_updates.ts

* Update use_data_view_updates.ts

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
alexwizp and kibanamachine committed May 17, 2022
1 parent b2feceb commit 1bb63f8
Show file tree
Hide file tree
Showing 31 changed files with 151 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type AggId = IAggConfig['id'];
type AggParams = IAggConfig['params'];

type AddNewAgg = ActionType<EditorStateActionTypes.ADD_NEW_AGG, { schema: Schema }>;
type DiscardChanges = ActionType<EditorStateActionTypes.DISCARD_CHANGES, Vis>;
type DiscardChanges = ActionType<EditorStateActionTypes.DISCARD_CHANGES, { vis: Vis }>;
type ChangeAggType = ActionType<
EditorStateActionTypes.CHANGE_AGG_TYPE,
{ aggId: AggId; value: IAggConfig['type'] }
Expand Down Expand Up @@ -90,7 +90,7 @@ const addNewAgg: EditorActions['addNewAgg'] = (schema) => ({

const discardChanges: EditorActions['discardChanges'] = (vis) => ({
type: EditorStateActionTypes.DISCARD_CHANGES,
payload: vis,
payload: { vis },
});

const changeAggType: EditorActions['changeAggType'] = (aggId, value) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const createEditorStateReducer =
}

case EditorStateActionTypes.DISCARD_CHANGES: {
return initEditorState(action.payload);
return initEditorState(action.payload.vis);
}

case EditorStateActionTypes.CHANGE_AGG_TYPE: {
Expand Down
1 change: 0 additions & 1 deletion src/plugins/vis_default_editor/public/default_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ function DefaultEditor({
if (!visRef.current) {
return;
}

embeddableHandler.render(visRef.current).then(() => {
setTimeout(async () => {
eventEmitter.emit('embeddableRendered');
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/gauge/public/vis_type/gauge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const getGaugeVisTypeDefinition = (
},
},
editorConfig: {
enableDataViewChange: true,
optionsTemplate: getGaugeOptions(props),
schemas: [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/gauge/public/vis_type/goal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const getGoalVisTypeDefinition = (
},
},
editorConfig: {
enableDataViewChange: true,
optionsTemplate: getGaugeOptions(props),
schemas: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('PalettePicker', function () {
vis: {
type: {
editorConfig: {
enableDataViewChange: true,
collections: {
legendPositions: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const sampleAreaVis = {
},
},
editorConfig: {
enableDataViewChange: true,
optionTabs: [
{
name: 'advanced',
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/heatmap/public/vis_type/heatmap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const getHeatmapVisTypeDefinition = ({
},
},
editorConfig: {
enableDataViewChange: true,
optionsTemplate: getHeatmapOptions({
showElasticChartsOptions,
palettes,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/metric/public/metric_vis_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const createMetricVisTypeDefinition = (): VisTypeDefinition<VisParams> =>
},
},
editorConfig: {
enableDataViewChange: true,
optionsTemplate: MetricVisOptions,
schemas: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('PalettePicker', function () {
vis: {
type: {
editorConfig: {
enableDataViewChange: true,
collections: {
legendPositions: [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/pie/public/sample_vis.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const samplePieVis = {
},
},
editorConfig: {
enableDataViewChange: true,
collections: {
legendPositions: [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/pie/public/vis_type/pie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const getPieVisTypeDefinition = ({
},
},
editorConfig: {
enableDataViewChange: true,
optionsTemplate: getPieOptions({
showElasticChartsOptions,
palettes,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/table/public/table_vis_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const tableVisTypeDefinition: VisTypeDefinition<TableVisParams> = {
},
},
editorConfig: {
enableDataViewChange: true,
optionsTemplate: TableOptions,
schemas: [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/tagcloud/public/tag_cloud_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const getTagCloudVisTypeDefinition = ({ palettes }: TagCloudVisDependenci
},
toExpressionAst,
editorConfig: {
enableDataViewChange: true,
optionsTemplate: getTagCloudOptions({
palettes,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ export const getVis = (bucketType: string) => {
},
},
editorConfig: {
enableDataViewChange: true,
collections: {
legendPositions: [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/xy/public/sample_vis.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const sampleAreaVis = {
},
},
editorConfig: {
enableDataViewChange: true,
optionTabs: [
{
name: 'advanced',
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/xy/public/vis_types/area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export const areaVisTypeDefinition = {
},
},
editorConfig: {
enableDataViewChange: true,
optionTabs,
schemas: [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/xy/public/vis_types/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const histogramVisTypeDefinition = {
},
},
editorConfig: {
enableDataViewChange: true,
optionTabs,
schemas: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const horizontalBarVisTypeDefinition = {
},
},
editorConfig: {
enableDataViewChange: true,
optionTabs,
schemas: [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_types/xy/public/vis_types/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export const lineVisTypeDefinition = {
},
},
editorConfig: {
enableDataViewChange: true,
optionTabs,
schemas: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
ExpressionAstExpression,
} from '@kbn/expressions-plugin/public';
import type { RenderMode } from '@kbn/expressions-plugin';
import VisualizationError from '../components/visualization_error';
import { VISUALIZE_EMBEDDABLE_TYPE } from './constants';
import { Vis, SerializedVis } from '../vis';
import { getExecutionContext, getExpressions, getTheme, getUiActions } from '../services';
Expand Down Expand Up @@ -381,6 +382,12 @@ export class VisualizeEmbeddable
this.subscriptions.push(this.handler.loading$.subscribe(this.onContainerLoading));
this.subscriptions.push(this.handler.render$.subscribe(this.onContainerRender));

this.subscriptions.push(
this.getOutput$().subscribe(
({ error }) => error && render(<VisualizationError error={error} />, this.domNode)
)
);

await this.updateHandler();
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/visualizations/public/vis_types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ interface DefaultEditorConfig<TVisParams> {
[key: string]: Array<{ text: string; value: string }> | Array<{ id: string; label: string }>;
};
enableAutoApply?: boolean;
enableDataViewChange?: boolean;
defaultSize?: string;
optionsTemplate?: DefaultEditorOptionsComponent<TVisParams>;
optionTabs?: Array<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
useVisualizeAppState,
useEditorUpdates,
useLinkedSearchUpdates,
useDataViewUpdates,
} from '../utils';
import { VisualizeServices } from '../types';
import { VisualizeEditorCommon } from './visualize_editor_common';
Expand Down Expand Up @@ -84,6 +85,7 @@ export const VisualizeByValueEditor = ({ onAppLeave }: VisualizeAppProps) => {
visEditorController
);
useLinkedSearchUpdates(services, eventEmitter, appState, byValueVisInstance);
useDataViewUpdates(services, eventEmitter, appState, byValueVisInstance);

useEffect(() => {
// clean up all registered listeners if any is left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
useVisualizeAppState,
useEditorUpdates,
useLinkedSearchUpdates,
useDataViewUpdates,
} from '../utils';
import { VisualizeServices } from '../types';
import { VisualizeEditorCommon } from './visualize_editor_common';
Expand Down Expand Up @@ -63,6 +64,7 @@ export const VisualizeEditor = ({ onAppLeave }: VisualizeAppProps) => {
visEditorController
);
useLinkedSearchUpdates(services, eventEmitter, appState, savedVisInstance);
useDataViewUpdates(services, eventEmitter, appState, savedVisInstance);

useEffect(() => {
const { stateTransferService, data } = services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React from 'react';

import { shallowWithIntl, mountWithIntl } from '@kbn/test-jest-helpers';
import { VisualizeEditorCommon } from './visualize_editor_common';
import { VisualizeEditorVisInstance } from '../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { i18n } from '@kbn/i18n';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import {
import type {
VisualizeServices,
VisualizeAppState,
VisualizeAppStateContainer,
Expand Down Expand Up @@ -151,9 +151,7 @@ const TopNav = ({
hideLensBadge,
hideTryInLensBadge,
]);
const [indexPatterns, setIndexPatterns] = useState<DataView[]>(
vis.data.indexPattern ? [vis.data.indexPattern] : []
);
const [indexPatterns, setIndexPatterns] = useState<DataView[]>([]);
const showDatePicker = () => {
// tsvb loads without an indexPattern initially (TODO investigate).
// hide timefilter only if timeFieldName is explicitly undefined.
Expand Down Expand Up @@ -213,7 +211,9 @@ const TopNav = ({
const asyncSetIndexPattern = async () => {
let indexes: DataView[] | undefined;

if (vis.type.getUsedIndexPattern) {
if (vis.data.indexPattern) {
indexes = [vis.data.indexPattern];
} else if (vis.type.getUsedIndexPattern) {
indexes = await vis.type.getUsedIndexPattern(vis.params);
}
if (!indexes || !indexes.length) {
Expand All @@ -227,10 +227,28 @@ const TopNav = ({
}
};

if (!vis.data.indexPattern) {
asyncSetIndexPattern();
}
}, [vis.params, vis.type, vis.data.indexPattern, services.dataViews]);
asyncSetIndexPattern();
}, [services.dataViews, vis.data.indexPattern, vis.params, vis.type]);

/** Synchronizing dataView with state **/
useEffect(() => {
const stateContainerSubscription = stateContainer.state$.subscribe(async ({ dataView }) => {
if (
dataView &&
visInstance.vis.data.indexPattern &&
dataView !== visInstance.vis.data.indexPattern.id
) {
const dataViewFromState = await services.dataViews.get(dataView);

if (dataViewFromState) {
setIndexPatterns([dataViewFromState]);
}
}
});
return () => {
stateContainerSubscription.unsubscribe();
};
}, [services.dataViews, stateContainer.state$, visInstance.vis.data.indexPattern]);

useEffect(() => {
const autoRefreshFetchSub = services.data.query.timefilter.timefilter
Expand All @@ -247,6 +265,22 @@ const TopNav = ({
};
}, [services.data.query.timefilter.timefilter, doReload]);

const shouldShowDataViewPicker = Boolean(
vis.type.editorConfig?.enableDataViewChange &&
!vis.data.savedSearchId &&
vis.data.indexPattern &&
indexPatterns.length
);

const onChangeDataView = useCallback(
async (selectedDataViewId: string) => {
if (selectedDataViewId) {
stateContainer.transitions.updateDataView(selectedDataViewId);
}
},
[stateContainer.transitions]
);

return isChromeVisible ? (
/**
* Most visualizations have all search bar components enabled.
Expand All @@ -255,6 +289,7 @@ const TopNav = ({
* All visualizations also have the timepicker\autorefresh component,
* it is enabled by default in the TopNavMenu component.
*/

<TopNavMenu
appName={VISUALIZE_APP_NAME}
config={config}
Expand All @@ -269,6 +304,18 @@ const TopNav = ({
showFilterBar={showFilterBar}
showQueryInput={showQueryInput}
showSaveQuery={Boolean(services.visualizeCapabilities.saveQuery)}
dataViewPickerComponentProps={
shouldShowDataViewPicker
? {
currentDataViewId: vis.data.indexPattern!.id,
trigger: {
label: vis.data.indexPattern!.title,
},
onChangeDataView,
showNewMenuTour: false,
}
: undefined
}
showSearchBar
useDefaultBehaviors
/>
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/visualizations/public/visualize_app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type { createVisEmbeddableFromObject } from '../embeddable';
import type { VisEditorsRegistry } from '../vis_editors_registry';

export interface VisualizeAppState {
dataView?: string;
filters: Filter[];
uiState: SerializableRecord;
vis: SavedVisState;
Expand All @@ -72,6 +73,7 @@ export interface VisualizeAppStateTransitions {
) => ({ query, parentFilters }: { query?: Query; parentFilters?: Filter[] }) => VisualizeAppState;
updateVisState: (state: VisualizeAppState) => (vis: SavedVisState) => VisualizeAppState;
updateSavedQuery: (state: VisualizeAppState) => (savedQueryId?: string) => VisualizeAppState;
updateDataView: (state: VisualizeAppState) => (dataViewId?: string) => VisualizeAppState;
}

export type VisualizeAppStateContainer = ReduxLikeStateContainer<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const pureTransitions = {
filters: union(state.filters, parentFilters),
linked: false,
}),
updateDataView: (state) => (dataViewId) => ({ ...state, dataView: dataViewId }),
updateVisState: (state) => (newVisState) => ({ ...state, vis: toObject(newVisState) }),
updateSavedQuery: (state) => (savedQueryId) => {
const updatedState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { useSavedVisInstance } from './use_saved_vis_instance';
export { useVisualizeAppState } from './use_visualize_app_state';
export { useLinkedSearchUpdates } from './use_linked_search_updates';
export { useVisByValue } from './use_vis_byvalue';
export { useDataViewUpdates } from './use_data_view_updates';
Loading

0 comments on commit 1bb63f8

Please sign in to comment.