1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.stat.collector;
\r
20 import java.sql.Connection;
\r
21 import java.sql.DriverManager;
\r
22 import java.sql.PreparedStatement;
\r
23 import java.sql.ResultSet;
\r
24 import java.sql.SQLException;
\r
25 import java.sql.Statement;
\r
26 import java.sql.Timestamp;
\r
27 import java.util.ArrayList;
\r
28 import java.util.Date;
\r
29 import java.util.List;
\r
30 import java.util.Set;
\r
32 import org.apache.log4j.Logger;
\r
34 import compbio.engine.conf.PropertyHelperManager;
\r
35 import compbio.util.Util;
\r
36 import compbio.ws.client.Services;
\r
39 * The database must be stored in the application root directory and called
\r
40 * "ExecutionStatistic"
\r
45 public class StatDB {
\r
47 private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
\r
48 private static final String protocol = "jdbc:derby:";
\r
49 private static final String statDBName = "ExecutionStatistic";
\r
51 private static final Logger log = Logger.getLogger(StatDB.class);
\r
53 static Connection conn;
\r
55 private synchronized static Connection getDBConnection() throws SQLException {
\r
57 if (conn != null && !conn.isClosed()) {
\r
61 String dbpath = PropertyHelperManager.getLocalPath();
\r
62 log.info("Looking for JABAWS access statistics database at: " + dbpath);
\r
63 System.setProperty("derby.system.home", dbpath);
\r
64 // Apparently under Tomcat webapp you cannot rely on Java
\r
65 // auto discovery and have to register the driver explicitly
\r
66 Class.forName(driver);
\r
67 conn = DriverManager.getConnection(protocol + statDBName + ";create=false");
\r
69 conn.setAutoCommit(true);
\r
70 } catch (ClassNotFoundException e) {
\r
71 log.error(e.getMessage(), e);
\r
77 public StatDB() throws SQLException {
\r
78 this.conn = getDBConnection();
\r
82 * Connect to test database
\r
85 * @throws SQLException
\r
87 StatDB(boolean ignored) throws SQLException {
\r
88 this.conn = getTestDBConnection();
\r
91 private static Connection getTestDBConnection() throws SQLException {
\r
92 System.setProperty("derby.system.home", "testsrc/testdata");
\r
93 Connection conn = DriverManager.getConnection(protocol + statDBName
\r
95 conn.setAutoCommit(true);
\r
96 log.debug("Connecting to the TEST database!");
\r
100 // ServiceName,jobname,start,finish,inputSize,resultSize,isCancelled,isCollected
\r
103 * rs.getBoolean(i) will return true for any non-zero value and false for 0
\r
104 * on SMALLINT data column.
\r
106 * @throws SQLException
\r
108 private void createStatTable() throws SQLException {
\r
109 // Creating a statement object that we can use for running various SQL
\r
110 // statements commands against the database.
\r
111 Statement s = conn.createStatement();
\r
112 String create = "create table exec_stat("
\r
113 + "number INT GENERATED ALWAYS AS IDENTITY,"
\r
114 + "service_name VARCHAR(15) NOT NULL, "
\r
115 + "cluster_job_id VARCHAR(30), "
\r
116 + "job_id VARCHAR(35) NOT NULL PRIMARY KEY, "
\r
117 + "start TIMESTAMP," + "finish TIMESTAMP,"
\r
118 + "inputsize BIGINT," + "resultsize BIGINT,"
\r
119 + "isCancelled SMALLINT NOT NULL,"
\r
120 + "isCollected SMALLINT NOT NULL, "
\r
121 + "isClusterJob SMALLINT NOT NULL)";
\r
122 // We create a table...
\r
129 static void clearStatTable() throws SQLException {
\r
130 Connection conn = getDBConnection();
\r
131 String query = "delete from exec_stat";
\r
132 Statement st = conn.createStatement();
\r
133 st.executeUpdate(query);
\r
139 void insertData(Set<JobStat> jobstatus) throws SQLException {
\r
140 log.info("Inserting " + jobstatus.size()
\r
141 + " new records into the statistics database");
\r
143 conn.setAutoCommit(false);
\r
144 String insert = "insert into exec_stat (service_name, cluster_job_id, job_id, start, finish, "
\r
145 + "inputsize, resultsize, isCancelled, isCollected, isClusterJob) "
\r
146 + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
147 PreparedStatement pstm = conn.prepareStatement(insert);
\r
148 for (JobStat js : jobstatus) {
\r
150 pstm.setString(1, js.webService.toString());
\r
152 if (!Util.isEmpty(js.clusterJobId)) {
\r
153 pstm.setString(2, js.clusterJobId);
\r
155 pstm.setString(2, null);
\r
158 pstm.setString(3, js.jobname);
\r
160 if (js.start != ExecutionStatCollector.UNDEFINED) {
\r
161 pstm.setTimestamp(4, new Timestamp(js.start));
\r
163 pstm.setTimestamp(4, null);
\r
165 if (js.finish != ExecutionStatCollector.UNDEFINED) {
\r
166 pstm.setTimestamp(5, new Timestamp(js.finish));
\r
168 pstm.setTimestamp(5, null);
\r
171 pstm.setLong(6, js.inputSize);
\r
173 pstm.setLong(7, js.resultSize);
\r
175 pstm.setBoolean(8, js.isCancelled);
\r
176 pstm.setBoolean(9, js.isCollected);
\r
177 pstm.setBoolean(10, js.isClusterJob());
\r
178 pstm.executeUpdate();
\r
181 conn.setAutoCommit(true);
\r
185 public Date getEarliestRecord() throws SQLException {
\r
186 String query = "select min(start) from exec_stat";
\r
187 Statement st = conn.createStatement();
\r
188 ResultSet res = st.executeQuery(query);
\r
189 boolean exist = res.next();
\r
190 Date date = new Date();
\r
192 date = res.getDate(1);
\r
199 public int getTotalJobsCount(Timestamp from, Timestamp to)
\r
200 throws SQLException {
\r
201 String allQuery = "select count(*) from exec_stat where start BETWEEN ? and ? ";
\r
202 return getIntResult(from, to, allQuery);
\r
205 public int getCancelledCount(Timestamp from, Timestamp to)
\r
206 throws SQLException {
\r
208 String cancelledQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCancelled=1 ";
\r
209 return getIntResult(from, to, cancelledQuery);
\r
212 public int getAbandonedCount(Timestamp from, Timestamp to)
\r
213 throws SQLException {
\r
214 // !js.isCollected && !js.isCancelled && js.hasResult()
\r
215 String abandonedQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCollected=0 and isCancelled=0 and resultsize>0 ";
\r
216 return getIntResult(from, to, abandonedQuery);
\r
219 public int getIncompleteCount(Timestamp from, Timestamp to)
\r
220 throws SQLException {
\r
222 String incompleteQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and resultsize<=0 and isCancelled=0";
\r
223 return getIntResult(from, to, incompleteQuery);
\r
226 private int getIntResult(Timestamp from, Timestamp to, String query)
\r
227 throws SQLException {
\r
229 log.debug("getIntRes: QUERY: " + query);
\r
230 log.debug("getIntRes: FROM: " + from);
\r
231 log.debug("getIntRes: TO: " + to);
\r
233 PreparedStatement pstm = conn.prepareStatement(query);
\r
234 pstm.setTimestamp(1, from);
\r
235 pstm.setTimestamp(2, to);
\r
237 ResultSet res = pstm.getResultSet();
\r
238 boolean exist = res.next();
\r
241 count = res.getInt(1);
\r
243 log.debug("getIntRes: RES: " + count);
\r
249 public List<JobStat> readData(Timestamp from, Timestamp to,
\r
250 Services wservice, Boolean clusterOnly) throws SQLException {
\r
252 String query = "select service_name, cluster_job_id, job_id, start, finish, inputsize, "
\r
253 + "resultsize, isCancelled, isCollected from exec_stat where start BETWEEN ? and ? ";
\r
255 if (wservice != null) {
\r
256 query += " and service_name=? ";
\r
259 if (clusterOnly != null) {
\r
261 query += " and isClusterJob!=0 ";
\r
263 query += " and isClusterJob=0 ";
\r
267 log.debug("QUERY: " + query);
\r
268 log.debug("FROM: " + from);
\r
269 log.debug("TO: " + to);
\r
270 log.debug("WS: " + wservice);
\r
272 PreparedStatement pstm = conn.prepareStatement(query);
\r
273 pstm.setTimestamp(1, from);
\r
274 pstm.setTimestamp(2, to);
\r
275 if (wservice != null) {
\r
276 pstm.setString(3, wservice.toString());
\r
279 List<JobStat> stats = new ArrayList<JobStat>();
\r
280 ResultSet rs = pstm.getResultSet();
\r
283 while (rs.next()) {
\r
285 stats.add(JobStat.newInstance(Services.getService(rs.getString(1)),
\r
286 rs.getString(2), rs.getString(3), rs.getTimestamp(4),
\r
287 rs.getTimestamp(5), rs.getLong(6), rs.getLong(7),
\r
288 rs.getBoolean(8), rs.getBoolean(9)));
\r
291 log.debug("QUERY result len: " + rcount);
\r
299 * Removes the job if
\r
301 * 1) It has already been recorded
\r
303 * 2) It has not completed and did not timeout - this is to prevent
\r
304 * recording the information on the incomplete jobs.
\r
307 * @throws SQLException
\r
309 public void removeRecordedJobs(Set<JobStat> fsJobs) throws SQLException {
\r
311 String query = "select job_id from exec_stat";
\r
313 Statement st = conn.createStatement();
\r
314 ResultSet result = st.executeQuery(query);
\r
316 while (result.next()) {
\r
317 String recordedJob = result.getString(1);
\r
318 JobStat recStat = JobStat.newIncompleteStat(recordedJob);
\r
319 if (fsJobs.contains(recStat)) {
\r
320 fsJobs.remove(recStat);
\r
327 public static synchronized final void shutdownDBServer() {
\r
328 // ## DATABASE SHUTDOWN SECTION ##
\r
330 * In embedded mode, an application should shut down Derby. Shutdown
\r
331 * throws the XJ015 exception to confirm success.
\r
334 if (conn != null) {
\r
337 } catch (SQLException e) {
\r
338 log.warn("Database commit failed with " + e.getLocalizedMessage());
\r
340 boolean gotSQLExc = false;
\r
342 DriverManager.getConnection("jdbc:derby:;shutdown=true");
\r
343 } catch (SQLException se) {
\r
344 if (se.getSQLState().equals("XJ015")) {
\r
349 log.warn("Database did not shut down normally");
\r
351 log.info("Database shut down normally");
\r
354 public static void main(String[] args) {
\r
355 // This is called from Ant cleanStatTable task
\r
358 shutdownDBServer();
\r
359 } catch (SQLException e) {
\r
360 System.err.println("Fails to clean up JABAWS stat database!");
\r
361 e.printStackTrace();
\r
363 // new StatDB().createStatTable();
\r
364 // insertData(null);
\r
366 * StatDB statdb = new StatDB(); Date from = new Date();
\r
367 * from.setMonth(1); System.out.println(new
\r
368 * StatProcessor(statdb.readData( new Timestamp(from.getTime()), new
\r
369 * Timestamp(new Date().getTime()), null, null)).reportStat());
\r