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 @@ -42,6 +42,7 @@ import {
operationFromColumn,
} from '../utils';
import { fromBucketLensApiToLensState } from '../columns/buckets';
import { getHistogramColumn } from '../../columns/date_histogram';
import { getValueApiColumn, getValueColumn } from '../columns/esql_column';
import type { MetricConfig } from '../../schema';
import { fromMetricAPItoLensState } from '../columns/metric';
Expand Down Expand Up @@ -577,8 +578,9 @@ function buildFormBasedLayer(layer: MetricConfigNoESQL): FormBasedPersistedState

addLayerColumn(defaultLayer, getAccessorName('metric'), newPrimaryColumns);
if (trendLineLayer) {
// Histogram first so columnOrder matches editor-built trendline layers and tabify agg order.
addLayerColumn(trendLineLayer, HISTOGRAM_COLUMN_NAME, getHistogramColumn({}));
addLayerColumn(trendLineLayer, `${ACCESSOR}_trendline`, newPrimaryColumns);
addLayerColumn(trendLineLayer, HISTOGRAM_COLUMN_NAME, newPrimaryColumns);
}

if (layer.breakdown_by) {
Expand Down Expand Up @@ -661,11 +663,15 @@ export function fromAPItoLensState(config: MetricConfig): MetricAttributesWithou
const visualization = buildVisualizationState(config);

const { adHocDataViews, internalReferences } = getAdhocDataviews(usedDataviews);
const regularDataViews = Object.values(usedDataviews).filter(
(v): v is { id: string; type: 'dataView' } => v.type === 'dataView'
);
const references = regularDataViews.length
? buildReferences({ [DEFAULT_LAYER_ID]: regularDataViews[0]?.id })

const regularDataViewsByLayer: Record<string, string> = {};
for (const [layerId, dv] of Object.entries(usedDataviews)) {
if (dv.type === 'dataView') {
regularDataViewsByLayer[layerId] = dv.id;
}
}
const references = Object.keys(regularDataViewsByLayer).length
? buildReferences(regularDataViewsByLayer)
: [];

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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 { spaceTest, tags } from '@kbn/scout';
import { expect } from '@kbn/scout/ui';
import type { KbnClient } from '@kbn/scout';
import { testData } from '../fixtures';

const DASHBOARD_API_PATH = '/api/dashboards';
const DASHBOARD_API_VERSION = '2023-10-31';

const LOGSTASH_TIME_RANGE = {
from: '2015-09-19T06:31:44.000Z',
to: '2015-09-23T18:31:44.000Z',
};

function withSpace(path: string, spaceId: string): string {
return `/s/${spaceId}${path}`;
}

async function createDashboard(client: KbnClient, body: unknown, spaceId: string): Promise<string> {
const response = await client.request<unknown>({
method: 'POST',
Comment on lines +14 to +27
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.

The createDashboard helper duplicates the dashboard-creation pattern already in esql_timeseries_dashboard_api.spec.ts (createDashboardWithLensPanel). Both hit the same API with the same version header.

See details

Consider extracting a shared createDashboard(client, body, spaceId) into the existing fixtures/helpers.ts so both spec files (and future ones) reuse the same helper. This keeps the API path, version header, and status assertion in one place.

// fixtures/helpers.ts
export async function createDashboard(
  client: KbnClient,
  body: unknown,
  spaceId: string
): Promise<string> {
  const response = await client.request<unknown>({
    method: 'POST',
    path: `/s/${spaceId}/api/dashboards`,
    body,
    headers: { 'elastic-api-version': '2023-10-31' },
  });
  if (response.status !== 200 && response.status !== 201) {
    throw new Error(`Dashboard create failed: ${response.status}`);
  }
  const { id } = response.data as Record<string, unknown>;
  if (typeof id !== 'string' || id.length === 0) {
    throw new Error('Dashboard create: expected a non-empty id');
  }
  return id;
}

Then both spec files import createDashboard from '../fixtures'.

Posted via Macroscope — Scout Test Review

path: withSpace(DASHBOARD_API_PATH, spaceId),
body,
headers: { 'elastic-api-version': DASHBOARD_API_VERSION },
});

if (response.status !== 201) {
throw new Error(
`Expected dashboard create status 201, got ${response.status}: ${JSON.stringify(
response.data
)}`
);
}

const { id } = response.data as Record<string, unknown>;
if (typeof id !== 'string' || id.length === 0) {
throw new Error('Dashboard create response: expected a non-empty string id');
}
return id;
}

spaceTest.describe(
'Lens metric trendline on dashboard (DSL)',
{ tag: tags.stateful.classic },
() => {
let storedDataViewId: string | undefined;

spaceTest.beforeAll(async ({ scoutSpace, apiServices }) => {
await scoutSpace.uiSettings.set({
defaultIndex: testData.DATA_VIEW_ID.LOGSTASH,
'dateFormat:tz': 'UTC',
'timepicker:timeDefaults': JSON.stringify({
from: testData.LOGSTASH_IN_RANGE_DATES.from,
to: testData.LOGSTASH_IN_RANGE_DATES.to,
}),
});

const { data: dataView } = await apiServices.dataViews.create({
title: testData.DATA_VIEW_ID.LOGSTASH,
name: `scout-metric-trendline-dv-${Date.now()}`,
timeFieldName: '@timestamp',
spaceId: scoutSpace.id,
});
storedDataViewId = dataView.id;
});

spaceTest.afterAll(async ({ scoutSpace, apiServices }) => {
if (storedDataViewId) {
await apiServices.dataViews.delete(storedDataViewId, scoutSpace.id);
}
await scoutSpace.uiSettings.unset('defaultIndex', 'dateFormat:tz', 'timepicker:timeDefaults');
await scoutSpace.savedObjects.cleanStandardList();
});

spaceTest(
'renders trendline when panel uses inline data view spec',
async ({ browserAuth, kbnClient, page, pageObjects, scoutSpace }) => {
const body = {
title: 'Metric trendline spec',
time_range: LOGSTASH_TIME_RANGE,
panels: [
{
type: 'vis',
grid: { x: 0, y: 0, w: 12, h: 8 },
config: {
type: 'metric',
title: 'Average bytes with trend',
data_source: {
type: 'data_view_spec',
index_pattern: testData.DATA_VIEW_ID.LOGSTASH,
time_field: '@timestamp',
},
metrics: [
{
type: 'primary',
operation: 'average',
field: 'bytes',
background_chart: { type: 'trend' },
},
],
},
},
],
};

const dashboardId = await createDashboard(kbnClient, body, scoutSpace.id);
await browserAuth.loginAsPrivilegedUser();
await pageObjects.dashboard.openDashboardWithId(dashboardId);

await expect(page.getByTestId('mtrVis')).toBeVisible();
await expect(page.locator('.echSingleMetricSparkline')).toBeVisible();
Comment on lines +116 to +117
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.

Locate UI elements reliably

.echSingleMetricSparkline is a CSS class from @elastic/charts — no data-test-subj exists for it today. Raw class selectors can break silently when the library updates class names.

See details

The existing FTR page object (lens_page.ts) uses the same CSS class, so this isn't new risk. However, the Scout best practice is to prefer data-test-subj, and if one is missing, to add it in source code.

Consider scoping the CSS selector under the mtrVis test-subj container so it's at least more resilient:

const metricVis = page.getByTestId('mtrVis');
await expect(metricVis).toBeVisible();
await expect(metricVis.locator('.echSingleMetricSparkline')).toBeVisible();

Longer-term, a data-test-subj on the sparkline wrapper in @elastic/charts would make all these selectors more stable.

Posted via Macroscope — Scout Test Review

}
);

spaceTest(
'renders trendline when panel uses stored data view reference',
async ({ browserAuth, kbnClient, page, pageObjects, scoutSpace }) => {
spaceTest.fail(!storedDataViewId, 'Stored data view was not created in beforeAll');

const body = {
title: 'Metric trendline ref',
time_range: LOGSTASH_TIME_RANGE,
panels: [
{
type: 'vis',
grid: { x: 0, y: 0, w: 12, h: 8 },
config: {
type: 'metric',
title: 'Average bytes with trend',
data_source: {
type: 'data_view_reference',
ref_id: storedDataViewId!,
},
metrics: [
{
type: 'primary',
operation: 'average',
field: 'bytes',
background_chart: { type: 'trend' },
},
],
},
},
],
};

const dashboardId = await createDashboard(kbnClient, body, scoutSpace.id);
await browserAuth.loginAsPrivilegedUser();
await pageObjects.dashboard.openDashboardWithId(dashboardId);

await expect(page.getByTestId('mtrVis')).toBeVisible();
await expect(page.locator('.echSingleMetricSparkline')).toBeVisible();
}
);
}
);
Loading