query_aggregation_ctrl.ts 3.4 KB

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