usageStatsProjects.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {LocationDescriptorObject} from 'history';
  5. import isEqual from 'lodash/isEqual';
  6. import AsyncComponent from 'sentry/components/asyncComponent';
  7. import {DateTimeObject, getSeriesApiInterval} from 'sentry/components/charts/utils';
  8. import SortLink, {Alignments, Directions} from 'sentry/components/gridEditable/sortLink';
  9. import Pagination from 'sentry/components/pagination';
  10. import SearchBar from 'sentry/components/searchBar';
  11. import {DATA_CATEGORY_INFO, DEFAULT_STATS_PERIOD} from 'sentry/constants';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {DataCategoryInfo, Organization, Outcome, Project} from 'sentry/types';
  15. import withProjects from 'sentry/utils/withProjects';
  16. import {UsageSeries} from './types';
  17. import UsageTable, {CellProject, CellStat, TableStat} from './usageTable';
  18. type Props = {
  19. dataCategory: DataCategoryInfo['plural'];
  20. dataCategoryName: string;
  21. dataDatetime: DateTimeObject;
  22. getNextLocations: (project: Project) => Record<string, LocationDescriptorObject>;
  23. handleChangeState: (
  24. nextState: {
  25. cursor?: string;
  26. query?: string;
  27. sort?: string;
  28. },
  29. options?: {willUpdateRouter?: boolean}
  30. ) => LocationDescriptorObject;
  31. loadingProjects: boolean;
  32. organization: Organization;
  33. projectIds: number[];
  34. projects: Project[];
  35. tableCursor?: string;
  36. tableQuery?: string;
  37. tableSort?: string;
  38. } & AsyncComponent['props'];
  39. type State = {
  40. projectStats: UsageSeries | undefined;
  41. } & AsyncComponent['state'];
  42. export enum SortBy {
  43. PROJECT = 'project',
  44. TOTAL = 'total',
  45. ACCEPTED = 'accepted',
  46. FILTERED = 'filtered',
  47. DROPPED = 'dropped',
  48. INVALID = 'invalid',
  49. RATE_LIMITED = 'rate_limited',
  50. }
  51. class UsageStatsProjects extends AsyncComponent<Props, State> {
  52. static MAX_ROWS_USAGE_TABLE = 25;
  53. componentDidUpdate(prevProps: Props) {
  54. const {
  55. dataDatetime: prevDateTime,
  56. dataCategory: prevDataCategory,
  57. projectIds: prevProjectIds,
  58. } = prevProps;
  59. const {
  60. dataDatetime: currDateTime,
  61. dataCategory: currDataCategory,
  62. projectIds: currProjectIds,
  63. } = this.props;
  64. if (
  65. prevDateTime.start !== currDateTime.start ||
  66. prevDateTime.end !== currDateTime.end ||
  67. prevDateTime.period !== currDateTime.period ||
  68. prevDateTime.utc !== currDateTime.utc ||
  69. prevDataCategory !== currDataCategory ||
  70. !isEqual(prevProjectIds, currProjectIds)
  71. ) {
  72. this.reloadData();
  73. }
  74. }
  75. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  76. return [['projectStats', this.endpointPath, {query: this.endpointQuery}]];
  77. }
  78. get endpointPath() {
  79. const {organization} = this.props;
  80. return `/organizations/${organization.slug}/stats_v2/`;
  81. }
  82. get endpointQuery() {
  83. const {dataDatetime, dataCategory, projectIds} = this.props;
  84. const queryDatetime =
  85. dataDatetime.start && dataDatetime.end
  86. ? {
  87. start: dataDatetime.start,
  88. end: dataDatetime.end,
  89. utc: dataDatetime.utc,
  90. }
  91. : {
  92. statsPeriod: dataDatetime.period || DEFAULT_STATS_PERIOD,
  93. };
  94. // We do not need more granularity in the data so interval is '1d'
  95. return {
  96. ...queryDatetime,
  97. interval: getSeriesApiInterval(dataDatetime),
  98. groupBy: ['outcome', 'project'],
  99. field: ['sum(quantity)'],
  100. project: projectIds,
  101. category: dataCategory.slice(0, -1), // backend is singular
  102. };
  103. }
  104. get tableData() {
  105. const {projectStats} = this.state;
  106. return {
  107. headers: this.tableHeader,
  108. ...this.mapSeriesToTable(projectStats),
  109. };
  110. }
  111. get tableSort(): {
  112. direction: number;
  113. key: SortBy;
  114. } {
  115. const {tableSort} = this.props;
  116. if (!tableSort) {
  117. return {
  118. key: SortBy.TOTAL,
  119. direction: 1,
  120. };
  121. }
  122. let key: string = tableSort;
  123. let direction: number = -1;
  124. if (tableSort.charAt(0) === '-') {
  125. key = key.slice(1);
  126. direction = 1;
  127. }
  128. switch (key) {
  129. case SortBy.PROJECT:
  130. case SortBy.TOTAL:
  131. case SortBy.ACCEPTED:
  132. case SortBy.FILTERED:
  133. case SortBy.DROPPED:
  134. return {key, direction};
  135. default:
  136. return {key: SortBy.ACCEPTED, direction: -1};
  137. }
  138. }
  139. get tableCursor() {
  140. const {tableCursor} = this.props;
  141. const offset = Number(tableCursor?.split(':')[1]);
  142. return isNaN(offset) ? 0 : offset;
  143. }
  144. /**
  145. * OrganizationStatsEndpointV2 does not have any performance issues. We use
  146. * client-side pagination to limit the number of rows on the table so the
  147. * page doesn't scroll too deeply for organizations with a lot of projects
  148. */
  149. get pageLink() {
  150. const numRows = this.filteredProjects.length;
  151. const offset = this.tableCursor;
  152. const prevOffset = offset - UsageStatsProjects.MAX_ROWS_USAGE_TABLE;
  153. const nextOffset = offset + UsageStatsProjects.MAX_ROWS_USAGE_TABLE;
  154. return `<link>; rel="previous"; results="${prevOffset >= 0}"; cursor="0:${Math.max(
  155. 0,
  156. prevOffset
  157. )}:1", <link>; rel="next"; results="${
  158. nextOffset < numRows
  159. }"; cursor="0:${nextOffset}:0"`;
  160. }
  161. get projectSelectionFilter(): (p: Project) => boolean {
  162. const {projectIds} = this.props;
  163. const selectedProjects = new Set(projectIds.map(id => `${id}`));
  164. // If 'My Projects' or 'All Projects' are selected
  165. return selectedProjects.size === 0 || selectedProjects.has('-1')
  166. ? _p => true
  167. : p => selectedProjects.has(p.id);
  168. }
  169. /**
  170. * Filter projects if there's a query
  171. */
  172. get filteredProjects() {
  173. const {projects, tableQuery} = this.props;
  174. return tableQuery
  175. ? projects.filter(
  176. p =>
  177. p.slug.includes(tableQuery) && p.hasAccess && this.projectSelectionFilter(p)
  178. )
  179. : projects.filter(p => p.hasAccess && this.projectSelectionFilter(p));
  180. }
  181. get tableHeader() {
  182. const {key, direction} = this.tableSort;
  183. const getArrowDirection = (linkKey: SortBy): Directions => {
  184. if (linkKey !== key) {
  185. return undefined;
  186. }
  187. return direction > 0 ? 'desc' : 'asc';
  188. };
  189. return [
  190. {
  191. key: SortBy.PROJECT,
  192. title: t('Project'),
  193. align: 'left',
  194. direction: getArrowDirection(SortBy.PROJECT),
  195. onClick: () => this.handleChangeSort(SortBy.PROJECT),
  196. },
  197. {
  198. key: SortBy.TOTAL,
  199. title: t('Total'),
  200. align: 'right',
  201. direction: getArrowDirection(SortBy.TOTAL),
  202. onClick: () => this.handleChangeSort(SortBy.TOTAL),
  203. },
  204. {
  205. key: SortBy.ACCEPTED,
  206. title: t('Accepted'),
  207. align: 'right',
  208. direction: getArrowDirection(SortBy.ACCEPTED),
  209. onClick: () => this.handleChangeSort(SortBy.ACCEPTED),
  210. },
  211. {
  212. key: SortBy.FILTERED,
  213. title: t('Filtered'),
  214. align: 'right',
  215. direction: getArrowDirection(SortBy.FILTERED),
  216. onClick: () => this.handleChangeSort(SortBy.FILTERED),
  217. },
  218. {
  219. key: SortBy.DROPPED,
  220. title: t('Dropped'),
  221. align: 'right',
  222. direction: getArrowDirection(SortBy.DROPPED),
  223. onClick: () => this.handleChangeSort(SortBy.DROPPED),
  224. },
  225. ].map(h => {
  226. const Cell = h.key === SortBy.PROJECT ? CellProject : CellStat;
  227. return (
  228. <Cell key={h.key}>
  229. <SortLink
  230. canSort
  231. title={h.title}
  232. align={h.align as Alignments}
  233. direction={h.direction}
  234. generateSortLink={h.onClick}
  235. />
  236. </Cell>
  237. );
  238. });
  239. }
  240. getProjectLink(project: Project) {
  241. const {dataCategory, getNextLocations, organization} = this.props;
  242. const {performance, projectDetail, settings} = getNextLocations(project);
  243. if (
  244. dataCategory === DATA_CATEGORY_INFO.transaction.plural &&
  245. organization.features.includes('performance-view')
  246. ) {
  247. return {
  248. projectLink: performance,
  249. projectSettingsLink: settings,
  250. };
  251. }
  252. return {
  253. projectLink: projectDetail,
  254. projectSettingsLink: settings,
  255. };
  256. }
  257. handleChangeSort = (nextKey: SortBy) => {
  258. const {handleChangeState} = this.props;
  259. const {key, direction} = this.tableSort;
  260. let nextDirection = 1; // Default to descending
  261. if (key === nextKey) {
  262. nextDirection = direction * -1; // Toggle if clicking on the same column
  263. } else if (nextKey === SortBy.PROJECT) {
  264. nextDirection = -1; // Default PROJECT to ascending
  265. }
  266. // The header uses SortLink, which takes a LocationDescriptor and pushes
  267. // that to the router. As such, we do not need to update the router in
  268. // handleChangeState
  269. return handleChangeState(
  270. {sort: `${nextDirection > 0 ? '-' : ''}${nextKey}`},
  271. {willUpdateRouter: false}
  272. );
  273. };
  274. handleSearch = (query: string) => {
  275. const {handleChangeState, tableQuery} = this.props;
  276. if (query === tableQuery) {
  277. return;
  278. }
  279. if (!query) {
  280. handleChangeState({query: undefined, cursor: undefined});
  281. return;
  282. }
  283. handleChangeState({query, cursor: undefined});
  284. };
  285. mapSeriesToTable(projectStats?: UsageSeries): {
  286. tableStats: TableStat[];
  287. error?: Error;
  288. } {
  289. if (!projectStats) {
  290. return {tableStats: []};
  291. }
  292. const stats: Record<number, object> = {};
  293. try {
  294. const baseStat: Partial<TableStat> = {
  295. [SortBy.TOTAL]: 0,
  296. [SortBy.ACCEPTED]: 0,
  297. [SortBy.FILTERED]: 0,
  298. [SortBy.DROPPED]: 0,
  299. };
  300. const projectList = this.filteredProjects;
  301. const projectSet = new Set(projectList.map(p => p.id));
  302. projectStats.groups.forEach(group => {
  303. const {outcome, project: projectId} = group.by;
  304. // Backend enum is singlar. Frontend enum is plural.
  305. if (!projectSet.has(projectId.toString())) {
  306. return;
  307. }
  308. if (!stats[projectId]) {
  309. stats[projectId] = {...baseStat};
  310. }
  311. if (outcome !== Outcome.CLIENT_DISCARD) {
  312. stats[projectId].total += group.totals['sum(quantity)'];
  313. }
  314. if (outcome === Outcome.ACCEPTED || outcome === Outcome.FILTERED) {
  315. stats[projectId][outcome] += group.totals['sum(quantity)'];
  316. } else if (
  317. outcome === Outcome.RATE_LIMITED ||
  318. outcome === Outcome.INVALID ||
  319. outcome === Outcome.DROPPED
  320. ) {
  321. stats[projectId][SortBy.DROPPED] += group.totals['sum(quantity)'];
  322. }
  323. });
  324. // For projects without stats, fill in with zero
  325. const tableStats: TableStat[] = projectList.map(proj => {
  326. const stat = stats[proj.id] ?? {...baseStat};
  327. return {
  328. project: {...proj},
  329. ...this.getProjectLink(proj),
  330. ...stat,
  331. };
  332. });
  333. const {key, direction} = this.tableSort;
  334. tableStats.sort((a, b) => {
  335. if (key === SortBy.PROJECT) {
  336. return b.project.slug.localeCompare(a.project.slug) * direction;
  337. }
  338. return a[key] !== b[key]
  339. ? (b[key] - a[key]) * direction
  340. : a.project.slug.localeCompare(b.project.slug);
  341. });
  342. const offset = this.tableCursor;
  343. return {
  344. tableStats: tableStats.slice(
  345. offset,
  346. offset + UsageStatsProjects.MAX_ROWS_USAGE_TABLE
  347. ),
  348. };
  349. } catch (err) {
  350. Sentry.withScope(scope => {
  351. scope.setContext('query', this.endpointQuery);
  352. scope.setContext('body', {...projectStats});
  353. Sentry.captureException(err);
  354. });
  355. return {
  356. tableStats: [],
  357. error: err,
  358. };
  359. }
  360. }
  361. renderComponent() {
  362. const {error, errors, loading} = this.state;
  363. const {dataCategory, loadingProjects, tableQuery} = this.props;
  364. const {headers, tableStats} = this.tableData;
  365. return (
  366. <Fragment>
  367. <Container>
  368. <SearchBar
  369. defaultQuery=""
  370. query={tableQuery}
  371. placeholder={t('Filter your projects')}
  372. onSearch={this.handleSearch}
  373. />
  374. </Container>
  375. <Container data-test-id="usage-stats-table">
  376. <UsageTable
  377. isLoading={loading || loadingProjects}
  378. isError={error}
  379. errors={errors as any} // TODO(ts)
  380. isEmpty={tableStats.length === 0}
  381. headers={headers}
  382. dataCategory={dataCategory}
  383. usageStats={tableStats}
  384. />
  385. <Pagination pageLinks={this.pageLink} />
  386. </Container>
  387. </Fragment>
  388. );
  389. }
  390. }
  391. export default withProjects(UsageStatsProjects);
  392. const Container = styled('div')`
  393. margin-bottom: ${space(2)};
  394. `;