query_aggregation_ctrl.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import coreModule from 'app/core/core_module';
  2. import _ from 'lodash';
  3. import * as options from './constants';
  4. import kbn from 'app/core/utils/kbn';
  5. export class StackdriverAggregation {
  6. constructor() {
  7. return {
  8. templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
  9. controller: 'StackdriverAggregationCtrl',
  10. restrict: 'E',
  11. scope: {
  12. target: '=',
  13. alignmentPeriod: '<',
  14. refresh: '&',
  15. },
  16. };
  17. }
  18. }
  19. export class StackdriverAggregationCtrl {
  20. alignmentPeriods: any[];
  21. aggOptions: any[];
  22. alignOptions: any[];
  23. target: any;
  24. /** @ngInject */
  25. constructor(private $scope) {
  26. this.$scope.ctrl = this;
  27. this.target = $scope.target;
  28. this.alignmentPeriods = options.alignmentPeriods;
  29. this.aggOptions = options.aggOptions;
  30. this.alignOptions = options.alignOptions;
  31. this.setAggOptions();
  32. this.setAlignOptions();
  33. const self = this;
  34. $scope.$on('metricTypeChanged', () => {
  35. self.setAggOptions();
  36. self.setAlignOptions();
  37. });
  38. }
  39. setAlignOptions() {
  40. this.alignOptions = !this.target.valueType
  41. ? []
  42. : options.alignOptions.filter(i => {
  43. return (
  44. i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
  45. );
  46. });
  47. if (!this.alignOptions.find(o => o.value === this.target.aggregation.perSeriesAligner)) {
  48. this.target.aggregation.perSeriesAligner = this.alignOptions.length > 0 ? this.alignOptions[0].value : '';
  49. }
  50. }
  51. setAggOptions() {
  52. this.aggOptions = !this.target.metricKind
  53. ? []
  54. : options.aggOptions.filter(i => {
  55. return (
  56. i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
  57. );
  58. });
  59. if (!this.aggOptions.find(o => o.value === this.target.aggregation.crossSeriesReducer)) {
  60. this.deselectAggregationOption('REDUCE_NONE');
  61. }
  62. if (this.target.aggregation.groupBys.length > 0) {
  63. this.aggOptions = this.aggOptions.filter(o => o.value !== 'REDUCE_NONE');
  64. this.deselectAggregationOption('REDUCE_NONE');
  65. }
  66. }
  67. formatAlignmentText() {
  68. const selectedAlignment = this.alignOptions.find(ap => ap.value === this.target.aggregation.perSeriesAligner);
  69. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`;
  70. }
  71. deselectAggregationOption(notValidOptionValue: string) {
  72. const newValue = this.aggOptions.find(o => o.value !== notValidOptionValue);
  73. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  74. }
  75. }
  76. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  77. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);