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

Commit 059666d

Browse files
authored
Merge pull request #1262 from BlueBrain/3939/improve-file-size-indication
3939 // improve file size indication
2 parents c5bb8ed + 02fc4eb commit 059666d

File tree

5 files changed

+177
-65
lines changed

5 files changed

+177
-65
lines changed

src/shared/molecules/MyDataTable/MyDataTable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export type TDataSource = {
6868
distribution?: {
6969
contentSize: number;
7070
encodingFormat: string | string[];
71-
label: string | string[];
72-
hasDistribution?: boolean;
71+
label: string;
72+
hasDistribution: boolean;
7373
};
7474
};
7575
type TProps = {

src/shared/organisms/DataPanel/DataPanel.tsx

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ import {
6363
} from '../../molecules/MyDataTable/MyDataTable';
6464

6565
import './styles.less';
66-
import { fileNameForDistributionItem } from '../../../shared/utils/datapanel';
66+
import {
67+
distributionMatchesTypes,
68+
fileNameForDistributionItem,
69+
getSizeOfResourcesToDownload,
70+
} from '../../../shared/utils/datapanel';
6771

6872
type Props = {
6973
authenticated?: boolean;
@@ -133,25 +137,6 @@ function getPathForChildResource(child: any, parent: ResourceObscured) {
133137
return `${parentPathWithoutExtension}/${childNameWithExtension}`;
134138
}
135139

136-
const distributionMatchesTypes = (
137-
distItem: any,
138-
fileExtenstions: string[]
139-
): boolean => {
140-
if (fileExtenstions.length === 0) {
141-
return true;
142-
}
143-
144-
const distributionName = fileNameForDistributionItem(distItem, '');
145-
const distributionExtension =
146-
distributionName
147-
.split('.')
148-
.pop()
149-
?.trim()
150-
.toLowerCase() ?? '';
151-
152-
return fileExtenstions.includes(distributionExtension);
153-
};
154-
155140
function makePayload(resourcesPayload: DownloadResourcePayload[]) {
156141
const archiveId = uuidv4();
157142
const payload: ArchivePayload = {
@@ -161,7 +146,7 @@ function makePayload(resourcesPayload: DownloadResourcePayload[]) {
161146
return { payload, archiveId };
162147
}
163148

164-
type ResourceObscured = {
149+
export type ResourceObscured = {
165150
size: number;
166151
contentType: string | undefined;
167152
distribution:
@@ -493,14 +478,8 @@ const DataPanel: React.FC<Props> = ({}) => {
493478
Boolean(resource.distribution?.contentSize)
494479
? 'File'
495480
: 'Resource';
496-
// TODO: Add this as part of localstorage object
497-
const contentType = resource.distribution
498-
? isArray(resource.distribution?.label)
499-
? resource.distribution?.label[0].split('.').pop()
500-
: resource.distribution?.label?.split('.').pop() ?? ''
501-
: type === 'Resource'
502-
? 'json'
503-
: '';
481+
const contentType =
482+
resource.distribution?.label?.split('.').pop() ?? '';
504483
return {
505484
size,
506485
contentType: contentType?.toLowerCase(),
@@ -570,15 +549,8 @@ const DataPanel: React.FC<Props> = ({}) => {
570549
i => !isEmpty(i) && !isNil(i)
571550
);
572551

573-
const totalSize = sum(
574-
...compact(
575-
flatMap(resultsObject)
576-
.filter(item => {
577-
return distributionMatchesTypes(item?.distribution, types);
578-
})
579-
.map(item => item?.size)
580-
)
581-
);
552+
const totalSize = getSizeOfResourcesToDownload(resultsObject, types);
553+
582554
const parsedData: ParsedNexusUrl | undefined = resourcesObscured.length
583555
? parseURL(resourcesObscured.find(item => !!item!._self)?._self as string)
584556
: undefined;

src/shared/utils/__mocks__/data_panel_download_resource.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,46 @@ export const resourceWithoutDistrition = {
4444
'https://staging.nise.bbp.epfl.ch/nexus/v1/realms/serviceaccounts/users/service-account-nexus-sa',
4545
};
4646

47+
export const fileResourceWithNoDistribution = {
48+
'@context': [
49+
'https://bluebrain.github.io/nexus/contexts/files.json',
50+
'https://bluebrain.github.io/nexus/contexts/metadata.json',
51+
],
52+
'@id':
53+
'https://bbp.epfl.ch/neurosciencegraph/data/938fb28f-6e5c-434f-8d12-572e5c005f73',
54+
'@type': 'File',
55+
_bytes: 318076,
56+
_constrainedBy: 'https://bluebrain.github.io/nexus/schemas/files.json',
57+
_createdAt: '2022-03-31T16:54:52.580Z',
58+
_createdBy:
59+
'https://dev.nise.bbp.epfl.ch/nexus/v1/realms/serviceaccounts/users/service-account-nexus-sa',
60+
_deprecated: false,
61+
_digest: {
62+
_algorithm: 'SHA-256',
63+
_value: '84da895fdc949ae7b7bd55192a8fef3b1cd5721c06b0993339e0cc42997bd3a4',
64+
},
65+
_filename: 'C120501B1-MT-C1_IDRest.zip',
66+
_incoming:
67+
'https://dev.nise.bbp.epfl.ch/nexus/v1/files/copies/sscx/https:%2F%2Fbbp.epfl.ch%2Fneurosciencegraph%2Fdata%2F938fb28f-6e5c-434f-8d12-572e5c005f73/incoming',
68+
_mediaType: 'application/zip',
69+
_origin: 'Client',
70+
_outgoing:
71+
'https://dev.nise.bbp.epfl.ch/nexus/v1/files/copies/sscx/https:%2F%2Fbbp.epfl.ch%2Fneurosciencegraph%2Fdata%2F938fb28f-6e5c-434f-8d12-572e5c005f73/outgoing',
72+
_project: 'https://dev.nise.bbp.epfl.ch/nexus/v1/projects/copies/sscx',
73+
_rev: 1,
74+
_self:
75+
'https://dev.nise.bbp.epfl.ch/nexus/v1/files/copies/sscx/https:%2F%2Fbbp.epfl.ch%2Fneurosciencegraph%2Fdata%2F938fb28f-6e5c-434f-8d12-572e5c005f73',
76+
_storage: {
77+
'@id': 'https://bluebrain.github.io/nexus/vocabulary/diskStorageDefault',
78+
'@type': 'DiskStorage',
79+
_rev: 1,
80+
},
81+
_updatedAt: '2022-03-31T16:54:52.580Z',
82+
_updatedBy:
83+
'https://dev.nise.bbp.epfl.ch/nexus/v1/realms/serviceaccounts/users/service-account-nexus-sa',
84+
_uuid: '5673b3ae-bdd6-43b4-a3ca-aa6c771edbfa',
85+
};
86+
4787
export const resourceWithDistributionArray = {
4888
'@context': [
4989
'https://bluebrain.github.io/nexus/contexts/metadata.json',

src/shared/utils/__tests__/datapanel.spec.ts

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
fileResourceWithNoDistribution,
23
resourceWithDistributionArray,
34
resourceWithDistributionObject,
45
resourceWithoutDistrition,
@@ -11,9 +12,16 @@ describe('ToLocalStorageResources', () => {
1112
resourceWithoutDistrition,
1213
'studios'
1314
);
15+
const expectedParentDistributionValue = {
16+
hasDistribution: false,
17+
contentSize: 0,
18+
encodingFormat: 'json',
19+
label: 'metadata.json',
20+
};
1421

1522
expect(actualLSResources.length).toEqual(1);
16-
expect(actualLSResources[0].distribution).not.toBeDefined();
23+
24+
expect(actualLSResources[0].localStorageType).toEqual('resource');
1725
expect(actualLSResources[0]._self).toEqual(resourceWithoutDistrition._self);
1826
expect(actualLSResources[0].key).toEqual(resourceWithoutDistrition._self);
1927
expect(actualLSResources[0].project).toEqual(
@@ -23,6 +31,27 @@ describe('ToLocalStorageResources', () => {
2331
resourceWithoutDistrition['@type'],
2432
]);
2533
expect(actualLSResources[0].source).toEqual('studios');
34+
expect(actualLSResources[0].distribution).toEqual(
35+
expectedParentDistributionValue
36+
);
37+
});
38+
39+
it('serializes resource of type file with no distribution correctly', () => {
40+
const actualLSResources = toLocalStorageResources(
41+
fileResourceWithNoDistribution,
42+
'my-data'
43+
);
44+
expect(actualLSResources.length).toEqual(1);
45+
const expectedParentDistributionValue = {
46+
hasDistribution: false,
47+
contentSize: fileResourceWithNoDistribution._bytes,
48+
encodingFormat: fileResourceWithNoDistribution._mediaType,
49+
label: fileResourceWithNoDistribution._filename,
50+
};
51+
expect(actualLSResources[0].distribution).toEqual(
52+
expectedParentDistributionValue
53+
);
54+
expect(actualLSResources[0].source).toEqual('my-data');
2655
});
2756

2857
it('serializes resources with distribution array correctly to local storage object', () => {
@@ -34,8 +63,8 @@ describe('ToLocalStorageResources', () => {
3463
const expectedParentDistributionValue = {
3564
hasDistribution: true,
3665
contentSize: 0,
37-
encodingFormat: '',
38-
label: '',
66+
encodingFormat: 'json',
67+
label: 'metadata.json',
3968
};
4069

4170
expect(actualParentDistributionValue).toEqual(
@@ -59,6 +88,7 @@ describe('ToLocalStorageResources', () => {
5988
encodingFormat: originalDistItems[index].encodingFormat,
6089
label: originalDistItems[index].name,
6190
};
91+
expect(actualDistItem.localStorageType).toEqual('distribution');
6292
expect(actualDistItem.distribution).toEqual(expectedDistributionValue);
6393
expect(actualDistItem._self).toEqual(resource._self);
6494
expect(actualDistItem.key).toEqual(`${resource._self}-${index}`);
@@ -69,19 +99,32 @@ describe('ToLocalStorageResources', () => {
6999
const resource = resourceWithDistributionObject;
70100
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
71101

72-
expect(actualSerializedItems.length).toEqual(1);
73-
const expectedDistributionValue = {
102+
expect(actualSerializedItems.length).toEqual(2);
103+
104+
const expectedDistributionValueForParent = {
105+
hasDistribution: true,
106+
contentSize: 0,
107+
encodingFormat: 'json',
108+
label: 'metadata.json',
109+
};
110+
expect(actualSerializedItems[0].distribution).toEqual(
111+
expectedDistributionValueForParent
112+
);
113+
expect(actualSerializedItems[0].localStorageType).toEqual('resource');
114+
115+
const expectedDistributionValueForChild = {
74116
hasDistribution: true,
75117
contentSize: 15135,
76118
encodingFormat: 'text/turtle',
77119
label: 'molecular-systems.ttl',
78120
};
79-
expect(actualSerializedItems[0].distribution).toEqual(
80-
expectedDistributionValue
121+
expect(actualSerializedItems[1].distribution).toEqual(
122+
expectedDistributionValueForChild
81123
);
82-
expect(actualSerializedItems[0]._self).toEqual(resource._self);
83-
expect(actualSerializedItems[0].key).toEqual(resource._self);
84-
expect(actualSerializedItems[0].project).toEqual(resource._project);
124+
expect(actualSerializedItems[1]._self).toEqual(resource._self);
125+
expect(actualSerializedItems[1].key).toEqual(resource._self);
126+
expect(actualSerializedItems[1].project).toEqual(resource._project);
127+
expect(actualSerializedItems[1].localStorageType).toEqual('distribution');
85128
});
86129

87130
it('serializes resources with distribution object when content size is number', () => {
@@ -94,10 +137,10 @@ describe('ToLocalStorageResources', () => {
94137
};
95138
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
96139

97-
expect(actualSerializedItems[0].distribution?.contentSize).toEqual(123);
140+
expect(actualSerializedItems[1].distribution?.contentSize).toEqual(123);
98141
});
99142

100-
it('serializes resources with distribution object when content size is array', () => {
143+
it('sums up content size for distribution item when it is an array', () => {
101144
const resource = {
102145
...resourceWithDistributionObject,
103146
distribution: {
@@ -107,7 +150,7 @@ describe('ToLocalStorageResources', () => {
107150
};
108151
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
109152

110-
expect(actualSerializedItems[0].distribution?.contentSize).toEqual(30);
153+
expect(actualSerializedItems[1].distribution?.contentSize).toEqual(30);
111154
});
112155

113156
it('serializes resources when distribution is empty array', () => {
@@ -118,8 +161,8 @@ describe('ToLocalStorageResources', () => {
118161
const expectedDistributionValue = {
119162
hasDistribution: true,
120163
contentSize: 0,
121-
encodingFormat: '',
122-
label: '',
164+
encodingFormat: 'json',
165+
label: 'metadata.json',
123166
};
124167

125168
expect(actualSerializedItems[0].distribution).toEqual(
@@ -131,7 +174,7 @@ describe('ToLocalStorageResources', () => {
131174
const resource = { ...resourceWithoutDistrition, distribution: {} };
132175
const actualSerializedItems = toLocalStorageResources(resource, 'studios');
133176

134-
expect(actualSerializedItems.length).toEqual(1);
177+
expect(actualSerializedItems.length).toEqual(2);
135178
expect(actualSerializedItems[0].distribution).toBeDefined();
136179
});
137180
});

0 commit comments

Comments
 (0)