teamIssuesBreakdown.tsx 6.8 KB

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