Skip to content

Commit

Permalink
Merge pull request #778 from hms-dbmi-cellenics/check-file-size
Browse files Browse the repository at this point in the history
[BIOMAGE-1774] Display spinner during sample validation
  • Loading branch information
kafkasl authored Jul 21, 2022
2 parents 17963e6 + 595b385 commit 34cbf65
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/__test__/redux/actions/samples/createSample.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { v4 as uuidv4 } from 'uuid';
import createSample from 'redux/actions/samples/createSample';
import initialSampleState from 'redux/reducers/samples/initialState';
import initialExperimentState, { experimentTemplate } from 'redux/reducers/experiments/initialState';
import 'utils/upload/sampleValidator';

import {
SAMPLES_CREATE, SAMPLES_SAVING, SAMPLES_ERROR, SAMPLES_SAVED,
Expand All @@ -15,6 +16,7 @@ import {
import endUserMessages from 'utils/endUserMessages';
import pushNotificationMessage from 'utils/pushNotificationMessage';

jest.mock('utils/upload/sampleValidator');
pushNotificationMessage.mockImplementation(() => async () => { });

enableFetchMocks();
Expand All @@ -26,6 +28,7 @@ const sampleUuid = 'abc123';
uuidv4.mockImplementation(() => sampleUuid);

const sampleName = 'test sample';
const sample = {};

describe('createSample action', () => {
const experimentId = 'exp234';
Expand Down Expand Up @@ -62,7 +65,7 @@ describe('createSample action', () => {
it('Works correctly with one file being uploaded', async () => {
fetchMock.mockResponse(JSON.stringify({}), { url: 'mockedUrl', status: 200 });

const newUuid = await store.dispatch(createSample(experimentId, sampleName, mockType, ['matrix.tsv.gz']));
const newUuid = await store.dispatch(createSample(experimentId, sampleName, sample, mockType, ['matrix.tsv.gz']));

// Returns a new sampleUuid
expect(newUuid).toEqual(sampleUuid);
Expand All @@ -85,7 +88,7 @@ describe('createSample action', () => {
it('Works correctly with many files being uploaded', async () => {
fetchMock.mockResponse(JSON.stringify({}), { url: 'mockedUrl', status: 200 });

const newUuid = await store.dispatch(createSample(experimentId, sampleName, mockType, ['matrix.tsv.gz', 'features.tsv.gz', 'barcodes.tsv.gz']));
const newUuid = await store.dispatch(createSample(experimentId, sampleName, sample, mockType, ['matrix.tsv.gz', 'features.tsv.gz', 'barcodes.tsv.gz']));

// Returns a new sampleUuid
expect(newUuid).toEqual(sampleUuid);
Expand All @@ -110,7 +113,7 @@ describe('createSample action', () => {

await expect(
store.dispatch(
createSample(experimentId, sampleName, mockType, ['matrix.tsv.gz']),
createSample(experimentId, sampleName, sample, mockType, ['matrix.tsv.gz']),
),
).rejects.toThrow(endUserMessages.ERROR_CREATING_SAMPLE);

Expand All @@ -125,7 +128,7 @@ describe('createSample action', () => {

await expect(
store.dispatch(
createSample(experimentId, sampleName, 'unrecognizable type', ['matrix.tsv.gz', 'features.tsv.gz', 'barcodes.tsv.gz']),
createSample(experimentId, sampleName, sample, 'unrecognizable type', ['matrix.tsv.gz', 'features.tsv.gz', 'barcodes.tsv.gz']),
),
).rejects.toThrow('Sample technology unrecognizable type is not recognized');
});
Expand Down
9 changes: 9 additions & 0 deletions src/redux/actions/samples/createSample.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import { METADATA_DEFAULT_VALUE } from 'redux/reducers/experiments/initialState'
import { sampleTemplate } from 'redux/reducers/samples/initialState';

import UploadStatus from 'utils/upload/UploadStatus';
import pushNotificationMessage from 'utils/pushNotificationMessage';
import validate from 'utils/upload/sampleValidator';

const createSample = (
experimentId,
name,
sample,
type,
filesToUpload,
) => async (dispatch, getState) => {
Expand Down Expand Up @@ -52,6 +55,12 @@ const createSample = (
} else {
throw new Error(`Sample technology ${type} is not recognized`);
}
const errors = await validate(sample);
if (errors && errors.length > 0) {
const errorMessage = `Error uploading sample ${name}.\n${errors.join('\n')}`;
pushNotificationMessage('error', errorMessage, 15);
throw new Error(errorMessage);
}

filesToUpload.forEach((fileName) => {
newSample.files[fileName] = { upload: { status: UploadStatus.UPLOADING } };
Expand Down
2 changes: 1 addition & 1 deletion src/redux/actions/samples/createSampleFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const createSampleFile = (

return signedUrl;
} catch (e) {
// Can't update the upload status becuase we didn't even get to create the sample file
// Can't update the upload status because we didn't even get to create the sample file
handleError(e, endUserMessages.ERROR_BEGIN_SAMPLE_FILE_UPLOAD);

throw e;
Expand Down
11 changes: 1 addition & 10 deletions src/utils/upload/processUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import _ from 'lodash';
import axios from 'axios';

import { createSample, createSampleFile, updateSampleFileUpload } from 'redux/actions/samples';
import validate from 'utils/upload/sampleValidator';

import UploadStatus from 'utils/upload/UploadStatus';
import loadAndCompressIfNecessary from 'utils/upload/loadAndCompressIfNecessary';
import { inspectFile, Verdict } from 'utils/upload/fileInspector';

import getFileTypeV2 from 'utils/getFileTypeV2';
import pushNotificationMessage from 'utils/pushNotificationMessage';

const putInS3 = async (loadedFileData, signedUrl, onUploadProgress) => (
await axios.request({
Expand Down Expand Up @@ -131,22 +129,15 @@ const processUpload = async (filesList, sampleType, samples, experimentId, dispa
}, {});

Object.entries(samplesMap).forEach(async ([name, sample]) => {
const errors = await validate(sample);

const filesToUploadForSample = Object.keys(sample.files);

if (errors && errors.length > 0) {
const errorMessage = errors.join('\n');
pushNotificationMessage('error', `Error uploading sample ${name}.\n${errorMessage}`, 15);
return;
}

// Create sample if not exists.
try {
sample.uuid ??= await dispatch(
createSample(
experimentId,
name,
sample,
sampleType,
filesToUploadForSample,
),
Expand Down

0 comments on commit 34cbf65

Please sign in to comment.