From 7b2786354c839ccc068d49ae004f2ef421afb390 Mon Sep 17 00:00:00 2001 From: Dimitris Date: Wed, 13 Jan 2021 13:01:47 +0200 Subject: [PATCH] Commit pr pr_p1 reports --- .../controllers/SushiLiteController.java | 3 +- .../repositories/UsageStatsRepository.java | 49 ++++++++++++++++--- .../usagestats/services/SushiLiteService.java | 4 +- .../services/SushiLiteServiceImpl.java | 8 +-- src/main/resources/log4j.properties | 8 +-- 5 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java b/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java index 6028e71..0e4df74 100755 --- a/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java +++ b/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java @@ -59,9 +59,10 @@ class SushiLiteController { @RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier, @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, @RequestParam(value = "EndDate", defaultValue = "") String endDate, + @RequestParam(value = "MetricType", defaultValue = "") String metricType, @RequestParam(value = "Granularity", defaultValue = "Monthly") String granularity) { log.info("COUNTER PR Report request for repository " + repositoryIdentifier); - return sushiLiteService.displayReportPR(repositoryIdentifier, beginDate, endDate, granularity); + return sushiLiteService.displayReportPR(repositoryIdentifier, beginDate, endDate, metricType, granularity); } @RequestMapping(value = "/sushilite/r5/reports/pr_p1", method = RequestMethod.GET) diff --git a/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java b/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java index ede7344..8a509b5 100755 --- a/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java +++ b/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java @@ -1503,7 +1503,7 @@ public class UsageStatsRepository { public void executeBatchItemsPR(List reportItems, String repositoryIdentifier, Date beginDate, - Date endDate, String granularity) { + Date endDate, String metricType, String granularity) { SimpleDateFormat report_dateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat postgresFormat = new SimpleDateFormat("yyyy/MM"); String beginDateStr = postgresFormat.format(beginDate); @@ -1515,7 +1515,6 @@ public class UsageStatsRepository { try { connection = usageStatsDB.getConnection(); - if (granularity.equalsIgnoreCase("totals")) { st = connection.prepareStatement("SELECT rc.type, sum(us.downloads) as downloads, sum(us.views) as views FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id GROUP BY rc.type order by rc.type ASC;"); @@ -1527,11 +1526,30 @@ public class UsageStatsRepository { rs = st.executeQuery(); ReportItem reportItem = null; - while (rs.next()) { - reportItem = new ReportItem("", "OpenAIRE", rs.getString(1), "Regular", ""); - reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), rs.getString(2), rs.getString(3))); - reportItems.add(reportItem); + switch (metricType) { + case (""): + while (rs.next()) { + reportItem = new ReportItem("", "OpenAIRE", rs.getString(1), "Regular", ""); + reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), rs.getString(2), rs.getString(3))); + reportItems.add(reportItem); + } + break; + case ("Total_Items_Investigations"): + while (rs.next()) { + reportItem = new ReportItem("", "OpenAIRE", rs.getString(1), "Regular", ""); + reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), null, rs.getString(3))); + reportItems.add(reportItem); + } + break; + case ("Total_Items_Requests"): + while (rs.next()) { + reportItem = new ReportItem("", "OpenAIRE", rs.getString(1), "Regular", ""); + reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), rs.getString(2), null)); + reportItems.add(reportItem); + } + break; } + } else if (granularity.equalsIgnoreCase("monthly")) { st = connection.prepareStatement("SELECT rc.type, us.`date`, sum(us.downloads) as downloads, sum(us.views) as views FROM " + usagestatsImpalaDB + ".usage_stats us, " + statsDB + ".result_classifications rc WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? AND rc.id=us.result_id GROUP BY rc.type, us.`date` order by rc.type, us.`date` ASC;"); @@ -1568,7 +1586,21 @@ public class UsageStatsRepository { datatype = rs.getString(1); } if (reportItem != null) { - reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), rs.getString(3), rs.getString(4))); + switch (metricType) { + case (""): + reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), rs.getString(3), rs.getString(4))); + break; + + case ("Total_Items_Investigations"): + reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), null, rs.getString(4))); + break; + + case ("Total_Items_Requests"): + reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(2))), report_dateFormat.format(endC.getTime()), rs.getString(3), null)); + break; + + } + } endC.setTime(postgresFormat.parse(rs.getString(2))); endC.add(Calendar.MONTH, 1); @@ -1646,8 +1678,9 @@ public class UsageStatsRepository { //if (reportItem != null) { //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); } - if(reportItem.getItemPerformances().size()>0) + if (reportItem.getItemPerformances().size() > 0) { reportItems.add(reportItem); + } /* jedis.put(redis_key, "persistent", "false"); diff --git a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java index f9f8e70..e86c128 100755 --- a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java +++ b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java @@ -21,8 +21,8 @@ public interface SushiLiteService { ArrayList buildReportSupported(); String displayReportsSupported(); - ReportPR buildReportPR(String repositoryIdentifier, String beginDate,String endDate, String granularity); - String displayReportPR(String repositoryIdentifier, String beginDate,String endDate, String granularity); + ReportPR buildReportPR(String repositoryIdentifier, String beginDate,String endDate, String metricType, String granularity); + String displayReportPR(String repositoryIdentifier, String beginDate,String endDate, String metricType, String granularity); ReportPR buildReportPR_P1(String repositoryIdentifier, String beginDate,String endDate); String displayReportPR_P1(String repositoryIdentifier, String beginDate, String endDate); diff --git a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java index 610a2d0..150fd02 100755 --- a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java +++ b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java @@ -300,7 +300,7 @@ public class SushiLiteServiceImpl implements SushiLiteService { @Override public ReportPR buildReportPR(String repositoryIdentifier, String beginDate, - String endDate, String granularity) { + String endDate, String metricType, String granularity) { List reportExceptions = new ArrayList<>(); ZonedDateTime dateTime = ZonedDateTime.now(); // Gets the current date and time, with your default time-zone DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); @@ -364,7 +364,7 @@ public class SushiLiteServiceImpl implements SushiLiteService { institutionIdD.add(new InstitutionID("Openaire", repositoryIdentifier)); List reportItems = new ArrayList(); - usageStatsRepository.executeBatchItemsPR(reportItems, repositoryIdentifier, beginDateParsed, endDateParsed, granularity); + usageStatsRepository.executeBatchItemsPR(reportItems, repositoryIdentifier, beginDateParsed, endDateParsed, metricType, granularity); if (reportItems.isEmpty()) { reportExceptions.add(new ReportException("3030", "Error", "No Usage Available for Requested Dates", "Service did not find any data")); } @@ -458,11 +458,11 @@ public class SushiLiteServiceImpl implements SushiLiteService { @Override public String displayReportPR(String repositoryIdentifier, String beginDate, - String endDate, String granularity) { + String endDate, String metricType, String granularity) { ObjectMapper objectMapper = new ObjectMapper(); log.info((beginDate + " " + endDate)); try { - return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR(repositoryIdentifier, beginDate, endDate, granularity)) + "
"; + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR(repositoryIdentifier, beginDate, endDate, metricType, granularity)) + "
"; } catch (JsonProcessingException ex) { java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties index ea54386..270177e 100755 --- a/src/main/resources/log4j.properties +++ b/src/main/resources/log4j.properties @@ -9,16 +9,16 @@ log4j.additivity.org.springframework = false log4j.appender.R=org.apache.log4j.RollingFileAppender #log4j.appender.R.File=/var/log/dnet/usageStatsAPI/usageStatsAPI.log -log4j.appender.R.File=/Users/dpie/Desktop/usageStatsAPI.log -#log4j.appender.R.File=/Volumes/Zeus/dpie/Desktop/usageStatsAPI.log +#log4j.appender.R.File=/Users/dpie/Desktop/usageStatsAPI.log +log4j.appender.R.File=/Volumes/Zeus/dpie/Desktop/usageStatsAPI.log loglog4j.appender.R.MaxFileSize=10MB log4j.appender.R.MaxBackupIndex=10 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern= %d %p %t [%c] - %m%n log4j.appender.S=org.apache.log4j.RollingFileAppender #log4j.appender.S.File=/var/log/dnet/usageStatsAPI/usageStatsAPI-spring.log -log4j.appender.S.File=/Users/dpie/Desktop/usageStatsAPI-spring.log -#log4j.appender.S.File=/Volumes/Zeus/dpie/Desktop/usageStatsAPI-spring.log +#log4j.appender.S.File=/Users/dpie/Desktop/usageStatsAPI-spring.log +log4j.appender.S.File=/Volumes/Zeus/dpie/Desktop/usageStatsAPI-spring.log log4j.appender.S.MaxFileSize=10MB log4j.appender.S.MaxBackupIndex=10 log4j.appender.S.layout=org.apache.log4j.PatternLayout