usageStatsPerMin.tsx 2.8 KB

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