query_aggregation_ctrl.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import * as options from './constants';
  4. import * as options from './constants';
  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. constructor(private $scope) {
  22. $scope.aggOptions = options.aggOptions;
  23. this.setAggOptions();
  24. this.setAlignOptions();
  25. $scope.alignmentPeriods = options.alignmentPeriods;
  26. $scope.onAlignmentChange = this.onAlignmentChange.bind(this);
  27. $scope.onAggregationChange = this.onAggregationChange.bind(this);
  28. $scope.formatAlignmentText = this.formatAlignmentText.bind(this);
  29. $scope.$on('metricTypeChanged', this.setAlignOptions.bind(this));
  30. }
  31. onAlignmentChange(newVal: string) {
  32. if (newVal === 'ALIGN_NONE') {
  33. this.$scope.target.aggregation.crossSeriesReducer = 'REDUCE_NONE';
  34. }
  35. this.$scope.refresh();
  36. }
  37. onAggregationChange(newVal: string) {
  38. if (newVal !== 'REDUCE_NONE' && this.$scope.target.aggregation.perSeriesAligner === 'ALIGN_NONE') {
  39. const newAlignmentOption = options.alignOptions.find(
  40. o =>
  41. o.value !== 'ALIGN_NONE' &&
  42. o.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  43. o.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  44. );
  45. this.$scope.target.aggregation.perSeriesAligner = newAlignmentOption ? newAlignmentOption.value : '';
  46. }
  47. this.$scope.refresh();
  48. }
  49. setAlignOptions() {
  50. this.$scope.alignOptions = !this.$scope.target.valueType
  51. ? []
  52. : options.alignOptions.filter(i => {
  53. return (
  54. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  55. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  56. );
  57. });
  58. if (!this.$scope.alignOptions.find(o => o.value === this.$scope.target.aggregation.perSeriesAligner)) {
  59. const newValue = this.$scope.alignOptions.find(o => o.value !== 'ALIGN_NONE');
  60. this.$scope.target.aggregation.perSeriesAligner = newValue ? newValue.value : '';
  61. }
  62. }
  63. setAggOptions() {
  64. this.$scope.aggOptions = !this.$scope.target.metricKind
  65. ? []
  66. : options.aggOptions.filter(i => {
  67. return (
  68. i.valueTypes.indexOf(this.$scope.target.valueType) !== -1 &&
  69. i.metricKinds.indexOf(this.$scope.target.metricKind) !== -1
  70. );
  71. });
  72. if (!this.$scope.aggOptions.find(o => o.value === this.$scope.target.aggregation.crossSeriesReducer)) {
  73. const newValue = this.$scope.aggOptions.find(o => o.value !== 'REDUCE_NONE');
  74. this.$scope.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  75. }
  76. }
  77. formatAlignmentText() {
  78. const selectedAlignment = this.$scope.alignOptions.find(
  79. ap => ap.value === this.$scope.target.aggregation.perSeriesAligner
  80. );
  81. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`;
  82. }
  83. }
  84. angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
  85. angular.module('grafana.controllers').controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);