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

Add GetDatasetFilesTotalDownloadSize use case #92

Merged
merged 15 commits into from
Oct 17, 2023
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
54 changes: 42 additions & 12 deletions src/files/domain/models/FileCriteria.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
export class FileCriteria {
export class FileSearchCriteria {
constructor(
public readonly orderCriteria: FileOrderCriteria = FileOrderCriteria.NAME_AZ,
public readonly contentType?: string,
public readonly accessStatus?: FileAccessStatus,
public readonly categoryName?: string,
public readonly tabularTagName?: string,
public readonly searchText?: string,
) {}

withOrderCriteria(orderCriteria: FileOrderCriteria): FileCriteria {
return new FileCriteria(orderCriteria, this.contentType, this.accessStatus, this.categoryName);
withContentType(contentType: string | undefined): FileSearchCriteria {
return new FileSearchCriteria(
contentType,
this.accessStatus,
this.categoryName,
this.tabularTagName,
this.searchText,
);
}

withContentType(contentType: string | undefined): FileCriteria {
return new FileCriteria(this.orderCriteria, contentType, this.accessStatus, this.categoryName);
withAccessStatus(accessStatus: FileAccessStatus | undefined): FileSearchCriteria {
return new FileSearchCriteria(
this.contentType,
accessStatus,
this.categoryName,
this.tabularTagName,
this.searchText,
);
}

withAccessStatus(accessStatus: FileAccessStatus | undefined): FileCriteria {
return new FileCriteria(this.orderCriteria, this.contentType, accessStatus, this.categoryName);
withCategoryName(categoryName: string | undefined): FileSearchCriteria {
return new FileSearchCriteria(
this.contentType,
this.accessStatus,
categoryName,
this.tabularTagName,
this.searchText,
);
}

withCategoryName(categoryName: string | undefined): FileCriteria {
return new FileCriteria(this.orderCriteria, this.contentType, this.accessStatus, categoryName);
withTabularTagName(tabularTagName: string | undefined): FileSearchCriteria {
return new FileSearchCriteria(
this.contentType,
this.accessStatus,
this.categoryName,
tabularTagName,
this.searchText,
);
}

withSearchText(searchText: string | undefined): FileCriteria {
return new FileCriteria(this.orderCriteria, this.contentType, this.accessStatus, this.categoryName, searchText);
withSearchText(searchText: string | undefined): FileSearchCriteria {
return new FileSearchCriteria(
this.contentType,
this.accessStatus,
this.categoryName,
this.tabularTagName,
searchText,
);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/files/domain/models/FileDownloadSizeMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum FileDownloadSizeMode {
ALL = 'All',
ORIGINAL = 'Original',
ARCHIVAL = 'Archival',
MellyGray marked this conversation as resolved.
Show resolved Hide resolved
}
13 changes: 11 additions & 2 deletions src/files/domain/repositories/IFilesRepository.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { File } from '../models/File';
import { FileDataTable } from '../models/FileDataTable';
import { FileUserPermissions } from '../models/FileUserPermissions';
import { FileCriteria } from '../models/FileCriteria';
import { FileSearchCriteria, FileOrderCriteria } from '../models/FileCriteria';
import { FileCounts } from '../models/FileCounts';
import { FileDownloadSizeMode } from '../models/FileDownloadSizeMode';

export interface IFilesRepository {
getDatasetFiles(
datasetId: number | string,
datasetVersionId: string,
includeDeaccessioned: boolean,
fileOrderCriteria: FileOrderCriteria,
limit?: number,
offset?: number,
fileCriteria?: FileCriteria,
fileSearchCriteria?: FileSearchCriteria,
): Promise<File[]>;

getDatasetFileCounts(
datasetId: number | string,
datasetVersionId: string,
includeDeaccessioned: boolean,
fileSearchCriteria?: FileSearchCriteria,
): Promise<FileCounts>;

getDatasetFilesTotalDownloadSize(
datasetId: number | string,
datasetVersionId: string,
fileDownloadSizeMode: FileDownloadSizeMode,
): Promise<number>;

getFileDownloadCount(fileId: number | string): Promise<number>;

getFileUserPermissions(fileId: number | string): Promise<FileUserPermissions>;
Expand Down
9 changes: 8 additions & 1 deletion src/files/domain/useCases/GetDatasetFileCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UseCase } from '../../../core/domain/useCases/UseCase';
import { IFilesRepository } from '../repositories/IFilesRepository';
import { DatasetNotNumberedVersion } from '../../../datasets';
import { FileCounts } from '../models/FileCounts';
import { FileSearchCriteria } from '../models/FileCriteria';

export class GetDatasetFileCounts implements UseCase<FileCounts> {
private filesRepository: IFilesRepository;
Expand All @@ -14,7 +15,13 @@ export class GetDatasetFileCounts implements UseCase<FileCounts> {
datasetId: number | string,
datasetVersionId: string | DatasetNotNumberedVersion = DatasetNotNumberedVersion.LATEST,
includeDeaccessioned: boolean = false,
fileSearchCriteria?: FileSearchCriteria,
): Promise<FileCounts> {
return await this.filesRepository.getDatasetFileCounts(datasetId, datasetVersionId, includeDeaccessioned);
return await this.filesRepository.getDatasetFileCounts(
datasetId,
datasetVersionId,
includeDeaccessioned,
fileSearchCriteria,
);
}
}
8 changes: 5 additions & 3 deletions src/files/domain/useCases/GetDatasetFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UseCase } from '../../../core/domain/useCases/UseCase';
import { IFilesRepository } from '../repositories/IFilesRepository';
import { File } from '../models/File';
import { FileCriteria } from '../models/FileCriteria';
import { FileSearchCriteria, FileOrderCriteria } from '../models/FileCriteria';
import { DatasetNotNumberedVersion } from '../../../datasets';

export class GetDatasetFiles implements UseCase<File[]> {
Expand All @@ -17,15 +17,17 @@ export class GetDatasetFiles implements UseCase<File[]> {
includeDeaccessioned: boolean = false,
limit?: number,
offset?: number,
fileCriteria?: FileCriteria,
fileSearchCriteria?: FileSearchCriteria,
fileOrderCriteria: FileOrderCriteria = FileOrderCriteria.NAME_AZ,
): Promise<File[]> {
return await this.filesRepository.getDatasetFiles(
datasetId,
datasetVersionId,
includeDeaccessioned,
fileOrderCriteria,
limit,
offset,
fileCriteria,
fileSearchCriteria,
);
}
}
24 changes: 24 additions & 0 deletions src/files/domain/useCases/GetDatasetFilesTotalDownloadSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { UseCase } from '../../../core/domain/useCases/UseCase';
import { IFilesRepository } from '../repositories/IFilesRepository';
import { DatasetNotNumberedVersion } from '../../../datasets';
import { FileDownloadSizeMode } from '../models/FileDownloadSizeMode';

export class GetDatasetFilesTotalDownloadSize implements UseCase<number> {
private filesRepository: IFilesRepository;

constructor(filesRepository: IFilesRepository) {
this.filesRepository = filesRepository;
}

async execute(
datasetId: number | string,
datasetVersionId: string | DatasetNotNumberedVersion = DatasetNotNumberedVersion.LATEST,
fileDownloadSizeMode: FileDownloadSizeMode = FileDownloadSizeMode.ALL,
): Promise<number> {
return await this.filesRepository.getDatasetFilesTotalDownloadSize(
datasetId,
datasetVersionId,
fileDownloadSizeMode,
);
}
}
14 changes: 12 additions & 2 deletions src/files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GetDatasetFileCounts } from './domain/useCases/GetDatasetFileCounts';
import { GetFileDownloadCount } from './domain/useCases/GetFileDownloadCount';
import { GetFileUserPermissions } from './domain/useCases/GetFileUserPermissions';
import { GetFileDataTables } from './domain/useCases/GetFileDataTables';
import { GetDatasetFilesTotalDownloadSize } from './domain/useCases/GetDatasetFilesTotalDownloadSize';

const filesRepository = new FilesRepository();

Expand All @@ -12,12 +13,20 @@ const getDatasetFileCounts = new GetDatasetFileCounts(filesRepository);
const getFileDownloadCount = new GetFileDownloadCount(filesRepository);
const getFileUserPermissions = new GetFileUserPermissions(filesRepository);
const getFileDataTables = new GetFileDataTables(filesRepository);
const getDatasetFilesTotalDownloadSize = new GetDatasetFilesTotalDownloadSize(filesRepository);

export { getDatasetFiles, getFileDownloadCount, getFileUserPermissions, getFileDataTables, getDatasetFileCounts };
export {
getDatasetFiles,
getFileDownloadCount,
getFileUserPermissions,
getFileDataTables,
getDatasetFileCounts,
getDatasetFilesTotalDownloadSize,
};

export { File, FileEmbargo, FileChecksum } from './domain/models/File';
export { FileUserPermissions } from './domain/models/FileUserPermissions';
export { FileCriteria, FileOrderCriteria, FileAccessStatus } from './domain/models/FileCriteria';
export { FileSearchCriteria, FileOrderCriteria, FileAccessStatus } from './domain/models/FileCriteria';
export {
FileCounts,
FileContentTypeCount,
Expand All @@ -34,3 +43,4 @@ export {
FileDataVariableIntervalType,
FileDataVariableFormatType,
} from './domain/models/FileDataTable';
export { FileDownloadSizeMode } from './domain/models/FileDownloadSizeMode';
64 changes: 47 additions & 17 deletions src/files/infra/repositories/FilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { FileDataTable } from '../../domain/models/FileDataTable';
import { transformDataTablesResponseToDataTables } from './transformers/fileDataTableTransformers';
import { FileUserPermissions } from '../../domain/models/FileUserPermissions';
import { transformFileUserPermissionsResponseToFileUserPermissions } from './transformers/fileUserPermissionsTransformers';
import { FileCriteria } from '../../domain/models/FileCriteria';
import { FileSearchCriteria, FileOrderCriteria } from '../../domain/models/FileCriteria';
import { FileCounts } from '../../domain/models/FileCounts';
import { transformFileCountsResponseToFileCounts } from './transformers/fileCountsTransformers';
import { FileDownloadSizeMode } from '../../domain/models/FileDownloadSizeMode';

export interface GetFilesQueryParams {
includeDeaccessioned: boolean;
Expand All @@ -18,6 +19,7 @@ export interface GetFilesQueryParams {
contentType?: string;
accessStatus?: string;
categoryName?: string;
tabularTagName?: string;
searchText?: string;
}

Expand All @@ -30,21 +32,23 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
datasetId: number | string,
datasetVersionId: string,
includeDeaccessioned: boolean,
fileOrderCriteria: FileOrderCriteria,
limit?: number,
offset?: number,
fileCriteria?: FileCriteria,
fileSearchCriteria?: FileSearchCriteria,
): Promise<File[]> {
const queryParams: GetFilesQueryParams = {
includeDeaccessioned: includeDeaccessioned,
orderCriteria: fileOrderCriteria.toString(),
};
if (limit !== undefined) {
queryParams.limit = limit;
}
if (offset !== undefined) {
queryParams.offset = offset;
}
if (fileCriteria !== undefined) {
this.applyFileCriteriaToQueryParams(queryParams, fileCriteria);
if (fileSearchCriteria !== undefined) {
this.applyFileSearchCriteriaToQueryParams(queryParams, fileSearchCriteria);
}
return this.doGet(
this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/files`, datasetId),
Expand All @@ -61,15 +65,38 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
datasetId: string | number,
datasetVersionId: string,
includeDeaccessioned: boolean,
fileSearchCriteria?: FileSearchCriteria,
): Promise<FileCounts> {
const queryParams: GetFilesQueryParams = {
includeDeaccessioned: includeDeaccessioned,
};
if (fileSearchCriteria !== undefined) {
this.applyFileSearchCriteriaToQueryParams(queryParams, fileSearchCriteria);
}
return this.doGet(
this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/files/counts`, datasetId),
true,
queryParams,
)
.then((response) => transformFileCountsResponseToFileCounts(response))
.catch((error) => {
throw error;
});
}

public async getDatasetFilesTotalDownloadSize(
datasetId: number | string,
datasetVersionId: string,
fileDownloadSizeMode: FileDownloadSizeMode,
): Promise<number> {
return this.doGet(
this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/downloadsize`, datasetId),
true,
{
includeDeaccessioned: includeDeaccessioned,
mode: fileDownloadSizeMode.toString(),
},
)
.then((response) => transformFileCountsResponseToFileCounts(response))
.then((response) => response.data.data.storageSize)
.catch((error) => {
throw error;
});
Expand Down Expand Up @@ -99,21 +126,24 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
});
}

private applyFileCriteriaToQueryParams(queryParams: GetFilesQueryParams, fileCriteria: FileCriteria) {
if (fileCriteria.accessStatus !== undefined) {
queryParams.accessStatus = fileCriteria.accessStatus.toString();
private applyFileSearchCriteriaToQueryParams(
queryParams: GetFilesQueryParams,
fileSearchCriteria: FileSearchCriteria,
) {
if (fileSearchCriteria.accessStatus !== undefined) {
queryParams.accessStatus = fileSearchCriteria.accessStatus.toString();
}
if (fileCriteria.categoryName !== undefined) {
queryParams.categoryName = fileCriteria.categoryName;
if (fileSearchCriteria.categoryName !== undefined) {
queryParams.categoryName = fileSearchCriteria.categoryName;
}
if (fileCriteria.contentType !== undefined) {
queryParams.contentType = fileCriteria.contentType;
if (fileSearchCriteria.tabularTagName !== undefined) {
queryParams.tabularTagName = fileSearchCriteria.tabularTagName;
}
if (fileCriteria.searchText !== undefined) {
queryParams.searchText = fileCriteria.searchText;
if (fileSearchCriteria.contentType !== undefined) {
queryParams.contentType = fileSearchCriteria.contentType;
}
if (fileCriteria.orderCriteria !== undefined) {
queryParams.orderCriteria = fileCriteria.orderCriteria.toString();
if (fileSearchCriteria.searchText !== undefined) {
queryParams.searchText = fileSearchCriteria.searchText;
}
}
}
2 changes: 1 addition & 1 deletion test/integration/environment/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
POSTGRES_VERSION=13
DATAVERSE_DB_USER=dataverse
SOLR_VERSION=8.11.1
SOLR_VERSION=9.3.0
DATAVERSE_IMAGE_REGISTRY=docker.io
DATAVERSE_IMAGE_TAG=unstable
DATAVERSE_BOOTSTRAP_TIMEOUT=5m
Loading
Loading