From b9582ea93dd9024cb6bc2349aeb5f1ac2819848d Mon Sep 17 00:00:00 2001 From: Dan Fairburn <47511336+dfairburn@users.noreply.github.com> Date: Wed, 23 Jun 2021 14:23:02 +0100 Subject: [PATCH] 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 --- public/app/core/utils/query.test.ts | 36 ++++++++++++++--------------- public/app/core/utils/query.ts | 26 +++++++++++++-------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/public/app/core/utils/query.test.ts b/public/app/core/utils/query.test.ts index c6a46b181e0..00c69667432 100644 --- a/public/app/core/utils/query.test.ts +++ b/public/app/core/utils/query.test.ts @@ -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'); + }); }); diff --git a/public/app/core/utils/query.ts b/public/app/core/utils/query.ts index d0118d10c33..b0823afe47d 100644 --- a/public/app/core/utils/query.ts +++ b/public/app/core/utils/query.ts @@ -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[] { @@ -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]; + } +}