teamIssuesAge.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import moment from 'moment';
  5. import AsyncComponent from 'sentry/components/asyncComponent';
  6. import {BarChart} from 'sentry/components/charts/barChart';
  7. import Count from 'sentry/components/count';
  8. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  9. import Link from 'sentry/components/links/link';
  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 {Group, Organization} from 'sentry/types';
  17. import {getTitle} from 'sentry/utils/events';
  18. type Props = AsyncComponent['props'] & {
  19. organization: Organization;
  20. teamSlug: string;
  21. };
  22. type State = AsyncComponent['state'] & {
  23. oldestIssues: Group[] | null;
  24. unresolvedIssueAge: Record<string, number> | null;
  25. };
  26. /**
  27. * takes "< 1 hour" and returns a datetime of 1 hour ago
  28. */
  29. function parseBucket(bucket: string): number {
  30. if (bucket === '> 1 year') {
  31. return moment().subtract(1, 'y').subtract(1, 'd').valueOf();
  32. }
  33. const [_, num, unit] = bucket.split(' ');
  34. return moment()
  35. .subtract(num, unit as any)
  36. .valueOf();
  37. }
  38. const bucketLabels = {
  39. '< 1 hour': t('1 hour'),
  40. '< 4 hour': t('4 hours'),
  41. '< 12 hour': t('12 hours'),
  42. '< 1 day': t('1 day'),
  43. '< 1 week': t('1 week'),
  44. '< 4 week': t('1 month'),
  45. '< 24 week': t('6 months'),
  46. '< 1 year': t('1 year'),
  47. '> 1 year': t('> 1 year'),
  48. };
  49. class TeamIssuesAge extends AsyncComponent<Props, State> {
  50. shouldRenderBadRequests = true;
  51. getDefaultState(): State {
  52. return {
  53. ...super.getDefaultState(),
  54. oldestIssues: null,
  55. unresolvedIssueAge: null,
  56. };
  57. }
  58. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  59. const {organization, teamSlug} = this.props;
  60. return [
  61. [
  62. 'oldestIssues',
  63. `/teams/${organization.slug}/${teamSlug}/issues/old/`,
  64. {query: {limit: 7}},
  65. ],
  66. [
  67. 'unresolvedIssueAge',
  68. `/teams/${organization.slug}/${teamSlug}/unresolved-issue-age/`,
  69. ],
  70. ];
  71. }
  72. componentDidUpdate(prevProps: Props) {
  73. const {teamSlug} = this.props;
  74. if (prevProps.teamSlug !== teamSlug) {
  75. this.remountComponent();
  76. }
  77. }
  78. renderLoading() {
  79. return this.renderBody();
  80. }
  81. renderBody() {
  82. const {organization} = this.props;
  83. const {unresolvedIssueAge, oldestIssues, loading} = this.state;
  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. {loading && <Placeholder height="200px" />}
  94. {!loading && (
  95. <BarChart
  96. style={{height: 190}}
  97. legend={{right: 3, top: 0}}
  98. yAxis={{minInterval: 1}}
  99. xAxis={{
  100. splitNumber: seriesData.length,
  101. type: 'category',
  102. min: 0,
  103. axisLabel: {
  104. showMaxLabel: true,
  105. showMinLabel: true,
  106. formatter: (bucket: string) => {
  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="12px" color="gray300" />
  131. </RightAligned>,
  132. ]}
  133. isLoading={loading}
  134. >
  135. {oldestIssues?.map(issue => {
  136. const {title} = getTitle(issue, organization?.features, false);
  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. }
  173. export default TeamIssuesAge;
  174. const ChartWrapper = styled('div')`
  175. padding: ${space(2)} ${space(2)} 0 ${space(2)};
  176. border-bottom: 1px solid ${p => p.theme.border};
  177. `;
  178. const StyledPanelTable = styled(PanelTable)`
  179. grid-template-columns: 1fr 0.15fr 0.15fr 0.25fr;
  180. white-space: nowrap;
  181. margin-bottom: 0;
  182. border: 0;
  183. font-size: ${p => p.theme.fontSizeMedium};
  184. box-shadow: unset;
  185. > * {
  186. padding: ${space(1)} ${space(2)};
  187. }
  188. ${p =>
  189. p.isEmpty &&
  190. css`
  191. & > div:last-child {
  192. padding: 48px ${space(2)};
  193. }
  194. `}
  195. `;
  196. const RightAligned = styled('span')`
  197. display: flex;
  198. align-items: center;
  199. justify-content: flex-end;
  200. `;
  201. const ProjectTitleContainer = styled('div')`
  202. ${p => p.theme.overflowEllipsis};
  203. display: flex;
  204. align-items: center;
  205. `;
  206. const TitleOverflow = styled('div')`
  207. ${p => p.theme.overflowEllipsis};
  208. `;
  209. const ShadowlessProjectBadge = styled(ProjectBadge)`
  210. display: inline-flex;
  211. align-items: center;
  212. margin-right: ${space(1)};
  213. * > img {
  214. box-shadow: none;
  215. }
  216. `;