query_aggregation_ctrl.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  52. ...a,
  53. label: a.text,
  54. }));
  55. this.alignOptions = [
  56. this.getTemplateVariablesGroup(),
  57. {
  58. label: 'Alignment Options',
  59. options: alignments,
  60. },
  61. ];
  62. if (!alignments.find(o => o.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner))) {
  63. this.target.aggregation.perSeriesAligner = alignments.length > 0 ? alignments[0].value : '';
  64. }
  65. }
  66. setAggOptions() {
  67. let aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  68. ...a,
  69. label: a.text,
  70. }));
  71. if (!aggregations.find(o => o.value === this.templateSrv.replace(this.target.aggregation.crossSeriesReducer))) {
  72. this.deselectAggregationOption('REDUCE_NONE');
  73. }
  74. if (this.target.aggregation.groupBys.length > 0) {
  75. aggregations = this.aggOptions.filter(o => o.value !== 'REDUCE_NONE');
  76. this.deselectAggregationOption('REDUCE_NONE');
  77. }
  78. this.aggOptions = [
  79. this.getTemplateVariablesGroup(),
  80. {
  81. label: 'Aggregations',
  82. options: aggregations,
  83. },
  84. ];
  85. }
  86. handleAlignmentChange(value) {
  87. this.target.aggregation.perSeriesAligner = value;
  88. this.$scope.refresh();
  89. }
  90. handleAggregationChange(value) {
  91. this.target.aggregation.crossSeriesReducer = value;
  92. this.$scope.refresh();
  93. }
  94. handleAlignmentPeriodChange(value) {
  95. this.target.aggregation.alignmentPeriod = value;
  96. this.$scope.refresh();
  97. }
  98. formatAlignmentText() {
  99. const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
  100. const selectedAlignment = alignments.find(
  101. ap => ap.value === this.templateSrv.replace(this.target.aggregation.perSeriesAligner)
  102. );
  103. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${
  104. selectedAlignment ? selectedAlignment.text : ''
  105. })`;
  106. }
  107. deselectAggregationOption(notValidOptionValue: string) {
  108. const newValue = this.aggOptions.find(o => o.value !== notValidOptionValue);
  109. this.target.aggregation.crossSeriesReducer = newValue ? newValue.value : '';
  110. }
  111. getTemplateVariablesGroup() {
  112. return {
  113. label: 'Template Variables',
  114. options: this.templateSrv.variables.map(v => ({
  115. label: `$${v.name}`,
  116. value: `$${v.name}`,
  117. })),
  118. };
  119. }
  120. }
  121. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  122. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);