query_aggregation_ctrl.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 = alignmentPeriods.map(ap => ({
  30. ...ap,
  31. label: ap.text,
  32. }));
  33. this.setAggOptions();
  34. this.setAlignOptions();
  35. const self = this;
  36. $scope.$on('metricTypeChanged', () => {
  37. self.setAggOptions();
  38. self.setAlignOptions();
  39. });
  40. }
  41. setAlignOptions() {
  42. this.alignOptions = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  43. ...a,
  44. label: a.text,
  45. }));
  46. }
  47. setAggOptions() {
  48. let aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind).map(a => ({
  49. ...a,
  50. label: a.text,
  51. }));
  52. if (!aggregations.find(o => o.value === this.templateSrv.replace(this.target.crossSeriesReducer))) {
  53. this.deselectAggregationOption('REDUCE_NONE');
  54. }
  55. if (this.target.groupBys.length > 0) {
  56. aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
  57. this.deselectAggregationOption('REDUCE_NONE');
  58. }
  59. this.aggOptions = [
  60. this.getTemplateVariablesGroup(),
  61. {
  62. label: 'Aggregations',
  63. options: aggregations,
  64. },
  65. ];
  66. }
  67. handleAlignmentChange(value) {
  68. this.target.perSeriesAligner = value;
  69. this.$scope.refresh();
  70. }
  71. handleAggregationChange(value) {
  72. this.target.crossSeriesReducer = value;
  73. this.$scope.refresh();
  74. }
  75. handleAlignmentPeriodChange(value) {
  76. this.target.alignmentPeriod = value;
  77. this.$scope.refresh();
  78. }
  79. formatAlignmentText() {
  80. const alignments = getAlignmentOptionsByMetric(this.target.valueType, this.target.metricKind);
  81. const selectedAlignment = alignments.find(
  82. ap => ap.value === this.templateSrv.replace(this.target.perSeriesAligner)
  83. );
  84. return `${kbn.secondsToHms(this.$scope.alignmentPeriod)} interval (${
  85. selectedAlignment ? selectedAlignment.text : ''
  86. })`;
  87. }
  88. deselectAggregationOption(notValidOptionValue: string) {
  89. const aggregations = getAggregationOptionsByMetric(this.target.valueType, this.target.metricKind);
  90. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  91. this.target.crossSeriesReducer = newValue ? newValue.value : '';
  92. }
  93. getTemplateVariablesGroup() {
  94. return {
  95. label: 'Template Variables',
  96. options: this.templateSrv.variables.map(v => ({
  97. label: `$${v.name}`,
  98. value: `$${v.name}`,
  99. })),
  100. };
  101. }
  102. }
  103. coreModule.directive('stackdriverAggregation', StackdriverAggregation);
  104. coreModule.controller('StackdriverAggregationCtrl', StackdriverAggregationCtrl);