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:
Dan Fairburn 2021-06-23 14:23:02 +01:00 committed by GitHub
parent db81e55512
commit b9582ea93d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 28 deletions

View File

@ -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');
});
});

View File

@ -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];
}
}