mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
1e85d65ce0
commit
7218e11e23
@ -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,
|
||||
|
1
packages/grafana-data/src/query/index.ts
Normal file
1
packages/grafana-data/src/query/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './refId';
|
35
packages/grafana-data/src/query/refId.test.ts
Normal file
35
packages/grafana-data/src/query/refId.test.ts
Normal 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');
|
||||
});
|
||||
});
|
23
packages/grafana-data/src/query/refId.ts
Normal file
23
packages/grafana-data/src/query/refId.ts
Normal 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];
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user