usageTable.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {updateProjects} from 'sentry/actionCreators/pageFilters';
  4. import {Button, LinkButton} from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import ErrorPanel from 'sentry/components/charts/errorPanel';
  7. import EmptyMessage from 'sentry/components/emptyMessage';
  8. import IdBadge from 'sentry/components/idBadge';
  9. import ExternalLink from 'sentry/components/links/externalLink';
  10. import Link from 'sentry/components/links/link';
  11. import Panel from 'sentry/components/panels/panel';
  12. import {PanelTable} from 'sentry/components/panels/panelTable';
  13. import {IconGraph, IconSettings, IconWarning} from 'sentry/icons';
  14. import {t, tct} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {DataCategoryInfo} from 'sentry/types/core';
  17. import type {WithRouterProps} from 'sentry/types/legacyReactRouter';
  18. import type {Project} from 'sentry/types/project';
  19. // eslint-disable-next-line no-restricted-imports
  20. import withSentryRouter from 'sentry/utils/withSentryRouter';
  21. import {formatUsageWithUnits, getFormatUsageOptions} from './utils';
  22. const DOCS_URL = 'https://docs.sentry.io/product/accounts/membership/#restricting-access';
  23. type Props = {
  24. dataCategory: DataCategoryInfo;
  25. headers: React.ReactNode[];
  26. usageStats: TableStat[];
  27. errors?: Record<string, Error>;
  28. isEmpty?: boolean;
  29. isError?: boolean;
  30. isLoading?: boolean;
  31. showStoredOutcome?: boolean;
  32. } & WithRouterProps<{}, {}>;
  33. export type TableStat = {
  34. accepted: number;
  35. accepted_stored: number;
  36. filtered: number;
  37. invalid: number;
  38. project: Project;
  39. projectLink: string;
  40. projectSettingsLink: string;
  41. rate_limited: number;
  42. total: number;
  43. };
  44. class UsageTable extends Component<Props> {
  45. getErrorMessage = (errorMessage: any) => {
  46. if (errorMessage.projectStats.responseJSON.detail === 'No projects available') {
  47. return (
  48. <EmptyMessage
  49. icon={<IconWarning color="gray300" legacySize="48px" />}
  50. title={t(
  51. "You don't have access to any projects, or your organization has no projects."
  52. )}
  53. description={tct('Learn more about [link:Project Access]', {
  54. link: <ExternalLink href={DOCS_URL} />,
  55. })}
  56. />
  57. );
  58. }
  59. return <IconWarning color="gray300" legacySize="48px" />;
  60. };
  61. loadProject(projectId: number) {
  62. updateProjects([projectId], this.props.router, {
  63. save: true,
  64. environments: [], // Clear environments when switching projects
  65. });
  66. window.scrollTo({top: 0, left: 0, behavior: 'smooth'});
  67. }
  68. renderTableRow(stat: TableStat & {project: Project}) {
  69. const {dataCategory, showStoredOutcome} = this.props;
  70. const {project, total, accepted, accepted_stored, filtered, invalid, rate_limited} =
  71. stat;
  72. return [
  73. <CellProject key={0}>
  74. <Link to={stat.projectLink}>
  75. <StyledIdBadge
  76. avatarSize={16}
  77. disableLink
  78. hideOverflow
  79. project={project}
  80. displayName={project.slug}
  81. />
  82. </Link>
  83. </CellProject>,
  84. <CellStat key={1}>
  85. {formatUsageWithUnits(
  86. total,
  87. dataCategory.plural,
  88. getFormatUsageOptions(dataCategory.plural)
  89. )}
  90. </CellStat>,
  91. <CellStat key={2}>
  92. {formatUsageWithUnits(
  93. accepted,
  94. dataCategory.plural,
  95. getFormatUsageOptions(dataCategory.plural)
  96. )}
  97. {showStoredOutcome && (
  98. <SubText>
  99. {`(${formatUsageWithUnits(
  100. accepted_stored,
  101. dataCategory.plural,
  102. getFormatUsageOptions(dataCategory.plural)
  103. )})`}
  104. </SubText>
  105. )}
  106. </CellStat>,
  107. <CellStat key={3}>
  108. {formatUsageWithUnits(
  109. filtered,
  110. dataCategory.plural,
  111. getFormatUsageOptions(dataCategory.plural)
  112. )}
  113. </CellStat>,
  114. <CellStat key={4}>
  115. {formatUsageWithUnits(
  116. rate_limited,
  117. dataCategory.plural,
  118. getFormatUsageOptions(dataCategory.plural)
  119. )}
  120. </CellStat>,
  121. <CellStat key={5}>
  122. {formatUsageWithUnits(
  123. invalid,
  124. dataCategory.plural,
  125. getFormatUsageOptions(dataCategory.plural)
  126. )}
  127. </CellStat>,
  128. <CellStat key={6}>
  129. <ButtonBar gap={1}>
  130. <Button
  131. icon={<IconGraph type="bar" />}
  132. title="Go to project level stats"
  133. data-test-id={project.slug}
  134. size="xs"
  135. onClick={() => {
  136. this.loadProject(parseInt(stat.project.id, 10));
  137. }}
  138. >
  139. {t('View Stats')}
  140. </Button>
  141. <LinkButton
  142. icon={<IconSettings />}
  143. size="xs"
  144. aria-label={t('Project Settings')}
  145. title={t('Go to project settings')}
  146. to={stat.projectSettingsLink}
  147. />
  148. </ButtonBar>
  149. </CellStat>,
  150. ];
  151. }
  152. render() {
  153. const {isEmpty, isLoading, isError, errors, headers, usageStats} = this.props;
  154. if (isError) {
  155. return (
  156. <Panel>
  157. <ErrorPanel height="256px">{this.getErrorMessage(errors)}</ErrorPanel>
  158. </Panel>
  159. );
  160. }
  161. return (
  162. <StyledPanelTable isLoading={isLoading} isEmpty={isEmpty} headers={headers}>
  163. {usageStats.map(s => this.renderTableRow(s))}
  164. </StyledPanelTable>
  165. );
  166. }
  167. }
  168. export default withSentryRouter(UsageTable);
  169. const StyledPanelTable = styled(PanelTable)`
  170. grid-template-columns: repeat(7, auto);
  171. @media (min-width: ${p => p.theme.breakpoints.small}) {
  172. grid-template-columns: 1fr repeat(6, minmax(0, auto));
  173. }
  174. `;
  175. export const CellStat = styled('div')`
  176. display: flex;
  177. align-items: center;
  178. font-variant-numeric: tabular-nums;
  179. justify-content: right;
  180. `;
  181. export const CellProject = styled(CellStat)`
  182. justify-content: left;
  183. `;
  184. const StyledIdBadge = styled(IdBadge)`
  185. overflow: hidden;
  186. white-space: nowrap;
  187. flex-shrink: 1;
  188. `;
  189. const SubText = styled('span')`
  190. color: ${p => p.theme.subText};
  191. margin-left: ${space(0.5)};
  192. `;