teamIssuesAge.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. return bucketLabels[bucket] ?? bucket;
  107. },
  108. },
  109. }}
  110. series={[
  111. {
  112. seriesName: t('Unresolved Issues'),
  113. silent: true,
  114. data: seriesData,
  115. barCategoryGap: '5%',
  116. },
  117. ]}
  118. />
  119. )}
  120. </ChartWrapper>
  121. <StyledPanelTable
  122. isEmpty={!oldestIssues || oldestIssues?.length === 0}
  123. emptyMessage={t('No unresolved issues for this team’s projects')}
  124. headers={[
  125. t('Oldest Issues'),
  126. <RightAligned key="events">{t('Events')}</RightAligned>,
  127. <RightAligned key="users">{t('Users')}</RightAligned>,
  128. <RightAligned key="age">
  129. {t('Age')} <IconArrow direction="down" size="xs" color="gray300" />
  130. </RightAligned>,
  131. ]}
  132. isLoading={isLoading}
  133. >
  134. {oldestIssues?.map(issue => {
  135. const {title} = getTitle(issue);
  136. return (
  137. <Fragment key={issue.id}>
  138. <ProjectTitleContainer>
  139. <ShadowlessProjectBadge
  140. disableLink
  141. hideName
  142. avatarSize={18}
  143. project={issue.project}
  144. />
  145. <TitleOverflow>
  146. <Link
  147. to={{
  148. pathname: `/organizations/${organization.slug}/issues/${issue.id}/`,
  149. }}
  150. >
  151. {title}
  152. </Link>
  153. </TitleOverflow>
  154. </ProjectTitleContainer>
  155. <RightAligned>
  156. <Count value={issue.count} />
  157. </RightAligned>
  158. <RightAligned>
  159. <Count value={issue.userCount} />
  160. </RightAligned>
  161. <RightAligned>
  162. <TimeSince date={issue.firstSeen} />
  163. </RightAligned>
  164. </Fragment>
  165. );
  166. })}
  167. </StyledPanelTable>
  168. </div>
  169. );
  170. }
  171. export default TeamIssuesAge;
  172. const ChartWrapper = styled('div')`
  173. padding: ${space(2)} ${space(2)} 0 ${space(2)};
  174. border-bottom: 1px solid ${p => p.theme.border};
  175. `;
  176. const StyledPanelTable = styled(PanelTable)`
  177. grid-template-columns: 1fr 0.15fr 0.15fr 0.25fr;
  178. white-space: nowrap;
  179. margin-bottom: 0;
  180. border: 0;
  181. font-size: ${p => p.theme.fontSizeMedium};
  182. box-shadow: unset;
  183. > * {
  184. padding: ${space(1)} ${space(2)};
  185. }
  186. ${p =>
  187. p.isEmpty &&
  188. css`
  189. & > div:last-child {
  190. padding: 48px ${space(2)};
  191. }
  192. `}
  193. `;
  194. const RightAligned = styled('span')`
  195. display: flex;
  196. align-items: center;
  197. justify-content: flex-end;
  198. `;
  199. const ProjectTitleContainer = styled('div')`
  200. ${p => p.theme.overflowEllipsis};
  201. display: flex;
  202. align-items: center;
  203. `;
  204. const TitleOverflow = styled('div')`
  205. ${p => p.theme.overflowEllipsis};
  206. `;
  207. const ShadowlessProjectBadge = styled(ProjectBadge)`
  208. display: inline-flex;
  209. align-items: center;
  210. margin-right: ${space(1)};
  211. * > img {
  212. box-shadow: none;
  213. }
  214. `;