query_aggregation_ctrl.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import * as options from './constants';
  4. export class StackdriverAggregation {
  5. constructor() {
  6. return {
  7. templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
  8. controller: 'StackdriverAggregationCtrl',
  9. restrict: 'E',
  10. scope: {
  11. target: '=',
  12. refresh: '&',
  13. },
  14. };
  15. }
  16. }
  17. export class StackdriverAggregationCtrl {
  18. target: any;
  19. alignOptions: any[];
  20. aggOptions: any[];
  21. refresh: () => void;
  22. constructor(private $scope) {
  23. this.aggOptions = options.aggOptions;
  24. this.alignOptions = options.alignOptions;
  25. $scope.alignmentPeriods = options.alignmentPeriods;
  26. $scope.getAlignOptions = this.getAlignOptions;
  27. $scope.getAggOptions = this.getAggOptions;
  28. $scope.onAlignmentChange = this.onAlignmentChange;
  29. $scope.onAggregationChange = this.onAggregationChange;
  30. this.refresh = $scope.refresh;
  31. }
  32. onAlignmentChange(newVal) {
  33. if (newVal === 'ALIGN_NONE') {
  34. this.target.aggregation.crossSeriesReducer = 'REDUCE_NONE';
  35. }
  36. this.refresh();
  37. }
  38. onAggregationChange(newVal) {
  39. if (newVal !== 'REDUCE_NONE') {
  40. const newAlignmentOption = options.alignOptions.find(o => o.value !== 'ALIGN_NONE');
  41. this.target.aggregation.perSeriesAligner = newAlignmentOption ? newAlignmentOption.value : '';
  42. }
  43. this.refresh();
  44. }
  45. getAlignOptions() {
  46. return !this.target.valueType
  47. ? options.alignOptions
  48. : options.alignOptions.filter(i => {
  49. return (
  50. i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
  51. );
  52. });
  53. }
  54. getAggOptions() {
  55. return !this.target.metricKind
  56. ? options.aggOptions
  57. : options.aggOptions.filter(i => {
  58. return (
  59. i.valueTypes.indexOf(this.target.valueType) !== -1 && i.metricKinds.indexOf(this.target.metricKind) !== -1
  60. );
  61. });
  62. }
  63. }
  64. angular.module('grafana.controllers').directive('stackdriverAggregation', StackdriverAggregation);
  65. angular.module('grafana.controllers').controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);