Explore: Preserve time range when creating a dashboard panel from Explore (#80070)

* Explore: Preserve time range when creating a dashboard panel from Explore

* fix tests
This commit is contained in:
Giordano Ricci 2024-01-05 15:10:31 +00:00 committed by GitHub
parent 41eff02d75
commit c3934ba60b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 4 deletions

View File

@ -102,6 +102,8 @@ export function AddToDashboardForm(props: Props): ReactElement {
queries: exploreItem.queries.length, queries: exploreItem.queries.length,
}); });
const { from, to } = exploreItem.range.raw;
try { try {
await setDashboardInLocalStorage({ await setDashboardInLocalStorage({
dashboardUid, dashboardUid,
@ -109,6 +111,10 @@ export function AddToDashboardForm(props: Props): ReactElement {
queries: exploreItem.queries, queries: exploreItem.queries,
queryResponse: exploreItem.queryResponse, queryResponse: exploreItem.queryResponse,
panelState: exploreItem?.panelsState, panelState: exploreItem?.panelsState,
time: {
from: typeof from === 'string' ? from : from.toISOString(),
to: typeof to === 'string' ? to : to.toISOString(),
},
}); });
} catch (error) { } catch (error) {
switch (error) { switch (error) {

View File

@ -23,6 +23,7 @@ describe('addPanelToDashboard', () => {
queries: [], queries: [],
queryResponse: createEmptyQueryResponse(), queryResponse: createEmptyQueryResponse(),
datasource: { type: 'loki', uid: 'someUid' }, datasource: { type: 'loki', uid: 'someUid' },
time: { from: 'now-1h', to: 'now' },
}); });
expect(spy).toHaveBeenCalledWith( expect(spy).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
@ -33,12 +34,30 @@ describe('addPanelToDashboard', () => {
); );
}); });
it('Correct time range is used', async () => {
await setDashboardInLocalStorage({
queries: [],
queryResponse: createEmptyQueryResponse(),
datasource: { type: 'loki', uid: 'someUid' },
time: { from: 'now-10h', to: 'now' },
});
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
dashboard: expect.objectContaining({
time: expect.objectContaining({ from: 'now-10h', to: 'now' }),
}),
})
);
});
it('All queries are correctly passed through', async () => { it('All queries are correctly passed through', async () => {
const queries: DataQuery[] = [{ refId: 'A' }, { refId: 'B', hide: true }]; const queries: DataQuery[] = [{ refId: 'A' }, { refId: 'B', hide: true }];
await setDashboardInLocalStorage({ await setDashboardInLocalStorage({
queries, queries,
queryResponse: createEmptyQueryResponse(), queryResponse: createEmptyQueryResponse(),
time: { from: 'now-1h', to: 'now' },
}); });
expect(spy).toHaveBeenCalledWith( expect(spy).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
@ -68,6 +87,7 @@ describe('addPanelToDashboard', () => {
queryResponse: createEmptyQueryResponse(), queryResponse: createEmptyQueryResponse(),
dashboardUid: 'someUid', dashboardUid: 'someUid',
datasource: { type: '' }, datasource: { type: '' },
time: { from: 'now-1h', to: 'now' },
}); });
expect(spy).toHaveBeenCalledWith( expect(spy).toHaveBeenCalledWith(
@ -95,7 +115,7 @@ describe('addPanelToDashboard', () => {
]; ];
it.each(cases)('%s', async (_, queries, queryResponse) => { it.each(cases)('%s', async (_, queries, queryResponse) => {
await setDashboardInLocalStorage({ queries, queryResponse }); await setDashboardInLocalStorage({ queries, queryResponse, time: { from: 'now-1h', to: 'now' } });
expect(spy).toHaveBeenCalledWith( expect(spy).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
dashboard: expect.objectContaining({ dashboard: expect.objectContaining({
@ -127,7 +147,7 @@ describe('addPanelToDashboard', () => {
[framesType]: [new MutableDataFrame({ refId: 'A', fields: [] })], [framesType]: [new MutableDataFrame({ refId: 'A', fields: [] })],
}; };
await setDashboardInLocalStorage({ queries, queryResponse }); await setDashboardInLocalStorage({ queries, queryResponse, time: { from: 'now-1h', to: 'now' } });
expect(spy).toHaveBeenCalledWith( expect(spy).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
dashboard: expect.objectContaining({ dashboard: expect.objectContaining({
@ -151,7 +171,7 @@ describe('addPanelToDashboard', () => {
], ],
}; };
await setDashboardInLocalStorage({ queries, queryResponse }); await setDashboardInLocalStorage({ queries, queryResponse, time: { from: 'now-1h', to: 'now' } });
expect(spy).toHaveBeenCalledWith( expect(spy).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
dashboard: expect.objectContaining({ dashboard: expect.objectContaining({

View File

@ -1,5 +1,5 @@
import { DataFrame, ExplorePanelsState } from '@grafana/data'; import { DataFrame, ExplorePanelsState } from '@grafana/data';
import { DataQuery, DataSourceRef } from '@grafana/schema'; import { Dashboard, DataQuery, DataSourceRef } from '@grafana/schema';
import { DataTransformerConfig } from '@grafana/schema/dist/esm/raw/dashboard/x/dashboard_types.gen'; import { DataTransformerConfig } from '@grafana/schema/dist/esm/raw/dashboard/x/dashboard_types.gen';
import { backendSrv } from 'app/core/services/backend_srv'; import { backendSrv } from 'app/core/services/backend_srv';
import { import {
@ -19,6 +19,7 @@ interface AddPanelToDashboardOptions {
datasource?: DataSourceRef; datasource?: DataSourceRef;
dashboardUid?: string; dashboardUid?: string;
panelState?: ExplorePanelsState; panelState?: ExplorePanelsState;
time: Dashboard['time'];
} }
function createDashboard(): DashboardDTO { function createDashboard(): DashboardDTO {
@ -93,6 +94,8 @@ export async function setDashboardInLocalStorage(options: AddPanelToDashboardOpt
dto.dashboard.panels = [panel, ...(dto.dashboard.panels ?? [])]; dto.dashboard.panels = [panel, ...(dto.dashboard.panels ?? [])];
dto.dashboard.time = options.time;
try { try {
setDashboardToFetchFromLocalStorage(dto); setDashboardToFetchFromLocalStorage(dto);
} catch { } catch {

View File

@ -24,6 +24,11 @@ const setup = (children: ReactNode, queries: DataQuery[] = [{ refId: 'A' }]) =>
explore: { explore: {
panes: { panes: {
left: { left: {
range: {
from: 'now-6h',
to: 'now',
raw: { from: 'now-6h', to: 'now' },
},
queries, queries,
queryResponse: createEmptyQueryResponse(), queryResponse: createEmptyQueryResponse(),
}, },