query_aggregation_ctrl.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, private templateSrv) {
  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.templateSrv.replace(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.templateSrv.replace(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(
  58. ap => ap.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner)
  59. );
  60. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`;
  61. }
  62. deselectAggregationOption(notValidOptionValue: string) {
  63. const newValue = this.aggOptions.find(o => o.value !== notValidOptionValue);
  64. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  65. }
  66. }
  67. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  68. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);