query_aggregation_ctrl.ts 2.5 KB

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