Skip to content

Commit

Permalink
Metrics: api endpoint rename, query fix, doc update #4527
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-a-dunlap committed May 22, 2018
1 parent a8fcd3e commit 1e01f8f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 66 deletions.
24 changes: 12 additions & 12 deletions doc/sphinx-guides/source/api/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ The Metrics API

.. _CORS: https://www.w3.org/TR/cors/

dataverses/byMonth
dataverses/toMonth
----------------------

Returns the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.
Returns a count up to the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.

``curl https://demo.dataverse.org/api/info/metrics/dataverses/byMonth``
``curl https://demo.dataverse.org/api/info/metrics/dataverses/toMonth``

datasets/byMonth
datasets/toMonth
------------------------

Returns the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.
Returns a count up to the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.

``curl https://demo.dataverse.org/api/info/metrics/datasets/byMonth``
``curl https://demo.dataverse.org/api/info/metrics/datasets/toMonth``

files/byMonth
files/toMonth
------------------------

Returns the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.
Returns a count up to the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.

``curl https://demo.dataverse.org/api/info/metrics/files/byMonth``
``curl https://demo.dataverse.org/api/info/metrics/files/toMonth``

downloads/byMonth
downloads/toMonth
------------------------

Returns the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.
Returns a count up to the current month or append a date in YYYY-MM format (i.e. ``/2018-01``) for a specific month.

``curl https://demo.dataverse.org/api/info/metrics/downloads/byMonth``
``curl https://demo.dataverse.org/api/info/metrics/downloads/toMonth``

dataverses/byCategory
------------------------
Expand Down
2 changes: 1 addition & 1 deletion doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ Clear all cached metric results::

DELETE http://$SERVER/api/admin/clearMetricsCache

Clear a specific metric cache. Currently this must match the name of the row in the table, which is named *metricName*_*metricYYYYMM* (or just *metricName* if there is no date range for the metric). For example dataversesByMonth_2018-05::
Clear a specific metric cache. Currently this must match the name of the row in the table, which is named *metricName*_*metricYYYYMM* (or just *metricName* if there is no date range for the metric). For example dataversesToMonth_2018-05::

DELETE http://$SERVER/api/admin/clearMetricsCache/$metricDbName

Expand Down
56 changes: 28 additions & 28 deletions src/main/java/edu/harvard/iq/dataverse/api/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@
public class Metrics extends AbstractApiBean {

@GET
@Path("dataverses/byMonth")
public Response getDataversesByMonthCurrent() {
return getDataversesByMonth(MetricsUtil.getCurrentMonth());
@Path("dataverses/toMonth")
public Response getDataversesToMonthCurrent() {
return getDataversesToMonth(MetricsUtil.getCurrentMonth());
}

@GET
@Path("dataverses/byMonth/{yyyymm}")
public Response getDataversesByMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "dataversesByMonth";
@Path("dataverses/toMonth/{yyyymm}")
public Response getDataversesToMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "dataversesToMonth";

try {
String sanitizedyyyymm = MetricsUtil.sanitizeYearMonthUserInput(yyyymm);
String jsonString = metricsSvc.returnUnexpiredCacheMonthly(metricName, sanitizedyyyymm);

if (null == jsonString) { //run query and save
Long count = metricsSvc.dataversesByMonth(sanitizedyyyymm);
Long count = metricsSvc.dataversesToMonth(sanitizedyyyymm);
JsonObjectBuilder jsonObjBuilder = MetricsUtil.countToJson(count);
jsonString = jsonObjBuilder.build().toString();
metricsSvc.save(new Metric(metricName, sanitizedyyyymm, jsonString), true); //if not using cache save new
Expand All @@ -62,22 +62,22 @@ public Response getDataversesByMonth(@PathParam("yyyymm") String yyyymm) {
}

@GET
@Path("datasets/byMonth")
public Response getDatasetsByMonthCurrent() {
return getDatasetsByMonth(MetricsUtil.getCurrentMonth());
@Path("datasets/toMonth")
public Response getDatasetsToMonthCurrent() {
return getDatasetsToMonth(MetricsUtil.getCurrentMonth());
}

@GET
@Path("datasets/byMonth/{yyyymm}")
public Response getDatasetsByMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "datasetsByMonth";
@Path("datasets/toMonth/{yyyymm}")
public Response getDatasetsToMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "datasetsToMonth";

try {
String sanitizedyyyymm = MetricsUtil.sanitizeYearMonthUserInput(yyyymm);
String jsonString = metricsSvc.returnUnexpiredCacheMonthly(metricName, sanitizedyyyymm);

if (null == jsonString) { //run query and save
Long count = metricsSvc.datasetsByMonth(sanitizedyyyymm);
Long count = metricsSvc.datasetsToMonth(sanitizedyyyymm);
JsonObjectBuilder jsonObjBuilder = MetricsUtil.countToJson(count);
jsonString = jsonObjBuilder.build().toString();
metricsSvc.save(new Metric(metricName, sanitizedyyyymm, jsonString), true); //if not using cache save new
Expand All @@ -91,22 +91,22 @@ public Response getDatasetsByMonth(@PathParam("yyyymm") String yyyymm) {
}

@GET
@Path("files/byMonth")
public Response getFilesByMonthCurrent() {
return getFilesByMonth(MetricsUtil.getCurrentMonth());
@Path("files/toMonth")
public Response getFilesToMonthCurrent() {
return getFilesToMonth(MetricsUtil.getCurrentMonth());
}

@GET
@Path("files/byMonth/{yyyymm}")
public Response getFilesByMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "filesByMonth";
@Path("files/toMonth/{yyyymm}")
public Response getFilesToMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "filesToMonth";

try {
String sanitizedyyyymm = MetricsUtil.sanitizeYearMonthUserInput(yyyymm);
String jsonString = metricsSvc.returnUnexpiredCacheMonthly(metricName, sanitizedyyyymm);

if (null == jsonString) { //run query and save
Long count = metricsSvc.filesByMonth(sanitizedyyyymm);
Long count = metricsSvc.filesToMonth(sanitizedyyyymm);
JsonObjectBuilder jsonObjBuilder = MetricsUtil.countToJson(count);
jsonString = jsonObjBuilder.build().toString();
metricsSvc.save(new Metric(metricName, sanitizedyyyymm, jsonString), true); //if not using cache save new
Expand All @@ -119,22 +119,22 @@ public Response getFilesByMonth(@PathParam("yyyymm") String yyyymm) {
}

@GET
@Path("downloads/byMonth")
public Response getDownloadsByMonthCurrent() {
return getDownloadsByMonth(MetricsUtil.getCurrentMonth());
@Path("downloads/toMonth")
public Response getDownloadsToMonthCurrent() {
return getDownloadsToMonth(MetricsUtil.getCurrentMonth());
}

@GET
@Path("downloads/byMonth/{yyyymm}")
public Response getDownloadsByMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "downloadsByMonth";
@Path("downloads/toMonth/{yyyymm}")
public Response getDownloadsToMonth(@PathParam("yyyymm") String yyyymm) {
String metricName = "downloadsToMonth";

try {
String sanitizedyyyymm = MetricsUtil.sanitizeYearMonthUserInput(yyyymm);
String jsonString = metricsSvc.returnUnexpiredCacheMonthly(metricName, sanitizedyyyymm);

if (null == jsonString) { //run query and save
Long count = metricsSvc.downloadsByMonth(sanitizedyyyymm);
Long count = metricsSvc.downloadsToMonth(sanitizedyyyymm);
JsonObjectBuilder jsonObjBuilder = MetricsUtil.countToJson(count);
jsonString = jsonObjBuilder.build().toString();
metricsSvc.save(new Metric(metricName, sanitizedyyyymm, jsonString), true); //if not using cache save new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MetricsServiceBean implements Serializable {
/**
* @param yyyymm Month in YYYY-MM format.
*/
public long dataversesByMonth(String yyyymm) throws Exception {
public long dataversesToMonth(String yyyymm) throws Exception {
Query query = em.createNativeQuery(""
+ "select count(dvobject.id)\n"
+ "from dataverse\n"
Expand All @@ -47,7 +47,7 @@ public long dataversesByMonth(String yyyymm) throws Exception {
/**
* @param yyyymm Month in YYYY-MM format.
*/
public long datasetsByMonth(String yyyymm) throws Exception {
public long datasetsToMonth(String yyyymm) throws Exception {
Query query = em.createNativeQuery(""
+ "select count(*)\n"
+ "from datasetversion\n"
Expand All @@ -70,7 +70,7 @@ public long datasetsByMonth(String yyyymm) throws Exception {
/**
* @param yyyymm Month in YYYY-MM format.
*/
public long filesByMonth(String yyyymm) throws Exception {
public long filesToMonth(String yyyymm) throws Exception {
Query query = em.createNativeQuery(""
+ "select count(*)\n"
+ "from filemetadata\n"
Expand All @@ -94,7 +94,7 @@ public long filesByMonth(String yyyymm) throws Exception {
/**
* @param yyyymm Month in YYYY-MM format.
*/
public long downloadsByMonth(String yyyymm) throws Exception {
public long downloadsToMonth(String yyyymm) throws Exception {
Query query = em.createNativeQuery(""
+ "select count(id)\n"
+ "from guestbookresponse\n"
Expand Down Expand Up @@ -135,7 +135,6 @@ public List<Object[]> datasetsBySubject() {
+ "from datasetversion\n"
+ "join dataset on dataset.id = datasetversion.dataset_id\n"
+ "where versionstate='RELEASED'\n"
+ "and date_trunc('month', releasetime) <= to_date('2018-05','YYYY-MM')\n"
+ "and dataset.harvestingclient_id is null\n"
+ "group by dataset_id \n"
+ ")\n"
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public static void cleanUpClass() {
}

@Test
public void testGetDataversesByMonth() {
public void testGetDataversesToMonth() {
String yyyymm = "2018-04";
// yyyymm = null;
Response response = UtilIT.metricsDataversesByMonth(yyyymm);
Response response = UtilIT.metricsDataversesToMonth(yyyymm);
String precache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());

//Run each query twice and compare results to tests caching
response = UtilIT.metricsDataversesByMonth(yyyymm);
response = UtilIT.metricsDataversesToMonth(yyyymm);
String postcache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());
Expand All @@ -40,16 +40,16 @@ public void testGetDataversesByMonth() {
}

@Test
public void testGetDatasetsByMonth() {
public void testGetDatasetsToMonth() {
String yyyymm = "2018-04";
// yyyymm = null;
Response response = UtilIT.metricsDatasetsByMonth(yyyymm);
Response response = UtilIT.metricsDatasetsToMonth(yyyymm);
String precache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());

//Run each query twice and compare results to tests caching
response = UtilIT.metricsDatasetsByMonth(yyyymm);
response = UtilIT.metricsDatasetsToMonth(yyyymm);
String postcache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());
Expand All @@ -58,16 +58,16 @@ public void testGetDatasetsByMonth() {
}

@Test
public void testGetFilesByMonth() {
public void testGetFilesToMonth() {
String yyyymm = "2018-04";
// yyyymm = null;
Response response = UtilIT.metricsFilesByMonth(yyyymm);
Response response = UtilIT.metricsFilesToMonth(yyyymm);
String precache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());

//Run each query twice and compare results to tests caching
response = UtilIT.metricsFilesByMonth(yyyymm);
response = UtilIT.metricsFilesToMonth(yyyymm);
String postcache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());
Expand All @@ -76,16 +76,16 @@ public void testGetFilesByMonth() {
}

@Test
public void testGetDownloadsByMonth() {
public void testGetDownloadsToMonth() {
String yyyymm = "2018-04";
// yyyymm = null;
Response response = UtilIT.metricsDownloadsByMonth(yyyymm);
Response response = UtilIT.metricsDownloadsToMonth(yyyymm);
String precache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());

//Run each query twice and compare results to tests caching
response = UtilIT.metricsDownloadsByMonth(yyyymm);
response = UtilIT.metricsDownloadsToMonth(yyyymm);
String postcache = response.prettyPrint();
response.then().assertThat()
.statusCode(OK.getStatusCode());
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1442,44 +1442,44 @@ static Response deleteStorageSite(long storageSiteId) {
.delete("/api/admin/storageSites/" + storageSiteId);
}

static Response metricsDataversesByMonth(String yyyymm) {
static Response metricsDataversesToMonth(String yyyymm) {
String optionalYyyyMm = "";
if (yyyymm != null) {
optionalYyyyMm = "/" + yyyymm;
}
RequestSpecification requestSpecification = given();
requestSpecification = given();
return requestSpecification.get("/api/info/metrics/dataverses/byMonth" + optionalYyyyMm);
return requestSpecification.get("/api/info/metrics/dataverses/toMonth" + optionalYyyyMm);
}

static Response metricsDatasetsByMonth(String yyyymm) {
static Response metricsDatasetsToMonth(String yyyymm) {
String optionalYyyyMm = "";
if (yyyymm != null) {
optionalYyyyMm = "/" + yyyymm;
}
RequestSpecification requestSpecification = given();
requestSpecification = given();
return requestSpecification.get("/api/info/metrics/datasets/byMonth" + optionalYyyyMm);
return requestSpecification.get("/api/info/metrics/datasets/toMonth" + optionalYyyyMm);
}

static Response metricsFilesByMonth(String yyyymm) {
static Response metricsFilesToMonth(String yyyymm) {
String optionalYyyyMm = "";
if (yyyymm != null) {
optionalYyyyMm = "/" + yyyymm;
}
RequestSpecification requestSpecification = given();
requestSpecification = given();
return requestSpecification.get("/api/info/metrics/files/byMonth" + optionalYyyyMm);
return requestSpecification.get("/api/info/metrics/files/toMonth" + optionalYyyyMm);
}

static Response metricsDownloadsByMonth(String yyyymm) {
static Response metricsDownloadsToMonth(String yyyymm) {
String optionalYyyyMm = "";
if (yyyymm != null) {
optionalYyyyMm = "/" + yyyymm;
}
RequestSpecification requestSpecification = given();
requestSpecification = given();
return requestSpecification.get("/api/info/metrics/downloads/byMonth" + optionalYyyyMm);
return requestSpecification.get("/api/info/metrics/downloads/toMonth" + optionalYyyyMm);
}

static Response metricsDataverseByCategory() {
Expand Down

0 comments on commit 1e01f8f

Please sign in to comment.