AggregationPicker.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React from 'react';
  2. import _ from 'lodash';
  3. // import { OptionPicker } from './OptionPicker';
  4. // import { alignmentPeriods } from '../constants';
  5. // import { getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from '../functions';
  6. import { getAggregationOptionsByMetric } from '../functions';
  7. import { StackdriverPicker } from './StackdriverPicker';
  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(this.props);
  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(nextProps);
  46. }
  47. }
  48. setAggOptions({ valueType, metricKind, aggregation }) {
  49. const { templateSrv } = this.props;
  50. let aggregations = getAggregationOptionsByMetric(valueType, metricKind).map(a => ({
  51. ...a,
  52. label: a.text,
  53. }));
  54. if (
  55. aggregations.length > 0 &&
  56. !aggregations.find(o => o.value === templateSrv.replace(aggregation.crossSeriesReducer))
  57. ) {
  58. this.deselectAggregationOption('REDUCE_NONE');
  59. }
  60. if (aggregation.groupBys.length > 0) {
  61. aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
  62. this.deselectAggregationOption('REDUCE_NONE');
  63. }
  64. this.setState({ aggOptions: aggregations });
  65. }
  66. deselectAggregationOption(notValidOptionValue: string) {
  67. const aggregations = getAggregationOptionsByMetric(this.props.valueType, this.props.metricKind);
  68. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  69. this.handleAggregationChange(newValue ? newValue.value : '');
  70. }
  71. handleAggregationChange(value) {
  72. this.props.onChange(value);
  73. }
  74. render() {
  75. const { aggOptions } = this.state;
  76. const { aggregation } = this.props;
  77. return (
  78. <React.Fragment>
  79. <div className="gf-form-inline">
  80. <div className="gf-form">
  81. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  82. <StackdriverPicker
  83. onChange={value => this.handleAggregationChange(value)}
  84. selected={aggregation.crossSeriesReducer}
  85. options={aggOptions}
  86. searchable={true}
  87. placeholder="Select Aggregation"
  88. className="width-15"
  89. groupName="Aggregations"
  90. />
  91. </div>
  92. <div className="gf-form gf-form--grow">
  93. <label className="gf-form-label gf-form-label--grow">
  94. <a ng-click="ctrl.target.showAggregationOptions = !ctrl.target.showAggregationOptions">
  95. <i className="fa fa-caret-down" ng-show="ctrl.target.showAggregationOptions" />
  96. <i className="fa fa-caret-right" ng-hide="ctrl.target.showAggregationOptions" /> Advanced Options
  97. </a>
  98. </label>
  99. </div>
  100. </div>
  101. </React.Fragment>
  102. );
  103. }
  104. }