Prometheus: Bug fix to check default expr is empty string to set legend format as auto (#69255)

* check default query.expr is empty string to set default legend format

* fix tests with default base query

* fix tests
pull/69275/head
Brendan O'Handley 2 years ago committed by GitHub
parent 345b7fadc9
commit 27c18b8c0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      public/app/plugins/datasource/prometheus/datasource.tsx
  2. 3
      public/app/plugins/datasource/prometheus/querybuilder/components/PromQueryBuilderOptions.test.tsx
  3. 12
      public/app/plugins/datasource/prometheus/querybuilder/state.test.ts
  4. 3
      public/app/plugins/datasource/prometheus/querybuilder/state.ts

@ -1265,12 +1265,7 @@ export class PrometheusDatasource
}
getDefaultQuery(app: CoreApp): PromQuery {
const defaults = {
refId: 'A',
expr: '',
range: true,
instant: false,
};
const defaults = promDefaultBaseQuery();
if (app === CoreApp.UnifiedAlerting) {
return {
@ -1340,3 +1335,12 @@ export function prometheusRegularEscape(value: any) {
export function prometheusSpecialRegexEscape(value: any) {
return typeof value === 'string' ? value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]\'+?.()|]/g, '\\\\$&') : value;
}
export function promDefaultBaseQuery(): PromQuery {
return {
refId: 'A',
expr: '',
range: true,
instant: false,
};
}

@ -5,6 +5,7 @@ import { selectOptionInTest } from 'test/helpers/selectOptionInTest';
import { CoreApp } from '@grafana/data';
import { promDefaultBaseQuery } from '../../datasource';
import { PromQuery } from '../../types';
import { getQueryWithDefaults } from '../state';
@ -106,7 +107,7 @@ function setup(queryOverrides: Partial<PromQuery> = {}, app: CoreApp = CoreApp.P
const props = {
app,
query: {
...getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.PanelEditor),
...getQueryWithDefaults(promDefaultBaseQuery(), CoreApp.PanelEditor),
...queryOverrides,
},
onRunQuery: jest.fn(),

@ -7,7 +7,7 @@ import { changeEditorMode, getQueryWithDefaults } from './state';
describe('getQueryWithDefaults(', () => {
it('should set defaults', () => {
expect(getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.Dashboard)).toEqual({
expect(getQueryWithDefaults({ expr: '', refId: 'A' } as PromQuery, CoreApp.Dashboard)).toEqual({
editorMode: 'builder',
expr: '',
legendFormat: '__auto',
@ -17,7 +17,7 @@ describe('getQueryWithDefaults(', () => {
});
it('should set both range and instant to true when in Explore', () => {
expect(getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.Explore)).toEqual({
expect(getQueryWithDefaults({ expr: '', refId: 'A' } as PromQuery, CoreApp.Explore)).toEqual({
editorMode: 'builder',
expr: '',
legendFormat: '__auto',
@ -29,7 +29,7 @@ describe('getQueryWithDefaults(', () => {
it('should not set both instant and range for Prometheus queries in Alert Creation', () => {
expect(
getQueryWithDefaults({ refId: 'A', range: true, instant: true } as PromQuery, CoreApp.UnifiedAlerting)
getQueryWithDefaults({ expr: '', refId: 'A', range: true, instant: true } as PromQuery, CoreApp.UnifiedAlerting)
).toEqual({
editorMode: 'builder',
expr: '',
@ -45,13 +45,15 @@ describe('getQueryWithDefaults(', () => {
expect(query.editorMode).toBe(QueryEditorMode.Code);
});
expect(getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.Dashboard).editorMode).toEqual(
expect(getQueryWithDefaults({ expr: '', refId: 'A' } as PromQuery, CoreApp.Dashboard).editorMode).toEqual(
QueryEditorMode.Code
);
});
it('should return default editor mode when it is provided', () => {
expect(getQueryWithDefaults({ refId: 'A' } as PromQuery, CoreApp.Dashboard, QueryEditorMode.Code)).toEqual({
expect(
getQueryWithDefaults({ expr: '', refId: 'A' } as PromQuery, CoreApp.Dashboard, QueryEditorMode.Code)
).toEqual({
editorMode: 'code',
expr: '',
legendFormat: '__auto',

@ -46,7 +46,8 @@ export function getQueryWithDefaults(
result = { ...query, editorMode: getDefaultEditorMode(query.expr, defaultEditor) };
}
if (query.expr == null) {
// default query expr is now empty string, set in getDefaultQuery
if (query.expr === '') {
result = { ...result, expr: '', legendFormat: LegendFormatMode.Auto };
}

Loading…
Cancel
Save