Cloudwatch: Move getNextRefIdChar util from app/core/utils to @grafana/data (#80471)

---------

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
This commit is contained in:
Ida Štambuk 2024-01-24 14:51:51 +01:00 committed by GitHub
parent 1e85d65ce0
commit 7218e11e23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 64 additions and 3 deletions

View File

@ -16,6 +16,7 @@ export * from './events';
export * from './themes';
export * from './monaco';
export * from './geo/layer';
export * from './query';
export {
type ValueMatcherOptions,
type BasicValueMatcherOptions,

View File

@ -0,0 +1 @@
export * from './refId';

View File

@ -0,0 +1,35 @@
import { DataQuery } from '@grafana/schema';
import { getNextRefId } from '.';
export interface TestQuery extends DataQuery {
name?: string;
}
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(getNextRefId(singleDataQuery)).toEqual('F');
});
it('should get first char', () => {
expect(getNextRefId([])).toEqual('A');
});
it('should get the first available character if a query has been deleted out of order', () => {
expect(getNextRefId(outOfOrderDataQuery)).toEqual('C');
});
it('should append a new char and start from AA when Z is reached', () => {
expect(getNextRefId(singleExtendedDataQuery)).toEqual('AA');
});
});

View File

@ -0,0 +1,23 @@
import { DataQuery } from '@grafana/schema';
/**
* Finds the next available refId for a query
*/
export const getNextRefId = (queries: DataQuery[]): string => {
for (let num = 0; ; num++) {
const refId = getRefId(num);
if (!queries.some((query) => query.refId === refId)) {
return refId;
}
}
};
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];
}
}

View File

@ -1,5 +1,6 @@
import { DataQuery, DataSourceRef } from '@grafana/data';
// @deprecated use the `getNextRefId` function from grafana/data instead
export const getNextRefIdChar = (queries: DataQuery[]): string => {
for (let num = 0; ; num++) {
const refId = getRefId(num);

View File

@ -2,8 +2,8 @@
// Migrations applied by the DashboardMigrator are performed before the plugin is loaded.
// DashboardMigrator migrations are tied to a certain minimum version of a dashboard which means they will only be ran once.
import { DataQuery, AnnotationQuery } from '@grafana/data';
import { getNextRefIdChar } from 'app/core/utils/query';
import { AnnotationQuery, getNextRefId } from '@grafana/data';
import { DataQuery } from '@grafana/schema';
import { CloudWatchMetricsQuery, LegacyAnnotationQuery, MetricQueryType, MetricEditorMode } from '../types';
@ -20,7 +20,7 @@ export function migrateMultipleStatsMetricsQuery(
}
}
for (const newTarget of newQueries) {
newTarget.refId = getNextRefIdChar(panelQueries);
newTarget.refId = getNextRefId(panelQueries);
delete newTarget.statistics;
panelQueries.push(newTarget);
}