index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import partition from 'lodash/partition';
  5. import sortBy from 'lodash/sortBy';
  6. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  7. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  8. import {Button, LinkButton} from 'sentry/components/button';
  9. import ButtonBar from 'sentry/components/buttonBar';
  10. import LoadingError from 'sentry/components/loadingError';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {DebugFile} from 'sentry/types/debugFiles';
  14. import {DebugFileFeature} from 'sentry/types/debugFiles';
  15. import type {Image, ImageCandidate, ImageStatus} from 'sentry/types/debugImage';
  16. import {CandidateDownloadStatus} from 'sentry/types/debugImage';
  17. import type {Event} from 'sentry/types/event';
  18. import type {Organization} from 'sentry/types/organization';
  19. import type {Project} from 'sentry/types/project';
  20. import {displayReprocessEventAction} from 'sentry/utils/displayReprocessEventAction';
  21. import {useApiQuery} from 'sentry/utils/queryClient';
  22. import theme from 'sentry/utils/theme';
  23. import useApi from 'sentry/utils/useApi';
  24. import useOrganization from 'sentry/utils/useOrganization';
  25. import {getPrettyFileType} from 'sentry/views/settings/projectDebugFiles/utils';
  26. import {getFileName} from '../utils';
  27. import Candidates from './candidates';
  28. import GeneralInfo from './generalInfo';
  29. import ReprocessAlert from './reprocessAlert';
  30. import {INTERNAL_SOURCE, INTERNAL_SOURCE_LOCATION} from './utils';
  31. type ImageCandidates = ImageCandidate[];
  32. type DebugImageDetailsProps = ModalRenderProps & {
  33. event: Event;
  34. organization: Organization;
  35. projSlug: Project['slug'];
  36. image?: Image & {status: ImageStatus};
  37. onReprocessEvent?: () => void;
  38. };
  39. function sortCandidates(
  40. candidates: ImageCandidates,
  41. unAppliedCandidates: ImageCandidates
  42. ): ImageCandidates {
  43. const [noPermissionCandidates, restNoPermissionCandidates] = partition(
  44. candidates,
  45. candidate => candidate.download.status === CandidateDownloadStatus.NO_PERMISSION
  46. );
  47. const [malFormedCandidates, restMalFormedCandidates] = partition(
  48. restNoPermissionCandidates,
  49. candidate => candidate.download.status === CandidateDownloadStatus.MALFORMED
  50. );
  51. const [errorCandidates, restErrorCandidates] = partition(
  52. restMalFormedCandidates,
  53. candidate => candidate.download.status === CandidateDownloadStatus.ERROR
  54. );
  55. const [okCandidates, restOKCandidates] = partition(
  56. restErrorCandidates,
  57. candidate => candidate.download.status === CandidateDownloadStatus.OK
  58. );
  59. const [deletedCandidates, notFoundCandidates] = partition(
  60. restOKCandidates,
  61. candidate => candidate.download.status === CandidateDownloadStatus.DELETED
  62. );
  63. return [
  64. ...sortBy(noPermissionCandidates, ['source_name', 'location']),
  65. ...sortBy(malFormedCandidates, ['source_name', 'location']),
  66. ...sortBy(errorCandidates, ['source_name', 'location']),
  67. ...sortBy(okCandidates, ['source_name', 'location']),
  68. ...sortBy(deletedCandidates, ['source_name', 'location']),
  69. ...sortBy(unAppliedCandidates, ['source_name', 'location']),
  70. ...sortBy(notFoundCandidates, ['source_name', 'location']),
  71. ];
  72. }
  73. function getCandidates({
  74. debugFiles,
  75. image,
  76. isLoading,
  77. }: {
  78. debugFiles: DebugFile[] | undefined;
  79. image: DebugImageDetailsProps['image'];
  80. isLoading: boolean;
  81. }) {
  82. const {candidates = []} = image ?? {};
  83. if (!debugFiles || isLoading) {
  84. return candidates;
  85. }
  86. const debugFileCandidates = candidates.map(({location, ...candidate}) => ({
  87. ...candidate,
  88. location: location?.includes(INTERNAL_SOURCE_LOCATION)
  89. ? location.split(INTERNAL_SOURCE_LOCATION)[1]
  90. : location,
  91. }));
  92. const candidateLocations = new Set(
  93. debugFileCandidates.map(({location}) => location).filter(location => !!location)
  94. );
  95. const [unAppliedDebugFiles, appliedDebugFiles] = partition(
  96. debugFiles,
  97. debugFile => !candidateLocations.has(debugFile.id)
  98. );
  99. const unAppliedCandidates = unAppliedDebugFiles.map(debugFile => {
  100. const {
  101. data,
  102. symbolType,
  103. objectName: filename,
  104. id: location,
  105. size,
  106. dateCreated,
  107. cpuName,
  108. } = debugFile;
  109. const features = data?.features ?? [];
  110. return {
  111. download: {
  112. status: CandidateDownloadStatus.UNAPPLIED,
  113. features: {
  114. has_sources: features.includes(DebugFileFeature.SOURCES),
  115. has_debug_info: features.includes(DebugFileFeature.DEBUG),
  116. has_unwind_info: features.includes(DebugFileFeature.UNWIND),
  117. has_symbols: features.includes(DebugFileFeature.SYMTAB),
  118. },
  119. },
  120. cpuName,
  121. location,
  122. filename,
  123. size,
  124. dateCreated,
  125. symbolType,
  126. fileType: getPrettyFileType(debugFile),
  127. source: INTERNAL_SOURCE,
  128. source_name: t('Sentry'),
  129. };
  130. });
  131. const [debugFileInternalOkCandidates, debugFileOtherCandidates] = partition(
  132. debugFileCandidates,
  133. debugFileCandidate =>
  134. debugFileCandidate.download.status === CandidateDownloadStatus.OK &&
  135. debugFileCandidate.source === INTERNAL_SOURCE
  136. );
  137. const convertedDebugFileInternalOkCandidates = debugFileInternalOkCandidates.map(
  138. debugFileOkCandidate => {
  139. const internalDebugFileInfo = appliedDebugFiles.find(
  140. appliedDebugFile => appliedDebugFile.id === debugFileOkCandidate.location
  141. );
  142. if (!internalDebugFileInfo) {
  143. return {
  144. ...debugFileOkCandidate,
  145. download: {
  146. ...debugFileOkCandidate.download,
  147. status: CandidateDownloadStatus.DELETED,
  148. },
  149. };
  150. }
  151. const {
  152. symbolType,
  153. objectName: filename,
  154. id: location,
  155. size,
  156. dateCreated,
  157. } = internalDebugFileInfo;
  158. return {
  159. ...debugFileOkCandidate,
  160. location,
  161. filename,
  162. size,
  163. dateCreated,
  164. symbolType,
  165. prettyFileType: getPrettyFileType(internalDebugFileInfo),
  166. };
  167. }
  168. );
  169. return sortCandidates(
  170. [
  171. ...convertedDebugFileInternalOkCandidates,
  172. ...debugFileOtherCandidates,
  173. ] as ImageCandidates,
  174. unAppliedCandidates as ImageCandidates
  175. );
  176. }
  177. export function DebugImageDetails({
  178. image,
  179. projSlug,
  180. Header,
  181. Body,
  182. Footer,
  183. event,
  184. onReprocessEvent,
  185. }: DebugImageDetailsProps) {
  186. const organization = useOrganization();
  187. const api = useApi();
  188. const hasUploadedDebugFiles =
  189. image?.candidates?.some(candidate => candidate.source === INTERNAL_SOURCE) ?? false;
  190. const {
  191. data: debugFiles,
  192. isLoading,
  193. isError,
  194. refetch,
  195. } = useApiQuery<DebugFile[]>(
  196. [
  197. `/projects/${organization.slug}/${projSlug}/files/dsyms/?debug_id=${image?.debug_id}`,
  198. {
  199. query: {
  200. // FIXME(swatinem): Ideally we should not filter here at all,
  201. // though Symbolicator does not currently report `bcsymbolmap` and `il2cpp`
  202. // candidates, and we would thus show bogus "unapplied" entries for those,
  203. // which would probably confuse people more than not seeing successfully
  204. // fetched candidates for those two types of files.
  205. file_formats: [
  206. 'breakpad',
  207. 'macho',
  208. 'elf',
  209. 'pe',
  210. 'pdb',
  211. 'sourcebundle',
  212. 'wasm',
  213. 'portablepdb',
  214. ],
  215. },
  216. },
  217. ],
  218. {
  219. enabled: hasUploadedDebugFiles,
  220. staleTime: 0,
  221. }
  222. );
  223. const {code_file, status} = image ?? {};
  224. const candidates = getCandidates({debugFiles, image, isLoading});
  225. const baseUrl = api.baseUrl;
  226. const fileName = getFileName(code_file);
  227. const haveCandidatesUnappliedDebugFile = candidates.some(
  228. candidate => candidate.download.status === CandidateDownloadStatus.UNAPPLIED
  229. );
  230. const hasReprocessWarning =
  231. haveCandidatesUnappliedDebugFile &&
  232. displayReprocessEventAction(event) &&
  233. !!onReprocessEvent;
  234. if (isError) {
  235. return <LoadingError />;
  236. }
  237. const shouldShowLoadingIndicator = isLoading && hasUploadedDebugFiles;
  238. const handleDelete = async (debugId: string) => {
  239. try {
  240. await api.requestPromise(
  241. `/projects/${organization.slug}/${projSlug}/files/dsyms/?id=${debugId}`,
  242. {method: 'DELETE'}
  243. );
  244. refetch();
  245. } catch {
  246. addErrorMessage(t('An error occurred while deleting the debug file.'));
  247. }
  248. };
  249. const debugFilesSettingsLink =
  250. projSlug && image?.debug_id
  251. ? `/settings/${organization.slug}/projects/${projSlug}/debug-symbols/?query=${image?.debug_id}`
  252. : undefined;
  253. return (
  254. <Fragment>
  255. <Header closeButton>
  256. <Title>
  257. {t('Image')}
  258. <FileName>{fileName ?? t('Unknown')}</FileName>
  259. </Title>
  260. </Header>
  261. <Body>
  262. <Content>
  263. <GeneralInfo image={image} />
  264. {hasReprocessWarning && (
  265. <ReprocessAlert
  266. api={api}
  267. orgSlug={organization.slug}
  268. projSlug={projSlug}
  269. eventId={event.id}
  270. onReprocessEvent={onReprocessEvent}
  271. />
  272. )}
  273. <Candidates
  274. imageStatus={status}
  275. candidates={candidates}
  276. organization={organization}
  277. projSlug={projSlug}
  278. baseUrl={baseUrl}
  279. isLoading={shouldShowLoadingIndicator}
  280. eventDateReceived={event.dateReceived}
  281. onDelete={handleDelete}
  282. hasReprocessWarning={hasReprocessWarning}
  283. />
  284. </Content>
  285. </Body>
  286. <Footer>
  287. <StyledButtonBar gap={1}>
  288. <Button
  289. href="https://docs.sentry.io/platforms/native/data-management/debug-files/"
  290. external
  291. >
  292. {t('Read the docs')}
  293. </Button>
  294. {debugFilesSettingsLink && (
  295. <LinkButton
  296. title={t(
  297. 'Search for this debug file in all images for the %s project',
  298. projSlug
  299. )}
  300. to={debugFilesSettingsLink}
  301. >
  302. {t('Open in Settings')}
  303. </LinkButton>
  304. )}
  305. </StyledButtonBar>
  306. </Footer>
  307. </Fragment>
  308. );
  309. }
  310. const Content = styled('div')`
  311. display: grid;
  312. gap: ${space(3)};
  313. font-size: ${p => p.theme.fontSizeMedium};
  314. `;
  315. const Title = styled('div')`
  316. display: grid;
  317. grid-template-columns: max-content 1fr;
  318. gap: ${space(1)};
  319. align-items: center;
  320. font-size: ${p => p.theme.fontSizeExtraLarge};
  321. max-width: calc(100% - 40px);
  322. word-break: break-all;
  323. `;
  324. const FileName = styled('span')`
  325. font-family: ${p => p.theme.text.familyMono};
  326. `;
  327. const StyledButtonBar = styled(ButtonBar)`
  328. white-space: nowrap;
  329. `;
  330. export const modalCss = css`
  331. [role='document'] {
  332. overflow: initial;
  333. }
  334. @media (min-width: ${theme.breakpoints.small}) {
  335. width: 90%;
  336. }
  337. @media (min-width: ${theme.breakpoints.xlarge}) {
  338. width: 70%;
  339. }
  340. @media (min-width: ${theme.breakpoints.xxlarge}) {
  341. width: 50%;
  342. }
  343. `;