Aggregations.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. 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 = getAggregationOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(a => ({
  35. ...a,
  36. label: a.text,
  37. }));
  38. }
  39. this.setState({ aggOptions });
  40. }
  41. handleToggleDisplayAdvanced() {
  42. this.setState(state => ({
  43. displayAdvancedOptions: !state.displayAdvancedOptions,
  44. }));
  45. }
  46. render() {
  47. const { displayAdvancedOptions, aggOptions } = this.state;
  48. const { templateSrv, onChange, crossSeriesReducer } = this.props;
  49. return (
  50. <React.Fragment>
  51. <div className="gf-form-inline">
  52. <div className="gf-form">
  53. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  54. <StackdriverPicker
  55. onChange={value => onChange(value)}
  56. selected={crossSeriesReducer}
  57. templateVariables={templateSrv.variables}
  58. options={aggOptions}
  59. searchable={true}
  60. placeholder="Select Aggregation"
  61. className="width-15"
  62. groupName="Aggregations"
  63. />
  64. </div>
  65. <div className="gf-form gf-form--grow">
  66. <label className="gf-form-label gf-form-label--grow">
  67. <a onClick={() => this.handleToggleDisplayAdvanced()}>
  68. {displayAdvancedOptions ? (
  69. <i className="fa fa-caret-down" ng-show="ctrl.target.showAggregationOptions" />
  70. ) : (
  71. <React.Fragment>
  72. <i className="fa fa-caret-right" ng-hide="ctrl.target.showAggregationOptions" /> Advanced Options
  73. </React.Fragment>
  74. )}
  75. </a>
  76. </label>
  77. </div>
  78. </div>
  79. {this.props.children(this.state.displayAdvancedOptions)}
  80. </React.Fragment>
  81. );
  82. }
  83. }