Aggregations.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import React from 'react';
  2. import _ from 'lodash';
  3. import { getAggregationOptionsByMetric } from '../functions';
  4. import { StackdriverPicker } from './StackdriverPicker';
  5. export interface Props {
  6. onChange: (metricDescriptor) => void;
  7. templateSrv: any;
  8. metricDescriptor: {
  9. valueType: string;
  10. metricKind: string;
  11. };
  12. crossSeriesReducer: string;
  13. groupBys: string[];
  14. children?: (renderProps: any) => JSX.Element;
  15. }
  16. interface State {
  17. aggOptions: any[];
  18. displayAdvancedOptions: boolean;
  19. }
  20. export class Aggregations extends React.Component<Props, State> {
  21. state: State = {
  22. aggOptions: [],
  23. displayAdvancedOptions: false,
  24. };
  25. constructor(props) {
  26. super(props);
  27. }
  28. componentDidMount() {
  29. if (this.props.metricDescriptor !== null) {
  30. this.setAggOptions(this.props);
  31. }
  32. }
  33. componentWillReceiveProps(nextProps: Props) {
  34. if (nextProps.metricDescriptor !== null) {
  35. this.setAggOptions(nextProps);
  36. }
  37. }
  38. setAggOptions({ metricDescriptor, crossSeriesReducer, groupBys, templateSrv }) {
  39. let aggregations = getAggregationOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(
  40. a => ({
  41. ...a,
  42. label: a.text,
  43. })
  44. );
  45. if (aggregations.length > 0 && !aggregations.find(o => o.value === templateSrv.replace(crossSeriesReducer))) {
  46. this.deselectAggregationOption('REDUCE_NONE');
  47. }
  48. if (groupBys.length > 0) {
  49. aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
  50. if (crossSeriesReducer === 'REDUCE_NONE') {
  51. this.deselectAggregationOption('REDUCE_NONE');
  52. }
  53. }
  54. this.setState({ aggOptions: aggregations });
  55. }
  56. deselectAggregationOption(notValidOptionValue: string) {
  57. const aggregations = getAggregationOptionsByMetric(
  58. this.props.metricDescriptor.valueType,
  59. this.props.metricDescriptor.metricKind
  60. );
  61. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  62. this.props.onChange(newValue ? newValue.value : '');
  63. }
  64. handleToggleDisplayAdvanced() {
  65. this.setState(state => ({
  66. displayAdvancedOptions: !state.displayAdvancedOptions,
  67. }));
  68. }
  69. render() {
  70. const { aggOptions, displayAdvancedOptions } = this.state;
  71. const { templateSrv, onChange, crossSeriesReducer } = this.props;
  72. return (
  73. <React.Fragment>
  74. <div className="gf-form-inline">
  75. <div className="gf-form">
  76. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  77. <StackdriverPicker
  78. onChange={value => onChange(value)}
  79. selected={crossSeriesReducer}
  80. templateVariables={templateSrv.variables}
  81. options={aggOptions}
  82. searchable={true}
  83. placeholder="Select Aggregation"
  84. className="width-15"
  85. groupName="Aggregations"
  86. />
  87. </div>
  88. <div className="gf-form gf-form--grow">
  89. <label className="gf-form-label gf-form-label--grow">
  90. <a onClick={() => this.handleToggleDisplayAdvanced()}>
  91. {displayAdvancedOptions ? (
  92. <i className="fa fa-caret-down" ng-show="ctrl.target.showAggregationOptions" />
  93. ) : (
  94. <React.Fragment>
  95. <i className="fa fa-caret-right" ng-hide="ctrl.target.showAggregationOptions" /> Advanced Options
  96. </React.Fragment>
  97. )}
  98. </a>
  99. </label>
  100. </div>
  101. </div>
  102. {this.props.children(this.state.displayAdvancedOptions)}
  103. </React.Fragment>
  104. );
  105. }
  106. }