diff --git a/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java b/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java index b048055..1b0aba6 100755 --- a/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java +++ b/src/main/java/eu/dnetlib/usagestats/controllers/SushiLiteController.java @@ -47,9 +47,10 @@ class SushiLiteController { } @RequestMapping(value = "/sushilite/r5/reports/pr", method = RequestMethod.GET) public String getReportPR(@RequestParam(value = "RepositoryIdentifier", defaultValue = "") String repositoryIdentifier, - @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, @RequestParam(value = "EndDate", defaultValue = "") String endDate) { + @RequestParam(value = "BeginDate", defaultValue = "") String beginDate, @RequestParam(value = "EndDate", defaultValue = "") String endDate, + @RequestParam(value = "Granularity", defaultValue = "Monthly") String granularity) { log.info("Sushi PR Report request for repository "+repositoryIdentifier); - return sushiLiteService.displayReportPR(repositoryIdentifier, beginDate, endDate); + return sushiLiteService.displayReportPR(repositoryIdentifier, beginDate, endDate, granularity); } } diff --git a/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java b/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java index 2c47873..381896e 100755 --- a/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java +++ b/src/main/java/eu/dnetlib/usagestats/repositories/UsageStatsRepository.java @@ -606,6 +606,56 @@ public class UsageStatsRepository { return "-1"; } + public String getInstitutionID(String repositoryIdentifier) { + PreparedStatement st = null; + Connection connection = null; + ResultSet rs = null; + log.info("database " + statsDB); + try { + connection = usageStatsDB.getConnection(); + String[] split = repositoryIdentifier.split(":"); + String openaire_id = "-1"; + switch (split[0].toLowerCase()) { + case "openaire": + st = connection.prepareStatement("select id from " + statsDB + ".datasource where id=?"); + st.setString(1, repositoryIdentifier.replaceFirst(split[0] + ":", "")); + + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + + case "opendoar": + st = connection.prepareStatement("select id from " + statsDB + ".datasource_oids where oid=?"); + st.setString(1, "opendoar____::" + repositoryIdentifier.replaceFirst(split[0] + ":", "")); + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + case "issn": + st = connection.prepareStatement("select distinct d.id from " + statsDB + ".datasource d, " + statsDB + ".datasource_oids di, " + statsDB + ".datasource_results dr where d.id=dr.id and di.oid like ? and d.id=di.id and (type='Journal' OR type='Journal Aggregator/Publisher')"); + st.setString(1, "%" + repositoryIdentifier.replaceFirst(split[0] + ":", "") + "%"); + + rs = st.executeQuery(); + while (rs.next()) { + openaire_id = rs.getString(1); + } + return openaire_id; + default: + return "-1"; + } + } catch (Exception e) { + log.error("Repository id failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + return "-1"; + } + public void executeItem(List reportItems, String itemIdentifier, String repositoryIdentifier, String itemDataType, Date beginDate, Date endDate, String granularity) { @@ -1466,64 +1516,66 @@ public class UsageStatsRepository { try { connection = usageStatsDB.getConnection(); - st = connection.prepareStatement("SELECT `date`, sum(us.downloads) as downloads, sum(us.views) as views FROM " + usagestatsImpalaDB + ".usage_stats us " - + "WHERE us.`date`>=? AND us.`date`<=? AND us.repository_id=? GROUP BY us.repository_id, us.`date` order by us.`date` ASC;"); - - st.setString(1, beginDateStr); - st.setString(2, endDateStr); - st.setString(3, repositoryIdentifier); - - log.info(st.toString()); - - rs = st.executeQuery(); - String result = ""; - String lastDate = ""; - ReportItem reportItem = null; - - int ft_total = 0; - int abstr = 0; if (granularity.equalsIgnoreCase("totals")) { - reportItem = new ReportItem(" " + repositoryIdentifier + " Platform", "Platform", "Regular", "", ""); - ft_total = 0; - abstr = 0; + 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;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + ReportItem reportItem = null; while (rs.next()) { -// if (!rs.getString(1).equals(result)) { -// if (reportItem != null) { -// reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr))); -// reportItems.add(reportItem); -// } -// //result = rs.getString(1); -// } - ft_total += rs.getInt(2); - abstr += rs.getInt(3); - } - if (reportItem != null) { - reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(beginDate), report_dateFormat.format(endDate), Integer.toString(ft_total), Integer.toString(abstr))); + 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); } } 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;"); + + st.setString(1, beginDateStr); + st.setString(2, endDateStr); + st.setString(3, repositoryIdentifier); + + rs = st.executeQuery(); + String result = ""; + String lastDate = ""; + ReportItem reportItem = null; + + int ft_total = 0; + int abstr = 0; + Calendar endCal = Calendar.getInstance(); endCal.setTime(postgresFormat.parse(endDateStr)); endCal.add(Calendar.MONTH, 1); Date endDateForZeros = endCal.getTime(); - reportItem = new ReportItem(" " + repositoryIdentifier + " Platform", "Platform", "Regular", "", ""); + lastDate = beginDateStr; + String datatype = ""; while (rs.next()) { Calendar endC = Calendar.getInstance(); - endC.setTime(postgresFormat.parse(rs.getString(1))); + endC.setTime(postgresFormat.parse(rs.getString(2))); endC.set(Calendar.DATE, endC.getActualMaximum(Calendar.DATE)); - if (reportItem != null) { - reportItem.addPerformance(new ItemPerformance(report_dateFormat.format(postgresFormat.parse(rs.getString(1))), report_dateFormat.format(endC.getTime()), rs.getString(2), rs.getString(3))); + if (!datatype.equals(rs.getString(1))) { + if (reportItem != null) { + reportItems.add(reportItem); + } + reportItem = new ReportItem("", "OpenAIRE", rs.getString(1), "Regular", ""); + datatype = rs.getString(1); } - endC.setTime(postgresFormat.parse(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))); + } + endC.setTime(postgresFormat.parse(rs.getString(2))); endC.add(Calendar.MONTH, 1); lastDate = postgresFormat.format(endC.getTime()); + //if (reportItem != null) { + //fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); } - if (reportItem != null) { - fillWithZeros(postgresFormat.parse(lastDate), endDateForZeros, reportItem); - reportItems.add(reportItem); - } + reportItems.add(reportItem); } /* @@ -1560,4 +1612,31 @@ public class UsageStatsRepository { from = fromCalendar.getTime(); } } + + public String getInstitutionName(String repositoryIdentifier) { + PreparedStatement st = null; + Connection connection = null; + ResultSet rs = null; + log.info("database " + statsDB); + String institutionName = ""; + try { + connection = usageStatsDB.getConnection(); + st = connection.prepareStatement("select name from " + statsDB + ".datasource where id=?"); + st.setString(1, repositoryIdentifier); + + rs = st.executeQuery(); + while (rs.next()) { + institutionName = rs.getString(1); + } + return institutionName; + } catch (Exception e) { + log.error("Repository name failed: ", e); + } finally { + DbUtils.closeQuietly(rs); + DbUtils.closeQuietly(st); + DbUtils.closeQuietly(connection); + } + + return "-1"; + } } diff --git a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java index 0c8ea2c..64b7fcf 100755 --- a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java +++ b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteService.java @@ -21,7 +21,7 @@ public interface SushiLiteService { ArrayList buildReportSupported(); String displayReportsSupported(); - ReportPR buildReportPR(String repositoryIdentifier, String beginDate,String endDate); - String displayReportPR(String repositoryIdentifier, String beginDate,String endDate); + ReportPR buildReportPR(String repositoryIdentifier, String beginDate,String endDate, String granularity); + String displayReportPR(String repositoryIdentifier, String beginDate,String endDate, String granularity); } diff --git a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java index 58809d9..16560d9 100755 --- a/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java +++ b/src/main/java/eu/dnetlib/usagestats/services/SushiLiteServiceImpl.java @@ -300,12 +300,14 @@ public class SushiLiteServiceImpl implements SushiLiteService { @Override public ReportPR buildReportPR(String repositoryIdentifier, String beginDate, - String endDate) { + String endDate, 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'"); //Display Report Created Day - String granularity = "totals"; + + repositoryIdentifier=usageStatsRepository.getInstitutionID(repositoryIdentifier); + log.info("Repo Identifier "+repositoryIdentifier); Date beginDateParsed; if (!beginDate.equals("")) { @@ -355,9 +357,11 @@ public class SushiLiteServiceImpl implements SushiLiteService { List reportFilters = new ArrayList(); reportFilters.add(new Filter("BeginDate", beginDate)); reportFilters.add(new Filter("EndDate", endDate)); - String reportID = "Platform"; - String reportName = "Plaform Total Views & Downloads"; - String insitutionName = "Insititution Name " + repositoryIdentifier; + String reportID = "PR"; + String reportName = "Platform Master Report"; + //String insitutionName = "Insititution Name " + repositoryIdentifier; + String insitutionName = usageStatsRepository.getInstitutionName(repositoryIdentifier); + log.info("repo name "+insitutionName); List institutionIdD = new ArrayList(); institutionIdD.add(new InstitutionID("Openaire", repositoryIdentifier)); @@ -373,11 +377,11 @@ public class SushiLiteServiceImpl implements SushiLiteService { @Override public String displayReportPR(String repositoryIdentifier, String beginDate, - String endDate) { + String endDate, String granularity) { ObjectMapper objectMapper = new ObjectMapper(); log.info((beginDate + " " + endDate)); try { - return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR(repositoryIdentifier, beginDate, endDate)) + "
"; + return "
" + objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildReportPR(repositoryIdentifier, beginDate, endDate,granularity)) + "
"; } catch (JsonProcessingException ex) { java.util.logging.Logger.getLogger(SushiLiteServiceImpl.class.getName()).log(Level.SEVERE, null, ex); }