teamUnresolvedIssues.tsx 7.1 KB

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