mirror of
https://github.com/grafana/grafana.git
synced 2024-11-22 08:56:43 -06:00
Dashboard: Allow more than 26 queries per panel. (#35442)
* Dashboard: Allow more than 26 queries per panel. Fixes #4978 * Chore: Remove underscores from helper function names Co-authored-by: Danyal Fairburn <danyal.fairburn@bt.com>
This commit is contained in:
parent
db81e55512
commit
b9582ea93d
@ -1,30 +1,30 @@
|
||||
import { DataQuery } from '@grafana/data';
|
||||
import { getNextRefIdChar } from './query';
|
||||
|
||||
const dataQueries: DataQuery[] = [
|
||||
{
|
||||
refId: 'A',
|
||||
},
|
||||
{
|
||||
refId: 'B',
|
||||
},
|
||||
{
|
||||
refId: 'C',
|
||||
},
|
||||
{
|
||||
refId: 'D',
|
||||
},
|
||||
{
|
||||
refId: 'E',
|
||||
},
|
||||
];
|
||||
function dataQueryHelper(ids: string[]): DataQuery[] {
|
||||
return ids.map((letter) => {
|
||||
return { refId: letter };
|
||||
});
|
||||
}
|
||||
|
||||
const singleDataQuery: DataQuery[] = dataQueryHelper('ABCDE'.split(''));
|
||||
const outOfOrderDataQuery: DataQuery[] = dataQueryHelper('ABD'.split(''));
|
||||
const singleExtendedDataQuery: DataQuery[] = dataQueryHelper('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
|
||||
|
||||
describe('Get next refId char', () => {
|
||||
it('should return next char', () => {
|
||||
expect(getNextRefIdChar(dataQueries)).toEqual('F');
|
||||
expect(getNextRefIdChar(singleDataQuery)).toEqual('F');
|
||||
});
|
||||
|
||||
it('should get first char', () => {
|
||||
expect(getNextRefIdChar([])).toEqual('A');
|
||||
});
|
||||
|
||||
it('should get the first avaliable character if a query has been deleted out of order', () => {
|
||||
expect(getNextRefIdChar(outOfOrderDataQuery)).toEqual('C');
|
||||
});
|
||||
|
||||
it('should append a new char and start from AA when Z is reached', () => {
|
||||
expect(getNextRefIdChar(singleExtendedDataQuery)).toEqual('AA');
|
||||
});
|
||||
});
|
||||
|
@ -1,16 +1,12 @@
|
||||
import { every, find } from 'lodash';
|
||||
import { DataQuery } from '@grafana/data';
|
||||
|
||||
export const getNextRefIdChar = (queries: DataQuery[]): string => {
|
||||
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
return (
|
||||
find(letters, (refId) => {
|
||||
return every(queries, (other) => {
|
||||
return other.refId !== refId;
|
||||
});
|
||||
}) ?? 'NA'
|
||||
);
|
||||
for (let num = 0; ; num++) {
|
||||
const refId = getRefId(num);
|
||||
if (!queries.some((query) => query.refId === refId)) {
|
||||
return refId;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export function addQuery(queries: DataQuery[], query?: Partial<DataQuery>): DataQuery[] {
|
||||
@ -35,3 +31,13 @@ export function isDataQuery(url: string): boolean {
|
||||
export function isLocalUrl(url: string) {
|
||||
return !url.match(/^http/);
|
||||
}
|
||||
|
||||
function getRefId(num: number): string {
|
||||
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
|
||||
if (num < letters.length) {
|
||||
return letters[num];
|
||||
} else {
|
||||
return getRefId(Math.floor(num / letters.length) - 1) + letters[num % letters.length];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user