Pyroscope: Fix normalize query function (#70836)

pull/70839/head
Andrej Ocenas 2 years ago committed by GitHub
parent e0619e8aa7
commit 4969a78f83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 41
      public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.test.ts
  2. 2
      public/app/plugins/datasource/grafana-pyroscope-datasource/datasource.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',

@ -114,7 +114,7 @@ export const defaultQuery: Partial<Query> = {
};
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

Loading…
Cancel
Save