query_aggregation_ctrl.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. target: any;
  19. alignOptions: any[];
  20. aggOptions: any[];
  21. constructor(private $scope) {
  22. $scope.aggOptions = options.aggOptions;
  23. this.setAggOptions();
  24. this.setAlignOptions();
  25. $scope.onAlignmentChange = this.onAlignmentChange.bind(this);
  26. $scope.onAggregationChange = this.onAggregationChange.bind(this);
  27. $scope.$on('metricTypeChange', this.setAlignOptions.bind(this));
  28. }
  29. onAlignmentChange(newVal: string) {
  30. if (newVal === 'ALIGN_NONE') {
  31. this.$scope.target.aggregation.crossSeriesReducer = 'REDUCE_NONE';
  32. }
  33. this.$scope.refresh();
  34. }
  35. onAggregationChange(newVal: string) {
  36. if (newVal !== 'REDUCE_NONE' && this.$scope.target.aggregation.perSeriesAligner === 'ALIGN_NONE') {
  37. const newAlignmentOption = options.alignOptions.find(
  38. o =>
  39. o.value !== 'ALIGN_NONE' &&
  40. o.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  41. o.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  42. );
  43. this.$scope.target.aggregation.perSeriesAligner = newAlignmentOption ? newAlignmentOption.value : '';
  44. }
  45. this.$scope.refresh();
  46. }
  47. setAlignOptions() {
  48. this.$scope.alignOptions = !this.$scope.target.valueType
  49. ? []
  50. : options.alignOptions.filter(i => {
  51. return (
  52. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  53. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  54. );
  55. });
  56. if (!this.$scope.alignOptions.find(o => o.value === this.$scope.target.aggregation.perSeriesAligner)) {
  57. const newValue = this.$scope.alignOptions.find(o => o.value !== 'ALIGN_NONE');
  58. this.$scope.target.aggregation.perSeriesAligner = newValue ? newValue.value : '';
  59. }
  60. }
  61. setAggOptions() {
  62. this.$scope.aggOptions = !this.$scope.target.metricKind
  63. ? []
  64. : options.aggOptions.filter(i => {
  65. return (
  66. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  67. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  68. );
  69. });
  70. if (!this.$scope.aggOptions.find(o => o.value === this.$scope.target.aggregation.crossSeriesReducer)) {
  71. const newValue = this.$scope.aggOptions.find(o => o.value !== 'REDUCE_NONE');
  72. this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  73. }
  74. }
  75. }
  76. angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
  77. angular.module('grafana.controllers').controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);