teamIssuesBreakdown.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {BarChart, BarChartSeries} from 'sentry/components/charts/barChart';
  4. import {DateTimeObject} from 'sentry/components/charts/utils';
  5. import CollapsePanel, {COLLAPSE_COUNT} from 'sentry/components/collapsePanel';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  8. import PanelTable from 'sentry/components/panels/panelTable';
  9. import Placeholder from 'sentry/components/placeholder';
  10. import {IconArrow} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import ProjectsStore from 'sentry/stores/projectsStore';
  13. import {space} from 'sentry/styles/space';
  14. import type {Organization, Project} from 'sentry/types';
  15. import {useApiQuery} from 'sentry/utils/queryClient';
  16. import {ProjectBadge, ProjectBadgeContainer} from './styles';
  17. import {barAxisLabel, convertDayValueObjectToSeries, sortSeriesByDay} from './utils';
  18. interface StatusCounts {
  19. total: number;
  20. deleted?: number;
  21. ignored?: number;
  22. new?: number;
  23. regressed?: number;
  24. resolved?: number;
  25. unignored?: number;
  26. }
  27. type IssuesBreakdown = Record<string, Record<string, StatusCounts>>;
  28. type Statuses = keyof Omit<StatusCounts, 'total'>;
  29. interface TeamIssuesBreakdownProps extends DateTimeObject {
  30. organization: Organization;
  31. projects: Project[];
  32. statuses: Statuses[];
  33. teamSlug: string;
  34. environment?: string;
  35. }
  36. const keys = ['deleted', 'ignored', 'resolved', 'unignored', 'regressed', 'new', 'total'];
  37. function TeamIssuesBreakdown({
  38. organization,
  39. projects,
  40. start,
  41. end,
  42. period,
  43. utc,
  44. teamSlug,
  45. statuses,
  46. environment,
  47. }: TeamIssuesBreakdownProps) {
  48. const {
  49. data: issuesBreakdown = {},
  50. isLoading,
  51. isError,
  52. refetch,
  53. } = useApiQuery<IssuesBreakdown>(
  54. [
  55. `/teams/${organization.slug}/${teamSlug}/issue-breakdown/`,
  56. {
  57. query: {
  58. ...normalizeDateTimeParams({start, end, period, utc}),
  59. statuses,
  60. environment,
  61. },
  62. },
  63. ],
  64. {staleTime: 5000}
  65. );
  66. const allReviewedByDay: Record<string, Record<string, number>> = {};
  67. // Total statuses & total reviewed keyed by project ID
  68. const projectTotals: Record<string, StatusCounts> = {};
  69. // The issues breakdown is keyed by projectId
  70. for (const [projectId, entries] of Object.entries(issuesBreakdown)) {
  71. // Each bucket is 1 day
  72. for (const [bucket, counts] of Object.entries(entries)) {
  73. if (!projectTotals[projectId]) {
  74. projectTotals[projectId] = {
  75. deleted: 0,
  76. ignored: 0,
  77. resolved: 0,
  78. unignored: 0,
  79. regressed: 0,
  80. new: 0,
  81. total: 0,
  82. };
  83. }
  84. for (const key of keys) {
  85. projectTotals[projectId][key] += counts[key];
  86. }
  87. if (!allReviewedByDay[projectId]) {
  88. allReviewedByDay[projectId] = {};
  89. }
  90. if (allReviewedByDay[projectId][bucket] === undefined) {
  91. allReviewedByDay[projectId][bucket] = counts.total;
  92. } else {
  93. allReviewedByDay[projectId][bucket] += counts.total;
  94. }
  95. }
  96. }
  97. const sortedProjectIds = Object.entries(projectTotals)
  98. .map(([projectId, {total}]) => ({projectId, total}))
  99. .sort((a, b) => b.total - a.total);
  100. const allSeries = Object.keys(allReviewedByDay).map(
  101. (projectId, idx): BarChartSeries => ({
  102. seriesName: ProjectsStore.getById(projectId)?.slug ?? projectId,
  103. data: sortSeriesByDay(convertDayValueObjectToSeries(allReviewedByDay[projectId])),
  104. animationDuration: 500,
  105. animationDelay: idx * 500,
  106. silent: true,
  107. barCategoryGap: '5%',
  108. })
  109. );
  110. if (isError) {
  111. return <LoadingError onRetry={refetch} />;
  112. }
  113. return (
  114. <Fragment>
  115. <IssuesChartWrapper>
  116. {isLoading && <Placeholder height="200px" />}
  117. {!isLoading && (
  118. <BarChart
  119. style={{height: 200}}
  120. stacked
  121. isGroupedByDate
  122. useShortDate
  123. legend={{right: 0, top: 0}}
  124. xAxis={barAxisLabel()}
  125. yAxis={{minInterval: 1}}
  126. series={allSeries}
  127. />
  128. )}
  129. </IssuesChartWrapper>
  130. <CollapsePanel items={sortedProjectIds.length}>
  131. {({isExpanded, showMoreButton}) => (
  132. <Fragment>
  133. <StyledPanelTable
  134. numActions={statuses.length}
  135. headers={[
  136. t('Project'),
  137. ...statuses.map(action => <AlignRight key={action}>{action}</AlignRight>),
  138. <AlignRight key="total">
  139. {t('total')} <IconArrow direction="down" size="xs" color="gray300" />
  140. </AlignRight>,
  141. ]}
  142. isLoading={isLoading}
  143. >
  144. {sortedProjectIds.map(({projectId}, idx) => {
  145. const project = projects.find(p => p.id === projectId);
  146. if (idx >= COLLAPSE_COUNT && !isExpanded) {
  147. return null;
  148. }
  149. return (
  150. <Fragment key={projectId}>
  151. <ProjectBadgeContainer>
  152. {project && <ProjectBadge avatarSize={18} project={project} />}
  153. </ProjectBadgeContainer>
  154. {statuses.map(action => (
  155. <AlignRight key={action}>
  156. {projectTotals[projectId][action]}
  157. </AlignRight>
  158. ))}
  159. <AlignRight>{projectTotals[projectId].total}</AlignRight>
  160. </Fragment>
  161. );
  162. })}
  163. </StyledPanelTable>
  164. {!isLoading && showMoreButton}
  165. </Fragment>
  166. )}
  167. </CollapsePanel>
  168. </Fragment>
  169. );
  170. }
  171. export default TeamIssuesBreakdown;
  172. const ChartWrapper = styled('div')`
  173. padding: ${space(2)} ${space(2)} 0 ${space(2)};
  174. `;
  175. const IssuesChartWrapper = styled(ChartWrapper)`
  176. border-bottom: 1px solid ${p => p.theme.border};
  177. `;
  178. const StyledPanelTable = styled(PanelTable)<{numActions: number}>`
  179. grid-template-columns: 1fr ${p => ' 0.2fr'.repeat(p.numActions)} 0.2fr;
  180. font-size: ${p => p.theme.fontSizeMedium};
  181. white-space: nowrap;
  182. margin-bottom: 0;
  183. border: 0;
  184. box-shadow: unset;
  185. & > div {
  186. padding: ${space(1)} ${space(2)};
  187. }
  188. `;
  189. const AlignRight = styled('div')`
  190. text-align: right;
  191. font-variant-numeric: tabular-nums;
  192. `;