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.List;
\r
12 import java.util.Set;
\r
14 import org.apache.log4j.Logger;
\r
16 import compbio.engine.conf.PropertyHelperManager;
\r
17 import compbio.util.Util;
\r
18 import compbio.ws.client.Services;
\r
21 * The database must be stored in the application root directory and called
\r
22 * "ExecutionStatistic"
\r
27 public class StatDB {
\r
29 private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
\r
30 private static final String protocol = "jdbc:derby:";
\r
31 private static final String statDBName = "ExecutionStatistic";
\r
33 private static final Logger log = Logger.getLogger(StatDB.class);
\r
36 private static Connection getDBConnection() throws SQLException {
\r
37 String dbpath = PropertyHelperManager.getLocalPath();
\r
38 log.info("Looking for JABAWS access statistics database at: " + dbpath);
\r
39 System.setProperty("derby.system.home", dbpath);
\r
40 Connection conn = DriverManager.getConnection(protocol + statDBName
\r
43 conn.setAutoCommit(true);
\r
47 public StatDB() throws SQLException {
\r
48 this.conn = getDBConnection();
\r
52 * Connect to test database
\r
55 * @throws SQLException
\r
57 StatDB(boolean ignored) throws SQLException {
\r
58 this.conn = getTestDBConnection();
\r
61 private static Connection getTestDBConnection() throws SQLException {
\r
62 System.setProperty("derby.system.home", "testsrc/testdata");
\r
63 Connection conn = DriverManager.getConnection(protocol + statDBName
\r
65 conn.setAutoCommit(true);
\r
69 // ServiceName,jobname,start,finish,inputSize,resultSize,isCancelled,isCollected
\r
72 * rs.getBoolean(i) will return true for any non-zero value and false for 0
\r
73 * on SMALLINT data column.
\r
75 * @throws SQLException
\r
77 private void createStatTable() throws SQLException {
\r
80 * Creating a statement object that we can use for running various SQL
\r
81 * statements commands against the database.
\r
83 Statement s = conn.createStatement();
\r
84 String create = "create table exec_stat("
\r
85 + "number INT GENERATED ALWAYS AS IDENTITY,"
\r
86 + "service_name VARCHAR(15) NOT NULL, "
\r
87 + "cluster_job_id VARCHAR(30), "
\r
88 + "job_id VARCHAR(35) NOT NULL PRIMARY KEY, "
\r
89 + "start TIMESTAMP," + "finish TIMESTAMP,"
\r
90 + "inputsize BIGINT," + "resultsize BIGINT,"
\r
91 + "isCancelled SMALLINT NOT NULL,"
\r
92 + "isCollected SMALLINT NOT NULL, "
\r
93 + "isClusterJob SMALLINT NOT NULL)";
\r
94 // We create a table...
\r
95 System.out.println(create);
\r
101 void insertData(Set<JobStat> jobstatus) throws SQLException {
\r
102 System.out.println("Inserting " + jobstatus.size());
\r
104 conn.setAutoCommit(false);
\r
105 String insert = "insert into exec_stat (service_name, cluster_job_id, job_id, start, finish, "
\r
106 + "inputsize, resultsize, isCancelled, isCollected, isClusterJob) "
\r
107 + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
\r
108 PreparedStatement pstm = conn.prepareStatement(insert);
\r
109 for (JobStat js : jobstatus) {
\r
111 pstm.setString(1, js.webService.toString());
\r
113 if (!Util.isEmpty(js.clusterJobId)) {
\r
114 pstm.setString(2, js.clusterJobId);
\r
116 pstm.setString(2, null);
\r
119 pstm.setString(3, js.jobname);
\r
121 if (js.start != ExecutionStatCollector.UNDEFINED) {
\r
122 pstm.setTimestamp(4, new Timestamp(js.start));
\r
124 pstm.setTimestamp(4, null);
\r
126 if (js.finish != ExecutionStatCollector.UNDEFINED) {
\r
127 pstm.setTimestamp(5, new Timestamp(js.finish));
\r
129 pstm.setTimestamp(5, null);
\r
132 pstm.setLong(6, js.inputSize);
\r
134 pstm.setLong(7, js.resultSize);
\r
136 pstm.setBoolean(8, js.isCancelled);
\r
137 pstm.setBoolean(9, js.isCollected);
\r
138 pstm.setBoolean(10, js.isClusterJob());
\r
139 pstm.executeUpdate();
\r
145 public List<JobStat> readData(Timestamp from, Timestamp to,
\r
146 Services wservice, Boolean clusterOnly) throws SQLException {
\r
148 String query = "select service_name, cluster_job_id, job_id, start, finish, inputsize, "
\r
149 + "resultsize, isCancelled, isCollected from exec_stat where start BETWEEN ? and ? ";
\r
151 if (wservice != null) {
\r
152 query += " and service_name=? ";
\r
155 if (clusterOnly != null) {
\r
157 query += " and isClusterJob!=0 ";
\r
159 query += " and isClusterJob=0 ";
\r
163 PreparedStatement pstm = conn.prepareStatement(query);
\r
164 pstm.setTimestamp(1, from);
\r
165 pstm.setTimestamp(2, to);
\r
166 if (wservice != null) {
\r
167 pstm.setString(3, wservice.toString());
\r
170 List<JobStat> stats = new ArrayList<JobStat>();
\r
171 ResultSet rs = pstm.getResultSet();
\r
172 while (rs.next()) {
\r
173 stats.add(JobStat.newInstance(Services.getService(rs.getString(1)),
\r
174 rs.getString(2), rs.getString(3), rs.getTimestamp(4),
\r
175 rs.getTimestamp(5), rs.getLong(6), rs.getLong(7),
\r
176 rs.getBoolean(8), rs.getBoolean(9)));
\r
183 public void removeRecordedJobs(Set<JobStat> fsJobs) throws SQLException {
\r
185 String query = "select job_id from exec_stat";
\r
187 Statement st = conn.createStatement();
\r
188 ResultSet result = st.executeQuery(query);
\r
190 while (result.next()) {
\r
191 String recordedJob = result.getString(1);
\r
192 JobStat recStat = JobStat.newIncompleteStat(recordedJob);
\r
193 if (fsJobs.contains(recStat)) {
\r
194 fsJobs.remove(recStat);
\r
200 public void shutdownDBServer() {
\r
201 // ## DATABASE SHUTDOWN SECTION ##
\r
203 * In embedded mode, an application should shut down Derby. Shutdown
\r
204 * throws the XJ015 exception to confirm success.
\r
207 if (conn != null) {
\r
210 } catch (SQLException e) {
\r
211 System.err.println("Database commit failed with "
\r
212 + e.getLocalizedMessage());
\r
214 boolean gotSQLExc = false;
\r
216 DriverManager.getConnection("jdbc:derby:;shutdown=true");
\r
217 } catch (SQLException se) {
\r
218 if (se.getSQLState().equals("XJ015")) {
\r
223 System.err.println("Database did not shut down normally");
\r
225 System.out.println("Database shut down normally");
\r
228 public static void main(String[] args) throws SQLException {
\r
229 // new StatDB().createStatTable();
\r
230 // insertData(null);
\r
232 * StatDB statdb = new StatDB(); Date from = new Date();
\r
233 * from.setMonth(1); System.out.println(new
\r
234 * StatProcessor(statdb.readData( new Timestamp(from.getTime()), new
\r
235 * Timestamp(new Date().getTime()), null, null)).reportStat());
\r