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
56 public StatDB() throws SQLException {
\r
57 this.conn = getDBConnection();
\r
61 * Connect to test database
\r
64 * @throws SQLException
\r
66 StatDB(boolean ignored) throws SQLException {
\r
67 this.conn = getTestDBConnection();
\r
70 private static Connection getTestDBConnection() throws SQLException {
\r
71 System.setProperty("derby.system.home", "testsrc/testdata");
\r
72 Connection conn = DriverManager.getConnection(protocol + statDBName
\r
74 conn.setAutoCommit(true);
\r
78 // ServiceName,jobname,start,finish,inputSize,resultSize,isCancelled,isCollected
\r
81 * rs.getBoolean(i) will return true for any non-zero value and false for 0
\r
82 * on SMALLINT data column.
\r
84 * @throws SQLException
\r
86 private void createStatTable() throws SQLException {
\r
89 * Creating a statement object that we can use for running various SQL
\r
90 * statements commands against the database.
\r
92 Statement s = conn.createStatement();
\r
93 String create = "create table exec_stat("
\r
94 + "number INT GENERATED ALWAYS AS IDENTITY,"
\r
95 + "service_name VARCHAR(15) NOT NULL, "
\r
96 + "cluster_job_id VARCHAR(30), "
\r
97 + "job_id VARCHAR(35) NOT NULL PRIMARY KEY, "
\r
98 + "start TIMESTAMP," + "finish TIMESTAMP,"
\r
99 + "inputsize BIGINT," + "resultsize BIGINT,"
\r
100 + "isCancelled SMALLINT NOT NULL,"
\r
101 + "isCollected SMALLINT NOT NULL, "
\r
102 + "isClusterJob SMALLINT NOT NULL)";
\r
103 // We create a table...
\r
104 System.out.println(create);
\r
110 void insertData(Set<JobStat> jobstatus) throws SQLException {
\r
111 System.out.println("Inserting " + jobstatus.size());
\r
113 conn.setAutoCommit(false);
\r
114 String insert = "insert into exec_stat (service_name, cluster_job_id, job_id, start, finish, "
\r
115 + "inputsize, resultsize, isCancelled, isCollected, isClusterJob) "
\r
116 + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
117 PreparedStatement pstm = conn.prepareStatement(insert);
\r
118 for (JobStat js : jobstatus) {
\r
120 pstm.setString(1, js.webService.toString());
\r
122 if (!Util.isEmpty(js.clusterJobId)) {
\r
123 pstm.setString(2, js.clusterJobId);
\r
125 pstm.setString(2, null);
\r
128 pstm.setString(3, js.jobname);
\r
130 if (js.start != ExecutionStatCollector.UNDEFINED) {
\r
131 pstm.setTimestamp(4, new Timestamp(js.start));
\r
133 pstm.setTimestamp(4, null);
\r
135 if (js.finish != ExecutionStatCollector.UNDEFINED) {
\r
136 pstm.setTimestamp(5, new Timestamp(js.finish));
\r
138 pstm.setTimestamp(5, null);
\r
141 pstm.setLong(6, js.inputSize);
\r
143 pstm.setLong(7, js.resultSize);
\r
145 pstm.setBoolean(8, js.isCancelled);
\r
146 pstm.setBoolean(9, js.isCollected);
\r
147 pstm.setBoolean(10, js.isClusterJob());
\r
148 pstm.executeUpdate();
\r
154 public Date getEarliestRecord() throws SQLException {
\r
155 String query = "select min(start) from exec_stat";
\r
156 Statement st = conn.createStatement();
\r
157 ResultSet res = st.executeQuery(query);
\r
158 boolean exist = res.next();
\r
159 Date date = new Date();
\r
161 date = res.getDate(1);
\r
169 public int getTotalJobsCount(Timestamp from, Timestamp to)
\r
170 throws SQLException {
\r
171 String allQuery = "select count(*) from exec_stat where start BETWEEN ? and ? ";
\r
172 return getIntResult(from, to, allQuery);
\r
175 public int getCancelledCount(Timestamp from, Timestamp to)
\r
176 throws SQLException {
\r
178 String cancelledQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCancelled=1 ";
\r
179 return getIntResult(from, to, cancelledQuery);
\r
182 public int getAbandonedCount(Timestamp from, Timestamp to)
\r
183 throws SQLException {
\r
184 // !js.isCollected && !js.isCancelled && js.hasResult()
\r
185 String abandonedQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and isCollected=0 and isCancelled=0 and resultsize>0 ";
\r
186 return getIntResult(from, to, abandonedQuery);
\r
189 public int getIncompleteCount(Timestamp from, Timestamp to)
\r
190 throws SQLException {
\r
192 String incompleteQuery = "select count(*) from exec_stat where start BETWEEN ? and ? and resultsize<=0 ";
\r
193 return getIntResult(from, to, incompleteQuery);
\r
196 private int getIntResult(Timestamp from, Timestamp to, String query)
\r
197 throws SQLException {
\r
199 log.debug("getIntRes: QUERY: " + query);
\r
200 log.debug("getIntRes: FROM: " + from);
\r
201 log.debug("getIntRes: TO: " + to);
\r
203 PreparedStatement pstm = conn.prepareStatement(query);
\r
204 pstm.setTimestamp(1, from);
\r
205 pstm.setTimestamp(2, to);
\r
207 ResultSet res = pstm.getResultSet();
\r
208 boolean exist = res.next();
\r
211 count = res.getInt(1);
\r
213 log.debug("getIntRes: RES: " + count);
\r
219 public List<JobStat> readData(Timestamp from, Timestamp to,
\r
220 Services wservice, Boolean clusterOnly) throws SQLException {
\r
222 String query = "select service_name, cluster_job_id, job_id, start, finish, inputsize, "
\r
223 + "resultsize, isCancelled, isCollected from exec_stat where start BETWEEN ? and ? ";
\r
225 if (wservice != null) {
\r
226 query += " and service_name=? ";
\r
229 if (clusterOnly != null) {
\r
231 query += " and isClusterJob!=0 ";
\r
233 query += " and isClusterJob=0 ";
\r
237 log.debug("QUERY: " + query);
\r
238 log.debug("FROM: " + from);
\r
239 log.debug("TO: " + to);
\r
240 log.debug("WS: " + wservice);
\r
242 PreparedStatement pstm = conn.prepareStatement(query);
\r
243 pstm.setTimestamp(1, from);
\r
244 pstm.setTimestamp(2, to);
\r
245 if (wservice != null) {
\r
246 pstm.setString(3, wservice.toString());
\r
249 List<JobStat> stats = new ArrayList<JobStat>();
\r
250 ResultSet rs = pstm.getResultSet();
\r
253 while (rs.next()) {
\r
255 stats.add(JobStat.newInstance(Services.getService(rs.getString(1)),
\r
256 rs.getString(2), rs.getString(3), rs.getTimestamp(4),
\r
257 rs.getTimestamp(5), rs.getLong(6), rs.getLong(7),
\r
258 rs.getBoolean(8), rs.getBoolean(9)));
\r
261 log.debug("QUERY result len: " + rcount);
\r
267 public void removeRecordedJobs(Set<JobStat> fsJobs) throws SQLException {
\r
269 String query = "select job_id from exec_stat";
\r
271 Statement st = conn.createStatement();
\r
272 ResultSet result = st.executeQuery(query);
\r
274 while (result.next()) {
\r
275 String recordedJob = result.getString(1);
\r
276 JobStat recStat = JobStat.newIncompleteStat(recordedJob);
\r
277 if (fsJobs.contains(recStat)) {
\r
278 fsJobs.remove(recStat);
\r
285 public void shutdownDBServer() {
\r
286 // ## DATABASE SHUTDOWN SECTION ##
\r
288 * In embedded mode, an application should shut down Derby. Shutdown
\r
289 * throws the XJ015 exception to confirm success.
\r
292 if (conn != null) {
\r
295 } catch (SQLException e) {
\r
296 System.err.println("Database commit failed with "
\r
297 + e.getLocalizedMessage());
\r
299 boolean gotSQLExc = false;
\r
301 DriverManager.getConnection("jdbc:derby:;shutdown=true");
\r
302 } catch (SQLException se) {
\r
303 if (se.getSQLState().equals("XJ015")) {
\r
308 System.err.println("Database did not shut down normally");
\r
310 System.out.println("Database shut down normally");
\r
313 public static void main(String[] args) throws SQLException {
\r
314 // new StatDB().createStatTable();
\r
315 // insertData(null);
\r
317 * StatDB statdb = new StatDB(); Date from = new Date();
\r
318 * from.setMonth(1); System.out.println(new
\r
319 * StatProcessor(statdb.readData( new Timestamp(from.getTime()), new
\r
320 * Timestamp(new Date().getTime()), null, null)).reportStat());
\r