usageStatsPerMin.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import styled from '@emotion/styled';
  2. import {t} from 'sentry/locale';
  3. import type {DataCategoryInfo, Organization} from 'sentry/types';
  4. import {Outcome} from 'sentry/types';
  5. import {useApiQuery} from 'sentry/utils/queryClient';
  6. import type {UsageSeries} from './types';
  7. import {formatUsageWithUnits, getFormatUsageOptions} from './utils';
  8. type Props = {
  9. dataCategory: DataCategoryInfo['plural'];
  10. organization: Organization;
  11. projectIds: number[];
  12. };
  13. /**
  14. * Making 1 extra API call to display this number isn't very efficient.
  15. * The other approach would be to fetch the data in UsageStatsOrg with 1min
  16. * interval and roll it up on the frontend, but that (1) adds unnecessary
  17. * complexity as it's gnarly to fetch + rollup 90 days of 1min intervals,
  18. * (3) API resultset has a limit of 1000, so 90 days of 1min would not work.
  19. *
  20. * We're going with this approach for simplicity sake. By keeping the range
  21. * as small as possible, this call is quite fast.
  22. */
  23. function UsageStatsPerMin({dataCategory, organization, projectIds}: Props) {
  24. const {
  25. data: orgStats,
  26. isLoading,
  27. isError,
  28. } = useApiQuery<UsageSeries>(
  29. [
  30. `/organizations/${organization.slug}/stats_v2/`,
  31. {
  32. query: {
  33. statsPeriod: '5m', // Any value <1h will return current hour's data
  34. interval: '1m',
  35. groupBy: ['category', 'outcome'],
  36. project: projectIds,
  37. field: ['sum(quantity)'],
  38. },
  39. },
  40. ],
  41. {
  42. staleTime: 0,
  43. }
  44. );
  45. if (isLoading || isError || !orgStats || orgStats.intervals.length === 0) {
  46. return null;
  47. }
  48. const minuteData = (): string | undefined => {
  49. // The last minute in the series is still "in progress"
  50. // Read data from 2nd last element for the latest complete minute
  51. const {intervals, groups} = orgStats;
  52. const lastMin = Math.max(intervals.length - 2, 0);
  53. const eventsLastMin = groups.reduce((count, group) => {
  54. const {outcome, category} = group.by;
  55. // HACK: The backend enum are singular, but the frontend enums are plural
  56. if (!dataCategory.includes(`${category}`) || outcome !== Outcome.ACCEPTED) {
  57. return count;
  58. }
  59. count += group.series['sum(quantity)'][lastMin];
  60. return count;
  61. }, 0);
  62. return formatUsageWithUnits(
  63. eventsLastMin,
  64. dataCategory,
  65. getFormatUsageOptions(dataCategory)
  66. );
  67. };
  68. return (
  69. <Wrapper>
  70. {minuteData()} {t('in last min')}
  71. </Wrapper>
  72. );
  73. }
  74. export default UsageStatsPerMin;
  75. const Wrapper = styled('div')`
  76. display: inline-block;
  77. color: ${p => p.theme.success};
  78. font-size: ${p => p.theme.fontSizeMedium};
  79. `;