dataDownload.tsx 6.9 KB

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