Skip to content

Commit

Permalink
#6524 add dataset size api
Browse files Browse the repository at this point in the history
  • Loading branch information
sekmiller committed Jan 29, 2020
1 parent 4f74e92 commit 802ae7b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
import edu.harvard.iq.dataverse.batch.util.LoggingUtil;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.engine.command.exception.UnforcedCommandException;
import edu.harvard.iq.dataverse.engine.command.impl.GetDatasetStorageSizeCommand;
import edu.harvard.iq.dataverse.engine.command.impl.GetDataverseStorageSizeCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDvObjectPIDMetadataCommand;
import edu.harvard.iq.dataverse.makedatacount.DatasetExternalCitations;
import edu.harvard.iq.dataverse.makedatacount.DatasetExternalCitationsServiceBean;
Expand All @@ -102,6 +104,7 @@
import java.io.InputStream;
import java.io.StringReader;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -1796,6 +1799,14 @@ public Response getMakeDataCountMetricCurrentMonth(@PathParam("id") String idSup
String nullCurrentMonth = null;
return getMakeDataCountMetric(idSupplied, metricSupplied, nullCurrentMonth, country);
}

@GET
@Path("{identifier}/storagesize")
public Response getStorageSize(@PathParam("identifier") String dvIdtf, @QueryParam("includeCached") boolean includeCached) throws WrappedResponse {

return response(req -> ok(MessageFormat.format(BundleUtil.getStringFromBundle("datasets.api.datasize"),
execCommand(new GetDatasetStorageSizeCommand(req, findDatasetOrDie(dvIdtf), includeCached)))));
}

@GET
@Path("{id}/makeDataCount/{metric}/{yyyymm}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.harvard.iq.dataverse.engine.command.impl;

import edu.harvard.iq.dataverse.Dataset;
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.util.BundleUtil;
import java.io.IOException;
import java.util.logging.Logger;

/**
*
* @author skraffmi
*/
@RequiredPermissions({})
public class GetDatasetStorageSizeCommand extends AbstractCommand<Long> {

private static final Logger logger = Logger.getLogger(GetDataverseStorageSizeCommand.class.getCanonicalName());

private final Dataset dataset;
private final Boolean countCachedFiles;

public GetDatasetStorageSizeCommand(DataverseRequest aRequest, Dataset target) {
super(aRequest, target);
dataset = target;
countCachedFiles = false;
}

public GetDatasetStorageSizeCommand(DataverseRequest aRequest, Dataset target, boolean countCachedFiles) {
super(aRequest, target);
dataset = target;
this.countCachedFiles = countCachedFiles;
}

@Override
public Long execute(CommandContext ctxt) throws CommandException {
logger.fine("getDataverseStorageSize called on " + dataset.getDisplayName());

long total = 0L;
if (dataset == null) {
// should never happen - must indicate some data corruption in the database
throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.listing.error"), this);
}

try {
total += ctxt.datasets().findStorageSize(dataset, countCachedFiles);
} catch (IOException ex) {
throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.datasize.ioerror"), this);
}

return total;
}

}
4 changes: 4 additions & 0 deletions src/main/java/propertyFiles/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,10 @@ datasets.api.updatePIDMetadata.success.for.update.all=All Dataset PID Metadata u
datasets.api.moveDataset.error.targetDataverseNotFound=Target dataverse not found.
datasets.api.moveDataset.error.suggestForce=Use the query parameter forceMove=true to complete the move.
datasets.api.moveDataset.success=Dataset moved successfully.
datasets.api.listing.error=Fatal error trying to list the contents of the dataset. Please report this error to the Dataverse administrator.
datasets.api.datasize=Total size of the files stored in this dataset: {0} bytes
datasets.api.datasize.ioerror=Fatal IO error while trying to determine the total size of the files stored in the dataset. Please report this error to the Dataverse administrator.


#Dataverses.java
dataverses.api.update.default.contributor.role.failure.role.not.found=Role {0} not found.
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,14 @@ public void testDataSizeInDataverse() throws InterruptedException {
dvStorageSizeResponse.prettyPrint();

assertEquals(magicControlString, JsonPath.from(dvStorageSizeResponse.body().asString()).getString("data.message"));

magicControlString = MessageFormat.format(BundleUtil.getStringFromBundle("datasets.api.datasize"), magicSizeNumber);

Response datasetStorageSizeResponse = UtilIT.findDatasetStorageSize(datasetId.toString());
datasetStorageSizeResponse.prettyPrint();

assertEquals(magicControlString, JsonPath.from(datasetStorageSizeResponse.body().asString()).getString("data.message"));

}

@Test
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2295,4 +2295,17 @@ static Response findDataverseStorageSize(String dataverseId, String apiToken) {
.header(API_TOKEN_HTTP_HEADER, apiToken)
.get("/api/dataverses/" + dataverseId + "/storagesize");
}


/**
* Determine the "payload" storage size of a dataverse
*
* @param dataverseId
* @param apiToken
* @return response
*/
static Response findDatasetStorageSize(String datasetId) {
return given()
.get("/api/datasets/" + datasetId + "/storagesize");
}
}

0 comments on commit 802ae7b

Please sign in to comment.