+package compbio.ws.execstat;\r
+\r
+import java.sql.Connection;\r
+import java.sql.DriverManager;\r
+import java.sql.PreparedStatement;\r
+import java.sql.ResultSet;\r
+import java.sql.SQLException;\r
+import java.sql.Statement;\r
+import java.sql.Timestamp;\r
+import java.util.ArrayList;\r
+import java.util.Date;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import compbio.ws.client.Services;\r
+import compbio.ws.execstat.StatProcessor.JobStat;\r
+\r
+public class StatDB {\r
+\r
+ /* the default framework is embedded */\r
+ // private final String framework = "embedded";\r
+ private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";\r
+ private static final String protocol = "jdbc:derby:";\r
+ private static final String statDBName = "ExecutionStatistic";\r
+\r
+ private static Connection getDBConnection() throws SQLException {\r
+ // TODO\r
+ System.setProperty("derby.system.home", ".");\r
+ Connection conn = DriverManager.getConnection(protocol + statDBName\r
+ + ";create=true");\r
+\r
+ // We want to control transactions manually. Autocommit is on by\r
+ // default in JDBC.\r
+ // conn.setAutoCommit(false);\r
+ return conn;\r
+ }\r
+\r
+ // ServiceName,jobname,start,finish,inputSize,resultSize,isCancelled,isCollected\r
+ /**\r
+ * \r
+ * rs.getBoolean(i) will return true for any non-zero value and false for 0\r
+ * on SMALLINT data column.\r
+ * \r
+ * @throws SQLException\r
+ */\r
+ private static void createStatTable() throws SQLException {\r
+ Connection conn = getDBConnection();\r
+ /*\r
+ * Creating a statement object that we can use for running various SQL\r
+ * statements commands against the database.\r
+ */\r
+ Statement s = conn.createStatement();\r
+ String create = "create table exec_stat("\r
+ + "number INT GENERATED ALWAYS AS IDENTITY,"\r
+ + "service_name VARCHAR(15) NOT NULL, "\r
+ + "cluster_job_id VARCHAR(30), "\r
+ + "job_id VARCHAR(35) NOT NULL PRIMARY KEY, "\r
+ + "start TIMESTAMP," + "finish TIMESTAMP,"\r
+ + "inputsize BIGINT," + "resultsize BIGINT,"\r
+ + "isCancelled SMALLINT NOT NULL,"\r
+ + "isCollected SMALLINT NOT NULL, "\r
+ + "isClusterJob SMALLINT NOT NULL)";\r
+ // We create a table...\r
+ System.out.println(create);\r
+ s.execute(create);\r
+ s.close();\r
+ conn.close();\r
+ }\r
+\r
+ static void insertData(Set<JobStat> jobstatus) throws SQLException {\r
+ System.out.println("Inserting " + jobstatus.size());\r
+ Connection conn = getDBConnection();\r
+ conn.setAutoCommit(false);\r
+ String insert = "insert into exec_stat (service_name, cluster_job_id, job_id, start, finish, "\r
+ + "inputsize, resultsize, isCancelled, isCollected, isClusterJob) "\r
+ + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";\r
+ PreparedStatement pstm = conn.prepareStatement(insert);\r
+ for (JobStat js : jobstatus) {\r
+ pstm.setString(1, js.webService.toString());\r
+ pstm.setString(2, js.clusterJobId);\r
+ pstm.setString(3, js.jobname);\r
+ pstm.setTimestamp(4, new Timestamp(js.start));\r
+ pstm.setTimestamp(5, new Timestamp(js.finish));\r
+ pstm.setLong(6, js.inputSize);\r
+ pstm.setLong(7, js.resultSize);\r
+ pstm.setBoolean(8, js.isCancelled);\r
+ pstm.setBoolean(9, js.isCollected);\r
+ pstm.setBoolean(10, js.isClusterJob());\r
+ pstm.executeUpdate();\r
+ }\r
+ conn.commit();\r
+ pstm.close();\r
+ conn.close();\r
+ }\r
+\r
+ static List<JobStat> readData(Timestamp from, Timestamp to,\r
+ Services wservice, Boolean clusterOnly) throws SQLException {\r
+ Connection conn = getDBConnection();\r
+ String query = "select service_name, cluster_job_id, job_id, start, finish, inputsize, "\r
+ + "resultsize, isCancelled, isCollected from exec_stat where start BETWEEN ? and ? ";\r
+\r
+ if (wservice != null) {\r
+ query += " and service_name=? ";\r
+ }\r
+\r
+ if (clusterOnly != null) {\r
+ if (clusterOnly) {\r
+ query += " and isClusterJob!=0 ";\r
+ } else {\r
+ query += " and isClusterJob=0 ";\r
+ }\r
+ }\r
+\r
+ PreparedStatement pstm = conn.prepareStatement(query);\r
+ pstm.setTimestamp(1, from);\r
+ pstm.setTimestamp(2, to);\r
+ if (wservice != null) {\r
+ pstm.setString(3, wservice.toString());\r
+ }\r
+ pstm.execute();\r
+ List<JobStat> stats = new ArrayList<StatProcessor.JobStat>();\r
+ ResultSet rs = pstm.getResultSet();\r
+ while (rs.next()) {\r
+ stats.add(new JobStat(Services.getService(rs.getString(1)), rs\r
+ .getString(2), rs.getString(3), rs.getTimestamp(4)\r
+ .getTime(), rs.getTimestamp(5).getTime(), rs.getLong(6), rs\r
+ .getLong(7), rs.getBoolean(8), rs.getBoolean(9)));\r
+ }\r
+ rs.close();\r
+ pstm.close();\r
+ conn.close();\r
+ return stats;\r
+ }\r
+\r
+ static void removeRecordedJobs(Set<String> fsJobs) throws SQLException {\r
+ Connection conn = getDBConnection();\r
+ String query = "select job_id from exec_stat";\r
+\r
+ Statement st = conn.createStatement();\r
+ ResultSet result = st.executeQuery(query);\r
+\r
+ while (result.next()) {\r
+ String recordedJob = result.getString(1);\r
+ if (fsJobs.contains(recordedJob)) {\r
+ fsJobs.remove(recordedJob);\r
+ }\r
+ }\r
+ result.close();\r
+ conn.close();\r
+ }\r
+\r
+ void shutdownDBServer() {\r
+ // ## DATABASE SHUTDOWN SECTION ##\r
+ /***\r
+ * In embedded mode, an application should shut down Derby. Shutdown\r
+ * throws the XJ015 exception to confirm success.\r
+ ***/\r
+ boolean gotSQLExc = false;\r
+ try {\r
+ DriverManager.getConnection("jdbc:derby:;shutdown=true");\r
+ } catch (SQLException se) {\r
+ if (se.getSQLState().equals("XJ015")) {\r
+ gotSQLExc = true;\r
+ }\r
+ }\r
+ if (!gotSQLExc) {\r
+ System.out.println("Database did not shut down normally");\r
+ } else {\r
+ System.out.println("Database shut down normally");\r
+ }\r
+ }\r
+ public static void main(String[] args) throws SQLException {\r
+ // createStatTable();\r
+ // insertData(null);\r
+\r
+ Date from = new Date();\r
+ from.setMonth(1);\r
+ System.out.println(new StatProcessor(readData(\r
+ new Timestamp(from.getTime()),\r
+ new Timestamp(new Date().getTime()), null, null)).reportStat());\r
+\r
+ }\r
+}\r