diff --git a/snapshot-bridge-webapp/src/main/java/org/duracloud/snapshot/bridge/rest/SnapshotResource.java b/snapshot-bridge-webapp/src/main/java/org/duracloud/snapshot/bridge/rest/SnapshotResource.java index 6dcb3462..9342b36e 100644 --- a/snapshot-bridge-webapp/src/main/java/org/duracloud/snapshot/bridge/rest/SnapshotResource.java +++ b/snapshot-bridge-webapp/src/main/java/org/duracloud/snapshot/bridge/rest/SnapshotResource.java @@ -60,6 +60,10 @@ import org.duracloud.snapshot.dto.bridge.GetSnapshotContentBridgeResult; import org.duracloud.snapshot.dto.bridge.GetSnapshotHistoryBridgeResult; import org.duracloud.snapshot.dto.bridge.GetSnapshotListBridgeResult; +import org.duracloud.snapshot.dto.bridge.GetSnapshotTotalCountBridgeResult; +import org.duracloud.snapshot.dto.bridge.GetSnapshotTotalFilesBridgeResult; +import org.duracloud.snapshot.dto.bridge.GetSnapshotTotalSizeBridgeResult; +import org.duracloud.snapshot.dto.bridge.GetSnapshotTotalsBridgeResult; import org.duracloud.snapshot.dto.bridge.RestartSnapshotBridgeResult; import org.duracloud.snapshot.dto.bridge.SnapshotErrorBridgeParameters; import org.duracloud.snapshot.dto.bridge.SnapshotErrorBridgeResult; @@ -202,6 +206,240 @@ protected List listSnapshots(String host, } } + /** + * Returns the total count of snapshots. + * + * @return + */ + @Path("total/count") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response count(@QueryParam("host") String host, + @QueryParam("storeId") String storeId, + @QueryParam("status") SnapshotStatus status) { + try { + long totalCount = getSnapshotsCount(host, storeId, status); + + log.debug("returning {} total count of snapshots", totalCount); + return Response.ok() + .entity(new GetSnapshotTotalCountBridgeResult(totalCount)) + .build(); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return Response.serverError() + .entity(new ResponseDetails(ex.getMessage())) + .build(); + } + } + + /* + * Returns a total count of snapshots. The parameters of host, store ID and status are all + * considered optional, so depending on which are provided (i.e. not null), the + * query used will vary. + */ + protected long getSnapshotsCount(String host, + String storeId, + SnapshotStatus status) { + if (null != host) { + if (null != storeId) { + if (null != status) { // Host & Store ID & Status + return snapshotRepo. + countBySourceHostAndSourceStoreIdAndStatus(host, storeId, status); + } else { // Host & Store ID + return snapshotRepo.countBySourceHostAndSourceStoreId(host, storeId); + } + } else if (null != status) { // Host & Status + return snapshotRepo.countBySourceHostAndStatus(host, status); + } else { // Host + return snapshotRepo.countBySourceHost(host); + } + } else if (null != storeId) { + if (null != status) { // Store ID & Status + return snapshotRepo.countBySourceStoreIdAndStatus(storeId, status); + } else { // Store ID + return snapshotRepo.countBySourceStoreId(storeId); + } + } else if (null != status) { // Status + return snapshotRepo.countByStatusOrderBySnapshotDateAsc(status); + } else { // No filters + return snapshotRepo.count(); + } + } + + /** + * Returns the total size of snapshot contents. + * + * @return + */ + @Path("total/size") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response size(@QueryParam("host") String host, + @QueryParam("storeId") String storeId, + @QueryParam("status") SnapshotStatus status) { + try { + long totalSize = getSnapshotsSize(host, storeId, status); + + log.debug("returning {} total size in bytes of snapshots", totalSize); + return Response.ok() + .entity(new GetSnapshotTotalSizeBridgeResult(totalSize)) + .build(); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return Response.serverError() + .entity(new ResponseDetails(ex.getMessage())) + .build(); + } + } + + /* + * Returns a total size of snapshots. The parameters of host, store ID and status are all + * considered optional, so depending on which are provided (i.e. not null), the + * query used will vary. + */ + protected long getSnapshotsSize(String host, + String storeId, + SnapshotStatus status) { + long totalSizeInBytes = 0L; + List snapshots = null; + + if (null != host) { + if (null != storeId) { + if (null != status) { // Host & Store ID & Status + snapshots = snapshotRepo.findBySourceHostAndSourceStoreIdAndStatus(host, storeId, status); + } else { // Host & Store ID + snapshots = snapshotRepo.findBySourceHostAndSourceStoreId(host, storeId); + } + } else if (null != status) { // Host & Status + snapshots = snapshotRepo.findBySourceHostAndStatus(host, status); + } else { // Host + snapshots = snapshotRepo.findBySourceHost(host); + } + } else if (null != storeId) { + if (null != status) { // Store ID & Status + snapshots = snapshotRepo.findBySourceStoreIdAndStatus(storeId, status); + } else { // Store ID + snapshots = snapshotRepo.findBySourceStoreId(storeId); + } + } else if (null != status) { // Status + snapshots = snapshotRepo.findByStatusOrderBySnapshotDateAsc(status); + } else { // No filters + snapshots = snapshotRepo.findAll(); + } + + if (null != snapshots) { + Iterator snapshotsItr = snapshots.iterator(); + while (snapshotsItr.hasNext()) { + Snapshot snapshot = snapshotsItr.next(); + totalSizeInBytes += snapshot.getTotalSizeInBytes(); + } + } + + return totalSizeInBytes; + } + + /** + * Returns the total files of snapshot contents. + * + * @return + */ + @Path("total/files") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response files(@QueryParam("host") String host, + @QueryParam("storeId") String storeId, + @QueryParam("status") SnapshotStatus status) { + try { + long totalFiles = getSnapshotsFiles(host, storeId, status); + + log.debug("returning {} total number of files in snapshots", totalFiles); + return Response.ok() + .entity(new GetSnapshotTotalFilesBridgeResult(totalFiles)) + .build(); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return Response.serverError() + .entity(new ResponseDetails(ex.getMessage())) + .build(); + } + } + + /* + * Returns a total files of snapshots. The parameters of host, store ID and status are all + * considered optional, so depending on which are provided (i.e. not null), the + * query used will vary. + */ + protected long getSnapshotsFiles(String host, + String storeId, + SnapshotStatus status) { + long totalFiles = 0L; + List snapshots = null; + + if (null != host) { + if (null != storeId) { + if (null != status) { // Host & Store ID & Status + snapshots = snapshotRepo.findBySourceHostAndSourceStoreIdAndStatus(host, storeId, status); + } else { // Host & Store ID + snapshots = snapshotRepo.findBySourceHostAndSourceStoreId(host, storeId); + } + } else if (null != status) { // Host & Status + snapshots = snapshotRepo.findBySourceHostAndStatus(host, status); + } else { // Host + snapshots = snapshotRepo.findBySourceHost(host); + } + } else if (null != storeId) { + if (null != status) { // Store ID & Status + snapshots = snapshotRepo.findBySourceStoreIdAndStatus(storeId, status); + } else { // Store ID + snapshots = snapshotRepo.findBySourceStoreId(storeId); + } + } else if (null != status) { // Status + snapshots = snapshotRepo.findByStatusOrderBySnapshotDateAsc(status); + } else { // No filters + snapshots = snapshotRepo.findAll(); + } + + if (null != snapshots) { + Iterator snapshotsItr = snapshots.iterator(); + while (snapshotsItr.hasNext()) { + Snapshot snapshot = snapshotsItr.next(); + totalFiles += snapshotContentItemRepo.countBySnapshotId(snapshot.getId()); + } + } + + return totalFiles; + } + + /** + * Returns the total count, size and files of snapshots + * + * @return + */ + @Path("total") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response total(@QueryParam("host") String host, + @QueryParam("storeId") String storeId, + @QueryParam("status") SnapshotStatus status) { + try { + long totalCount = getSnapshotsCount(host, storeId, status); + long totalSize = getSnapshotsSize(host, storeId, status); + long totalFiles = getSnapshotsFiles(host, storeId, status); + + log.debug("returning {} total count of snapshots", totalCount); + log.debug("returning {} total size in bytes of snapshots", totalSize); + log.debug("returning {} total number of files in snapshots", totalFiles); + return Response.ok() + .entity(new GetSnapshotTotalsBridgeResult(totalCount, totalSize, totalFiles)) + .build(); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return Response.serverError() + .entity(new ResponseDetails(ex.getMessage())) + .build(); + } + } + @Path("{snapshotId}") @GET @Produces(MediaType.APPLICATION_JSON) diff --git a/snapshot-bridge-webapp/src/test/java/org/duracloud/snapshot/bridge/rest/SnapshotResourceTest.java b/snapshot-bridge-webapp/src/test/java/org/duracloud/snapshot/bridge/rest/SnapshotResourceTest.java index 301a3999..b5bb3c51 100644 --- a/snapshot-bridge-webapp/src/test/java/org/duracloud/snapshot/bridge/rest/SnapshotResourceTest.java +++ b/snapshot-bridge-webapp/src/test/java/org/duracloud/snapshot/bridge/rest/SnapshotResourceTest.java @@ -508,6 +508,160 @@ public void testListSnapshotsStoreIdStatus() { resource.listSnapshots(null, storeId, status); } + @Test + public void testCountSnapshotsNoParams() { + expect(snapshotRepo.count()) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(null, null, null); + } + + @Test + public void testCountSnapshotsHost() { + String host = "host"; + expect(snapshotRepo.countBySourceHost(host)) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(host, null, null); + } + + @Test + public void testCountSnapshotsHostStoreId() { + String host = "host"; + String storeId = "store-id"; + expect(snapshotRepo.countBySourceHostAndSourceStoreId(host, storeId)) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(host, storeId, null); + } + + @Test + public void testCountSnapshotsHostStoreIdStatus() { + String host = "host"; + String storeId = "store-id"; + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo + .countBySourceHostAndSourceStoreIdAndStatus(host, storeId, status)) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(host, storeId, status); + } + + @Test + public void testCountSnapshotsStoreId() { + String storeId = "store-id"; + expect(snapshotRepo.countBySourceStoreId(storeId)) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(null, storeId, null); + } + + @Test + public void testCountSnapshotsStatus() { + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo.countByStatusOrderBySnapshotDateAsc(status)) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(null, null, status); + } + + @Test + public void testCountSnapshotsHostStatus() { + String host = "host"; + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo.countBySourceHostAndStatus(host, status)) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(host, null, status); + } + + @Test + public void testCountSnapshotsStoreIdStatus() { + String storeId = "store-id"; + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo.countBySourceStoreIdAndStatus(storeId, status)) + .andReturn(Long.valueOf(1)); + replayAll(); + resource.getSnapshotsCount(null, storeId, status); + } + + @Test + public void testCountSnapshotsFilesNoParams() { + expect(snapshotRepo.findAll()) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(null, null, null); + } + + @Test + public void testCountSnapshotsFilesHost() { + String host = "host"; + expect(snapshotRepo.findBySourceHost(host)) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(host, null, null); + } + + @Test + public void testCountSnapshotsFilesHostStoreId() { + String host = "host"; + String storeId = "store-id"; + expect(snapshotRepo.findBySourceHostAndSourceStoreId(host, storeId)) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(host, storeId, null); + } + + @Test + public void testCountSnapshotsFilesHostStoreIdStatus() { + String host = "host"; + String storeId = "store-id"; + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo + .findBySourceHostAndSourceStoreIdAndStatus(host, storeId, status)) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(host, storeId, status); + } + + @Test + public void testCountSnapshotsFilesStoreId() { + String storeId = "store-id"; + expect(snapshotRepo.findBySourceStoreId(storeId)) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(null, storeId, null); + } + + @Test + public void testCountSnapshotsFilesStatus() { + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo.findByStatusOrderBySnapshotDateAsc(status)) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(null, null, status); + } + + @Test + public void testCountSnapshotsFilesHostStatus() { + String host = "host"; + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo.findBySourceHostAndStatus(host, status)) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(host, null, status); + } + + @Test + public void testCountSnapshotsFilesStoreIdStatus() { + String storeId = "store-id"; + SnapshotStatus status = SnapshotStatus.SNAPSHOT_COMPLETE; + expect(snapshotRepo.findBySourceStoreIdAndStatus(storeId, status)) + .andReturn(new ArrayList()); + replayAll(); + resource.getSnapshotsFiles(null, storeId, status); + } + @Test public void testGetSnapshotContent() { String snapshotId = "snapshot-id"; diff --git a/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotContentItemRepo.java b/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotContentItemRepo.java index 21112d68..b6251d24 100644 --- a/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotContentItemRepo.java +++ b/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotContentItemRepo.java @@ -35,6 +35,8 @@ public List findBySnapshotNameAndContentIdStartingWithOrder public long countBySnapshotName(@Param("snapshotName") String snapshotName); + public long countBySnapshotId(@Param("snapshotId") Long snapshotId); + /** * @param snapshotName * @param pageable diff --git a/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotRepo.java b/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotRepo.java index f714dd2b..d3ce7f3b 100644 --- a/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotRepo.java +++ b/snapshot-common-db/src/main/java/org/duracloud/snapshot/db/repo/SnapshotRepo.java @@ -88,6 +88,61 @@ public List findBySourceHostAndSourceStoreIdAndStatus(String host, */ public Snapshot findBySnapshotAlternateIds(String alternateId); + /** + * @return count of snapshots + */ + public long count(); + + /** + * @param host where snapshot originated + * @return count of snapshots with the given host + */ + public long countBySourceHost(String host); + + /** + * @param storeId storage provider ID + * @return count of snapshots with the given store ID + */ + public long countBySourceStoreId(String storeId); + + /** + * @param host where snapshot originated + * @param storeId storage provider ID + * @return count of snapshots with the given host and store ID + */ + public long countBySourceHostAndSourceStoreId(String host, String storeId); + + /** + * @param status current snapshot status + * @return count of snapshots with the given status + */ + public long countByStatusOrderBySnapshotDateAsc(SnapshotStatus status); + + /** + * @param host where snapshot originated + * @param status current snapshot status + * @return count of snapshots with the given host and status + */ + public long countBySourceHostAndStatus(String host, SnapshotStatus status); + + /** + * @param storeId storage provider ID + * @param status current snapshot status + * @return count of snapshots with the given store ID and status + */ + public long countBySourceStoreIdAndStatus(String storeId, + SnapshotStatus status); + + /** + * @param host where snapshot originated + * @param storeId storage provider ID + * @param status current snapshot status + * @return count of snapshots with the given host, store ID, and status + */ + public long countBySourceHostAndSourceStoreIdAndStatus(String host, + String storeId, + SnapshotStatus status); + /** * @param snapshotId ID of snapshot */