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

feat: Generate unique and descriptive filenames for download #3021

Merged
merged 10 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"chart.js": "^4.4.6",
"flowbite-react": "^0.10.2",
"jsonwebtoken": "^9.0.2",
"just-kebab-case": "^4.2.0",
"jwks-rsa": "^3.1.0",
"luxon": "^3.5.0",
"neverthrow": "^8.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { type FC, type MouseEventHandler, useMemo } from 'react';

import type { DownloadParameters } from './DownloadParameters.tsx';
import { type DownloadOption, generateDownloadUrl } from './generateDownloadUrl.ts';
import { type DownloadOption, type DownloadUrlGenerator } from './DownloadUrlGenerator.ts';
import { approxMaxAcceptableUrlLength } from '../../../routes/routes.ts';

type DownloadButtonProps = {
downloadUrlGenerator: DownloadUrlGenerator;
downloadOption: DownloadOption | undefined;
lapisUrl: string;
downloadParameters: DownloadParameters;
disabled?: boolean;
onClick?: () => void;
};

export const DownloadButton: FC<DownloadButtonProps> = ({
downloadUrlGenerator,
downloadOption,
lapisUrl,
downloadParameters,
disabled = false,
onClick,
Expand All @@ -33,7 +33,7 @@ export const DownloadButton: FC<DownloadButtonProps> = ({
};
}

const { url, baseUrl, params } = generateDownloadUrl(downloadParameters, downloadOption, lapisUrl);
const { url, baseUrl, params } = downloadUrlGenerator.generateDownloadUrl(downloadParameters, downloadOption);
const useGet = url.length <= approxMaxAcceptableUrlLength;
if (useGet) {
return {
Expand All @@ -52,7 +52,7 @@ export const DownloadButton: FC<DownloadButtonProps> = ({
}
},
};
}, [downloadOption, disabled, downloadParameters, lapisUrl, onClick]);
}, [downloadUrlGenerator, downloadOption, disabled, downloadParameters, onClick]);

return (
<a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export type DownloadDataType =
| { type: 'metadata' }
| { type: 'unalignedNucleotideSequences'; segment?: string }
| { type: 'alignedNucleotideSequences'; segment?: string }
| { type: 'alignedAminoAcidSequences'; gene: string };

/**
* Get a shortened kebab-case datatype including the gene and sequence as well.
*/
export const dataTypeForFilename = (dataType: DownloadDataType): string => {
switch (dataType.type) {
case 'metadata':
return 'metadata';
case 'unalignedNucleotideSequences':
// segment is undefined in case of single segmented (not e.g. main)
return dataType.segment !== undefined ? `nuc-${dataType.segment}` : 'nuc';
fhennig marked this conversation as resolved.
Show resolved Hide resolved
fhennig marked this conversation as resolved.
Show resolved Hide resolved
case 'alignedNucleotideSequences':
return dataType.segment !== undefined ? `aligned-nuc-${dataType.segment}` : 'aligned-nuc';
case 'alignedAminoAcidSequences':
return `aligned-aa-${dataType.gene}`;
}
};

/**
* Get the LAPIS endpoint where to download this data type from.
*/
export const getEndpoint = (dataType: DownloadDataType) => {
const segmentPath = (segment?: string) => (segment !== undefined ? `/${segment}` : '');

switch (dataType.type) {
case 'metadata':
return '/sample/details';
case 'unalignedNucleotideSequences':
return '/sample/unalignedNucleotideSequences' + segmentPath(dataType.segment);
case 'alignedNucleotideSequences':
return '/sample/alignedNucleotideSequences' + segmentPath(dataType.segment);
case 'alignedAminoAcidSequences':
return `/sample/alignedAminoAcidSequences/${dataType.gene}`;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { beforeAll, describe, expect, test, vi } from 'vitest';

import { DownloadDialog } from './DownloadDialog.tsx';
import type { DownloadParameters } from './DownloadParameters.tsx';
import { DownloadUrlGenerator } from './DownloadUrlGenerator.ts';
import type { ReferenceGenomesSequenceNames, ReferenceAccession } from '../../../types/referencesGenomes.ts';

const defaultAccession: ReferenceAccession = {
Expand All @@ -18,13 +19,14 @@ const defaultReferenceGenome: ReferenceGenomesSequenceNames = {
};

const defaultLapisUrl = 'https://lapis';
const defaultOrganism = 'ebola';
fhennig marked this conversation as resolved.
Show resolved Hide resolved

async function renderDialog(downloadParams: DownloadParameters = { type: 'select', selectedSequences: new Set([]) }) {
render(
<DownloadDialog
downloadUrlGenerator={new DownloadUrlGenerator(defaultOrganism, defaultLapisUrl)}
downloadParams={downloadParams}
referenceGenomesSequenceNames={defaultReferenceGenome}
lapisUrl={defaultLapisUrl}
/>,
);

Expand Down Expand Up @@ -66,21 +68,29 @@ describe('DownloadDialog', () => {
});
await checkAgreement();

expect(getDownloadHref()).toBe(
`${defaultLapisUrl}/sample/details?downloadAsFile=true&versionStatus=LATEST_VERSION&isRevocation=false&dataUseTerms=OPEN&dataFormat=tsv&accession=accession1&accession=accession2&field1=value1`,
let [path, query] = getDownloadHref()?.split('?') ?? [];
expect(path).toBe(`${defaultLapisUrl}/sample/details`);
expect(query).toMatch(
/downloadAsFile=true&downloadFileBasename=ebola_metadata_\d{4}-\d{2}-\d{2}T\d{4}&versionStatus=LATEST_VERSION&isRevocation=false&dataUseTerms=OPEN&dataFormat=tsv&accession=accession1&accession=accession2&field1=value1/,
fhennig marked this conversation as resolved.
Show resolved Hide resolved
);

await userEvent.click(screen.getByLabelText(/Yes, include older versions/));
await userEvent.click(screen.getByLabelText(/Raw nucleotide sequences/));
await userEvent.click(screen.getByLabelText(/Gzip/));
expect(getDownloadHref()).toBe(
`${defaultLapisUrl}/sample/unalignedNucleotideSequences?downloadAsFile=true&dataUseTerms=OPEN&compression=gzip&accession=accession1&accession=accession2&field1=value1`,

[path, query] = getDownloadHref()?.split('?') ?? [];
expect(path).toBe(`${defaultLapisUrl}/sample/unalignedNucleotideSequences`);
expect(query).toMatch(
/downloadAsFile=true&downloadFileBasename=ebola_nuc_\d{4}-\d{2}-\d{2}T\d{4}&dataUseTerms=OPEN&compression=gzip&accession=accession1&accession=accession2&field1=value1/,
);

await userEvent.click(screen.getByLabelText(/include restricted data/));
await userEvent.click(screen.getByLabelText(/Zstandard/));
expect(getDownloadHref()).toBe(
`${defaultLapisUrl}/sample/unalignedNucleotideSequences?downloadAsFile=true&compression=zstd&accession=accession1&accession=accession2&field1=value1`,

[path, query] = getDownloadHref()?.split('?') ?? [];
expect(path).toBe(`${defaultLapisUrl}/sample/unalignedNucleotideSequences`);
expect(query).toMatch(
/downloadAsFile=true&downloadFileBasename=ebola_nuc_\d{4}-\d{2}-\d{2}T\d{4}&compression=zstd&accession=accession1&accession=accession2&field1=value1/,
);
});

Expand All @@ -91,21 +101,29 @@ describe('DownloadDialog', () => {
});
await checkAgreement();

expect(getDownloadHref()).toBe(
`${defaultLapisUrl}/sample/details?downloadAsFile=true&versionStatus=LATEST_VERSION&isRevocation=false&dataUseTerms=OPEN&dataFormat=tsv&accessionVersion=SEQID1&accessionVersion=SEQID2`,
let [path, query] = getDownloadHref()?.split('?') ?? [];
expect(path).toBe(`${defaultLapisUrl}/sample/details`);
expect(query).toMatch(
/downloadAsFile=true&downloadFileBasename=ebola_metadata_\d{4}-\d{2}-\d{2}T\d{4}&versionStatus=LATEST_VERSION&isRevocation=false&dataUseTerms=OPEN&dataFormat=tsv&accessionVersion=SEQID1&accessionVersion=SEQID2/,
);

await userEvent.click(screen.getByLabelText(/Yes, include older versions/));
await userEvent.click(screen.getByLabelText(/Raw nucleotide sequences/));
await userEvent.click(screen.getByLabelText(/Gzip/));
expect(getDownloadHref()).toBe(
`${defaultLapisUrl}/sample/unalignedNucleotideSequences?downloadAsFile=true&dataUseTerms=OPEN&compression=gzip&accessionVersion=SEQID1&accessionVersion=SEQID2`,

[path, query] = getDownloadHref()?.split('?') ?? [];
expect(path).toBe(`${defaultLapisUrl}/sample/unalignedNucleotideSequences`);
expect(query).toMatch(
/downloadAsFile=true&downloadFileBasename=ebola_nuc_\d{4}-\d{2}-\d{2}T\d{4}&dataUseTerms=OPEN&compression=gzip&accessionVersion=SEQID1&accessionVersion=SEQID2/,
);

await userEvent.click(screen.getByLabelText(/include restricted data/));
await userEvent.click(screen.getByLabelText(/Zstandard/));
expect(getDownloadHref()).toBe(
`${defaultLapisUrl}/sample/unalignedNucleotideSequences?downloadAsFile=true&compression=zstd&accessionVersion=SEQID1&accessionVersion=SEQID2`,

[path, query] = getDownloadHref()?.split('?') ?? [];
expect(path).toBe(`${defaultLapisUrl}/sample/unalignedNucleotideSequences`);
expect(query).toMatch(
/downloadAsFile=true&downloadFileBasename=ebola_nuc_\d{4}-\d{2}-\d{2}T\d{4}&compression=zstd&accessionVersion=SEQID1&accessionVersion=SEQID2/,
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { DownloadDialogButton } from './DowloadDialogButton.tsx';
import { DownloadButton } from './DownloadButton.tsx';
import { DownloadForm } from './DownloadForm.tsx';
import type { DownloadParameters } from './DownloadParameters.tsx';
import { type DownloadOption } from './generateDownloadUrl.ts';
import { type DownloadUrlGenerator, type DownloadOption } from './DownloadUrlGenerator.ts';
import { routes } from '../../../routes/routes.ts';
import type { ReferenceGenomesSequenceNames } from '../../../types/referencesGenomes.ts';

type DownloadDialogProps = {
downloadUrlGenerator: DownloadUrlGenerator;
downloadParams: DownloadParameters;
referenceGenomesSequenceNames: ReferenceGenomesSequenceNames;
lapisUrl: string;
};

export const DownloadDialog: FC<DownloadDialogProps> = ({
downloadUrlGenerator,
downloadParams,
referenceGenomesSequenceNames,
lapisUrl,
}) => {
const [isOpen, setIsOpen] = useState(false);
const [downloadOption, setDownloadOption] = useState<DownloadOption | undefined>();
Expand Down Expand Up @@ -83,10 +83,10 @@ export const DownloadDialog: FC<DownloadDialogProps> = ({
</label>
</div>
<DownloadButton
disabled={!agreedToDataUseTerms}
lapisUrl={lapisUrl}
downloadUrlGenerator={downloadUrlGenerator}
downloadOption={downloadOption}
downloadParameters={downloadParams}
disabled={!agreedToDataUseTerms}
onClick={closeDialog}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { type FC, useEffect, useState } from 'react';

import type { DownloadDataType } from './DownloadDataType.ts';
import type { DownloadOption } from './DownloadUrlGenerator.ts';
import { DropdownOptionBlock, RadioOptionBlock } from './OptionBlock.tsx';
import type { DownloadDataType, DownloadOption } from './generateDownloadUrl.ts';
import { routes } from '../../../routes/routes.ts';
import type { ReferenceGenomesSequenceNames } from '../../../types/referencesGenomes.ts';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import kebabCase from 'just-kebab-case';

import { getEndpoint, dataTypeForFilename, type DownloadDataType } from './DownloadDataType.ts';
import type { DownloadParameters } from './DownloadParameters.tsx';
import { IS_REVOCATION_FIELD, metadataDefaultDownloadDataFormat, VERSION_STATUS_FIELD } from '../../../settings.ts';
import { versionStatuses } from '../../../types/lapis.ts';

export type Compression = 'zstd' | 'gzip' | undefined;

export type DownloadOption = {
includeOldData: boolean;
includeRestricted: boolean;
dataType: DownloadDataType;
compression: Compression;
};

/**
* Given download parameters and options, generates matching download URLs
* from which the selected data can be downloaded.
*/
export class DownloadUrlGenerator {
private readonly organism: string;
private readonly lapisUrl: string;

/**
* Create new DownloadUrlGenerator with the given properties.
* @param organism The organism, will be part of the filename.
* @param lapisUrl The lapis API URL for downloading.
*/
constructor(organism: string, lapisUrl: string) {
this.organism = organism;
this.lapisUrl = lapisUrl;
}

public generateDownloadUrl(downloadParameters: DownloadParameters, option: DownloadOption) {
const baseUrl = `${this.lapisUrl}${getEndpoint(option.dataType)}`;
const params = new URLSearchParams();

params.set('downloadAsFile', 'true');
params.set('downloadFileBasename', this.generateFilename(option.dataType));
if (!option.includeOldData) {
params.set(VERSION_STATUS_FIELD, versionStatuses.latestVersion);
params.set(IS_REVOCATION_FIELD, 'false');
}
if (!option.includeRestricted) {
params.set('dataUseTerms', 'OPEN');
}
if (option.dataType.type === 'metadata') {
params.set('dataFormat', metadataDefaultDownloadDataFormat);
}
if (option.compression !== undefined) {
params.set('compression', option.compression);
}

switch (downloadParameters.type) {
case 'filter':
const lapisSearchParameters = downloadParameters.lapisSearchParameters;
if (lapisSearchParameters.accession !== undefined) {
for (const accession of lapisSearchParameters.accession) {
params.append('accession', accession);
}
}

const mutationKeys = [
'nucleotideMutations',
'aminoAcidMutations',
'nucleotideInsertions',
'aminoAcidInsertions',
];

for (const [key, value] of Object.entries(lapisSearchParameters)) {
// Skip accession and mutations
if (key === 'accession' || mutationKeys.includes(key)) {
continue;
}
const stringValue = String(value);
const trimmedValue = stringValue.trim();
if (trimmedValue.length > 0) {
params.set(key, trimmedValue);
}
}

mutationKeys.forEach((key) => {
if (lapisSearchParameters[key] !== undefined) {
params.set(key, lapisSearchParameters[key].join(','));
}
});
break;
case 'select':
const sortedIds = Array.from(downloadParameters.selectedSequences).sort();
sortedIds.forEach((accessionVersion) => {
params.append('accessionVersion', accessionVersion);
});
break;
}

return {
url: `${baseUrl}?${params}`,
baseUrl,
params,
};
}

private generateFilename(downloadDataType: DownloadDataType): string {
const organism = kebabCase(this.organism);
fhennig marked this conversation as resolved.
Show resolved Hide resolved
const dataType = dataTypeForFilename(downloadDataType);
const timestamp = new Date().toISOString().slice(0, 16).replace(':', '');
return `${organism}_${dataType}_${timestamp}`;
}
}
Loading
Loading