query_aggregation_ctrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = alignmentPeriods.map(ap => ({
  30. ...ap,
  31. label: ap.text,
  32. }));
  33. this.setAggOptions();
  34. this.setAlignOptions();
  35. const self = this;
  36. $scope.$on('metricTypeChanged', () => {
  37. self.setAggOptions();
  38. self.setAlignOptions();
  39. });
  40. this.handleAlignmentChange = this.handleAlignmentChange.bind(this);
  41. this.handleAggregationChange = this.handleAggregationChange.bind(this);
  42. this.handleAlignmentPeriodChange = this.handleAlignmentPeriodChange.bind(this);
  43. }
  44. setAlignOptions() {
  45. this.alignOptions = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  46. ...a,
  47. label: a.text,
  48. }));
  49. if (!this.alignOptions.find(o => o.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner))) {
  50. this.target.aggregation.perSeriesAligner = this.alignOptions.length > 0 ? this.alignOptions[0].value : '';
  51. }
  52. }
  53. setAggOptions() {
  54. this.aggOptions = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  55. ...a,
  56. label: a.text,
  57. }));
  58. if (!this.aggOptions.find(o => o.value === this.templateSrv.replace(this.target.aggregation.crossSeriesReducer))) {
  59. this.deselectAggregationOption('REDUCE_NONE');
  60. }
  61. if (this.target.aggregation.groupBys.length > 0) {
  62. this.aggOptions = this.aggOptions.filter(o => o.value !== 'REDUCE_NONE');
  63. this.deselectAggregationOption('REDUCE_NONE');
  64. }
  65. }
  66. handleAlignmentChange(value) {
  67. this.target.aggregation.perSeriesAligner = value;
  68. this.$scope.refresh();
  69. }
  70. handleAggregationChange(value) {
  71. this.target.aggregation.crossSeriesReducer = value;
  72. this.$scope.refresh();
  73. }
  74. handleAlignmentPeriodChange(value) {
  75. this.target.aggregation.alignmentPeriod = value;
  76. this.$scope.refresh();
  77. }
  78. formatAlignmentText() {
  79. const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
  80. const selectedAlignment = alignments.find(
  81. ap => ap.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner)
  82. );
  83. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${
  84. selectedAlignment ? selectedAlignment.text : ''
  85. })`;
  86. }
  87. deselectAggregationOption(notValidOptionValue: string) {
  88. const aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind);
  89. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  90. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  91. }
  92. }
  93. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  94. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);