Aggregations.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. alignmentPeriod: string;
  15. perSeriesAligner: string;
  16. groupBys: string[];
  17. };
  18. children?: (renderProps: any) => JSX.Element;
  19. }
  20. interface State {
  21. alignmentPeriods: any[];
  22. alignOptions: any[];
  23. aggOptions: any[];
  24. displayAdvancedOptions: boolean;
  25. }
  26. export class Aggregations extends React.Component<Props, State> {
  27. state: State = {
  28. alignmentPeriods: [],
  29. alignOptions: [],
  30. aggOptions: [],
  31. displayAdvancedOptions: false,
  32. };
  33. constructor(props) {
  34. super(props);
  35. }
  36. componentDidMount() {
  37. if (this.props.metricDescriptor !== null) {
  38. this.setAggOptions(this.props);
  39. }
  40. }
  41. componentWillReceiveProps(nextProps: Props) {
  42. if (nextProps.metricDescriptor !== null) {
  43. this.setAggOptions(nextProps);
  44. }
  45. }
  46. setAggOptions({ metricDescriptor, aggregation }) {
  47. const { templateSrv } = this.props;
  48. let aggregations = getAggregationOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(
  49. a => ({
  50. ...a,
  51. label: a.text,
  52. })
  53. );
  54. if (
  55. aggregations.length > 0 &&
  56. !aggregations.find(o => o.value === templateSrv.replace(aggregation.crossSeriesReducer))
  57. ) {
  58. this.deselectAggregationOption('REDUCE_NONE');
  59. }
  60. if (aggregation.groupBys.length > 0) {
  61. aggregations = aggregations.filter(o => o.value !== 'REDUCE_NONE');
  62. this.deselectAggregationOption('REDUCE_NONE');
  63. }
  64. this.setState({ aggOptions: aggregations });
  65. }
  66. deselectAggregationOption(notValidOptionValue: string) {
  67. const aggregations = getAggregationOptionsByMetric(
  68. this.props.metricDescriptor.valueType,
  69. this.props.metricDescriptor.metricKind
  70. );
  71. const newValue = aggregations.find(o => o.value !== notValidOptionValue);
  72. this.handleAggregationChange(newValue ? newValue.value : '');
  73. }
  74. handleAggregationChange(value) {
  75. this.props.onChange(value);
  76. }
  77. handleToggleDisplayAdvanced() {
  78. this.setState(state => ({
  79. displayAdvancedOptions: !state.displayAdvancedOptions,
  80. }));
  81. }
  82. render() {
  83. const { aggOptions, displayAdvancedOptions } = this.state;
  84. const { aggregation, templateSrv } = this.props;
  85. return (
  86. <React.Fragment>
  87. <div className="gf-form-inline">
  88. <div className="gf-form">
  89. <label className="gf-form-label query-keyword width-9">Aggregation</label>
  90. <StackdriverPicker
  91. onChange={value => this.handleAggregationChange(value)}
  92. selected={aggregation.crossSeriesReducer}
  93. templateVariables={templateSrv.variables}
  94. options={aggOptions}
  95. searchable={true}
  96. placeholder="Select Aggregation"
  97. className="width-15"
  98. groupName="Aggregations"
  99. />
  100. </div>
  101. <div className="gf-form gf-form--grow">
  102. <label className="gf-form-label gf-form-label--grow">
  103. <a onClick={() => this.handleToggleDisplayAdvanced()}>
  104. {displayAdvancedOptions ? (
  105. <i className="fa fa-caret-down" ng-show="ctrl.target.showAggregationOptions" />
  106. ) : (
  107. <React.Fragment>
  108. <i className="fa fa-caret-right" ng-hide="ctrl.target.showAggregationOptions" /> Advanced Options
  109. </React.Fragment>
  110. )}
  111. </a>
  112. </label>
  113. </div>
  114. </div>
  115. {this.props.children(this.state.displayAdvancedOptions)}
  116. </React.Fragment>
  117. );
  118. }
  119. }