query_aggregation_ctrl.ts 2.5 KB

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