git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@4179 e3abac25...
[jabaws.git] / webservices / compbio / stat / collector / StatDB.java
index c58974b..55bf71a 100644 (file)
@@ -49,10 +49,16 @@ public class StatDB {
                                        + ";create=false");\r
 \r
                        conn.setAutoCommit(true);\r
+\r
+                       Runtime.getRuntime().addShutdownHook(new Thread() {\r
+                               @Override\r
+                               public void run() {\r
+                                       shutdownDBServer();\r
+                               }\r
+                       });\r
                }\r
                return conn;\r
        }\r
-\r
        public StatDB() throws SQLException {\r
                this.conn = getDBConnection();\r
        }\r
@@ -166,6 +172,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 +240,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 +254,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,9 +285,10 @@ public class StatDB {
                        }\r
                }\r
                result.close();\r
+               st.close();\r
        }\r
 \r
-       public void shutdownDBServer() {\r
+       private static synchronized final void shutdownDBServer() {\r
                // ## DATABASE SHUTDOWN SECTION ##\r
                /***\r
                 * In embedded mode, an application should shut down Derby. Shutdown\r