query_aggregation_ctrl.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. alignmentPeriods: any[];
  21. aggOptions: any[];
  22. alignOptions: any[];
  23. target: any;
  24. constructor(private $scope) {
  25. this.$scope.ctrl = this;
  26. this.target = $scope.target;
  27. this.alignmentPeriods = options.alignmentPeriods;
  28. this.aggOptions = options.aggOptions;
  29. this.alignOptions = options.alignOptions;
  30. this.setAggOptions();
  31. this.setAlignOptions();
  32. const self = this;
  33. $scope.$on('metricTypeChanged', () => {
  34. self.setAggOptions();
  35. self.setAlignOptions();
  36. });
  37. }
  38. setAlignOptions() {
  39. this.alignOptions = !this.target.valueType
  40. ? []
  41. : options.alignOptions.filter(i => {
  42. return (
  43. i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
  44. );
  45. });
  46. if (!this.alignOptions.find(o => o.value === this.target.aggregation.perSeriesAligner)) {
  47. this.target.aggregation.perSeriesAligner = this.alignOptions.length > 0 ? this.alignOptions[0].value : '';
  48. }
  49. }
  50. setAggOptions() {
  51. this.aggOptions = !this.target.metricKind
  52. ? []
  53. : options.aggOptions.filter(i => {
  54. return (
  55. i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
  56. );
  57. });
  58. if (!this.aggOptions.find(o => o.value === this.target.aggregation.crossSeriesReducer)) {
  59. const newValue = this.aggOptions.find(o => o.value !== 'REDUCE_NONE');
  60. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  61. }
  62. }
  63. formatAlignmentText() {
  64. const selectedAlignment = this.alignOptions.find(ap => ap.value === this.target.aggregation.perSeriesAligner);
  65. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${selectedAlignment.text})`;
  66. }
  67. }
  68. angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
  69. angular.module('grafana.controllers').controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);