monitorStats.tsx 2.2 KB

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