Skip to content
Closed
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
14 changes: 12 additions & 2 deletions doc/sphinx-guides/source/api/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

<span class="label label-success pull-right">
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/api/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,31 @@ 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)));
} catch (Exception ex) {
return allowCors(error(BAD_REQUEST, ex.getLocalizedMessage()));
}
}

/** Files */
@GET
@Path("files")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public List<Object[]> dataversesBySubject() {
}

/** Datasets */

public List<Object[]> datasetsBySubject() {
public List<Object[]> datasetsBySubjectToMonth(String yyyymm) {
Query query = em.createNativeQuery(""
+ "SELECT strvalue, count(dataset.id)\n"
+ "FROM datasetfield_controlledvocabularyvalue \n"
Expand All @@ -108,6 +108,7 @@ public List<Object[]> 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"
Expand All @@ -119,6 +120,7 @@ public List<Object[]> datasetsBySubject() {
return query.getResultList();
}


/**
* @param yyyymm Month in YYYY-MM format.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
5 changes: 5 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 @@ -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");
Expand Down