metricQueryMigrations.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { config } from '@grafana/runtime';
  2. import { CloudWatchMetricsQuery } from '../types';
  3. // Call this function to migrate queries from within the plugin.
  4. export function migrateMetricQuery(query: CloudWatchMetricsQuery): CloudWatchMetricsQuery {
  5. //add metric query migrations here
  6. const migratedQuery = migrateAliasPatterns(query);
  7. return migratedQuery;
  8. }
  9. const aliasPatterns: Record<string, string> = {
  10. metric: `PROP('MetricName')`,
  11. namespace: `PROP('Namespace')`,
  12. period: `PROP('Period')`,
  13. region: `PROP('Region')`,
  14. stat: `PROP('Stat')`,
  15. label: `LABEL`,
  16. };
  17. export function migrateAliasPatterns(query: CloudWatchMetricsQuery): CloudWatchMetricsQuery {
  18. if (config.featureToggles.cloudWatchDynamicLabels && !query.hasOwnProperty('label')) {
  19. const regex = /{{\s*(.+?)\s*}}/g;
  20. query.label =
  21. query.alias?.replace(regex, (_, value) => {
  22. if (aliasPatterns.hasOwnProperty(value)) {
  23. return `\${${aliasPatterns[value]}}`;
  24. }
  25. return `\${PROP('Dim.${value}')}`;
  26. }) ?? '';
  27. }
  28. return query;
  29. }