internalStatChart.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {Component} from 'react';
  2. import {Client} from 'sentry/api';
  3. import MiniBarChart from 'sentry/components/charts/miniBarChart';
  4. import LoadingError from 'sentry/components/loadingError';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import withApi from 'sentry/utils/withApi';
  7. type Props = {
  8. api: Client;
  9. label: string;
  10. resolution: string;
  11. since: number;
  12. stat: string;
  13. height?: number;
  14. };
  15. type State = {
  16. data: [number, number][] | null;
  17. error: boolean;
  18. loading: boolean;
  19. };
  20. class InternalStatChart extends Component<Props, State> {
  21. state: State = {
  22. error: false,
  23. loading: true,
  24. data: null,
  25. };
  26. componentDidMount() {
  27. this.fetchData();
  28. }
  29. shouldComponentUpdate(_nextProps: Props, nextState: State) {
  30. return this.state.loading !== nextState.loading;
  31. }
  32. componentDidUpdate(prevProps: Props) {
  33. if (
  34. prevProps.since !== this.props.since ||
  35. prevProps.stat !== this.props.stat ||
  36. prevProps.resolution !== this.props.resolution
  37. ) {
  38. this.fetchData();
  39. }
  40. }
  41. fetchData = () => {
  42. this.setState({loading: true});
  43. this.props.api.request('/internal/stats/', {
  44. method: 'GET',
  45. data: {
  46. since: this.props.since,
  47. resolution: this.props.resolution,
  48. key: this.props.stat,
  49. },
  50. success: data =>
  51. this.setState({
  52. data,
  53. loading: false,
  54. error: false,
  55. }),
  56. error: () => this.setState({error: true, loading: false}),
  57. });
  58. };
  59. render() {
  60. const {loading, error, data} = this.state;
  61. const {label, height} = this.props;
  62. if (loading) {
  63. return <LoadingIndicator />;
  64. }
  65. if (error) {
  66. return <LoadingError onRetry={this.fetchData} />;
  67. }
  68. const series = {
  69. seriesName: label,
  70. data:
  71. data?.map(([timestamp, value]) => ({
  72. name: timestamp * 1000,
  73. value,
  74. })) ?? [],
  75. };
  76. return (
  77. <MiniBarChart
  78. height={height ?? 150}
  79. series={[series]}
  80. isGroupedByDate
  81. showTimeInTooltip
  82. labelYAxisExtents
  83. />
  84. );
  85. }
  86. }
  87. export default withApi(InternalStatChart);