dashboardMigrations.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Functions in this file are called from the app/features/dashboard/state/DashboardMigrator.
  2. // Migrations applied by the DashboardMigrator are performed before the plugin is loaded.
  3. // DashboardMigrator migrations are tied to a certain minimum version of a dashboard which means they will only be ran once.
  4. import { DataQuery, AnnotationQuery } from '@grafana/data';
  5. import { getNextRefIdChar } from 'app/core/utils/query';
  6. import { CloudWatchMetricsQuery, LegacyAnnotationQuery, MetricQueryType, MetricEditorMode } from '../types';
  7. // E.g query.statistics = ['Max', 'Min'] will be migrated to two queries - query1.statistic = 'Max' and query2.statistic = 'Min'
  8. export function migrateMultipleStatsMetricsQuery(
  9. query: CloudWatchMetricsQuery,
  10. panelQueries: DataQuery[]
  11. ): DataQuery[] {
  12. const newQueries = [];
  13. if (query?.statistics && query?.statistics.length) {
  14. query.statistic = query.statistics[0];
  15. for (const stat of query.statistics.splice(1)) {
  16. newQueries.push({ ...query, statistic: stat });
  17. }
  18. }
  19. for (const newTarget of newQueries) {
  20. newTarget.refId = getNextRefIdChar(panelQueries);
  21. delete newTarget.statistics;
  22. panelQueries.push(newTarget);
  23. }
  24. delete query.statistics;
  25. return newQueries;
  26. }
  27. // Migrates an annotation query that use more than one statistic into multiple queries
  28. // E.g query.statistics = ['Max', 'Min'] will be migrated to two queries - query1.statistic = 'Max' and query2.statistic = 'Min'
  29. export function migrateMultipleStatsAnnotationQuery(
  30. annotationQuery: AnnotationQuery<LegacyAnnotationQuery>
  31. ): Array<AnnotationQuery<DataQuery>> {
  32. const newAnnotations: Array<AnnotationQuery<LegacyAnnotationQuery>> = [];
  33. if (annotationQuery && 'statistics' in annotationQuery && annotationQuery?.statistics?.length) {
  34. for (const stat of annotationQuery.statistics.splice(1)) {
  35. const { statistics, name, ...newAnnotation } = annotationQuery;
  36. newAnnotations.push({ ...newAnnotation, statistic: stat, name: `${name} - ${stat}` });
  37. }
  38. annotationQuery.statistic = annotationQuery.statistics[0];
  39. // Only change the name of the original if new annotations have been created
  40. if (newAnnotations.length !== 0) {
  41. annotationQuery.name = `${annotationQuery.name} - ${annotationQuery.statistic}`;
  42. }
  43. delete annotationQuery.statistics;
  44. }
  45. return newAnnotations;
  46. }
  47. export function migrateCloudWatchQuery(query: CloudWatchMetricsQuery) {
  48. if (!query.hasOwnProperty('metricQueryType')) {
  49. query.metricQueryType = MetricQueryType.Search;
  50. }
  51. if (!query.hasOwnProperty('metricEditorMode')) {
  52. if (query.metricQueryType === MetricQueryType.Query) {
  53. query.metricEditorMode = MetricEditorMode.Code;
  54. } else {
  55. query.metricEditorMode = query.expression ? MetricEditorMode.Code : MetricEditorMode.Builder;
  56. }
  57. }
  58. }