query_aggregation_ctrl.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import coreModule from 'app/core/core_module';
  2. import _ from 'lodash';
  3. import * as options from './constants';
  4. import { getAlignmentOptionsByMetric } 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 = !this.target.metricKind
  48. ? []
  49. : options.aggOptions.filter(i => {
  50. return (
  51. i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
  52. );
  53. });
  54. if (!this.aggOptions.find(o => o.value === this.target.aggregation.crossSeriesReducer)) {
  55. this.deselectAggregationOption('REDUCE_NONE');
  56. }
  57. if (this.target.aggregation.groupBys.length > 0) {
  58. this.aggOptions = this.aggOptions.filter(o => o.value !== 'REDUCE_NONE');
  59. this.deselectAggregationOption('REDUCE_NONE');
  60. }
  61. }
  62. formatAlignmentText() {
  63. const selectedAlignment = this.alignOptions.find(ap => ap.value === this.target.aggregation.perSeriesAligner);
  64. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`;
  65. }
  66. deselectAggregationOption(notValidOptionValue: string) {
  67. const newValue = this.aggOptions.find(o => o.value !== notValidOptionValue);
  68. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  69. }
  70. }
  71. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  72. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);