dataDownload.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import {browserHistory} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import {Button} from 'sentry/components/button';
  6. import {ExportQueryType} from 'sentry/components/dataExport';
  7. import DateTime from 'sentry/components/dateTime';
  8. import {IconDownload} from 'sentry/icons';
  9. import {t, tct} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  12. import Layout from 'sentry/views/auth/layout';
  13. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  14. export enum DownloadStatus {
  15. EARLY = 'EARLY',
  16. VALID = 'VALID',
  17. EXPIRED = 'EXPIRED',
  18. }
  19. type RouteParams = {
  20. dataExportId: string;
  21. orgId: string;
  22. };
  23. type Download = {
  24. checksum: string;
  25. dateCreated: string;
  26. id: number;
  27. query: {
  28. info: object;
  29. type: ExportQueryType;
  30. };
  31. status: DownloadStatus;
  32. user: {
  33. email: string;
  34. id: number;
  35. username: string;
  36. };
  37. dateExpired?: string;
  38. dateFinished?: string;
  39. };
  40. type Props = {} & RouteComponentProps<RouteParams, {}>;
  41. type State = {
  42. download: Download;
  43. errors: {
  44. download: {
  45. responseJSON: {
  46. detail: string;
  47. };
  48. status: number;
  49. statusText: string;
  50. };
  51. };
  52. } & DeprecatedAsyncView['state'];
  53. class DataDownload extends DeprecatedAsyncView<Props, State> {
  54. disableErrorReport = false;
  55. getTitle(): string {
  56. return t('Download Center');
  57. }
  58. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  59. const {orgId, dataExportId} = this.props.params;
  60. return [['download', `/organizations/${orgId}/data-export/${dataExportId}/`]];
  61. }
  62. getActionLink(queryType): string {
  63. const {orgId} = this.props.params;
  64. switch (queryType) {
  65. case ExportQueryType.ISSUES_BY_TAG:
  66. return `/organizations/${orgId}/issues/`;
  67. case ExportQueryType.DISCOVER:
  68. return `/organizations/${orgId}/discover/queries/`;
  69. default:
  70. return '/';
  71. }
  72. }
  73. renderDate(date: string | undefined): React.ReactNode {
  74. if (!date) {
  75. return null;
  76. }
  77. const d = new Date(date);
  78. return (
  79. <strong>
  80. <DateTime date={d} />
  81. </strong>
  82. );
  83. }
  84. renderEarly(): React.ReactNode {
  85. return (
  86. <Fragment>
  87. <Header>
  88. <h3>
  89. {t('What are')}
  90. <i>{t(' you ')}</i>
  91. {t('doing here?')}
  92. </h3>
  93. </Header>
  94. <Body>
  95. <p>
  96. {t(
  97. "Not that its any of our business, but were you invited to this page? It's just that we don't exactly remember emailing you about it."
  98. )}
  99. </p>
  100. <p>{t("Close this window and we'll email you when your download is ready.")}</p>
  101. </Body>
  102. </Fragment>
  103. );
  104. }
  105. renderExpired(): React.ReactNode {
  106. const {query} = this.state.download;
  107. const actionLink = this.getActionLink(query.type);
  108. return (
  109. <Fragment>
  110. <Header>
  111. <h3>{t('This is awkward.')}</h3>
  112. </Header>
  113. <Body>
  114. <p>
  115. {t(
  116. "That link expired, so your download doesn't live here anymore. Just picked up one day and left town."
  117. )}
  118. </p>
  119. <p>
  120. {t(
  121. 'Make a new one with your latest data. Your old download will never see it coming.'
  122. )}
  123. </p>
  124. <DownloadButton href={actionLink} priority="primary">
  125. {t('Start a New Download')}
  126. </DownloadButton>
  127. </Body>
  128. </Fragment>
  129. );
  130. }
  131. openInDiscover() {
  132. const {
  133. download: {
  134. query: {info},
  135. },
  136. } = this.state;
  137. const {orgId} = this.props.params;
  138. const to = {
  139. pathname: `/organizations/${orgId}/discover/results/`,
  140. query: info,
  141. };
  142. browserHistory.push(normalizeUrl(to));
  143. }
  144. renderOpenInDiscover() {
  145. const {
  146. download: {
  147. query = {
  148. type: ExportQueryType.ISSUES_BY_TAG,
  149. info: {},
  150. },
  151. },
  152. } = this.state;
  153. // default to IssuesByTag because we don't want to
  154. // display this unless we're sure its a discover query
  155. const {type = ExportQueryType.ISSUES_BY_TAG} = query;
  156. return type === 'Discover' ? (
  157. <Fragment>
  158. <p>{t('Need to make changes?')}</p>
  159. <Button priority="primary" onClick={() => this.openInDiscover()}>
  160. {t('Open in Discover')}
  161. </Button>
  162. <br />
  163. </Fragment>
  164. ) : null;
  165. }
  166. renderValid(): React.ReactNode {
  167. const {
  168. download: {dateExpired, checksum},
  169. } = this.state;
  170. const {orgId, dataExportId} = this.props.params;
  171. return (
  172. <Fragment>
  173. <Header>
  174. <h3>{t('All done.')}</h3>
  175. </Header>
  176. <Body>
  177. <p>{t("See, that wasn't so bad. Your data is all ready for download.")}</p>
  178. <Button
  179. priority="primary"
  180. icon={<IconDownload />}
  181. href={`/api/0/organizations/${orgId}/data-export/${dataExportId}/?download=true`}
  182. >
  183. {t('Download CSV')}
  184. </Button>
  185. <p>
  186. {t("That link won't last forever — it expires:")}
  187. <br />
  188. {this.renderDate(dateExpired)}
  189. </p>
  190. {this.renderOpenInDiscover()}
  191. <p>
  192. <small>
  193. <strong>SHA1:{checksum}</strong>
  194. </small>
  195. <br />
  196. {tct('Need help verifying? [link].', {
  197. link: (
  198. <a
  199. href="https://docs.sentry.io/product/discover-queries/query-builder/#filter-by-table-columns"
  200. target="_blank"
  201. rel="noopener noreferrer"
  202. >
  203. {t('Check out our docs')}
  204. </a>
  205. ),
  206. })}
  207. </p>
  208. </Body>
  209. </Fragment>
  210. );
  211. }
  212. renderError(): React.ReactNode {
  213. const {
  214. errors: {download: err},
  215. } = this.state;
  216. const errDetail = err?.responseJSON?.detail;
  217. return (
  218. <Layout>
  219. <main>
  220. <Header>
  221. <h3>
  222. {err.status} - {err.statusText}
  223. </h3>
  224. </Header>
  225. {errDetail && (
  226. <Body>
  227. <p>{errDetail}</p>
  228. </Body>
  229. )}
  230. </main>
  231. </Layout>
  232. );
  233. }
  234. renderContent(): React.ReactNode {
  235. const {download} = this.state;
  236. switch (download.status) {
  237. case DownloadStatus.EARLY:
  238. return this.renderEarly();
  239. case DownloadStatus.EXPIRED:
  240. return this.renderExpired();
  241. default:
  242. return this.renderValid();
  243. }
  244. }
  245. renderBody() {
  246. return (
  247. <Layout>
  248. <main>{this.renderContent()}</main>
  249. </Layout>
  250. );
  251. }
  252. }
  253. const Header = styled('header')`
  254. border-bottom: 1px solid ${p => p.theme.border};
  255. padding: ${space(3)} 40px 0;
  256. h3 {
  257. font-size: 24px;
  258. margin: 0 0 ${space(3)} 0;
  259. }
  260. `;
  261. const Body = styled('div')`
  262. padding: ${space(2)} 40px;
  263. max-width: 500px;
  264. p {
  265. margin: ${space(1.5)} 0;
  266. }
  267. `;
  268. const DownloadButton = styled(Button)`
  269. margin-bottom: ${space(1.5)};
  270. `;
  271. export default DataDownload;