diff --git a/doc/sphinx-guides/source/api/metrics.rst b/doc/sphinx-guides/source/api/metrics.rst index 821b74b0a96..eca75a2da7f 100755 --- a/doc/sphinx-guides/source/api/metrics.rst +++ b/doc/sphinx-guides/source/api/metrics.rst @@ -24,7 +24,7 @@ Example: ``curl https://demo.dataverse.org/api/info/metrics/downloads`` To-Month -------- -Returns a count of various objects in dataverse up to a specified month ``$YYYY-DD`` in YYYY-MM format (i.e. ``2018-01``):: +Returns a count of various objects in dataverse up to a specified month ``$YYYY-DD`` in YYYY-MM format (e.g. ``2018-01``):: GET https://$SERVER/api/info/metrics/$type/toMonth/$YYYY-DD @@ -36,7 +36,7 @@ Example: ``curl https://demo.dataverse.org/api/info/metrics/dataverses/toMonth/2 Past Days --------- -Returns a count of various objects in dataverse for the past ``$days`` (i.e. ``30``):: +Returns a count of various objects in dataverse for the past ``$days`` (e.g. ``30``):: GET https://$SERVER/api/info/metrics/$type/pastDays/$days @@ -74,6 +74,16 @@ Returns the number of datasets by each subject:: GET https://$SERVER/api/info/metrics/datasets/bySubject + +By Subject, and to Month +~~~~~~~~~~~~~~~~~~~~~~~~ + +Returns the number of datasets by each subject, and up to a specified month ``$YYYY-DD`` in YYYY-MM format (e.g. ``2018-01``):: + + GET https://$SERVER/api/info/metrics/datasets/bySubject/toMonth/$YYYY-DD + +Example: ``curl https://demo.dataverse.org/api/info/metrics/datasets/bySubject/toMonth/2018-01`` + .. |CORS| raw:: html diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java b/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java index 6b77f7fa32c..e4367961932 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java @@ -191,15 +191,23 @@ public Response getDatasetsPastDays(@PathParam("days") int days) { @GET @Path("datasets/bySubject") public Response getDatasetsBySubject() { - String metricName = "datasetsBySubject"; + return getDatasetsBySubjectToMonth(MetricsUtil.getCurrentMonth()); + } + + @GET + @Path("datasets/bySubject/toMonth/{yyyymm}") + public Response getDatasetsBySubjectToMonth(@PathParam("yyyymm") String yyyymm) { + String metricName = "datasetsBySubjectToMonth"; try { - String jsonArrayString = metricsSvc.returnUnexpiredCacheAllTime(metricName); - + String sanitizedyyyymm = MetricsUtil.sanitizeYearMonthUserInput(yyyymm); + + String jsonArrayString = metricsSvc.returnUnexpiredCacheMonthly(metricName, sanitizedyyyymm); + if (null == jsonArrayString) { //run query and save - JsonArrayBuilder jsonArrayBuilder = MetricsUtil.datasetsBySubjectToJson(metricsSvc.datasetsBySubject()); + JsonArrayBuilder jsonArrayBuilder = MetricsUtil.datasetsBySubjectToJson(metricsSvc.datasetsBySubjectToMonth(sanitizedyyyymm)); jsonArrayString = jsonArrayBuilder.build().toString(); - metricsSvc.save(new Metric(metricName, jsonArrayString), false); + metricsSvc.save(new Metric(metricName, sanitizedyyyymm, jsonArrayString), false); } return allowCors(ok(MetricsUtil.stringToJsonArrayBuilder(jsonArrayString))); @@ -207,7 +215,7 @@ public Response getDatasetsBySubject() { return allowCors(error(BAD_REQUEST, ex.getLocalizedMessage())); } } - + /** Files */ @GET @Path("files") diff --git a/src/main/java/edu/harvard/iq/dataverse/metrics/MetricsServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/metrics/MetricsServiceBean.java index 10f9f7440f2..ae0a0160ae8 100644 --- a/src/main/java/edu/harvard/iq/dataverse/metrics/MetricsServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/metrics/MetricsServiceBean.java @@ -89,8 +89,8 @@ public List dataversesBySubject() { } /** Datasets */ - - public List datasetsBySubject() { + + public List datasetsBySubjectToMonth(String yyyymm) { Query query = em.createNativeQuery("" + "SELECT strvalue, count(dataset.id)\n" + "FROM datasetfield_controlledvocabularyvalue \n" @@ -108,6 +108,7 @@ public List datasetsBySubject() { + "join dataset on dataset.id = datasetversion.dataset_id\n" + "where versionstate='RELEASED'\n" + "and dataset.harvestingclient_id is null\n" + + "and date_trunc('month', releasetime) <= to_date('" + yyyymm + "','YYYY-MM')\n" + "group by dataset_id \n" + ")\n" + "AND datasetfieldtype.name = 'subject'\n" @@ -119,6 +120,7 @@ public List datasetsBySubject() { return query.getResultList(); } + /** * @param yyyymm Month in YYYY-MM format. */ diff --git a/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java b/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java index f0ae408b761..60c9a4cad53 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java @@ -2,6 +2,7 @@ import com.jayway.restassured.RestAssured; import com.jayway.restassured.response.Response; +import edu.harvard.iq.dataverse.metrics.MetricsUtil; import static javax.ws.rs.core.Response.Status.OK; import org.junit.AfterClass; import static org.junit.Assert.assertEquals; @@ -220,4 +221,22 @@ public void testGetDatasetsBySubject() { assertEquals(precache, postcache); } + @Test + public void testGetDatasetsBySubjectToMonth() { + String thismonth = MetricsUtil.getCurrentMonth(); + Response response = UtilIT.metricsDatasetsBySubjectToMonth(thismonth); + String precache = response.prettyPrint(); + response.then().assertThat() + .statusCode(OK.getStatusCode()); + + //Run each query twice and compare results to tests caching + // See the "TODO" at the beginning of the class; + // ideally, we'll want to have more comprehensive tests. + response = UtilIT.metricsDatasetsBySubjectToMonth(thismonth); + String postcache = response.prettyPrint(); + response.then().assertThat() + .statusCode(OK.getStatusCode()); + + assertEquals(precache, postcache); + } } diff --git a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java index 15dd5538da5..a00acf1d260 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -1770,6 +1770,11 @@ static Response metricsDatasetsBySubject() { return requestSpecification.get("/api/info/metrics/datasets/bySubject"); } + static Response metricsDatasetsBySubjectToMonth(String month) { + RequestSpecification requestSpecification = given(); + return requestSpecification.get("/api/info/metrics/datasets/bySubject/toMonth/" + month); + } + static Response clearMetricCache() { RequestSpecification requestSpecification = given(); return requestSpecification.delete("/api/admin/clearMetricsCache");