monitorStats.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import MiniBarChart from 'sentry/components/charts/miniBarChart';
  2. import EmptyMessage from 'sentry/components/emptyMessage';
  3. import {Panel, PanelBody} from 'sentry/components/panels';
  4. import {t} from 'sentry/locale';
  5. import {SeriesDataUnit} from 'sentry/types/echarts';
  6. import theme from 'sentry/utils/theme';
  7. import useApiRequests from 'sentry/utils/useApiRequests';
  8. import {Monitor, MonitorStat} from './types';
  9. type Props = {
  10. monitor: Monitor;
  11. };
  12. type State = {
  13. stats: MonitorStat[] | null;
  14. };
  15. const MonitorStats = ({monitor}: Props) => {
  16. const until = Math.floor(new Date().getTime() / 1000);
  17. const since = until - 3600 * 24 * 30;
  18. const {data, renderComponent} = useApiRequests<State>({
  19. endpoints: [
  20. [
  21. 'stats',
  22. `/monitors/${monitor.id}/stats/`,
  23. {
  24. query: {
  25. since: since.toString(),
  26. until: until.toString(),
  27. resolution: '1d',
  28. },
  29. },
  30. ],
  31. ],
  32. });
  33. let emptyStats = true;
  34. const success = {
  35. seriesName: t('Successful'),
  36. data: [] as SeriesDataUnit[],
  37. };
  38. const failed = {
  39. seriesName: t('Failed'),
  40. data: [] as SeriesDataUnit[],
  41. };
  42. data.stats?.forEach(p => {
  43. if (p.ok || p.error) {
  44. emptyStats = false;
  45. }
  46. const timestamp = p.ts * 1000;
  47. success.data.push({name: timestamp, value: p.ok});
  48. failed.data.push({name: timestamp, value: p.error});
  49. });
  50. const colors = [theme.green300, theme.red300];
  51. return renderComponent(
  52. <Panel>
  53. <PanelBody withPadding>
  54. {!emptyStats ? (
  55. <MiniBarChart
  56. isGroupedByDate
  57. showTimeInTooltip
  58. labelYAxisExtents
  59. stacked
  60. colors={colors}
  61. height={150}
  62. series={[success, failed]}
  63. />
  64. ) : (
  65. <EmptyMessage
  66. title={t('Nothing recorded in the last 30 days.')}
  67. description={t('All check-ins for this monitor.')}
  68. />
  69. )}
  70. </PanelBody>
  71. </Panel>
  72. );
  73. };
  74. export default MonitorStats;