teamIssuesBreakdown.tsx 6.9 KB

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