dataDownload.tsx 7.0 KB

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