teamIssuesBreakdown.tsx 6.4 KB

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