Aggregations.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React from 'react';
  2. import _ from 'lodash';
  3. import { MetricSelect } from 'app/core/components/Select/MetricSelect';
  4. import { getAggregationOptionsByMetric } from '../functions';
  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. componentDidMount() {
  26. this.setAggOptions(this.props);
  27. }
  28. componentWillReceiveProps(nextProps: Props) {
  29. this.setAggOptions(nextProps);
  30. }
  31. setAggOptions({ metricDescriptor }: Props) {
  32. let aggOptions = [];
  33. if (metricDescriptor) {
  34. aggOptions = [
  35. {
  36. label: 'Aggregations',
  37. expanded: true,
  38. options: getAggregationOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(a => ({
  39. ...a,
  40. label: a.text,
  41. })),
  42. },
  43. ];
  44. }
  45. this.setState({ aggOptions });
  46. }
  47. handleToggleDisplayAdvanced() {
  48. this.setState(state => ({
  49. displayAdvancedOptions: !state.displayAdvancedOptions,
  50. }));
  51. }
  52. render() {
  53. const { displayAdvancedOptions, aggOptions } = this.state;
  54. const { templateSrv, onChange, crossSeriesReducer } = this.props;
  55. return (
  56. <React.Fragment>
  57. <div className="gf-form-inline">
  58. <div className="gf-form">
  59. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  60. <MetricSelect
  61. onChange={value => onChange(value)}
  62. value={crossSeriesReducer}
  63. variables={templateSrv.variables}
  64. options={aggOptions}
  65. placeholder="Select Aggregation"
  66. className="width-15"
  67. />
  68. </div>
  69. <div className="gf-form gf-form--grow">
  70. <label className="gf-form-label gf-form-label--grow">
  71. <a onClick={() => this.handleToggleDisplayAdvanced()}>
  72. {displayAdvancedOptions ? (
  73. <i className="fa fa-caret-down" ng-show="ctrl.target.showAggregationOptions" />
  74. ) : (
  75. <React.Fragment>
  76. <i className="fa fa-caret-right" ng-hide="ctrl.target.showAggregationOptions" /> Advanced Options
  77. </React.Fragment>
  78. )}
  79. </a>
  80. </label>
  81. </div>
  82. </div>
  83. {this.props.children(this.state.displayAdvancedOptions)}
  84. </React.Fragment>
  85. );
  86. }
  87. }