Aggregations.tsx 3.9 KB

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