teamIssuesBreakdown.tsx 6.8 KB

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