mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
41eff02d75
commit
c3934ba60b
@ -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) {
|
||||||
|
@ -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({
|
||||||
|
@ -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 {
|
||||||
|
@ -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(),
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user