Aggregations.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React from 'react';
  2. import _ from 'lodash';
  3. import { MetricSelect } from 'app/core/components/Select/MetricSelect';
  4. import { getAggregationOptionsByMetric } from '../functions';
  5. import { TemplateSrv } from 'app/features/templating/template_srv';
  6. export interface Props {
  7. onChange: (metricDescriptor) => void;
  8. templateSrv: TemplateSrv;
  9. metricDescriptor: {
  10. valueType: string;
  11. metricKind: string;
  12. };
  13. crossSeriesReducer: string;
  14. groupBys: string[];
  15. children?: (renderProps: any) => JSX.Element;
  16. }
  17. export interface State {
  18. aggOptions: any[];
  19. displayAdvancedOptions: boolean;
  20. }
  21. export class Aggregations extends React.Component<Props, State> {
  22. state: State = {
  23. aggOptions: [],
  24. displayAdvancedOptions: false,
  25. };
  26. componentDidMount() {
  27. this.setAggOptions(this.props);
  28. }
  29. componentWillReceiveProps(nextProps: Props) {
  30. this.setAggOptions(nextProps);
  31. }
  32. setAggOptions({ metricDescriptor }: Props) {
  33. let aggOptions = [];
  34. if (metricDescriptor) {
  35. aggOptions = [
  36. {
  37. label: 'Aggregations',
  38. expanded: true,
  39. options: getAggregationOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(a => ({
  40. ...a,
  41. label: a.text,
  42. })),
  43. },
  44. ];
  45. }
  46. this.setState({ aggOptions });
  47. }
  48. onToggleDisplayAdvanced = () => {
  49. this.setState(state => ({
  50. displayAdvancedOptions: !state.displayAdvancedOptions,
  51. }));
  52. };
  53. render() {
  54. const { displayAdvancedOptions, aggOptions } = this.state;
  55. const { templateSrv, onChange, crossSeriesReducer } = this.props;
  56. return (
  57. <>
  58. <div className="gf-form-inline">
  59. <div className="gf-form">
  60. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  61. <MetricSelect
  62. onChange={onChange}
  63. value={crossSeriesReducer}
  64. variables={templateSrv.variables}
  65. options={aggOptions}
  66. placeholder="Select Aggregation"
  67. className="width-15"
  68. />
  69. </div>
  70. <div className="gf-form gf-form--grow">
  71. <label className="gf-form-label gf-form-label--grow">
  72. <a onClick={this.onToggleDisplayAdvanced}>
  73. <>
  74. <i className={`fa fa-caret-${displayAdvancedOptions ? 'down' : 'right'}`} /> Advanced Options
  75. </>
  76. </a>
  77. </label>
  78. </div>
  79. </div>
  80. {this.props.children(this.state.displayAdvancedOptions)}
  81. </>
  82. );
  83. }
  84. }