usageStatsPerMin.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import styled from '@emotion/styled';
  2. import AsyncComponent from 'sentry/components/asyncComponent';
  3. import {t} from 'sentry/locale';
  4. import {DataCategoryInfo, Organization, Outcome} from 'sentry/types';
  5. import {UsageSeries} from './types';
  6. import {formatUsageWithUnits, getFormatUsageOptions} from './utils';
  7. type Props = {
  8. dataCategory: DataCategoryInfo['plural'];
  9. organization: Organization;
  10. projectIds: number[];
  11. } & AsyncComponent['props'];
  12. type State = {
  13. orgStats: UsageSeries | undefined;
  14. } & AsyncComponent['state'];
  15. /**
  16. * Making 1 extra API call to display this number isn't very efficient.
  17. * The other approach would be to fetch the data in UsageStatsOrg with 1min
  18. * interval and roll it up on the frontend, but that (1) adds unnecessary
  19. * complexity as it's gnarly to fetch + rollup 90 days of 1min intervals,
  20. * (3) API resultset has a limit of 1000, so 90 days of 1min would not work.
  21. *
  22. * We're going with this approach for simplicity sake. By keeping the range
  23. * as small as possible, this call is quite fast.
  24. */
  25. class UsageStatsPerMin extends AsyncComponent<Props, State> {
  26. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  27. return [['orgStats', this.endpointPath, {query: this.endpointQuery}]];
  28. }
  29. get endpointPath() {
  30. const {organization} = this.props;
  31. return `/organizations/${organization.slug}/stats_v2/`;
  32. }
  33. get endpointQuery() {
  34. const {projectIds} = this.props;
  35. return {
  36. statsPeriod: '5m', // Any value <1h will return current hour's data
  37. interval: '1m',
  38. groupBy: ['category', 'outcome'],
  39. project: projectIds,
  40. field: ['sum(quantity)'],
  41. };
  42. }
  43. get minuteData(): string | undefined {
  44. const {dataCategory} = this.props;
  45. const {loading, error, orgStats} = this.state;
  46. if (loading || error || !orgStats || orgStats.intervals.length === 0) {
  47. return undefined;
  48. }
  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. renderComponent() {
  69. if (!this.minuteData) {
  70. return null;
  71. }
  72. return (
  73. <Wrapper>
  74. {this.minuteData} {t('in last min')}
  75. </Wrapper>
  76. );
  77. }
  78. }
  79. export default UsageStatsPerMin;
  80. const Wrapper = styled('div')`
  81. display: inline-block;
  82. color: ${p => p.theme.success};
  83. font-size: ${p => p.theme.fontSizeMedium};
  84. `;