Alignments.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 { getAlignmentOptionsByMetric } 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. perSeriesAligner: string;
  17. }
  18. interface State {
  19. alignOptions: any[];
  20. }
  21. export class Alignments extends React.Component<Props, State> {
  22. state: State = {
  23. alignOptions: [],
  24. };
  25. constructor(props) {
  26. super(props);
  27. }
  28. componentDidMount() {
  29. if (this.props.metricDescriptor !== null) {
  30. this.setAlignOptions(this.props);
  31. }
  32. }
  33. componentWillReceiveProps(nextProps: Props) {
  34. if (nextProps.metricDescriptor !== null) {
  35. this.setAlignOptions(nextProps);
  36. }
  37. }
  38. setAlignOptions({ metricDescriptor, perSeriesAligner, templateSrv, onChange }) {
  39. const alignOptions = getAlignmentOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(
  40. option => ({
  41. ...option,
  42. label: option.text,
  43. })
  44. );
  45. if (!alignOptions.some(o => o.value === templateSrv.replace(perSeriesAligner))) {
  46. onChange(alignOptions.length > 0 ? alignOptions[0].value : '');
  47. }
  48. this.setState({ alignOptions });
  49. }
  50. render() {
  51. const { alignOptions } = this.state;
  52. const { perSeriesAligner, templateSrv, onChange } = this.props;
  53. return (
  54. <React.Fragment>
  55. <div className="gf-form-group">
  56. <div className="gf-form offset-width-9">
  57. <label className="gf-form-label query-keyword width-15">Aligner</label>
  58. <StackdriverPicker
  59. onChange={value => onChange(value)}
  60. selected={perSeriesAligner}
  61. templateVariables={templateSrv.variables}
  62. options={alignOptions}
  63. searchable={true}
  64. placeholder="Select Alignment"
  65. className="width-15"
  66. groupName="Alignment Options"
  67. />
  68. </div>
  69. </div>
  70. </React.Fragment>
  71. );
  72. }
  73. }