query_aggregation_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import coreModule from 'app/core/core_module';
  2. import _ from 'lodash';
  3. import { alignmentPeriods } from './constants';
  4. import { getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from './functions';
  5. import kbn from 'app/core/utils/kbn';
  6. export class StackdriverAggregation {
  7. constructor() {
  8. return {
  9. templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
  10. controller: 'StackdriverAggregationCtrl',
  11. restrict: 'E',
  12. scope: {
  13. target: '=',
  14. alignmentPeriod: '<',
  15. refresh: '&',
  16. },
  17. };
  18. }
  19. }
  20. export class StackdriverAggregationCtrl {
  21. alignmentPeriods: any[];
  22. aggOptions: any[];
  23. alignOptions: any[];
  24. target: any;
  25. /** @ngInject */
  26. constructor(private $scope, private templateSrv) {
  27. this.$scope.ctrl = this;
  28. this.target = $scope.target;
  29. this.alignmentPeriods = [
  30. {
  31. label: 'Alignment Periods',
  32. expanded: true,
  33. options: alignmentPeriods.map(ap => ({
  34. ...ap,
  35. label: ap.text,
  36. })),
  37. },
  38. ];
  39. this.setAggOptions();
  40. this.setAlignOptions();
  41. const self = this;
  42. $scope.$on('metricTypeChanged', () => {
  43. self.setAggOptions();
  44. self.setAlignOptions();
  45. });
  46. this.handleAlignmentChange = this.handleAlignmentChange.bind(this);
  47. this.handleAggregationChange = this.handleAggregationChange.bind(this);
  48. this.handleAlignmentPeriodChange = this.handleAlignmentPeriodChange.bind(this);
  49. }
  50. setAlignOptions() {
  51. const alignOptions = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  52. ...a,
  53. label: a.text,
  54. }));
  55. if (!alignOptions.find(o => o.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner))) {
  56. this.target.aggregation.perSeriesAligner = alignOptions.length > 0 ? alignOptions[0].value : '';
  57. }
  58. this.alignOptions = [
  59. {
  60. label: 'Alignment Options',
  61. expanded: true,
  62. options: alignOptions,
  63. },
  64. ];
  65. }
  66. setAggOptions() {
  67. let aggOptions = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  68. ...a,
  69. label: a.text,
  70. }));
  71. if (!aggOptions.find(o => o.value === this.templateSrv.replace(this.target.aggregation.crossSeriesReducer))) {
  72. this.deselectAggregationOption('REDUCE_NONE');
  73. }
  74. if (this.target.aggregation.groupBys.length > 0) {
  75. aggOptions = aggOptions.filter(o => o.value !== 'REDUCE_NONE');
  76. this.deselectAggregationOption('REDUCE_NONE');
  77. }
  78. this.aggOptions = [
  79. {
  80. label: 'Aggregation Options',
  81. expanded: true,
  82. options: aggOptions,
  83. },
  84. ];
  85. }
  86. handleAlignmentChange(value) {
  87. this.target.aggregation.perSeriesAligner = value;
  88. this.$scope.refresh();
  89. }
  90. handleAggregationChange(value) {
  91. this.target.aggregation.crossSeriesReducer = value;
  92. this.$scope.refresh();
  93. }
  94. handleAlignmentPeriodChange(value) {
  95. this.target.aggregation.alignmentPeriod = value;
  96. this.$scope.refresh();
  97. }
  98. formatAlignmentText() {
  99. const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
  100. const selectedAlignment = alignments.find(
  101. ap => ap.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner)
  102. );
  103. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${
  104. selectedAlignment ? selectedAlignment.text : ''
  105. })`;
  106. }
  107. deselectAggregationOption(notValidOptionValue: string) {
  108. const aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind);
  109. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  110. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  111. }
  112. }
  113. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  114. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);