Further work on statistics display
authorpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Thu, 26 May 2011 15:02:54 +0000 (15:02 +0000)
committerpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Thu, 26 May 2011 15:02:54 +0000 (15:02 +0000)
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
webservices/compbio/stat/collector/StatDB.java
webservices/compbio/stat/collector/StatProcessor.java
webservices/compbio/stat/servlet/AnnualStat.java
webservices/compbio/stat/servlet/DisplayStat.java
webservices/compbio/stat/servlet/util/StatCollection.java

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