Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ describe('Metric Schema', () => {
},
background_chart: {
type: 'trend',
time_field: '@timestamp',
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const complementaryVizSchemaNoESQL = schema.oneOf([
}),
schema.object({
type: schema.literal('trend'),
time_field: schema.string({ meta: { description: 'Time field for trend chart' } }),
}),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,67 @@ export const metricAPIWithTermsRankedBySecondary = {
},
},
} as MetricState;

export const trendlineMetricAPIAttributes = {
type: 'metric',
title: 'Metric - Trendline',
description: 'Metric with trendline background chart',
dataset: { type: 'dataView', id: 'testId' },
metrics: [
{
type: 'primary',
operation: 'count',
empty_as_null: true,
background_chart: {
type: 'trend',
time_field: 'timestamp',
},
},
],
} as MetricState;

export const trendlineWithSecondaryMetricAPIAttributes = {
type: 'metric',
title: 'Metric - Trendline with Secondary',
description: 'Metric with trendline and secondary metric',
dataset: { type: 'dataView', id: 'testId' },
metrics: [
{
type: 'primary',
operation: 'count',
empty_as_null: true,
background_chart: {
type: 'trend',
time_field: 'timestamp',
},
},
{
type: 'secondary',
operation: 'average',
field: 'bytes',
},
],
} as MetricState;

export const trendlineWithBreakdownMetricAPIAttributes = {
type: 'metric',
title: 'Metric - Trendline with Breakdown',
description: 'Metric with trendline and breakdown',
dataset: { type: 'dataView', id: 'testId' },
metrics: [
{
type: 'primary',
operation: 'count',
empty_as_null: true,
background_chart: {
type: 'trend',
time_field: 'timestamp',
},
},
],
breakdown_by: {
operation: 'terms',
fields: ['extension.keyword'],
size: 5,
},
} as MetricState;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {
AvgIndexPatternColumn,
CountIndexPatternColumn,
DateHistogramIndexPatternColumn,
FormulaIndexPatternColumn,
MathIndexPatternColumn,
MedianIndexPatternColumn,
Expand Down Expand Up @@ -490,3 +491,113 @@ export const breakdownMetricWithFormulaRefColumnsAttributes: LensAttributes = {
adHocDataViews: {},
},
};

/**
* Metric with trendline generated from kibana
*/
export const trendlineMetricAttributes: LensAttributes = {
title: 'Metric - Trendline',
description: 'Metric with trendline background chart',
visualizationType: 'lnsMetric',
references: [
{
type: 'index-pattern',
id: '90943e30-9a47-11e8-b64d-95841ca0b247',
name: 'indexpattern-datasource-layer-29b51bd9-2fdc-43ff-ad5b-84361c410ff8',
},
{
type: 'index-pattern',
id: '90943e30-9a47-11e8-b64d-95841ca0b247',
name: 'indexpattern-datasource-layer-77aaea1d-fb58-42d2-bb5c-b7ac363316dd',
},
],
state: {
visualization: {
layerId: '29b51bd9-2fdc-43ff-ad5b-84361c410ff8',
layerType: 'data',
metricAccessor: 'ede0ece3-1093-4110-8672-ecf7e1724ccb',
showBar: false,
applyColorTo: 'background',
trendlineLayerId: '77aaea1d-fb58-42d2-bb5c-b7ac363316dd',
trendlineLayerType: 'metricTrendline',
trendlineTimeAccessor: 'ce401516-4d0c-42d0-a2c6-e13a58c17820',
trendlineMetricAccessor: '271f4e55-845f-4d4d-87ec-ea61df913678',
secondaryTrend: {
type: 'none',
},
secondaryLabelPosition: 'before',
},
query: {
query: '',
language: 'kuery',
},
filters: [],
datasourceStates: {
formBased: {
layers: {
'29b51bd9-2fdc-43ff-ad5b-84361c410ff8': {
columns: {
'ede0ece3-1093-4110-8672-ecf7e1724ccb': {
label: 'Count of records',
dataType: 'number',
operationType: 'count',
isBucketed: false,
sourceField: '___records___',
params: {
emptyAsNull: true,
},
} as CountIndexPatternColumn,
},
columnOrder: ['ede0ece3-1093-4110-8672-ecf7e1724ccb'],
incompleteColumns: {},
sampling: 1,
},
'77aaea1d-fb58-42d2-bb5c-b7ac363316dd': {
linkToLayers: ['29b51bd9-2fdc-43ff-ad5b-84361c410ff8'],
columns: {
'ce401516-4d0c-42d0-a2c6-e13a58c17820': {
label: 'timestamp',
dataType: 'date',
operationType: 'date_histogram',
sourceField: 'timestamp',
isBucketed: true,
params: {
interval: 'auto',
includeEmptyRows: true,
dropPartials: false,
},
} as DateHistogramIndexPatternColumn,
'271f4e55-845f-4d4d-87ec-ea61df913678': {
label: 'Count of records',
dataType: 'number',
operationType: 'count',
isBucketed: false,
sourceField: '___records___',
params: {
emptyAsNull: true,
},
} as CountIndexPatternColumn,
},
columnOrder: [
'ce401516-4d0c-42d0-a2c6-e13a58c17820',
'271f4e55-845f-4d4d-87ec-ea61df913678',
],
sampling: 1,
ignoreGlobalFilters: false,
incompleteColumns: {},
},
},
},
// @ts-expect-error
indexpattern: {
layers: {},
},
textBased: {
layers: {},
},
},
internalReferences: [],
adHocDataViews: {},
},
version: 2,
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import {
breakdownMetricAttributes,
complexMetricAttributes,
breakdownMetricWithFormulaRefColumnsAttributes,
trendlineMetricAttributes,
} from './lens_state_config.mock';
import {
simpleMetricAPIAttributes,
breakdownMetricAPIAttributes,
complexMetricAPIAttributes,
complexESQLMetricAPIAttributes,
metricAPIWithTermsRankedBySecondary,
trendlineMetricAPIAttributes,
trendlineWithSecondaryMetricAPIAttributes,
trendlineWithBreakdownMetricAPIAttributes,
} from './lens_api_config.mock';

describe('Metric', () => {
Expand All @@ -36,6 +40,10 @@ describe('Metric', () => {
it('should convert a breakdown-by metric', () => {
validateConverter(breakdownMetricAttributes, metricStateSchema);
});

it('should convert a metric with trendline', () => {
validateConverter(trendlineMetricAttributes, metricStateSchema);
});
});
describe('validateAPIConverter', () => {
it('should convert a simple metric', () => {
Expand All @@ -57,6 +65,18 @@ describe('Metric', () => {
it('should convert a metric with a terms agg ranked by secondary metric', () => {
validateAPIConverter(metricAPIWithTermsRankedBySecondary, metricStateSchema);
});

it('should convert a metric with trendline', () => {
validateAPIConverter(trendlineMetricAPIAttributes, metricStateSchema);
});

it('should convert a metric with trendline and secondary metric', () => {
validateAPIConverter(trendlineWithSecondaryMetricAPIAttributes, metricStateSchema);
});

it('should convert a metric with trendline and breakdown', () => {
validateAPIConverter(trendlineWithBreakdownMetricAPIAttributes, metricStateSchema);
});
});

it('should convert a breakdown-by metric with formula reference columns and rank_by in the terms bucket operation', () => {
Expand Down
Loading
Loading