Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit c5bb8ed

Browse files
authored
Merge pull request #1261 from BlueBrain/3939/fetch-distributions-in-my-data
3939 // Fetch resources in my-data when they dont have distributions
2 parents 82114b4 + ffc3719 commit c5bb8ed

File tree

3 files changed

+151
-131
lines changed

3 files changed

+151
-131
lines changed

src/shared/containers/DataTableContainer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ const DataTableContainer: React.FC<DataTableProps> = ({
193193

194194
const [searchboxValue, setSearchboxValue] = useState<string>('');
195195
const [searchboxFocused, setSearchboxFocused] = useState<boolean>(false);
196+
const [fetchingRowsForDownlaod, setFetchingRowsForDownload] = useState<
197+
boolean
198+
>(false);
196199
const nexus = useNexusContext();
197200
const history = useHistory();
198201
const location = useLocation();
@@ -465,7 +468,9 @@ const DataTableContainer: React.FC<DataTableProps> = ({
465468
<Table
466469
bordered
467470
loading={
468-
tableData.dataResult.isLoading || tableData.tableResult.isLoading
471+
tableData.dataResult.isLoading ||
472+
tableData.tableResult.isLoading ||
473+
fetchingRowsForDownlaod
469474
}
470475
rowClassName={'data-table-row'}
471476
title={() => renderTitle(options)}
@@ -487,7 +492,19 @@ const DataTableContainer: React.FC<DataTableProps> = ({
487492
rowSelection={{
488493
selectedRowKeys,
489494
onSelect: tableData.onSelectSingleRow,
490-
onSelectAll: tableData.onSelectAll,
495+
onSelectAll: async (
496+
selected: boolean,
497+
selectedRows: StudioTableRow[],
498+
changedRows: StudioTableRow[]
499+
) => {
500+
setFetchingRowsForDownload(true);
501+
await tableData.onSelectAll(
502+
selected,
503+
selectedRows,
504+
changedRows
505+
);
506+
setFetchingRowsForDownload(false);
507+
},
491508
}}
492509
rowKey={r => getStudioRowKey(r)}
493510
data-testid="dashboard-table"

src/shared/hooks/useAccessDataForTable.tsx

Lines changed: 71 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,7 @@ import { notification } from 'antd';
55
import { ColumnType } from 'antd/lib/table/interface';
66
import * as bodybuilder from 'bodybuilder';
77
import json2csv, { Parser } from 'json2csv';
8-
import {
9-
get,
10-
has,
11-
isArray,
12-
isNil,
13-
isString,
14-
pick,
15-
sum,
16-
sumBy,
17-
toNumber,
18-
uniq,
19-
uniqBy,
20-
} from 'lodash';
8+
import { isArray, isNil, isString, pick, sumBy, toNumber } from 'lodash';
219
import * as React from 'react';
2210
import { useQuery } from 'react-query';
2311
import {
@@ -48,6 +36,7 @@ import { isNumeric, parseJsonMaybe } from '../utils/index';
4836
import { addColumnsForES, rowRender } from '../utils/parseESResults';
4937
import { sparqlQueryExecutor } from '../utils/querySparqlView';
5038
import { CartContext } from './useDataCart';
39+
import PromisePool from '@supercharge/promise-pool';
5140

5241
export const EXPORT_CSV_FILENAME = 'nexus-query-result.csv';
5342
export const CSV_MEDIATYPE = 'text/csv';
@@ -415,6 +404,52 @@ const getTotalContentSize = (rows: TDataSource[]) => {
415404
return size;
416405
};
417406

407+
export const fetchResourceForDownload = async (
408+
selfUrl: string,
409+
nexus: ReturnType<typeof useNexusContext>
410+
): Promise<Resource> => {
411+
let compactResource;
412+
let expandedResource;
413+
414+
try {
415+
compactResource = await nexus.httpGet({
416+
path: selfUrl,
417+
headers: { Accept: 'application/json' },
418+
});
419+
420+
const receivedExpandedId =
421+
isValidUrl(compactResource['@id']) &&
422+
compactResource['@id']?.startsWith('http');
423+
424+
if (receivedExpandedId) {
425+
return compactResource;
426+
}
427+
428+
expandedResource = await nexus.httpGet({
429+
path: `${selfUrl}?format=expanded`,
430+
headers: { Accept: 'application/json' },
431+
});
432+
433+
return {
434+
...compactResource,
435+
['@id']:
436+
expandedResource?.[0]?.['@id'] ??
437+
expandedResource['@id'] ??
438+
compactResource['@id'],
439+
};
440+
} catch (err) {
441+
notification.warning({
442+
message: <>Could not fetch a resource with id for download {selfUrl}</>,
443+
description: <em>{(err as any)?.reason ?? (err as any)?.['@type']}</em>,
444+
key: selfUrl,
445+
});
446+
// @ts-ignore TODO: Remove when we supprot es2022 in ts.
447+
throw new Error(`Could not select resource ${selfUrl} for download`, {
448+
cause: err,
449+
});
450+
}
451+
};
452+
418453
export const useAccessDataForTable = (
419454
orgLabel: string,
420455
projectLabel: string,
@@ -446,34 +481,31 @@ export const useAccessDataForTable = (
446481
let rowsForLS = dataPanelLS?.selectedRows || [];
447482

448483
if (selected) {
449-
rowKeysForLS = [
450-
...rowKeysForLS,
451-
...changedRows.map(row => getStudioRowKey(row)),
452-
];
453-
454-
const futureResources = changedRows.map(row =>
455-
fetchResourceForDownload(getStudioRowKey(row))
456-
);
457-
458-
Promise.allSettled(futureResources)
459-
.then(result => {
460-
result.forEach(res => {
461-
if (res.status === 'fulfilled') {
462-
const localStorageResource = toLocalStorageResources(
463-
res.value,
464-
'studios'
465-
);
466-
rowsForLS = [...rowsForLS, ...localStorageResource];
467-
}
468-
});
484+
const { results, errors } = await PromisePool.withConcurrency(4)
485+
.for(changedRows)
486+
.handleError(async err => {
487+
console.log(
488+
'@@error in selecting multiple resources for download in studios',
489+
err
490+
);
469491
return;
470492
})
471-
.then(() => {
472-
saveSelectedRowsToLocalStorage(rowKeysForLS, rowsForLS);
473-
})
474-
.catch(err => {
475-
console.log('@@error', err);
493+
.process(async row => {
494+
const fetchedRow = await fetchResourceForDownload(
495+
getStudioRowKey(row),
496+
nexus
497+
);
498+
const localStorageResources = toLocalStorageResources(
499+
fetchedRow,
500+
'studios'
501+
);
502+
rowKeysForLS.push(getStudioRowKey(row));
503+
504+
return localStorageResources;
476505
});
506+
507+
rowsForLS = [...rowsForLS, ...results.flat()];
508+
saveSelectedRowsToLocalStorage(rowKeysForLS, rowsForLS);
477509
} else {
478510
const rowKeysToRemove = changedRows.map(row => getStudioRowKey(row));
479511

@@ -500,7 +532,7 @@ export const useAccessDataForTable = (
500532
let localStorageRows = dataPanelLS?.selectedRows || [];
501533

502534
if (selected) {
503-
const deltaResource = await fetchResourceForDownload(recordKey);
535+
const deltaResource = await fetchResourceForDownload(recordKey, nexus);
504536
const localStorageResource = toLocalStorageResources(
505537
deltaResource,
506538
'studios'
@@ -515,53 +547,6 @@ export const useAccessDataForTable = (
515547
saveSelectedRowsToLocalStorage(localStorageRowKeys, localStorageRows);
516548
};
517549

518-
const fetchResourceForDownload = async (
519-
selfUrl: string
520-
): Promise<Resource> => {
521-
let compactResource;
522-
let expandedResource;
523-
524-
try {
525-
compactResource = await nexus.httpGet({
526-
path: selfUrl,
527-
headers: { Accept: 'application/json' },
528-
});
529-
530-
const receivedExpandedId =
531-
isValidUrl(compactResource['@id']) &&
532-
!isUrlCurieFormat(compactResource['@id']);
533-
534-
if (receivedExpandedId) {
535-
return compactResource;
536-
}
537-
538-
expandedResource = await nexus.httpGet({
539-
path: `${selfUrl}?format=expanded`,
540-
headers: { Accept: 'application/json' },
541-
});
542-
543-
return {
544-
...compactResource,
545-
['@id']:
546-
expandedResource?.[0]?.['@id'] ??
547-
expandedResource['@id'] ??
548-
compactResource['@id'],
549-
};
550-
} catch (err) {
551-
notification.warning({
552-
message: (
553-
<div>Could not fetch a resource with id for download {selfUrl}</div>
554-
),
555-
description: <em>{(err as any)?.reason ?? (err as any)?.['@type']}</em>,
556-
key: selfUrl,
557-
});
558-
// @ts-ignore TODO: Remove when we supprot es2022 in ts.
559-
throw new Error(`Could not select resource ${selfUrl} for download`, {
560-
cause: err,
561-
});
562-
}
563-
};
564-
565550
const saveSelectedRowsToLocalStorage = (
566551
rowKeys: React.Key[],
567552
rows: TDataSource[]

0 commit comments

Comments
 (0)