teamIssuesAge.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import moment from 'moment-timezone';
  5. import {BarChart} from 'sentry/components/charts/barChart';
  6. import Count from 'sentry/components/count';
  7. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  8. import Link from 'sentry/components/links/link';
  9. import LoadingError from 'sentry/components/loadingError';
  10. import {PanelTable} from 'sentry/components/panels/panelTable';
  11. import Placeholder from 'sentry/components/placeholder';
  12. import TimeSince from 'sentry/components/timeSince';
  13. import {IconArrow} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {Group} from 'sentry/types/group';
  17. import type {Organization} from 'sentry/types/organization';
  18. import {getTitle} from 'sentry/utils/events';
  19. import {useApiQuery} from 'sentry/utils/queryClient';
  20. interface TeamIssuesAgeProps {
  21. organization: Organization;
  22. teamSlug: string;
  23. }
  24. /**
  25. * takes "< 1 hour" and returns a datetime of 1 hour ago
  26. */
  27. function parseBucket(bucket: string): number {
  28. if (bucket === '> 1 year') {
  29. return moment().subtract(1, 'y').subtract(1, 'd').valueOf();
  30. }
  31. const [_, num, unit] = bucket.split(' ');
  32. return moment()
  33. .subtract(num, unit as any)
  34. .valueOf();
  35. }
  36. const bucketLabels = {
  37. '< 1 hour': t('1 hour'),
  38. '< 4 hour': t('4 hours'),
  39. '< 12 hour': t('12 hours'),
  40. '< 1 day': t('1 day'),
  41. '< 1 week': t('1 week'),
  42. '< 4 week': t('1 month'),
  43. '< 24 week': t('6 months'),
  44. '< 1 year': t('1 year'),
  45. '> 1 year': t('> 1 year'),
  46. };
  47. function TeamIssuesAge({organization, teamSlug}: TeamIssuesAgeProps) {
  48. const {
  49. data: oldestIssues,
  50. isPending: isOldestIssuesLoading,
  51. isError: isOldestIssuesError,
  52. refetch: refetchOldestIssues,
  53. } = useApiQuery<Group[]>(
  54. [
  55. `/teams/${organization.slug}/${teamSlug}/issues/old/`,
  56. {
  57. query: {
  58. limit: 7,
  59. },
  60. },
  61. ],
  62. {staleTime: 5000}
  63. );
  64. const {
  65. data: unresolvedIssueAge,
  66. isPending: isUnresolvedIssueAgeLoading,
  67. isError: isUnresolvedIssueAgeError,
  68. refetch: refetchUnresolvedIssueAge,
  69. } = useApiQuery<Record<string, number>>(
  70. [`/teams/${organization.slug}/${teamSlug}/unresolved-issue-age/`],
  71. {staleTime: 5000}
  72. );
  73. const isLoading = isOldestIssuesLoading || isUnresolvedIssueAgeLoading;
  74. if (isOldestIssuesError || isUnresolvedIssueAgeError) {
  75. return (
  76. <LoadingError
  77. onRetry={() => {
  78. refetchOldestIssues();
  79. refetchUnresolvedIssueAge();
  80. }}
  81. />
  82. );
  83. }
  84. const seriesData = Object.entries(unresolvedIssueAge ?? {})
  85. .map(([bucket, value]) => ({
  86. name: bucket,
  87. value,
  88. }))
  89. .sort((a, b) => parseBucket(b.name) - parseBucket(a.name));
  90. return (
  91. <div>
  92. <ChartWrapper>
  93. {isLoading && <Placeholder height="200px" />}
  94. {!isLoading && (
  95. <BarChart
  96. style={{height: 190}}
  97. legend={{right: 3, top: 0}}
  98. yAxis={{minInterval: 1}}
  99. xAxis={{
  100. type: 'category',
  101. min: 0,
  102. axisLabel: {
  103. showMaxLabel: true,
  104. showMinLabel: true,
  105. formatter: (bucket: string) => {
  106. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  107. return bucketLabels[bucket] ?? bucket;
  108. },
  109. },
  110. }}
  111. series={[
  112. {
  113. seriesName: t('Unresolved Issues'),
  114. silent: true,
  115. data: seriesData,
  116. barCategoryGap: '5%',
  117. },
  118. ]}
  119. />
  120. )}
  121. </ChartWrapper>
  122. <StyledPanelTable
  123. isEmpty={!oldestIssues || oldestIssues?.length === 0}
  124. emptyMessage={t('No unresolved issues for this team’s projects')}
  125. headers={[
  126. t('Oldest Issues'),
  127. <RightAligned key="events">{t('Events')}</RightAligned>,
  128. <RightAligned key="users">{t('Users')}</RightAligned>,
  129. <RightAligned key="age">
  130. {t('Age')} <IconArrow direction="down" size="xs" color="gray300" />
  131. </RightAligned>,
  132. ]}
  133. isLoading={isLoading}
  134. >
  135. {oldestIssues?.map(issue => {
  136. const {title} = getTitle(issue);
  137. return (
  138. <Fragment key={issue.id}>
  139. <ProjectTitleContainer>
  140. <ShadowlessProjectBadge
  141. disableLink
  142. hideName
  143. avatarSize={18}
  144. project={issue.project}
  145. />
  146. <TitleOverflow>
  147. <Link
  148. to={{
  149. pathname: `/organizations/${organization.slug}/issues/${issue.id}/`,
  150. }}
  151. >
  152. {title}
  153. </Link>
  154. </TitleOverflow>
  155. </ProjectTitleContainer>
  156. <RightAligned>
  157. <Count value={issue.count} />
  158. </RightAligned>
  159. <RightAligned>
  160. <Count value={issue.userCount} />
  161. </RightAligned>
  162. <RightAligned>
  163. <TimeSince date={issue.firstSeen} />
  164. </RightAligned>
  165. </Fragment>
  166. );
  167. })}
  168. </StyledPanelTable>
  169. </div>
  170. );
  171. }
  172. export default TeamIssuesAge;
  173. const ChartWrapper = styled('div')`
  174. padding: ${space(2)} ${space(2)} 0 ${space(2)};
  175. border-bottom: 1px solid ${p => p.theme.border};
  176. `;
  177. const StyledPanelTable = styled(PanelTable)`
  178. grid-template-columns: 1fr 0.15fr 0.15fr 0.25fr;
  179. white-space: nowrap;
  180. margin-bottom: 0;
  181. border: 0;
  182. font-size: ${p => p.theme.fontSizeMedium};
  183. box-shadow: unset;
  184. > * {
  185. padding: ${space(1)} ${space(2)};
  186. }
  187. ${p =>
  188. p.isEmpty &&
  189. css`
  190. & > div:last-child {
  191. padding: 48px ${space(2)};
  192. }
  193. `}
  194. `;
  195. const RightAligned = styled('span')`
  196. display: flex;
  197. align-items: center;
  198. justify-content: flex-end;
  199. `;
  200. const ProjectTitleContainer = styled('div')`
  201. ${p => p.theme.overflowEllipsis};
  202. display: flex;
  203. align-items: center;
  204. `;
  205. const TitleOverflow = styled('div')`
  206. ${p => p.theme.overflowEllipsis};
  207. `;
  208. const ShadowlessProjectBadge = styled(ProjectBadge)`
  209. display: inline-flex;
  210. align-items: center;
  211. margin-right: ${space(1)};
  212. * > img {
  213. box-shadow: none;
  214. }
  215. `;