Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DT-966: Partition TDR Snapshot Query #2712

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/libs/ajax/TerraDataRepo.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import { Config } from '../config';
import axios from 'axios';
import { sleep, reportError } from '../ajax';

import {partition} from '../utils';

export const TerraDataRepo = {
listSnapshotsByDatasetIds: async (identifiers) => {
// Note that TDR is expecting dataset identifiers, not dataset ids
const url = `${await Config.getTdrApiUrl()}/api/repository/v1/snapshots?duosDatasetIds=${identifiers.join('&duosDatasetIds=')}`;
const res = await axios.get(url, Config.authOpts());
return res.data;
// We can hit max url length (2048) with many identifiers so partition them to a reasonable number and recombine
// back into the EnumerateSnapshotModel schema: https://data.terra.bio/swagger-ui.html#/snapshots/enumerateSnapshots
// DUOS ID + param (?duosDatasetIds=DUOS-000852): 27 chars
// ~70 should a safe default at 1890 characters
const partitionedIdentifiers = partition(identifiers, 70);
let enumerateSnapshotModel = {
total: 0,
filteredTotal: 0,
items: [],
roleMap: {},
errors: []
};
for await (const sublist of partitionedIdentifiers) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes each async call synchronously. Since each call is independent, they could all be made in parallel which should make this code faster. Would it be hard to change this to use a Promise.all() construct instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes each async call synchronously. Since each call is independent, they could all be made in parallel which should make this code faster. Would it be hard to change this to use a Promise.all() construct instead?

Yes, I think that shouldn't be too much of a lift

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pshapiro4broad - PTAL, the requests are now all executed in parallel.

// Note that TDR is expecting dataset identifiers, not dataset ids
const url = `${await Config.getTdrApiUrl()}/api/repository/v1/snapshots?duosDatasetIds=${sublist.join('&duosDatasetIds=')}`;
const res = await axios.get(url, Config.authOpts());
enumerateSnapshotModel.total = res.data.total;
enumerateSnapshotModel.filteredTotal += res.data.filteredTotal;
Object.assign(enumerateSnapshotModel.roleMap, res.data.roleMap);
enumerateSnapshotModel.items.push(...res.data.items);
enumerateSnapshotModel.errors.push(...res.data.errors);
}
return enumerateSnapshotModel;
},

prepareExport: async (snapshotId) => {
Expand Down
14 changes: 11 additions & 3 deletions src/libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ export const Notifications = {
*/
export const PromiseSerial = funcs =>
funcs.reduce((promise, func) =>
promise.then(result =>
func().then(Array.prototype.concat.bind(result))),
Promise.resolve([]));
promise.then(result =>
func().then(Array.prototype.concat.bind(result))),
Promise.resolve([]));

//////////////////////////////////
//DAR CONSOLES UTILITY FUNCTIONS//
Expand Down Expand Up @@ -638,3 +638,11 @@ export const hasDataSubmitterRole = (user) => {
const dsRole = find({'roleId': 8})(roles);
return !isNil(dsRole);
};

export const partition = (array, size) => {
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
};
Loading