teamUnresolvedIssues.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {BarChart} from 'sentry/components/charts/barChart';
  4. import type {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, tct} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization} from 'sentry/types/organization';
  14. import type {Project} from 'sentry/types/project';
  15. import {formatPercentage} from 'sentry/utils/number/formatPercentage';
  16. import {useApiQuery} from 'sentry/utils/queryClient';
  17. import type {ColorOrAlias} from 'sentry/utils/theme';
  18. import {ProjectBadge, ProjectBadgeContainer} from './styles';
  19. import {
  20. barAxisLabel,
  21. convertDayValueObjectToSeries,
  22. groupByTrend,
  23. sortSeriesByDay,
  24. } from './utils';
  25. interface TeamUnresolvedIssuesProps extends DateTimeObject {
  26. organization: Organization;
  27. projects: Project[];
  28. teamSlug: string;
  29. environment?: string;
  30. }
  31. type UnresolvedCount = {unresolved: number};
  32. type ProjectReleaseCount = Record<string, Record<string, UnresolvedCount>>;
  33. export function TeamUnresolvedIssues({
  34. organization,
  35. teamSlug,
  36. projects,
  37. start,
  38. end,
  39. period,
  40. utc,
  41. environment,
  42. }: TeamUnresolvedIssuesProps) {
  43. const {
  44. data: periodIssues = {},
  45. isPending,
  46. isError,
  47. refetch,
  48. } = useApiQuery<ProjectReleaseCount>(
  49. [
  50. `/teams/${organization.slug}/${teamSlug}/all-unresolved-issues/`,
  51. {
  52. query: {
  53. ...normalizeDateTimeParams({start, end, period, utc}),
  54. environment,
  55. },
  56. },
  57. ],
  58. {staleTime: 0}
  59. );
  60. function getTotalUnresolved(projectId: number): number {
  61. const entries = Object.values(periodIssues?.[projectId] ?? {});
  62. const total = entries.reduce((acc, current) => acc + current.unresolved, 0);
  63. return Math.round(total / entries.length);
  64. }
  65. const projectTotals: Record<
  66. string,
  67. {percentChange: number; periodAvg: number; projectId: string; today: number}
  68. > = {};
  69. for (const projectId of Object.keys(periodIssues)) {
  70. const periodAvg = getTotalUnresolved(Number(projectId));
  71. const projectPeriodEntries = Object.values(periodIssues?.[projectId] ?? {});
  72. const today = projectPeriodEntries[projectPeriodEntries.length - 1]?.unresolved ?? 0;
  73. const percentChange = (today - periodAvg) / periodAvg;
  74. projectTotals[projectId] = {
  75. projectId,
  76. periodAvg,
  77. today,
  78. percentChange: Number.isNaN(percentChange) ? 0 : percentChange,
  79. };
  80. }
  81. const sortedProjects = projects
  82. .map(project => ({project, trend: projectTotals[project.id]?.percentChange ?? 0}))
  83. .sort((a, b) => Math.abs(b.trend) - Math.abs(a.trend));
  84. const groupedProjects = groupByTrend(sortedProjects);
  85. // All data will contain all pairs of [day, unresolved_count].
  86. const allData = Object.values(periodIssues).flatMap(data =>
  87. Object.entries(data).map(
  88. ([bucket, {unresolved}]) => [bucket, unresolved] as [string, number]
  89. )
  90. );
  91. // Total by day for all projects
  92. const totalByDay = allData.reduce((acc, [bucket, unresolved]) => {
  93. if (acc[bucket] === undefined) {
  94. acc[bucket] = 0;
  95. }
  96. acc[bucket] += unresolved;
  97. return acc;
  98. }, {});
  99. const seriesData = sortSeriesByDay(convertDayValueObjectToSeries(totalByDay));
  100. if (isError) {
  101. return <LoadingError onRetry={refetch} />;
  102. }
  103. return (
  104. <div>
  105. <ChartWrapper>
  106. {isPending && <Placeholder height="200px" />}
  107. {!isPending && (
  108. <BarChart
  109. style={{height: 190}}
  110. isGroupedByDate
  111. useShortDate
  112. legend={{right: 3, top: 0}}
  113. yAxis={{minInterval: 1}}
  114. xAxis={barAxisLabel()}
  115. series={[
  116. {
  117. seriesName: t('Unresolved Issues'),
  118. silent: true,
  119. data: seriesData,
  120. barCategoryGap: '6%',
  121. },
  122. ]}
  123. />
  124. )}
  125. </ChartWrapper>
  126. <CollapsePanel items={groupedProjects.length}>
  127. {({isExpanded, showMoreButton}) => (
  128. <Fragment>
  129. <StyledPanelTable
  130. isEmpty={projects.length === 0}
  131. isLoading={isPending}
  132. headers={[
  133. t('Project'),
  134. <RightAligned key="last">
  135. {tct('Last [period] Average', {period})}
  136. </RightAligned>,
  137. <RightAligned key="curr">{t('Today')}</RightAligned>,
  138. <RightAligned key="diff">{t('Change')}</RightAligned>,
  139. ]}
  140. >
  141. {groupedProjects.map(({project}, idx) => {
  142. const totals = projectTotals[project.id] ?? {};
  143. if (idx >= COLLAPSE_COUNT && !isExpanded) {
  144. return null;
  145. }
  146. return (
  147. <Fragment key={project.id}>
  148. <ProjectBadgeContainer>
  149. <ProjectBadge avatarSize={18} project={project} />
  150. </ProjectBadgeContainer>
  151. <ScoreWrapper>{totals.periodAvg}</ScoreWrapper>
  152. <ScoreWrapper>{totals.today}</ScoreWrapper>
  153. <ScoreWrapper>
  154. <SubText
  155. color={
  156. totals.percentChange === 0
  157. ? 'subText'
  158. : totals.percentChange > 0
  159. ? 'errorText'
  160. : 'successText'
  161. }
  162. >
  163. {formatPercentage(
  164. Number.isNaN(totals.percentChange) ? 0 : totals.percentChange,
  165. 0
  166. )}
  167. <PaddedIconArrow
  168. direction={totals.percentChange > 0 ? 'up' : 'down'}
  169. size="xs"
  170. />
  171. </SubText>
  172. </ScoreWrapper>
  173. </Fragment>
  174. );
  175. })}
  176. </StyledPanelTable>
  177. {!isPending && showMoreButton}
  178. </Fragment>
  179. )}
  180. </CollapsePanel>
  181. </div>
  182. );
  183. }
  184. const ChartWrapper = styled('div')`
  185. padding: ${space(2)} ${space(2)} 0 ${space(2)};
  186. border-bottom: 1px solid ${p => p.theme.border};
  187. `;
  188. const StyledPanelTable = styled(PanelTable)`
  189. grid-template-columns: 1fr 0.2fr 0.2fr 0.2fr;
  190. white-space: nowrap;
  191. margin-bottom: 0;
  192. border: 0;
  193. font-size: ${p => p.theme.fontSizeMedium};
  194. box-shadow: unset;
  195. & > div {
  196. padding: ${space(1)} ${space(2)};
  197. }
  198. `;
  199. const RightAligned = styled('span')`
  200. text-align: right;
  201. `;
  202. const ScoreWrapper = styled('div')`
  203. display: flex;
  204. align-items: center;
  205. justify-content: flex-end;
  206. text-align: right;
  207. `;
  208. const PaddedIconArrow = styled(IconArrow)`
  209. margin: 0 ${space(0.5)};
  210. `;
  211. const SubText = styled('div')<{color: ColorOrAlias}>`
  212. color: ${p => p.theme[p.color]};
  213. `;