From bd0947557dcf7e83b1cd37b4f266646517d86df6 Mon Sep 17 00:00:00 2001 From: pvtroshin Date: Thu, 26 May 2011 15:02:54 +0000 Subject: [PATCH] Further work on statistics display git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@4173 e3abac25-378b-4346-85de-24260fe3988d --- webservices/compbio/stat/collector/JobStat.java | 4 +- webservices/compbio/stat/collector/StatDB.java | 61 ++++++++++++++++++++ .../compbio/stat/collector/StatProcessor.java | 1 - webservices/compbio/stat/servlet/AnnualStat.java | 4 +- webservices/compbio/stat/servlet/DisplayStat.java | 34 +++++------ .../compbio/stat/servlet/util/StatCollection.java | 40 ++++++------- 6 files changed, 94 insertions(+), 50 deletions(-) diff --git a/webservices/compbio/stat/collector/JobStat.java b/webservices/compbio/stat/collector/JobStat.java index 67767dc..25e0aa6 100644 --- a/webservices/compbio/stat/collector/JobStat.java +++ b/webservices/compbio/stat/collector/JobStat.java @@ -232,14 +232,14 @@ public class JobStat { } public long getResultSize() { - if (resultSize != ExecutionStatCollector.UNDEFINED) { + if (resultSize > 0) { return resultSize / 1000; } return 0; } public boolean hasResult() { - return resultSize != ExecutionStatCollector.UNDEFINED; + return resultSize > 0; } public boolean hasStarted() { diff --git a/webservices/compbio/stat/collector/StatDB.java b/webservices/compbio/stat/collector/StatDB.java index c58974b..40f9d08 100644 --- a/webservices/compbio/stat/collector/StatDB.java +++ b/webservices/compbio/stat/collector/StatDB.java @@ -166,6 +166,56 @@ public class StatDB { return date; } + public int getTotalJobsCount(Timestamp from, Timestamp to) + throws SQLException { + String allQuery = "select count(*) from exec_stat where start BETWEEN ? and ? "; + return getIntResult(from, to, allQuery); + } + + public int getCancelledCount(Timestamp from, Timestamp to) + throws SQLException { + // js.isCancelled + String cancelledQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCancelled=1 "; + return getIntResult(from, to, cancelledQuery); + } + + public int getAbandonedCount(Timestamp from, Timestamp to) + throws SQLException { + // !js.isCollected && !js.isCancelled && js.hasResult() + String abandonedQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCollected=0 and isCancelled=0 and resultsize>0 "; + return getIntResult(from, to, abandonedQuery); + } + + public int getIncompleteCount(Timestamp from, Timestamp to) + throws SQLException { + // !js.hasResult() + String incompleteQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and resultsize<=0 "; + return getIntResult(from, to, incompleteQuery); + } + + private int getIntResult(Timestamp from, Timestamp to, String query) + throws SQLException { + + log.debug("getIntRes: QUERY: " + query); + log.debug("getIntRes: FROM: " + from); + log.debug("getIntRes: TO: " + to); + + PreparedStatement pstm = conn.prepareStatement(query); + pstm.setTimestamp(1, from); + pstm.setTimestamp(2, to); + pstm.execute(); + ResultSet res = pstm.getResultSet(); + boolean exist = res.next(); + int count = 0; + if (exist) { + count = res.getInt(1); + } + log.debug("getIntRes: RES: " + count); + res.close(); + pstm.close(); + return count; + } + public List readData(Timestamp from, Timestamp to, Services wservice, Boolean clusterOnly) throws SQLException { @@ -184,6 +234,11 @@ public class StatDB { } } + log.debug("QUERY: " + query); + log.debug("FROM: " + from); + log.debug("TO: " + to); + log.debug("WS: " + wservice); + PreparedStatement pstm = conn.prepareStatement(query); pstm.setTimestamp(1, from); pstm.setTimestamp(2, to); @@ -193,12 +248,17 @@ public class StatDB { pstm.execute(); List stats = new ArrayList(); ResultSet rs = pstm.getResultSet(); + int rcount = 0; + while (rs.next()) { + rcount++; stats.add(JobStat.newInstance(Services.getService(rs.getString(1)), rs.getString(2), rs.getString(3), rs.getTimestamp(4), rs.getTimestamp(5), rs.getLong(6), rs.getLong(7), rs.getBoolean(8), rs.getBoolean(9))); } + + log.debug("QUERY result len: " + rcount); rs.close(); pstm.close(); @@ -219,6 +279,7 @@ public class StatDB { } } result.close(); + st.close(); } public void shutdownDBServer() { diff --git a/webservices/compbio/stat/collector/StatProcessor.java b/webservices/compbio/stat/collector/StatProcessor.java index 2e6d0eb..fdd832e 100644 --- a/webservices/compbio/stat/collector/StatProcessor.java +++ b/webservices/compbio/stat/collector/StatProcessor.java @@ -136,7 +136,6 @@ public class StatProcessor { public List getIncompleteJobs() { List aJobs = new ArrayList(); for (JobStat js : stats) { - int jobtime = js.getRuntime(); if (!js.hasResult()) { aJobs.add(js); } diff --git a/webservices/compbio/stat/servlet/AnnualStat.java b/webservices/compbio/stat/servlet/AnnualStat.java index b54af52..9176db5 100644 --- a/webservices/compbio/stat/servlet/AnnualStat.java +++ b/webservices/compbio/stat/servlet/AnnualStat.java @@ -24,10 +24,8 @@ public class AnnualStat extends HttpServlet { try { StatDB db = new StatDB(); Date earliestRec = db.getEarliestRecord(); - Map stats = StatCollection - .getStats(earliestRec); Map monthlyTotals = StatCollection - .getTotalStats(stats); + .getStats(earliestRec); req.setAttribute("stat", monthlyTotals); req.setAttribute("total", Totals.sumOfTotals(monthlyTotals)); diff --git a/webservices/compbio/stat/servlet/DisplayStat.java b/webservices/compbio/stat/servlet/DisplayStat.java index 957dd7b..a235f7e 100644 --- a/webservices/compbio/stat/servlet/DisplayStat.java +++ b/webservices/compbio/stat/servlet/DisplayStat.java @@ -2,13 +2,9 @@ package compbio.stat.servlet; import java.io.IOException; import java.sql.SQLException; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; -import java.util.Map; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -24,17 +20,19 @@ public class DisplayStat extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - String month = req.getParameter("month"); - System.out.println("? " + month); - DateFormat df = SimpleDateFormat.getInstance(); + String datetime = req.getParameter("datetime"); + System.out.println("? " + datetime); + Date fromDate = new Date(Long.parseLong(datetime)); + Calendar toCal = GregorianCalendar.getInstance(); + toCal.setTime(fromDate); + toCal.add(Calendar.MONTH, 1); try { - Date fromDate = df.parse(month); - Map statsMap = StatCollection - .getStats(fromDate); - assert statsMap.size() == 1; - StatCollection stats = statsMap.values().iterator().next(); - req.setAttribute("stat", stats.getAllStat()); + StatCollection stats = StatCollection.newStatCollecton(fromDate, + toCal.getTime()); + + System.out.println("stats: " + stats); + req.setAttribute("stat", stats); req.setAttribute("statTotal", Totals.sumStats(stats.getAllStat())); req.setAttribute("statTotalCluster", Totals.sumStats(stats.getClusterStat())); @@ -42,11 +40,8 @@ public class DisplayStat extends HttpServlet { Totals.sumStats(stats.getLocalStat())); req.setAttribute("startDate", fromDate.getTime()); - Calendar c = GregorianCalendar.getInstance(); - c.setTime(fromDate); - c.add(Calendar.MONTH, 1); - req.setAttribute("stopDate", c.getTime()); - + req.setAttribute("stopDate", toCal.getTime().getTime()); + System.out.println(fromDate + " " + toCal.getTime()); RequestDispatcher dispatcher = req .getRequestDispatcher("statpages/Statistics.jsp"); dispatcher.forward(req, resp); @@ -54,9 +49,6 @@ public class DisplayStat extends HttpServlet { } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } } diff --git a/webservices/compbio/stat/servlet/util/StatCollection.java b/webservices/compbio/stat/servlet/util/StatCollection.java index d6644c3..0409e2c 100644 --- a/webservices/compbio/stat/servlet/util/StatCollection.java +++ b/webservices/compbio/stat/servlet/util/StatCollection.java @@ -41,16 +41,15 @@ public class StatCollection { return localStat; } - public static Map getStats(int monthsToReport) + public static Map getStats(int monthsToReport) throws SQLException { Calendar fromCal = GregorianCalendar.getInstance(); fromCal.add(Calendar.MONTH, -monthsToReport); return getStats(fromCal.getTime()); } - public static Map getStats(Date fromDate) - throws SQLException { - Map allstats = new TreeMap(); + public static Map getStats(Date fromDate) throws SQLException { + Map allstats = new TreeMap(); Calendar fromCal = GregorianCalendar.getInstance(); fromCal.setTime(fromDate); @@ -58,35 +57,30 @@ public class StatCollection { Calendar toCal = GregorianCalendar.getInstance(); toCal.setTime(new Date()); - if (fromCal.after(toCal)) { throw new AssertionError("From Date must be before ToDate! "); } - - while (true) { + while (fromCal.before(toCal)) { Date from = fromCal.getTime(); fromCal.add(Calendar.MONTH, 1); - if (toCal.before(fromCal)) { - allstats.put(toCal.getTime(), - StatCollection.newStatCollecton(from, toCal.getTime())); - break; - } - // System.out.println("!" + from + " !!! " + fromCal.getTime()); - allstats.put(from, - StatCollection.newStatCollecton(from, fromCal.getTime())); + allstats.put(from, getJobCounts(from, fromCal.getTime())); } return allstats; } - public static Map getTotalStats( - Map detailedStats) { - Map totals = new TreeMap(); - for (Map.Entry stat : detailedStats.entrySet()) { - totals.put(stat.getKey(), Totals.sumStats(stat.getValue().allStat)); - } - return totals; + + private static Totals getJobCounts(Date from, Date to) throws SQLException { + StatDB db = new StatDB(); + Totals t = new Totals(); + Timestamp fromTime = new Timestamp(from.getTime()); + Timestamp toTime = new Timestamp(to.getTime()); + t.total = db.getTotalJobsCount(fromTime, toTime); + t.incomplete = db.getIncompleteCount(fromTime, toTime); + t.abandoned = db.getAbandonedCount(fromTime, toTime); + t.cancelled = db.getCancelledCount(fromTime, toTime); + return t; } - static StatCollection newStatCollecton(Date startDate, Date endDate) + public static StatCollection newStatCollecton(Date startDate, Date endDate) throws SQLException { Timestamp startStamp = new Timestamp(startDate.getTime()); -- 1.7.10.2