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 @@ -8,7 +8,7 @@
*/

import type { DatasetTypeESQL } from '../dataset';
import type { XYState, XYStateESQL } from './xy';
import type { XYState } from './xy';
import { statisticsOptionsSize, statisticsSchema, xyStateSchema } from './xy';

describe('XY', () => {
Expand Down Expand Up @@ -405,7 +405,7 @@ describe('XY', () => {
);

it.each(anyType.map((type) => anyType.map((anotherType) => [type, anotherType])).flat(1))(
'should handle multiple metric in multiple layers %s + %s with reference lines and annotations with mixed datasets',
'should handle multiple metric in multiple layers %s + %s with reference lines and annotations (DSL layers only)',
(type1, type2) => {
expect(() =>
xyStateSchema.validate({
Expand Down Expand Up @@ -441,13 +441,32 @@ describe('XY', () => {
},
},
{
dataset: { type: 'esql', query: 'FROM company_index' },
dataset: { type: 'dataView', id: 'companyBIndex' },
type: type2,
ignore_global_filters: false,
sampling: 1,
x: { column: 'order_date' },
y: [{ column: 'value' }, { column: 'price' }],
breakdown_by: { column: 'product' },
x: {
operation: 'date_histogram',
field: 'order_date',
include_empty_rows: false,
suggested_interval: 'auto',
use_original_time_range: true,
drop_partial_intervals: false,
},
y: [
{ operation: 'count', empty_as_null: false },
{ operation: 'average', field: 'price' },
],
breakdown_by: {
operation: 'terms',
fields: ['product', 'category'],
limit: 5,
rank_by: {
direction: 'desc',
metric: 0,
type: 'column',
},
},
},
{
dataset: { type: 'index', index: 'companyIndex', time_field: '@timestamp' },
Expand Down Expand Up @@ -621,7 +640,41 @@ describe('XY', () => {
],
},
],
} satisfies XYState | XYStateESQL)
} satisfies XYState)
).toThrow();
});

it('should reject mixing ES|QL and DSL layers in one chart', () => {
expect(() =>
xyStateSchema.validate({
type: 'xy',
title: 'Mixed mode chart',
layers: [
{
dataset: { type: 'dataView', id: 'companyAIndex' },
type: 'bar',
ignore_global_filters: false,
sampling: 1,
x: {
operation: 'date_histogram',
field: 'order_date',
include_empty_rows: false,
suggested_interval: 'auto',
use_original_time_range: true,
drop_partial_intervals: false,
},
y: [{ operation: 'count', empty_as_null: false }],
},
{
dataset: { type: 'esql', query: 'FROM company_index' },
type: 'line',
ignore_global_filters: false,
sampling: 1,
x: { operation: 'value', column: 'order_date' },
y: [{ operation: 'value', column: 'value' }],
},
],
} as XYState)
).toThrow();
});

Expand All @@ -643,12 +696,19 @@ describe('XY', () => {
layers: [minimalLayer],
})
).toThrowErrorMatchingInlineSnapshot(`
"[legend]: types that failed validation:
- [legend.0.position]: types that failed validation:
- [legend.position.0]: expected value to equal [top]
- [legend.position.1]: expected value to equal [bottom]
- [legend.1.layout.type]: expected value to equal [grid]
- [legend.2.placement]: expected value to equal [inside]"
"types that failed validation:
- [0.legend]: types that failed validation:
- [legend.0.position]: types that failed validation:
- [legend.position.0]: expected value to equal [top]
- [legend.position.1]: expected value to equal [bottom]
- [legend.1.layout.type]: expected value to equal [grid]
- [legend.2.placement]: expected value to equal [inside]
- [1.legend]: types that failed validation:
- [legend.0.position]: types that failed validation:
- [legend.position.0]: expected value to equal [top]
- [legend.position.1]: expected value to equal [bottom]
- [legend.1.layout.type]: expected value to equal [grid]
- [legend.2.placement]: expected value to equal [inside]"
`);
});

Expand All @@ -671,12 +731,19 @@ describe('XY', () => {
layers: [minimalLayer],
})
).toThrowErrorMatchingInlineSnapshot(`
"[legend]: types that failed validation:
- [legend.0.layout]: types that failed validation:
- [legend.layout.0.type]: expected value to equal [grid]
- [legend.layout.1.truncate.max_lines]: Additional properties are not allowed ('max_lines' was unexpected)
- [legend.1.layout.type]: expected value to equal [grid]
- [legend.2.placement]: expected value to equal [inside]"
"types that failed validation:
- [0.legend]: types that failed validation:
- [legend.0.layout]: types that failed validation:
- [legend.layout.0.type]: expected value to equal [grid]
- [legend.layout.1.truncate.max_lines]: Additional properties are not allowed ('max_lines' was unexpected)
- [legend.1.layout.type]: expected value to equal [grid]
- [legend.2.placement]: expected value to equal [inside]
- [1.legend]: types that failed validation:
- [legend.0.layout]: types that failed validation:
- [legend.layout.0.type]: expected value to equal [grid]
- [legend.layout.1.truncate.max_lines]: Additional properties are not allowed ('max_lines' was unexpected)
- [legend.1.layout.type]: expected value to equal [grid]
- [legend.2.placement]: expected value to equal [inside]"
`);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,58 +909,84 @@ const annotationLayerSchema = schema.oneOf(
}
);

const xyLayerUnionNoESQL = schema.oneOf(
[xyDataLayerSchemaNoESQL, referenceLineLayerSchemaNoESQL, annotationLayerSchema],
{
meta: {
id: 'xyLayersNoESQL',
description: 'XY chart layer types for DSL queries',
},
}
);

const xyLayerUnionESQL = schema.oneOf([xyDataLayerSchemaESQL, referenceLineLayerSchemaESQL], {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reference lines are not currently supported in ESQL right? Are we enabling them via API?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in #261586

meta: {
id: 'xyLayersESQL',
description: 'XY chart layer types for ES|QL queries',
},
});

/**
* Complete XY chart state configuration with layers and visualization settings
* XY chart state for DSL layers
*/
export const xyStateSchema = schema.object(
export const xyStateSchemaNoESQL = schema.object(
{
type: schema.literal('xy'),
...sharedPanelInfoSchema,
...xySharedSettings,
...dslOnlyPanelInfoSchema,
layers: schema.arrayOf(
/**
* Any valid XY chart layer type (data, reference line, or annotation)
*/
schema.oneOf([
xyDataLayerSchemaNoESQL,
xyDataLayerSchemaESQL,
referenceLineLayerSchemaNoESQL,
referenceLineLayerSchemaESQL,
annotationLayerSchema,
]),
{
minSize: 1,
maxSize: 100,
meta: { description: 'Chart layers (minimum 1 required)' },
}
),
layers: schema.arrayOf(xyLayerUnionNoESQL, {
minSize: 1,
maxSize: 100,
meta: { description: 'Chart layers' },
}),
},
{ meta: { id: 'xyChart', title: 'XY Chart', description: 'Complete XY chart configuration' } }
{
meta: {
id: 'xyChartNoESQL',
title: 'XY Chart (DSL)',
description: 'XY chart configuration for DSL queries',
},
}
);

// TODO: temporary ESQL schema for XY chart to not feed agent with heavy schema for DSL that is not used in agent
/**
* XY chart state for ES|QL layers only (data and reference lines)
*/
export const xyStateSchemaESQL = schema.object(
{
type: schema.literal('xy'),
...sharedPanelInfoSchema,
...xySharedSettings,
layers: schema.arrayOf(xyDataLayerSchemaESQL, {
layers: schema.arrayOf(xyLayerUnionESQL, {
minSize: 1,
maxSize: 1,
meta: { description: 'Only single layer ESQL charts are supported ' },
maxSize: 100,
meta: { description: 'ES|QL chart layers' },
}),
},
{
meta: {
id: 'xyChartESQL',
title: 'XY Chart (ES|QL)',
description: 'XY chart configuration for ES|QL queries',
},
}
);

export type XYState = TypeOf<typeof xyStateSchema>;
/**
* XY chart state
*/
export const xyStateSchema = schema.oneOf([xyStateSchemaNoESQL, xyStateSchemaESQL], {
meta: {
id: 'xyChart',
title: 'XY Chart',
description: 'XY chart configuration',
},
});

export type XYStateNoESQL = TypeOf<typeof xyStateSchemaNoESQL>;
export type XYStateESQL = TypeOf<typeof xyStateSchemaESQL>;
export type XYState = TypeOf<typeof xyStateSchema>;
export type DataLayerTypeESQL = TypeOf<typeof xyDataLayerSchemaESQL>;
export type DataLayerTypeNoESQL = TypeOf<typeof xyDataLayerSchemaNoESQL>;
export type DataLayerType = DataLayerTypeNoESQL | DataLayerTypeESQL;
Expand All @@ -975,5 +1001,6 @@ export type LayerTypeNoESQL =
| DataLayerTypeNoESQL
| ReferenceLineLayerTypeNoESQL
| AnnotationLayerType;
export type XYLayer = LayerTypeNoESQL | LayerTypeESQL;

export type XYStyling = TypeOf<typeof xyStylingSchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const lensApiStateSchema: Type<LensApiState> = _lensApiStateSchema;

export type { MetricState, metricStateSchemaNoESQL } from './charts/metric';
export type { LegacyMetricState, legacyMetricStateSchemaNoESQL } from './charts/legacy_metric';
export type { XYState } from './charts/xy';
export type { XYState, XYStateNoESQL, XYStateESQL, XYLayer } from './charts/xy';
export type { GaugeState, gaugeStateSchemaNoESQL } from './charts/gauge';
export type { HeatmapState, heatmapStateSchemaNoESQL } from './charts/heatmap';
export type { TagcloudState, TagcloudStateNoESQL, TagcloudStateESQL } from './charts/tagcloud';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe('XY', () => {
}
);
it.each(anyType.map((type) => anyType.map((anotherType) => [type, anotherType])).flat(1))(
'should handle multiple metric in multiple layers %s + %s with reference lines and annotations with mixed datasets',
'should handle multiple metric in multiple layers %s + %s with reference lines and annotations (DSL layers only)',
(type1, type2) => {
validateAPIConverter(
{
Expand Down Expand Up @@ -419,13 +419,32 @@ describe('XY', () => {
},
},
{
dataset: { type: 'esql', query: 'FROM company_index' },
dataset: { type: 'dataView', id: 'companyBIndex' },
type: type2,
ignore_global_filters: false,
sampling: 1,
x: { column: 'order_date' },
y: [{ column: 'value' }, { column: 'price' }],
breakdown_by: { column: 'product' },
x: {
operation: 'date_histogram',
field: 'order_date',
include_empty_rows: false,
suggested_interval: 'auto',
use_original_time_range: true,
drop_partial_intervals: false,
},
y: [
{ operation: 'count', empty_as_null: false },
{ operation: 'average', field: 'price' },
],
breakdown_by: {
operation: 'terms',
fields: ['product', 'category'],
limit: 5,
rank_by: {
direction: 'desc',
metric: 0,
type: 'column',
},
},
},
{
dataset: { type: 'index', index: 'companyIndex', time_field: '@timestamp' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { XYPersistedState, XYDataLayerConfig } from '@kbn/lens-common';
import type { AxisExtentConfig } from '@kbn/expression-xy-plugin/common';
import type { SavedObjectReference } from '@kbn/core/server';
import type { Writable } from '@kbn/utility-types';
import type { XYState } from '../../../schema';
import type { XYLayer, XYState } from '../../../schema';
import type { DataSourceStateLayer } from '../../utils';
import { convertLegendToAPIFormat, convertLegendToStateFormat } from './legend';
import { buildXYLayer } from './state_layers';
Expand Down Expand Up @@ -202,7 +202,7 @@ export function buildVisualizationAPI(
...convertAxisSettingsToAPIFormat(config, layers),
styling: convertStylingToAPIFormat(config, layerPresence),
layers: buildXYLayerAPI(config, layers, adHocDataViews, references, internalReferences),
};
} as XYState;
}

function convertExtendsToAPIFormat(extent: AxisExtentConfig | undefined): { extent?: ExtentsType } {
Expand Down Expand Up @@ -374,8 +374,8 @@ function buildXYLayerAPI(
adHocDataViews: Record<string, unknown>,
references: SavedObjectReference[],
adhocReferences?: SavedObjectReference[]
): XYState['layers'] {
const apiLayers: XYState['layers'] = [];
): XYLayer[] {
const apiLayers: XYLayer[] = [];
for (const visLayer of visualization.layers) {
if (visLayer.layerType === 'referenceLine') {
const datasourceLayer = layers[visLayer.layerId];
Expand Down
Loading