teamIssuesBreakdown.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 hasEscalatingIssues = organization.features.includes('escalating-issues');
  69. const allReviewedByDay: Record<string, Record<string, number>> = {};
  70. // Total statuses & total reviewed keyed by project ID
  71. const projectTotals: Record<string, StatusCounts> = {};
  72. // The issues breakdown is keyed by projectId
  73. for (const [projectId, entries] of Object.entries(issuesBreakdown)) {
  74. // Each bucket is 1 day
  75. for (const [bucket, counts] of Object.entries(entries)) {
  76. if (!projectTotals[projectId]) {
  77. projectTotals[projectId] = {
  78. deleted: 0,
  79. ignored: 0,
  80. resolved: 0,
  81. unignored: 0,
  82. regressed: 0,
  83. new: 0,
  84. total: 0,
  85. };
  86. }
  87. for (const key of keys) {
  88. projectTotals[projectId][key] += counts[key];
  89. }
  90. if (!allReviewedByDay[projectId]) {
  91. allReviewedByDay[projectId] = {};
  92. }
  93. if (allReviewedByDay[projectId][bucket] === undefined) {
  94. allReviewedByDay[projectId][bucket] = counts.total;
  95. } else {
  96. allReviewedByDay[projectId][bucket] += counts.total;
  97. }
  98. }
  99. }
  100. const sortedProjectIds = Object.entries(projectTotals)
  101. .map(([projectId, {total}]) => ({projectId, total}))
  102. .sort((a, b) => b.total - a.total);
  103. const allSeries = Object.keys(allReviewedByDay).map(
  104. (projectId, idx): BarChartSeries => ({
  105. seriesName: ProjectsStore.getById(projectId)?.slug ?? projectId,
  106. data: sortSeriesByDay(convertDayValueObjectToSeries(allReviewedByDay[projectId])),
  107. animationDuration: 500,
  108. animationDelay: idx * 500,
  109. silent: true,
  110. barCategoryGap: '5%',
  111. })
  112. );
  113. if (isError) {
  114. return <LoadingError onRetry={refetch} />;
  115. }
  116. return (
  117. <Fragment>
  118. <IssuesChartWrapper>
  119. {isLoading && <Placeholder height="200px" />}
  120. {!isLoading && (
  121. <BarChart
  122. style={{height: 200}}
  123. stacked
  124. isGroupedByDate
  125. useShortDate
  126. legend={{right: 0, top: 0}}
  127. xAxis={barAxisLabel()}
  128. yAxis={{minInterval: 1}}
  129. series={allSeries}
  130. />
  131. )}
  132. </IssuesChartWrapper>
  133. <CollapsePanel items={sortedProjectIds.length}>
  134. {({isExpanded, showMoreButton}) => (
  135. <Fragment>
  136. <StyledPanelTable
  137. numActions={statuses.length}
  138. headers={[
  139. t('Project'),
  140. ...statuses
  141. .map(action =>
  142. hasEscalatingIssues ? action.replace('ignore', 'archive') : action
  143. )
  144. .map(action => <AlignRight key={action}>{action}</AlignRight>),
  145. <AlignRight key="total">
  146. {t('total')} <IconArrow direction="down" size="xs" color="gray300" />
  147. </AlignRight>,
  148. ]}
  149. isLoading={isLoading}
  150. >
  151. {sortedProjectIds.map(({projectId}, idx) => {
  152. const project = projects.find(p => p.id === projectId);
  153. if (idx >= COLLAPSE_COUNT && !isExpanded) {
  154. return null;
  155. }
  156. return (
  157. <Fragment key={projectId}>
  158. <ProjectBadgeContainer>
  159. {project && <ProjectBadge avatarSize={18} project={project} />}
  160. </ProjectBadgeContainer>
  161. {statuses.map(action => (
  162. <AlignRight key={action}>
  163. {projectTotals[projectId][action]}
  164. </AlignRight>
  165. ))}
  166. <AlignRight>{projectTotals[projectId].total}</AlignRight>
  167. </Fragment>
  168. );
  169. })}
  170. </StyledPanelTable>
  171. {!isLoading && showMoreButton}
  172. </Fragment>
  173. )}
  174. </CollapsePanel>
  175. </Fragment>
  176. );
  177. }
  178. export default TeamIssuesBreakdown;
  179. const ChartWrapper = styled('div')`
  180. padding: ${space(2)} ${space(2)} 0 ${space(2)};
  181. `;
  182. const IssuesChartWrapper = styled(ChartWrapper)`
  183. border-bottom: 1px solid ${p => p.theme.border};
  184. `;
  185. const StyledPanelTable = styled(PanelTable)<{numActions: number}>`
  186. grid-template-columns: 1fr ${p => ' 0.2fr'.repeat(p.numActions)} 0.2fr;
  187. font-size: ${p => p.theme.fontSizeMedium};
  188. white-space: nowrap;
  189. margin-bottom: 0;
  190. border: 0;
  191. box-shadow: unset;
  192. & > div {
  193. padding: ${space(1)} ${space(2)};
  194. }
  195. `;
  196. const AlignRight = styled('div')`
  197. text-align: right;
  198. font-variant-numeric: tabular-nums;
  199. `;