query_aggregation_ctrl.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import coreModule from 'app/core/core_module';
  2. import _ from 'lodash';
  3. import { alignmentPeriods } from './constants';
  4. import { getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from './functions';
  5. import kbn from 'app/core/utils/kbn';
  6. export class StackdriverAggregation {
  7. constructor() {
  8. return {
  9. templateUrl: 'public/app/plugins/datasource/stackdriver/partials/query.aggregation.html',
  10. controller: 'StackdriverAggregationCtrl',
  11. restrict: 'E',
  12. scope: {
  13. target: '=',
  14. alignmentPeriod: '<',
  15. refresh: '&',
  16. },
  17. };
  18. }
  19. }
  20. export class StackdriverAggregationCtrl {
  21. alignmentPeriods: any[];
  22. aggOptions: any[];
  23. alignOptions: any[];
  24. target: any;
  25. /** @ngInject */
  26. constructor(private $scope, private templateSrv) {
  27. this.$scope.ctrl = this;
  28. this.target = $scope.target;
  29. this.alignmentPeriods = [
  30. this.getTemplateVariablesGroup(),
  31. {
  32. label: 'Alignment Periods',
  33. options: alignmentPeriods.map(ap => ({
  34. ...ap,
  35. label: ap.text,
  36. })),
  37. },
  38. ];
  39. this.setAggOptions();
  40. this.setAlignOptions();
  41. const self = this;
  42. $scope.$on('metricTypeChanged', () => {
  43. self.setAggOptions();
  44. self.setAlignOptions();
  45. });
  46. this.handleAlignmentChange = this.handleAlignmentChange.bind(this);
  47. this.handleAggregationChange = this.handleAggregationChange.bind(this);
  48. this.handleAlignmentPeriodChange = this.handleAlignmentPeriodChange.bind(this);
  49. }
  50. setAlignOptions() {
  51. console.log('this.target.metricKind', this.target.metricKind);
  52. const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  53. ...a,
  54. label: a.text,
  55. }));
  56. this.alignOptions = [
  57. this.getTemplateVariablesGroup(),
  58. {
  59. label: 'Alignment Options',
  60. options: alignments,
  61. },
  62. ];
  63. if (!alignments.find(o => o.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner))) {
  64. this.target.aggregation.perSeriesAligner = alignments.length > 0 ? alignments[0].value : '';
  65. }
  66. }
  67. setAggOptions() {
  68. let aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  69. ...a,
  70. label: a.text,
  71. }));
  72. if (!aggregations.find(o => o.value === this.templateSrv.replace(this.target.aggregation.crossSeriesReducer))) {
  73. this.deselectAggregationOption('REDUCE_NONE');
  74. }
  75. if (this.target.aggregation.groupBys.length > 0) {
  76. aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
  77. this.deselectAggregationOption('REDUCE_NONE');
  78. }
  79. this.aggOptions = [
  80. this.getTemplateVariablesGroup(),
  81. {
  82. label: 'Aggregations',
  83. options: aggregations,
  84. },
  85. ];
  86. }
  87. handleAlignmentChange(value) {
  88. this.target.aggregation.perSeriesAligner = value;
  89. this.$scope.refresh();
  90. }
  91. handleAggregationChange(value) {
  92. this.target.aggregation.crossSeriesReducer = value;
  93. this.$scope.refresh();
  94. }
  95. handleAlignmentPeriodChange(value) {
  96. this.target.aggregation.alignmentPeriod = value;
  97. this.$scope.refresh();
  98. }
  99. formatAlignmentText() {
  100. const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
  101. const selectedAlignment = alignments.find(
  102. ap => ap.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner)
  103. );
  104. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${
  105. selectedAlignment ? selectedAlignment.text : ''
  106. })`;
  107. }
  108. deselectAggregationOption(notValidOptionValue: string) {
  109. const aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind);
  110. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  111. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  112. }
  113. getTemplateVariablesGroup() {
  114. return {
  115. label: 'Template Variables',
  116. options: this.templateSrv.variables.map(v => ({
  117. label: `$${v.name}`,
  118. value: `$${v.name}`,
  119. })),
  120. };
  121. }
  122. }
  123. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  124. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);