Skip to content

Commit

Permalink
[9.0] [Lens] Fix activeData in multi-layer config (#208571) (#210436)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `9.0`:
- [[Lens] Fix `activeData` in multi-layer config
(#208571)](#208571)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nick
Partridge","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-02-10T17:37:47Z","message":"[Lens]
Fix `activeData` in multi-layer config (#208571)\n\n##
Summary\r\n\r\nFixes an issue in the lens inline editor that was
assigning the first\r\ndatatable to all layers in the
`activeData`.\r\n\r\nFixes #205754\r\n\r\n## Details\r\n\r\nUpdates the
`activeData` logic in the `lens_configuration_flyout.tsx` to\r\nmatch
`workspace_panel.tsx`.\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/82d94f17c91f8dcd93563dbbb47fba9f13c56d3a/x-pack/platform/plugins/shared/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx#L283-L295\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n\r\n##
Release note\r\n\r\nFixes issue assigning color mappings when multiple
layers are
defined.","sha":"dd2b833acb4a99cf06b5e4fed6d35be75a293b3a","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Visualizations","Feature:Lens","backport:current-major","v9.1.0"],"title":"[Lens]
Fix `activeData` in multi-layer
config","number":208571,"url":"https://github.com/elastic/kibana/pull/208571","mergeCommit":{"message":"[Lens]
Fix `activeData` in multi-layer config (#208571)\n\n##
Summary\r\n\r\nFixes an issue in the lens inline editor that was
assigning the first\r\ndatatable to all layers in the
`activeData`.\r\n\r\nFixes #205754\r\n\r\n## Details\r\n\r\nUpdates the
`activeData` logic in the `lens_configuration_flyout.tsx` to\r\nmatch
`workspace_panel.tsx`.\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/82d94f17c91f8dcd93563dbbb47fba9f13c56d3a/x-pack/platform/plugins/shared/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx#L283-L295\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n\r\n##
Release note\r\n\r\nFixes issue assigning color mappings when multiple
layers are
defined.","sha":"dd2b833acb4a99cf06b5e4fed6d35be75a293b3a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/208571","number":208571,"mergeCommit":{"message":"[Lens]
Fix `activeData` in multi-layer config (#208571)\n\n##
Summary\r\n\r\nFixes an issue in the lens inline editor that was
assigning the first\r\ndatatable to all layers in the
`activeData`.\r\n\r\nFixes #205754\r\n\r\n## Details\r\n\r\nUpdates the
`activeData` logic in the `lens_configuration_flyout.tsx` to\r\nmatch
`workspace_panel.tsx`.\r\n\r\n\r\nhttps://github.com/elastic/kibana/blob/82d94f17c91f8dcd93563dbbb47fba9f13c56d3a/x-pack/platform/plugins/shared/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_panel.tsx#L283-L295\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n\r\n##
Release note\r\n\r\nFixes issue assigning color mappings when multiple
layers are
defined.","sha":"dd2b833acb4a99cf06b5e4fed6d35be75a293b3a"}}]}]
BACKPORT-->

Co-authored-by: Nick Partridge <[email protected]>
  • Loading branch information
kibanamachine and nickofthyme authored Feb 10, 2025
1 parent 3421829 commit 520a007
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
keys,
} from '@elastic/eui';
import { euiThemeVars } from '@kbn/ui-theme';
import type { Datatable } from '@kbn/expressions-plugin/public';
import {
getAggregateQueryMode,
isOfAggregateQueryType,
Expand Down Expand Up @@ -55,6 +54,7 @@ import { trackSaveUiCounterEvents } from '../../../lens_ui_telemetry';
import { ESQLDataGridAccordion } from './esql_data_grid_accordion';
import { isApiESQLVariablesCompatible } from '../../../react_embeddable/types';
import { useESQLVariables } from './use_esql_variables';
import { getActiveDataFromDatatable } from '../../../state_management/shared_logic';

export function LensEditConfigurationFlyout({
attributes,
Expand Down Expand Up @@ -138,23 +138,29 @@ export function LensEditConfigurationFlyout({
if (isDataLoading) {
return;
}
const activeData: Record<string, Datatable> = {};
const adaptersTables = previousAdapters.current?.tables?.tables;
const [table] = Object.values(adaptersTables || {});
if (table) {
// 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
setSuggestsLimitedColumns(table.columns.length >= MAX_NUM_OF_COLUMNS);
layers.forEach((layer) => {
activeData[layer] = table;
});

const [defaultLayerId] = Object.keys(framePublicAPI.datasourceLayers);
const activeData = getActiveDataFromDatatable(
defaultLayerId,
previousAdapters.current?.tables?.tables
);

layers.forEach((layer) => {
const table = activeData[layer];

if (table) {
// 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`
setSuggestsLimitedColumns(table.columns.length >= MAX_NUM_OF_COLUMNS);
}
});

if (Object.keys(activeData).length > 0) {
dispatch(onActiveDataChange({ activeData }));
}
});
return () => s?.unsubscribe();
}, [dispatch, dataLoading$, layers]);
}, [dispatch, dataLoading$, layers, framePublicAPI.datasourceLayers]);

const attributesChanged: boolean = useMemo(() => {
const previousAttrs = previousAttributes.current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import type {
import type { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import { VIS_EVENT_TO_TRIGGER } from '@kbn/visualizations-plugin/public';
import type { DefaultInspectorAdapters } from '@kbn/expressions-plugin/common';
import type { Datatable } from '@kbn/expressions-plugin/public';
import { DropIllustration } from '@kbn/chart-icons';
import { useDragDropContext, DragDropIdentifier, Droppable } from '@kbn/dom-drag-drop';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
Expand Down Expand Up @@ -81,6 +80,7 @@ import {
} from '../../../utils';
import { setChangesApplied } from '../../../state_management/lens_slice';
import { WorkspaceErrors } from './workspace_errors';
import { getActiveDataFromDatatable } from '../../../state_management/shared_logic';

export interface WorkspacePanelProps {
visualizationMap: VisualizationMap;
Expand Down Expand Up @@ -282,14 +282,7 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
if (adapters && adapters.tables) {
dispatchLens(
onActiveDataChange({
activeData: Object.entries(adapters.tables?.tables).reduce<Record<string, Datatable>>(
(acc, [key, value], _index, tables) => {
const id = tables.length === 1 ? defaultLayerId : key;
acc[id] = value as Datatable;
return acc;
},
{}
),
activeData: getActiveDataFromDatatable(defaultLayerId, adapters.tables?.tables),
})
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 { Datatable } from '@kbn/expressions-plugin/common';
import { getActiveDataFromDatatable } from './shared_logic';

describe('lens shared logic', () => {
describe('#getActiveDataFromDatatable', () => {
const defaultLayerId = 'default-layer';
const firstTable: Datatable = {
type: 'datatable',
columns: [],
rows: [],
};
const secondTable: Datatable = {
type: 'datatable',
columns: [],
rows: [],
};

it('should return {} for empty datatable', () => {
expect(getActiveDataFromDatatable(defaultLayerId, undefined)).toEqual({});
});

it('should return multiple tables', () => {
const datatables: Record<string, Datatable> = {
first: firstTable,
second: secondTable,
};
expect(getActiveDataFromDatatable(defaultLayerId, datatables)).toEqual({
first: firstTable,
second: secondTable,
});
});

it('should return since table with default layer id', () => {
const datatables: Record<string, Datatable> = {
first: firstTable,
};
expect(getActiveDataFromDatatable(defaultLayerId, datatables)).toEqual({
[defaultLayerId]: firstTable,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { SavedObjectReference } from '@kbn/core-saved-objects-api-server';
import { DataViewSpec, DataViewPersistableStateService } from '@kbn/data-views-plugin/common';
import { AggregateQuery, Query, Filter } from '@kbn/es-query';
import { FilterManager } from '@kbn/data-plugin/public';
import { Datatable } from '@kbn/expressions-plugin/common';
import { DOC_TYPE, INDEX_PATTERN_TYPE } from '../../common/constants';
import { VisualizationState, DatasourceStates } from '.';
import { LensDocument } from '../persistence';
Expand Down Expand Up @@ -122,3 +123,17 @@ export function mergeToNewDoc(
},
};
}

export function getActiveDataFromDatatable(
defaultLayerId: string,
tables: Record<string, Datatable> = {}
) {
return Object.entries(tables).reduce<Record<string, Datatable>>(
(acc, [key, value], _index, { length }) => {
const id = length === 1 ? defaultLayerId : key;
acc[id] = value as Datatable;
return acc;
},
{}
);
}

0 comments on commit 520a007

Please sign in to comment.