Skip to content

Commit

Permalink
Merge branch 'refs/heads/develop' into gr-DT-969-show-draft-dar-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong committed Nov 5, 2024
2 parents 06a945b + 2bada7b commit df91a12
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 71 deletions.
55 changes: 12 additions & 43 deletions src/components/data_search/DatasetExportButton.jsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
import * as React from 'react';
import { CircularProgress, IconButton, Link } from '@mui/material';
import { useState } from 'react';
import IosShareIcon from '@mui/icons-material/IosShare';
import { TerraDataRepo } from '../../libs/ajax/TerraDataRepo';
import {useEffect, useState} from 'react';
import {Link} from '@mui/material';
import {Config} from '../../libs/config';

export const DatasetExportButton = (props) => {
const { snapshot, title } = props;
// The exportStatus flow is: initial -> prepping -> ready
// TODO: error handling?
const [exportStatus, setExportStatus] = useState('initial');
const [exportResult, setExportResult] = useState(null);
const {snapshot, title} = props;

// Not a supported export location
if (!snapshot) {
return null;
}
const [terraUrl, setTerraUrl] = useState('');

const prepExportHandler = async () => {
setExportStatus('prepping');
const job = await TerraDataRepo.prepareExport(snapshot.id);
const result = await TerraDataRepo.waitForJob(job.id);
setExportResult(result);
setExportStatus('ready');
};
useEffect(() => {
(async () => {
setTerraUrl(await Config.getTerraUrl());
})();
}, []);

if (exportStatus === 'initial') {
return (
<IconButton aria-label="prepare export to Terra" size="medium" onClick={prepExportHandler}>
<IosShareIcon size={15} />
</IconButton>
);
}

if (exportStatus === 'prepping') {
return (
<IconButton aria-label="prepare export to Terra" size="medium" onClick={() => ({})} disabled>
<CircularProgress size={15} />,
</IconButton>
);
}

if (exportStatus === 'ready') {
return (
<Link href={exportResult.terraImportLink} target="_blank" rel="noopener noreferrer" title={title} aria-label={title}>Export</Link>
);
}

return null;
const link = `${terraUrl}/#import-data?snapshotId=${snapshot.id}&format=tdrexport&tdrSyncPermissions=false`;

return <Link style={{marginRight: '5px'}} href={link} target="_blank" rel="noopener noreferrer" title={title} aria-label={title}>Export</Link>;
};

export default DatasetExportButton;
28 changes: 0 additions & 28 deletions src/libs/ajax/TerraDataRepo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Config } from '../config';
import axios from 'axios';
import { sleep, reportError } from '../ajax';


export const TerraDataRepo = {
Expand All @@ -10,31 +9,4 @@ export const TerraDataRepo = {
const res = await axios.get(url, Config.authOpts());
return res.data;
},

prepareExport: async (snapshotId) => {
const url = `${await Config.getTdrApiUrl()}/api/repository/v1/snapshots/${snapshotId}/export`;
const res = await axios.get(url, Config.authOpts());
return res.data;
},

waitForJob: async (jobId) => {
const url = `${await Config.getTdrApiUrl()}/api/repository/v1/jobs/${jobId}`;
const resultsUrl = `${await Config.getTdrApiUrl()}/api/repository/v1/jobs/${jobId}/result`;

// eslint-disable-next-line no-constant-condition
while (true) {
const res = await axios.get(url, Config.authOpts());
if (res.data.job_status === 'running') {
await sleep(1000);
} else if (res.data.job_status === 'succeeded') {
const finalResult = await axios.get(resultsUrl, Config.authOpts());
// Add the URL to link to
finalResult.data.terraImportLink =
`${await Config.getTerraUrl()}/#import-data?url=${window.location.origin}&snapshotId=${finalResult.data.snapshot.id}&format=tdrexport&snapshotName=${finalResult.data.snapshot.name}&tdrmanifest=${encodeURIComponent(finalResult.data.format.parquet.manifest)}&tdrSyncPermissions=false`;
return finalResult.data;
} else if (res.data.job_status === 'failed') {
return reportError(url, res.data.status_code);
}
}
},
};

0 comments on commit df91a12

Please sign in to comment.