query_aggregation_ctrl.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import coreModule from 'app/core/core_module';
  2. import _ from 'lodash';
  3. import * as options 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) {
  27. this.$scope.ctrl = this;
  28. this.target = $scope.target;
  29. this.alignmentPeriods = options.alignmentPeriods;
  30. this.aggOptions = options.aggOptions;
  31. this.alignOptions = options.alignOptions;
  32. this.setAggOptions();
  33. this.setAlignOptions();
  34. const self = this;
  35. $scope.$on('metricTypeChanged', () => {
  36. self.setAggOptions();
  37. self.setAlignOptions();
  38. });
  39. }
  40. setAlignOptions() {
  41. this.alignOptions = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
  42. if (!this.alignOptions.find(o => o.value === this.target.aggregation.perSeriesAligner)) {
  43. this.target.aggregation.perSeriesAligner = this.alignOptions.length > 0 ? this.alignOptions[0].value : '';
  44. }
  45. }
  46. setAggOptions() {
  47. this.aggOptions = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind);
  48. if (!this.aggOptions.find(o => o.value === this.target.aggregation.crossSeriesReducer)) {
  49. this.deselectAggregationOption('REDUCE_NONE');
  50. }
  51. if (this.target.aggregation.groupBys.length > 0) {
  52. this.aggOptions = this.aggOptions.filter(o => o.value !== 'REDUCE_NONE');
  53. this.deselectAggregationOption('REDUCE_NONE');
  54. }
  55. }
  56. formatAlignmentText() {
  57. const selectedAlignment = this.alignOptions.find(ap => ap.value === this.target.aggregation.perSeriesAligner);
  58. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`;
  59. }
  60. deselectAggregationOption(notValidOptionValue: string) {
  61. const newValue = this.aggOptions.find(o => o.value !== notValidOptionValue);
  62. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  63. }
  64. }
  65. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  66. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);