Aggregations.tsx 3.6 KB

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