mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 10:50:19 +08:00
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import deepEqual from 'fast-deep-equal';
|
|
|
|
import { CloudWatchMetricsQuery } from '../types';
|
|
|
|
// Call this function to migrate queries from within the plugin.
|
|
export function migrateMetricQuery(query: CloudWatchMetricsQuery): CloudWatchMetricsQuery {
|
|
//add metric query migrations here
|
|
const migratedQuery = migrateAliasPatterns(query);
|
|
return deepEqual(migratedQuery, query) ? query : migratedQuery;
|
|
}
|
|
|
|
const aliasPatterns: Record<string, string> = {
|
|
metric: `PROP('MetricName')`,
|
|
namespace: `PROP('Namespace')`,
|
|
period: `PROP('Period')`,
|
|
region: `PROP('Region')`,
|
|
stat: `PROP('Stat')`,
|
|
label: `LABEL`,
|
|
};
|
|
|
|
// migrateAliasPatterns in the context of https://github.com/grafana/grafana/issues/48434
|
|
export function migrateAliasPatterns(query: CloudWatchMetricsQuery): CloudWatchMetricsQuery {
|
|
if (!query.hasOwnProperty('label')) {
|
|
const newQuery = { ...query };
|
|
if (!query.hasOwnProperty('label')) {
|
|
const regex = /{{\s*(.+?)\s*}}/g;
|
|
newQuery.label =
|
|
query.alias?.replace(regex, (_, value) => {
|
|
if (aliasPatterns.hasOwnProperty(value)) {
|
|
return `\${${aliasPatterns[value]}}`;
|
|
}
|
|
|
|
return `\${PROP('Dim.${value}')}`;
|
|
}) ?? '';
|
|
}
|
|
return newQuery;
|
|
}
|
|
return query;
|
|
}
|