ServerStats.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { StoreState } from 'app/types';
  5. import { getNavModel } from 'app/core/selectors/navModel';
  6. import { getServerStats, ServerStat } from './state/apis';
  7. import Page from 'app/core/components/Page/Page';
  8. import { NavModel } from '@grafana/ui';
  9. interface Props {
  10. navModel: NavModel;
  11. getServerStats: () => Promise<ServerStat[]>;
  12. }
  13. interface State {
  14. stats: ServerStat[];
  15. isLoading: boolean;
  16. }
  17. export class ServerStats extends PureComponent<Props, State> {
  18. constructor(props: Props) {
  19. super(props);
  20. this.state = {
  21. stats: [],
  22. isLoading: false,
  23. };
  24. }
  25. async componentDidMount() {
  26. try {
  27. this.setState({ isLoading: true });
  28. const stats = await this.props.getServerStats();
  29. this.setState({ stats, isLoading: false });
  30. } catch (error) {
  31. console.error(error);
  32. }
  33. }
  34. render() {
  35. const { navModel } = this.props;
  36. const { stats, isLoading } = this.state;
  37. return (
  38. <Page navModel={navModel}>
  39. <Page.Contents isLoading={isLoading}>
  40. <table className="filter-table form-inline">
  41. <thead>
  42. <tr>
  43. <th>Name</th>
  44. <th>Value</th>
  45. </tr>
  46. </thead>
  47. <tbody>{stats.map(StatItem)}</tbody>
  48. </table>
  49. </Page.Contents>
  50. </Page>
  51. );
  52. }
  53. }
  54. function StatItem(stat: ServerStat) {
  55. return (
  56. <tr key={stat.name}>
  57. <td>{stat.name}</td>
  58. <td>{stat.value}</td>
  59. </tr>
  60. );
  61. }
  62. const mapStateToProps = (state: StoreState) => ({
  63. navModel: getNavModel(state.navIndex, 'server-stats'),
  64. getServerStats: getServerStats,
  65. });
  66. export default hot(module)(connect(mapStateToProps)(ServerStats));