mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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
This commit is contained in:
parent
345b7fadc9
commit
27c18b8c0c
@ -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…
Reference in New Issue
Block a user