AggregationPicker.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React from 'react';
  2. import _ from 'lodash';
  3. // import { OptionPicker } from './OptionPicker';
  4. import { OptionGroupPicker } from './OptionGroupPicker';
  5. // import { alignmentPeriods } from '../constants';
  6. // import { getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from '../functions';
  7. import { getAggregationOptionsByMetric } from '../functions';
  8. // import kbn from 'app/core/utils/kbn';
  9. export interface Props {
  10. onChange: (metricDescriptor) => void;
  11. templateSrv: any;
  12. valueType: string;
  13. metricKind: string;
  14. aggregation: {
  15. crossSeriesReducer: string;
  16. alignmentPeriod: string;
  17. perSeriesAligner: string;
  18. groupBys: string[];
  19. };
  20. }
  21. interface State {
  22. alignmentPeriods: any[];
  23. alignOptions: any[];
  24. aggOptions: any[];
  25. }
  26. export class AggregationPicker extends React.Component<Props, State> {
  27. state: State = {
  28. alignmentPeriods: [],
  29. alignOptions: [],
  30. aggOptions: [],
  31. };
  32. constructor(props) {
  33. super(props);
  34. }
  35. componentDidMount() {
  36. this.setAggOptions();
  37. }
  38. componentWillReceiveProps(nextProps: Props) {
  39. const { valueType, metricKind, aggregation } = this.props;
  40. if (
  41. nextProps.valueType !== valueType ||
  42. nextProps.metricKind !== metricKind ||
  43. nextProps.aggregation.groupBys !== aggregation.groupBys
  44. ) {
  45. this.setAggOptions();
  46. }
  47. }
  48. setAggOptions() {
  49. const { valueType, metricKind, aggregation, templateSrv } = this.props;
  50. let aggregations = getAggregationOptionsByMetric(valueType, metricKind).map(a => ({
  51. ...a,
  52. label: a.text,
  53. }));
  54. if (!aggregations.find(o => o.value === templateSrv.replace(aggregation.crossSeriesReducer))) {
  55. this.deselectAggregationOption('REDUCE_NONE');
  56. }
  57. if (aggregation.groupBys.length > 0) {
  58. aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
  59. this.deselectAggregationOption('REDUCE_NONE');
  60. }
  61. this.setState({
  62. aggOptions: [
  63. this.getTemplateVariablesGroup(),
  64. {
  65. label: 'Aggregations',
  66. options: aggregations,
  67. },
  68. ],
  69. });
  70. }
  71. deselectAggregationOption(notValidOptionValue: string) {
  72. const aggregations = getAggregationOptionsByMetric(this.props.valueType, this.props.metricKind);
  73. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  74. this.handleAggregationChange(newValue ? newValue.value : '');
  75. }
  76. handleAggregationChange(value) {
  77. this.props.onChange(value);
  78. // this.$scope.refresh();
  79. }
  80. getTemplateVariablesGroup() {
  81. return {
  82. label: 'Template Variables',
  83. options: this.props.templateSrv.variables.map(v => ({
  84. label: `$${v.name}`,
  85. value: `$${v.name}`,
  86. })),
  87. };
  88. }
  89. render() {
  90. const { aggOptions } = this.state;
  91. const { aggregation } = this.props;
  92. return (
  93. <React.Fragment>
  94. <div className="gf-form-inline">
  95. <div className="gf-form">
  96. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  97. <div className="gf-form-select-wrapper gf-form-select-wrapper--caret-indent">
  98. <OptionGroupPicker
  99. onChange={value => this.handleAggregationChange(value)}
  100. selected={aggregation.crossSeriesReducer}
  101. groups={aggOptions}
  102. searchable={true}
  103. placeholder="Select Aggregation"
  104. className="width-15"
  105. />
  106. </div>
  107. </div>
  108. <div className="gf-form gf-form--grow">
  109. <label className="gf-form-label gf-form-label--grow">
  110. <a ng-click="ctrl.target.showAggregationOptions = !ctrl.target.showAggregationOptions">
  111. <i className="fa fa-caret-down" ng-show="ctrl.target.showAggregationOptions" />
  112. <i className="fa fa-caret-right" ng-hide="ctrl.target.showAggregationOptions" /> Advanced Options
  113. </a>
  114. </label>
  115. </div>
  116. </div>
  117. </React.Fragment>
  118. );
  119. }
  120. }