query_aggregation_ctrl.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import * as options from './constants';
  4. export class StackdriverAggregation {
  5. constructor() {
  6. return {
  7. templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
  8. controller: 'StackdriverAggregationCtrl',
  9. restrict: 'E',
  10. scope: {
  11. target: '=',
  12. refresh: '&',
  13. },
  14. };
  15. }
  16. }
  17. export class StackdriverAggregationCtrl {
  18. constructor(private $scope) {
  19. $scope.aggOptions = options.aggOptions;
  20. this.setAggOptions();
  21. this.setAlignOptions();
  22. $scope.onAlignmentChange = this.onAlignmentChange.bind(this);
  23. $scope.onAggregationChange = this.onAggregationChange.bind(this);
  24. $scope.$on('metricTypeChange', this.setAlignOptions.bind(this));
  25. }
  26. onAlignmentChange(newVal: string) {
  27. if (newVal === 'ALIGN_NONE') {
  28. this.$scope.target.aggregation.crossSeriesReducer = 'REDUCE_NONE';
  29. }
  30. this.$scope.refresh();
  31. }
  32. onAggregationChange(newVal: string) {
  33. if (newVal !== 'REDUCE_NONE' && this.$scope.target.aggregation.perSeriesAligner === 'ALIGN_NONE') {
  34. const newAlignmentOption = options.alignOptions.find(
  35. o =>
  36. o.value !== 'ALIGN_NONE' &&
  37. o.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  38. o.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  39. );
  40. this.$scope.target.aggregation.perSeriesAligner = newAlignmentOption ? newAlignmentOption.value : '';
  41. }
  42. this.$scope.refresh();
  43. }
  44. setAlignOptions() {
  45. this.$scope.alignOptions = !this.$scope.target.valueType
  46. ? []
  47. : options.alignOptions.filter(i => {
  48. return (
  49. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  50. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  51. );
  52. });
  53. if (!this.$scope.alignOptions.find(o => o.value === this.$scope.target.aggregation.perSeriesAligner)) {
  54. const newValue = this.$scope.alignOptions.find(o => o.value !== 'ALIGN_NONE');
  55. this.$scope.target.aggregation.perSeriesAligner = newValue ? newValue.value : '';
  56. }
  57. }
  58. setAggOptions() {
  59. this.$scope.aggOptions = !this.$scope.target.metricKind
  60. ? []
  61. : options.aggOptions.filter(i => {
  62. return (
  63. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  64. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  65. );
  66. });
  67. if (!this.$scope.aggOptions.find(o => o.value === this.$scope.target.aggregation.crossSeriesReducer)) {
  68. const newValue = this.$scope.aggOptions.find(o => o.value !== 'REDUCE_NONE');
  69. this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  70. }
  71. }
  72. }
  73. angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
  74. angular.module('grafana.controllers').controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);