1 package compbio.stat.collector;
\r
3 import java.sql.Connection;
\r
4 import java.sql.DriverManager;
\r
5 import java.sql.PreparedStatement;
\r
6 import java.sql.ResultSet;
\r
7 import java.sql.SQLException;
\r
8 import java.sql.Statement;
\r
9 import java.sql.Timestamp;
\r
10 import java.util.ArrayList;
\r
11 import java.util.Date;
\r
12 import java.util.List;
\r
13 import java.util.Set;
\r
15 import org.apache.log4j.Logger;
\r
17 import compbio.engine.conf.PropertyHelperManager;
\r
18 import compbio.util.Util;
\r
19 import compbio.ws.client.Services;
\r
22 * The database must be stored in the application root directory and called
\r
23 * "ExecutionStatistic"
\r
28 public class StatDB {
\r
30 private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
\r
31 private static final String protocol = "jdbc:derby:";
\r
32 private static final String statDBName = "ExecutionStatistic";
\r
34 private static final Logger log = Logger.getLogger(StatDB.class);
\r
36 static Connection conn;
\r
38 private synchronized static Connection getDBConnection()
\r
39 throws SQLException {
\r
41 if (conn != null && !conn.isClosed()) {
\r
44 String dbpath = PropertyHelperManager.getLocalPath();
\r
45 log.info("Looking for JABAWS access statistics database at: "
\r
47 System.setProperty("derby.system.home", dbpath);
\r
48 conn = DriverManager.getConnection(protocol + statDBName
\r
51 conn.setAutoCommit(true);
\r
53 * Runtime.getRuntime().addShutdownHook(new Thread() {
\r
55 * @Override public void run() { shutdownDBServer(); } });
\r
60 public StatDB() throws SQLException {
\r
61 this.conn = getDBConnection();
\r
65 * Connect to test database
\r
68 * @throws SQLException
\r
70 StatDB(boolean ignored) throws SQLException {
\r
71 this.conn = getTestDBConnection();
\r
74 private static Connection getTestDBConnection() throws SQLException {
\r
75 System.setProperty("derby.system.home", "testsrc/testdata");
\r
76 Connection conn = DriverManager.getConnection(protocol + statDBName
\r
78 conn.setAutoCommit(true);
\r
79 log.debug("Connecting to the TEST database!");
\r
83 // ServiceName,jobname,start,finish,inputSize,resultSize,isCancelled,isCollected
\r
86 * rs.getBoolean(i) will return true for any non-zero value and false for 0
\r
87 * on SMALLINT data column.
\r
89 * @throws SQLException
\r
91 private void createStatTable() throws SQLException {
\r
94 * Creating a statement object that we can use for running various SQL
\r
95 * statements commands against the database.
\r
97 Statement s = conn.createStatement();
\r
98 String create = "create table exec_stat("
\r
99 + "number INT GENERATED ALWAYS AS IDENTITY,"
\r
100 + "service_name VARCHAR(15) NOT NULL, "
\r
101 + "cluster_job_id VARCHAR(30), "
\r
102 + "job_id VARCHAR(35) NOT NULL PRIMARY KEY, "
\r
103 + "start TIMESTAMP," + "finish TIMESTAMP,"
\r
104 + "inputsize BIGINT," + "resultsize BIGINT,"
\r
105 + "isCancelled SMALLINT NOT NULL,"
\r
106 + "isCollected SMALLINT NOT NULL, "
\r
107 + "isClusterJob SMALLINT NOT NULL)";
\r
108 // We create a table...
\r
115 static void clearStatTable() throws SQLException {
\r
116 Connection conn = getDBConnection();
\r
117 String query = "delete from exec_stat";
\r
118 Statement st = conn.createStatement();
\r
119 st.executeUpdate(query);
\r
125 void insertData(Set<JobStat> jobstatus) throws SQLException {
\r
126 log.info("Inserting " + jobstatus.size()
\r
127 + " new records into the statistics database");
\r
129 conn.setAutoCommit(false);
\r
130 String insert = "insert into exec_stat (service_name, cluster_job_id, job_id, start, finish, "
\r
131 + "inputsize, resultsize, isCancelled, isCollected, isClusterJob) "
\r
132 + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
133 PreparedStatement pstm = conn.prepareStatement(insert);
\r
134 for (JobStat js : jobstatus) {
\r
136 pstm.setString(1, js.webService.toString());
\r
138 if (!Util.isEmpty(js.clusterJobId)) {
\r
139 pstm.setString(2, js.clusterJobId);
\r
141 pstm.setString(2, null);
\r
144 pstm.setString(3, js.jobname);
\r
146 if (js.start != ExecutionStatCollector.UNDEFINED) {
\r
147 pstm.setTimestamp(4, new Timestamp(js.start));
\r
149 pstm.setTimestamp(4, null);
\r
151 if (js.finish != ExecutionStatCollector.UNDEFINED) {
\r
152 pstm.setTimestamp(5, new Timestamp(js.finish));
\r
154 pstm.setTimestamp(5, null);
\r
157 pstm.setLong(6, js.inputSize);
\r
159 pstm.setLong(7, js.resultSize);
\r
161 pstm.setBoolean(8, js.isCancelled);
\r
162 pstm.setBoolean(9, js.isCollected);
\r
163 pstm.setBoolean(10, js.isClusterJob());
\r
164 pstm.executeUpdate();
\r
167 conn.setAutoCommit(true);
\r
171 public Date getEarliestRecord() throws SQLException {
\r
172 String query = "select min(start) from exec_stat";
\r
173 Statement st = conn.createStatement();
\r
174 ResultSet res = st.executeQuery(query);
\r
175 boolean exist = res.next();
\r
176 Date date = new Date();
\r
178 date = res.getDate(1);
\r
186 public int getTotalJobsCount(Timestamp from, Timestamp to)
\r
187 throws SQLException {
\r
188 String allQuery = "select count(*) from exec_stat where start BETWEEN ? and ? ";
\r
189 return getIntResult(from, to, allQuery);
\r
192 public int getCancelledCount(Timestamp from, Timestamp to)
\r
193 throws SQLException {
\r
195 String cancelledQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCancelled=1 ";
\r
196 return getIntResult(from, to, cancelledQuery);
\r
199 public int getAbandonedCount(Timestamp from, Timestamp to)
\r
200 throws SQLException {
\r
201 // !js.isCollected && !js.isCancelled && js.hasResult()
\r
202 String abandonedQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCollected=0 and isCancelled=0 and resultsize>0 ";
\r
203 return getIntResult(from, to, abandonedQuery);
\r
206 public int getIncompleteCount(Timestamp from, Timestamp to)
\r
207 throws SQLException {
\r
209 String incompleteQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and resultsize<=0 and isCancelled=0";
\r
210 return getIntResult(from, to, incompleteQuery);
\r
213 private int getIntResult(Timestamp from, Timestamp to, String query)
\r
214 throws SQLException {
\r
216 log.debug("getIntRes: QUERY: " + query);
\r
217 log.debug("getIntRes: FROM: " + from);
\r
218 log.debug("getIntRes: TO: " + to);
\r
220 PreparedStatement pstm = conn.prepareStatement(query);
\r
221 pstm.setTimestamp(1, from);
\r
222 pstm.setTimestamp(2, to);
\r
224 ResultSet res = pstm.getResultSet();
\r
225 boolean exist = res.next();
\r
228 count = res.getInt(1);
\r
230 log.debug("getIntRes: RES: " + count);
\r
236 public List<JobStat> readData(Timestamp from, Timestamp to,
\r
237 Services wservice, Boolean clusterOnly) throws SQLException {
\r
239 String query = "select service_name, cluster_job_id, job_id, start, finish, inputsize, "
\r
240 + "resultsize, isCancelled, isCollected from exec_stat where start BETWEEN ? and ? ";
\r
242 if (wservice != null) {
\r
243 query += " and service_name=? ";
\r
246 if (clusterOnly != null) {
\r
248 query += " and isClusterJob!=0 ";
\r
250 query += " and isClusterJob=0 ";
\r
254 log.debug("QUERY: " + query);
\r
255 log.debug("FROM: " + from);
\r
256 log.debug("TO: " + to);
\r
257 log.debug("WS: " + wservice);
\r
259 PreparedStatement pstm = conn.prepareStatement(query);
\r
260 pstm.setTimestamp(1, from);
\r
261 pstm.setTimestamp(2, to);
\r
262 if (wservice != null) {
\r
263 pstm.setString(3, wservice.toString());
\r
266 List<JobStat> stats = new ArrayList<JobStat>();
\r
267 ResultSet rs = pstm.getResultSet();
\r
270 while (rs.next()) {
\r
272 stats.add(JobStat.newInstance(Services.getService(rs.getString(1)),
\r
273 rs.getString(2), rs.getString(3), rs.getTimestamp(4),
\r
274 rs.getTimestamp(5), rs.getLong(6), rs.getLong(7),
\r
275 rs.getBoolean(8), rs.getBoolean(9)));
\r
278 log.debug("QUERY result len: " + rcount);
\r
286 * Removes the job if
\r
288 * 1) It has already been recorded
\r
290 * 2) It has not completed and did not timeout - this is to prevent
\r
291 * recording the information on the incomplete jobs.
\r
294 * @throws SQLException
\r
296 public void removeRecordedJobs(Set<JobStat> fsJobs) throws SQLException {
\r
298 String query = "select job_id from exec_stat";
\r
300 Statement st = conn.createStatement();
\r
301 ResultSet result = st.executeQuery(query);
\r
303 while (result.next()) {
\r
304 String recordedJob = result.getString(1);
\r
305 JobStat recStat = JobStat.newIncompleteStat(recordedJob);
\r
306 if (fsJobs.contains(recStat)) {
\r
307 fsJobs.remove(recStat);
\r
314 public static synchronized final void shutdownDBServer() {
\r
315 // ## DATABASE SHUTDOWN SECTION ##
\r
317 * In embedded mode, an application should shut down Derby. Shutdown
\r
318 * throws the XJ015 exception to confirm success.
\r
321 if (conn != null) {
\r
324 } catch (SQLException e) {
\r
325 log.warn("Database commit failed with " + e.getLocalizedMessage());
\r
327 boolean gotSQLExc = false;
\r
329 DriverManager.getConnection("jdbc:derby:;shutdown=true");
\r
330 } catch (SQLException se) {
\r
331 if (se.getSQLState().equals("XJ015")) {
\r
336 log.warn("Database did not shut down normally");
\r
338 log.info("Database shut down normally");
\r
341 public static void main(String[] args) {
\r
342 // This is called from Ant cleanStatTable task
\r
345 shutdownDBServer();
\r
346 } catch (SQLException e) {
\r
347 System.err.println("Fails to clean up JABAWS stat database!");
\r
348 e.printStackTrace();
\r
350 // new StatDB().createStatTable();
\r
351 // insertData(null);
\r
353 * StatDB statdb = new StatDB(); Date from = new Date();
\r
354 * from.setMonth(1); System.out.println(new
\r
355 * StatProcessor(statdb.readData( new Timestamp(from.getTime()), new
\r
356 * Timestamp(new Date().getTime()), null, null)).reportStat());
\r