diff --git a/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.test.ts b/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.test.ts index f11c4ebc9bb..3d722b5b343 100644 --- a/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.test.ts +++ b/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.test.ts @@ -1,8 +1,9 @@ -import { AbstractLabelOperator, DataSourceInstanceSettings, PluginMetaInfo, PluginType } from '@grafana/data'; +import { AbstractLabelOperator, CoreApp, DataSourceInstanceSettings, PluginMetaInfo, PluginType } from '@grafana/data'; import { TemplateSrv } from 'app/features/templating/template_srv'; import { defaultPhlareQueryType } from './dataquery.gen'; -import { PhlareDataSource } from './datasource'; +import { normalizeQuery, PhlareDataSource } from './datasource'; +import { Query } from './types'; describe('Phlare data source', () => { let ds: PhlareDataSource; @@ -79,6 +80,42 @@ describe('Phlare data source', () => { }); }); +describe('normalizeQuery', () => { + it('correctly normalizes the query', () => { + // We need the type assertion here because the query types are inherently wrong in explore. + let normalized = normalizeQuery({} as Query); + expect(normalized).toMatchObject({ + labelSelector: '{}', + groupBy: [], + queryType: 'profile', + }); + + normalized = normalizeQuery({ + labelSelector: '{app="myapp"}', + groupBy: ['app'], + queryType: 'metrics', + profileTypeId: 'cpu', + refId: '', + }); + expect(normalized).toMatchObject({ + labelSelector: '{app="myapp"}', + groupBy: ['app'], + queryType: 'metrics', + profileTypeId: 'cpu', + }); + }); + + it('correctly normalizes the query when in explore', () => { + // We need the type assertion here because the query types are inherently wrong in explore. + const normalized = normalizeQuery({} as Query, CoreApp.Explore); + expect(normalized).toMatchObject({ + labelSelector: '{}', + groupBy: [], + queryType: 'both', + }); + }); +}); + const defaultQuery = (query: string) => { return { refId: 'x', diff --git a/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.ts b/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.ts index 3d5fe90b365..935ccee324e 100644 --- a/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.ts +++ b/public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.ts @@ -114,7 +114,7 @@ export const defaultQuery: Partial = { }; export function normalizeQuery(query: Query, app?: CoreApp | string) { - let normalized = { ...query, ...defaultQuery }; + let normalized = { ...defaultQuery, ...query }; if (app !== CoreApp.Explore && normalized.queryType === 'both') { // In dashboards and other places, we can't show both types of graphs at the same time. // This will also be a default when having 'both' query and adding it from explore to dashboard