internalStatChart.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React from 'react';
  2. import {Client} from 'app/api';
  3. import MiniBarChart from 'app/components/charts/miniBarChart';
  4. import LoadingError from 'app/components/loadingError';
  5. import LoadingIndicator from 'app/components/loadingIndicator';
  6. import withApi from 'app/utils/withApi';
  7. type Props = {
  8. api: Client;
  9. since: number;
  10. resolution: string;
  11. stat: string;
  12. label: string;
  13. height?: number;
  14. };
  15. type State = {
  16. error: boolean;
  17. loading: boolean;
  18. data: [number, number][] | null;
  19. };
  20. class InternalStatChart extends React.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. } else if (error) {
  65. return <LoadingError onRetry={this.fetchData} />;
  66. }
  67. const series = {
  68. seriesName: label,
  69. data:
  70. data?.map(([timestamp, value]) => ({
  71. name: timestamp * 1000,
  72. value,
  73. })) ?? [],
  74. };
  75. return (
  76. <MiniBarChart
  77. height={height ?? 150}
  78. series={[series]}
  79. isGroupedByDate
  80. showTimeInTooltip
  81. labelYAxisExtents
  82. />
  83. );
  84. }
  85. }
  86. export default withApi(InternalStatChart);