Aggregations.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. export 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. onToggleDisplayAdvanced() {
  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. <>
  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.onToggleDisplayAdvanced()}>
  72. <>
  73. <i className={`fa fa-caret-${displayAdvancedOptions ? 'down' : 'right'}`} /> Advanced Options
  74. </>
  75. </a>
  76. </label>
  77. </div>
  78. </div>
  79. {this.props.children(this.state.displayAdvancedOptions)}
  80. </>
  81. );
  82. }
  83. }