Skip to content
Merged
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
Expand Up @@ -15,17 +15,19 @@ import type {
import { partition } from 'lodash';
import type { CoreStart } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { getESQLForLayer } from '../../../datasources/form_based/to_esql';
import { getESQLForLayer, isEsqlQuerySuccess } from '../../../datasources/form_based/to_esql';
import {
esqlConversionFailureReasonMessages,
getFailureTooltip,
} from '../../../datasources/form_based/to_esql_failure_reasons';
import type { ConvertibleLayer } from './convert_to_esql_modal';
import { operationDefinitionMap } from '../../../datasources/form_based/operations';
import type { LensPluginStartDependencies } from '../../../plugin';
import { layerTypes } from '../../..';

const cannotConvertToEsqlTooltip = i18n.translate('xpack.lens.config.cannotConvertToEsqlTooltip', {
defaultMessage: 'This visualization cannot be converted to ES|QL',
});

const getEsqlConversionDisabledSettings = (tooltip: string = cannotConvertToEsqlTooltip) => ({
const getEsqlConversionDisabledSettings = (
tooltip: string = esqlConversionFailureReasonMessages.unknown
) => ({
isConvertToEsqlButtonDisabled: true,
convertToEsqlButtonTooltip: tooltip,
convertibleLayers: [],
Expand Down Expand Up @@ -74,18 +76,14 @@ export const useEsqlConversion = (
// Guard: trendline check
if (hasTrendLineLayer(state)) {
return getEsqlConversionDisabledSettings(
i18n.translate('xpack.lens.config.cannotConvertToEsqlMetricWithTrendlineTooltip', {
defaultMessage: 'Metric visualization with a trend line are not supported in query mode',
})
esqlConversionFailureReasonMessages.trend_line_not_supported
);
}

// Guard: layer count
if (layerIds.length > 1) {
return getEsqlConversionDisabledSettings(
i18n.translate('xpack.lens.config.cannotConvertToEsqlMultilayerTooltip', {
defaultMessage: 'Multi-layer visualizations cannot be converted to query mode',
})
esqlConversionFailureReasonMessages.multi_layer_not_supported
);
}

Expand Down Expand Up @@ -134,7 +132,7 @@ export const useEsqlConversion = (
// This prevents conversion errors from breaking the visualization
}

return esqlLayer
return isEsqlQuerySuccess(esqlLayer)
? {
isConvertToEsqlButtonDisabled: false,
convertToEsqlButtonTooltip: i18n.translate('xpack.lens.config.convertToEsqlTooltip', {
Expand All @@ -151,11 +149,7 @@ export const useEsqlConversion = (
},
],
}
: getEsqlConversionDisabledSettings(
i18n.translate('xpack.lens.config.cannotConvertToEsqlUnsupportedSettingsTooltip', {
defaultMessage: 'The visualization has unsupported settings for query mode',
})
);
: getEsqlConversionDisabledSettings(getFailureTooltip(esqlLayer?.reason));
}, [
activeVisualization,
coreStart.uiSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ describe('to_esql', () => {
new Date()
);

expect(esql?.esql).toEqual(
`FROM myIndexPattern
expect(esql).toEqual(
expect.objectContaining({
success: true,
esql: `FROM myIndexPattern
| WHERE order_date >= ?_tstart AND order_date <= ?_tend
| STATS bucket_0_0 = COUNT(*) BY order_date = BUCKET(order_date, 30 minutes)
| SORT order_date ASC`
| SORT order_date ASC`,
})
);
});

it('should return undefined if missing row option is set', () => {
it('should return failure with include_empty_rows_not_supported reason if missing row option is set', () => {
const esql = getESQLForLayer(
[
[
Expand Down Expand Up @@ -124,10 +127,13 @@ describe('to_esql', () => {
new Date()
);

expect(esql?.esql).toEqual(undefined);
expect(esql).toEqual({
success: false,
reason: 'include_empty_rows_not_supported',
});
});

it('should return undefined if lens formula is used', () => {
it('should return failure with formula_not_supported reason if lens formula is used', () => {
const esql = getESQLForLayer(
[
[
Expand All @@ -151,7 +157,10 @@ describe('to_esql', () => {
new Date()
);

expect(esql).toEqual(undefined);
expect(esql).toEqual({
success: false,
reason: 'formula_not_supported',
});
});

test('it should add a where condition to esql if timeField is set', () => {
Expand Down Expand Up @@ -189,11 +198,14 @@ describe('to_esql', () => {
new Date()
);

expect(esql?.esql).toEqual(
`FROM myIndexPattern
expect(esql).toEqual(
expect.objectContaining({
success: true,
esql: `FROM myIndexPattern
| WHERE order_date >= ?_tstart AND order_date <= ?_tend
| STATS bucket_0_0 = COUNT(*) BY order_date = BUCKET(order_date, 30 minutes)
| SORT order_date ASC`
| SORT order_date ASC`,
})
);
});

Expand Down Expand Up @@ -239,14 +251,17 @@ describe('to_esql', () => {
new Date()
);

expect(esql?.esql).toEqual(
`FROM myIndexPattern
expect(esql).toEqual(
expect.objectContaining({
success: true,
esql: `FROM myIndexPattern
| STATS bucket_0_0 = COUNT(*) BY order_date = BUCKET(order_date, 30 minutes)
| SORT order_date ASC`
| SORT order_date ASC`,
})
);
});

it('should return undefined if timezone is not UTC', () => {
it('should return failure with non_utc_timezone reason if timezone is not UTC', () => {
uiSettings.get.mockImplementation((key: string) => {
if (key === 'dateFormat:tz') return 'America/Chicago';
return defaultUiSettingsGet(key);
Expand Down Expand Up @@ -286,7 +301,10 @@ describe('to_esql', () => {
new Date()
);

expect(esql).toEqual(undefined);
expect(esql).toEqual({
success: false,
reason: 'non_utc_timezone',
});
});

it('should work with iana timezones that fall under UTC+0', () => {
Expand Down Expand Up @@ -330,11 +348,14 @@ describe('to_esql', () => {
new Date()
);

expect(esql?.esql).toEqual(
`FROM myIndexPattern
expect(esql).toEqual(
expect.objectContaining({
success: true,
esql: `FROM myIndexPattern
| WHERE order_date >= ?_tstart AND order_date <= ?_tend
| STATS bucket_0_0 = COUNT(*) BY order_date = BUCKET(order_date, 30 minutes)
| SORT order_date ASC`
| SORT order_date ASC`,
})
);
});

Expand Down Expand Up @@ -377,11 +398,14 @@ describe('to_esql', () => {
new Date()
);

expect(esql?.esql).toEqual(
`FROM myIndexPattern
expect(esql).toEqual(
expect.objectContaining({
success: true,
esql: `FROM myIndexPattern
| WHERE order_date >= ?_tstart AND order_date <= ?_tend
| STATS bucket_0_0 = COUNT(*) WHERE KQL("geo.src:\\"US\\"") BY order_date = BUCKET(order_date, 30 minutes)
| SORT order_date ASC`
| SORT order_date ASC`,
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ describe('to_esql top N', () => {
mockNowInstant
);

expect(result?.esql).toEqual(undefined);
expect(result).toEqual({
success: false,
reason: 'terms_not_supported',
});
});
});
Loading
Loading