query_aggregation_ctrl.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 (${
  61. selectedAlignment ? selectedAlignment.text : ''
  62. })`;
  63. }
  64. deselectAggregationOption(notValidOptionValue: string) {
  65. const newValue = this.aggOptions.find(o => o.value !== notValidOptionValue);
  66. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  67. }
  68. }
  69. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  70. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);