mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #16006 from grafana/fix-explore-refId
Explore: Query row using character refId instead of number
This commit is contained in:
@@ -9,6 +9,7 @@ import store from 'app/core/store';
|
||||
import { parse as parseDate } from 'app/core/utils/datemath';
|
||||
import { colors } from '@grafana/ui';
|
||||
import TableModel, { mergeTablesIntoModel } from 'app/core/table_model';
|
||||
import { getNextRefIdChar } from './query';
|
||||
|
||||
// Types
|
||||
import { RawTimeRange, IntervalValues, DataQuery, DataSourceApi } from '@grafana/ui';
|
||||
@@ -225,12 +226,8 @@ export function generateKey(index = 0): string {
|
||||
return `Q-${Date.now()}-${Math.random()}-${index}`;
|
||||
}
|
||||
|
||||
export function generateRefId(index = 0): string {
|
||||
return `${index + 1}`;
|
||||
}
|
||||
|
||||
export function generateEmptyQuery(index = 0): { refId: string; key: string } {
|
||||
return { refId: generateRefId(index), key: generateKey(index) };
|
||||
export function generateEmptyQuery(queries: DataQuery[], index = 0): DataQuery {
|
||||
return { refId: getNextRefIdChar(queries), key: generateKey(index) };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -238,9 +235,9 @@ export function generateEmptyQuery(index = 0): { refId: string; key: string } {
|
||||
*/
|
||||
export function ensureQueries(queries?: DataQuery[]): DataQuery[] {
|
||||
if (queries && typeof queries === 'object' && queries.length > 0) {
|
||||
return queries.map((query, i) => ({ ...query, ...generateEmptyQuery(i) }));
|
||||
return queries.map((query, i) => ({ ...query, ...generateEmptyQuery(queries, i) }));
|
||||
}
|
||||
return [{ ...generateEmptyQuery() }];
|
||||
return [{ ...generateEmptyQuery(queries) }];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
30
public/app/core/utils/query.test.ts
Normal file
30
public/app/core/utils/query.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { DataQuery } from '@grafana/ui';
|
||||
import { getNextRefIdChar } from './query';
|
||||
|
||||
const dataQueries: DataQuery[] = [
|
||||
{
|
||||
refId: 'A',
|
||||
},
|
||||
{
|
||||
refId: 'B',
|
||||
},
|
||||
{
|
||||
refId: 'C',
|
||||
},
|
||||
{
|
||||
refId: 'D',
|
||||
},
|
||||
{
|
||||
refId: 'E',
|
||||
},
|
||||
];
|
||||
|
||||
describe('Get next refId char', () => {
|
||||
it('should return next char', () => {
|
||||
expect(getNextRefIdChar(dataQueries)).toEqual('F');
|
||||
});
|
||||
|
||||
it('should get first char', () => {
|
||||
expect(getNextRefIdChar([])).toEqual('A');
|
||||
});
|
||||
});
|
||||
12
public/app/core/utils/query.ts
Normal file
12
public/app/core/utils/query.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import _ from 'lodash';
|
||||
import { DataQuery } from '@grafana/ui/';
|
||||
|
||||
export const getNextRefIdChar = (queries: DataQuery[]): string => {
|
||||
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
return _.find(letters, refId => {
|
||||
return _.every(queries, other => {
|
||||
return other.refId !== refId;
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user