Aggregations.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 }) {
  41. const { templateSrv } = this.props;
  42. let aggregations = getAggregationOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(
  43. a => ({
  44. ...a,
  45. label: a.text,
  46. })
  47. );
  48. if (
  49. aggregations.length > 0 &&
  50. !aggregations.find(o => o.value === templateSrv.replace(aggregation.crossSeriesReducer))
  51. ) {
  52. this.deselectAggregationOption('REDUCE_NONE');
  53. }
  54. if (aggregation.groupBys.length > 0) {
  55. aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
  56. this.deselectAggregationOption('REDUCE_NONE');
  57. }
  58. this.setState({ aggOptions: aggregations });
  59. }
  60. deselectAggregationOption(notValidOptionValue: string) {
  61. const aggregations = getAggregationOptionsByMetric(
  62. this.props.metricDescriptor.valueType,
  63. this.props.metricDescriptor.metricKind
  64. );
  65. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  66. this.props.onChange(newValue ? newValue.value : '');
  67. }
  68. handleToggleDisplayAdvanced() {
  69. this.setState(state => ({
  70. displayAdvancedOptions: !state.displayAdvancedOptions,
  71. }));
  72. }
  73. render() {
  74. const { aggOptions, displayAdvancedOptions } = this.state;
  75. const { aggregation, templateSrv, onChange } = this.props;
  76. return (
  77. <React.Fragment>
  78. <div className="gf-form-inline">
  79. <div className="gf-form">
  80. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  81. <StackdriverPicker
  82. onChange={value => onChange(value)}
  83. selected={aggregation.crossSeriesReducer}
  84. templateVariables={templateSrv.variables}
  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 onClick={() => this.handleToggleDisplayAdvanced()}>
  95. {displayAdvancedOptions ? (
  96. <i className="fa fa-caret-down" ng-show="ctrl.target.showAggregationOptions" />
  97. ) : (
  98. <React.Fragment>
  99. <i className="fa fa-caret-right" ng-hide="ctrl.target.showAggregationOptions" /> Advanced Options
  100. </React.Fragment>
  101. )}
  102. </a>
  103. </label>
  104. </div>
  105. </div>
  106. {this.props.children(this.state.displayAdvancedOptions)}
  107. </React.Fragment>
  108. );
  109. }
  110. }