Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES|QL] [Discover] Keeps the preferred chart type when possible #195588

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions packages/kbn-visualization-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { getTimeZone } from './src/get_timezone';
export { getLensAttributesFromSuggestion } from './src/get_lens_attributes';
export { TooltipWrapper } from './src/tooltip_wrapper';
export { useDebouncedValue } from './src/debounced_value';
export { ChartType } from './src/types';
13 changes: 13 additions & 0 deletions packages/kbn-visualization-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ export interface Suggestion<T = unknown, V = unknown> {
changeType: TableChangeType;
keptLayerIds: string[];
}

export enum ChartType {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Moving this here for reusability

XY = 'XY',
Bar = 'Bar',
Line = 'Line',
Area = 'Area',
Donut = 'Donut',
Heatmap = 'Heat map',
Treemap = 'Treemap',
Tagcloud = 'Tag cloud',
Waffle = 'Waffle',
Table = 'Table',
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { computeInterval } from '../utils/compute_interval';
import { fieldSupportsBreakdown } from '../utils/field_supports_breakdown';
import { shouldDisplayHistogram } from '../layout/helpers';
import { enrichLensAttributesWithTablesData } from '../utils/lens_vis_from_table';
import { getPreferredChartType } from '../utils/get_preferred_chart_type';

const UNIFIED_HISTOGRAM_LAYER_ID = 'unifiedHistogram';

Expand Down Expand Up @@ -147,7 +148,10 @@ export class LensVisService {
externalVisContextStatus: UnifiedHistogramExternalVisContextStatus
) => void;
}) => {
const allSuggestions = this.getAllSuggestions({ queryParams });
const allSuggestions = this.getAllSuggestions({
queryParams,
preferredVisAttributes: externalVisContext?.attributes,
});

const suggestionState = this.getCurrentSuggestionState({
externalVisContext,
Expand Down Expand Up @@ -252,6 +256,7 @@ export class LensVisService {
const histogramSuggestionForESQL = this.getHistogramSuggestionForESQL({
queryParams,
breakdownField,
preferredVisAttributes: externalVisContext?.attributes,
});
if (histogramSuggestionForESQL) {
// In case if histogram suggestion, we want to empty the array and push the new suggestion
Expand Down Expand Up @@ -463,9 +468,11 @@ export class LensVisService {
private getHistogramSuggestionForESQL = ({
queryParams,
breakdownField,
preferredVisAttributes,
}: {
queryParams: QueryParams;
breakdownField?: DataViewField;
preferredVisAttributes?: UnifiedHistogramVisContext['attributes'];
}): Suggestion | undefined => {
const { dataView, query, timeRange, columns } = queryParams;
const breakdownColumn = breakdownField?.name
Expand Down Expand Up @@ -510,7 +517,12 @@ export class LensVisService {
if (breakdownColumn) {
context.textBasedColumns.push(breakdownColumn);
}
const suggestions = this.lensSuggestionsApi(context, dataView, ['lnsDatatable']) ?? [];
const preferredChartType = preferredVisAttributes
? getPreferredChartType(preferredVisAttributes)
: undefined;

const suggestions =
this.lensSuggestionsApi(context, dataView, ['lnsDatatable'], preferredChartType) ?? [];
if (suggestions.length) {
const suggestion = suggestions[0];
const suggestionVisualizationState = Object.assign({}, suggestion?.visualizationState);
Expand Down Expand Up @@ -574,17 +586,27 @@ export class LensVisService {
);
};

private getAllSuggestions = ({ queryParams }: { queryParams: QueryParams }): Suggestion[] => {
private getAllSuggestions = ({
queryParams,
preferredVisAttributes,
}: {
queryParams: QueryParams;
preferredVisAttributes?: UnifiedHistogramVisContext['attributes'];
}): Suggestion[] => {
const { dataView, columns, query, isPlainRecord } = queryParams;

const preferredChartType = preferredVisAttributes
? getPreferredChartType(preferredVisAttributes)
: undefined;

const context = {
dataViewSpec: dataView?.toSpec(),
fieldName: '',
textBasedColumns: columns,
query: query && isOfAggregateQueryType(query) ? query : undefined,
};
const allSuggestions = isPlainRecord
? this.lensSuggestionsApi(context, dataView, ['lnsDatatable']) ?? []
? this.lensSuggestionsApi(context, dataView, ['lnsDatatable'], preferredChartType) ?? []
: [];

return allSuggestions;
Expand Down
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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import type { PieVisualizationState, XYState } from '@kbn/lens-plugin/public';
import type { ChartType } from '@kbn/visualization-utils';
import type { UnifiedHistogramVisContext } from '../types';

export const getPreferredChartType = (visAttributes: UnifiedHistogramVisContext['attributes']) => {
let preferredChartType = visAttributes ? visAttributes?.visualizationType : undefined;

if (preferredChartType === 'lnsXY') {
preferredChartType = (visAttributes?.state?.visualization as XYState)?.preferredSeriesType;
}
if (preferredChartType === 'lnsPie') {
preferredChartType = (visAttributes?.state?.visualization as PieVisualizationState)?.shape;
}

return preferredChartType as ChartType;
};
3 changes: 2 additions & 1 deletion x-pack/plugins/lens/public/lens_suggestions_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/
import type { DataView } from '@kbn/data-views-plugin/public';
import type { DatatableColumn } from '@kbn/expressions-plugin/common';
import { ChartType } from '@kbn/visualization-utils';
import { createMockVisualization, DatasourceMock, createMockDatasource } from './mocks';
import { DatasourceSuggestion } from './types';
import { suggestionsApi, ChartType } from './lens_suggestions_api';
import { suggestionsApi } from './lens_suggestions_api';

const generateSuggestion = (state = {}, layerId: string = 'first'): DatasourceSuggestion => ({
state,
Expand Down
14 changes: 1 addition & 13 deletions x-pack/plugins/lens/public/lens_suggestions_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,11 @@
*/
import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { ChartType } from '@kbn/visualization-utils';
import { getSuggestions } from './editor_frame_service/editor_frame/suggestion_helpers';
import type { DatasourceMap, VisualizationMap, VisualizeEditorContext } from './types';
import type { DataViewsState } from './state_management';

export enum ChartType {
XY = 'XY',
Bar = 'Bar',
Line = 'Line',
Area = 'Area',
Donut = 'Donut',
Heatmap = 'Heat map',
Treemap = 'Treemap',
Tagcloud = 'Tag cloud',
Waffle = 'Waffle',
Table = 'Table',
}

interface SuggestionsApiProps {
context: VisualizeFieldContext | VisualizeEditorContext;
dataView: DataView;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
ContentManagementPublicStart,
} from '@kbn/content-management-plugin/public';
import { i18n } from '@kbn/i18n';
import type { ChartType } from '@kbn/visualization-utils';
import type { ServerlessPluginStart } from '@kbn/serverless/public';
import { LicensingPluginStart } from '@kbn/licensing-plugin/public';
import type { EditorFrameService as EditorFrameServiceType } from './editor_frame_service';
Expand Down Expand Up @@ -137,7 +138,6 @@ import {
} from '../common/content_management';
import type { EditLensConfigurationProps } from './app_plugin/shared/edit_on_the_fly/get_edit_lens_configuration';
import { savedObjectToEmbeddableAttributes } from './lens_attribute_service';
import { ChartType } from './lens_suggestions_api';

export type { SaveProps } from './app_plugin';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
VisualizeESQLUserIntention,
} from '@kbn/observability-ai-assistant-plugin/public';
import type { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import { getLensAttributesFromSuggestion } from '@kbn/visualization-utils';
import { getLensAttributesFromSuggestion, ChartType } from '@kbn/visualization-utils';
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import ReactDOM from 'react-dom';
import useAsync from 'react-use/lib/useAsync';
Expand All @@ -48,19 +48,6 @@ import type {
} from '../../common/functions/visualize_esql';
import { ObservabilityAIAssistantAppPluginStartDependencies } from '../types';

enum ChartType {
XY = 'XY',
Bar = 'Bar',
Line = 'Line',
Area = 'Area',
Donut = 'Donut',
Heatmap = 'Heat map',
Treemap = 'Treemap',
Tagcloud = 'Tag cloud',
Waffle = 'Waffle',
Table = 'Table',
}

interface VisualizeESQLProps {
/** Lens start contract, get the ES|QL charts suggestions api */
lens: LensPublicStart;
Expand Down