Merge branch 'master' into PROT-9-webservice
[proteocache.git] / datadb / compbio / cassandra / readers / ExecutionTimeReader.java
diff --git a/datadb/compbio/cassandra/readers/ExecutionTimeReader.java b/datadb/compbio/cassandra/readers/ExecutionTimeReader.java
new file mode 100644 (file)
index 0000000..d019c35
--- /dev/null
@@ -0,0 +1,103 @@
+package compbio.cassandra.readers;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+
+import compbio.beans.DateBean;
+import compbio.beans.ExecutionTimeBean;
+import compbio.beans.TotalExecutionTime;
+import compbio.cassandra.DateFormatter;
+import compbio.engine.Pair;
+
+public class ExecutionTimeReader extends CassandraReader {
+
+               public ExecutionTimeReader() {
+                       super();
+               }
+
+               /**
+                * query: execution time statistics for the period from date1 till date2
+                * 
+                * @param dateStart
+                *            the first date in the period
+                * @param dateEnd
+                *            the last date in the period
+                * 
+                * @return TotalJobsStatisticBean to the controller
+                *         JobController
+                **/
+               public ExecutionTimeBean query(long dateStart, long dateEnd) {
+                       Calendar start = Calendar.getInstance();
+                       start.setTime(new Date(dateStart));
+                       Calendar end = Calendar.getInstance();
+                       end.setTime(new Date(dateEnd));
+                       ExecutionTimeBean query = new ExecutionTimeBean();
+                       TotalExecutionTime wholeTotal = new TotalExecutionTime(0, 0, 0, 0, 0, 0);
+                                               
+                       for (Date date = end.getTime(); !end.before(start); end.add(Calendar.DATE, -1), date = end.getTime()) {
+                               ResultSet results = CassandraQuery("SELECT ExecTime FROM ProteinData WHERE jobtime = " + date.getTime() + ";");
+                               if (results == null)
+                                       return null;
+                               if (results.isExhausted())
+                                       continue;
+                               List<Row> rows = results.all();
+                               TotalExecutionTime currentTotal = new TotalExecutionTime(0, 0, 0, 0, 0, 0);
+                               for (Row r : rows) {    
+                                               long timeExec = r.getInt("ExecTime")/1000;
+                                               if (timeExec <= 30)
+                                                       currentTotal.setTotal0_30s(currentTotal.getTotal0_30s() + 1);
+                                               else if (timeExec > 30 && timeExec <= 60)
+                                                       currentTotal.setTotal30_60s(currentTotal.getTotal30_60s() + 1);
+                                               else if (timeExec > 60 && timeExec <= 120)
+                                                       currentTotal.setTotal1_2m(currentTotal.getTotal1_2m() + 1);
+                                               else if (timeExec > 120 && timeExec <= 600)
+                                                       currentTotal.setTotal2_10m(currentTotal.getTotal2_10m() + 1);
+                                               else 
+                                                       currentTotal.setTotal10m(currentTotal.getTotal10m() + 1);
+                                               
+                               }
+                               currentTotal.setTotal(currentTotal.getTotal0_30s() + currentTotal.getTotal30_60s() + currentTotal.getTotal1_2m() + 
+                                               currentTotal.getTotal2_10m() + currentTotal.getTotal10m());
+                               query.setDateTotal(DateFormatter.DateLongToString(date.getTime(), DateFormatter.getFormatDDMMYY()), currentTotal);
+                               wholeTotal.setTotal(currentTotal.getTotal() + wholeTotal.getTotal());
+                               wholeTotal.setTotal0_30s(currentTotal.getTotal0_30s() + wholeTotal.getTotal0_30s());
+                               wholeTotal.setTotal30_60s(currentTotal.getTotal30_60s() + wholeTotal.getTotal30_60s());
+                               wholeTotal.setTotal1_2m(currentTotal.getTotal1_2m() + wholeTotal.getTotal1_2m());
+                               wholeTotal.setTotal2_10m(currentTotal.getTotal2_10m() + wholeTotal.getTotal2_10m());
+                               wholeTotal.setTotal10m(currentTotal.getTotal10m() + wholeTotal.getTotal10m());
+                       }
+                       query.setWholeTotal(wholeTotal);
+                       return query;                                                                                                                                   
+               }
+                       
+                       /**
+                        * query: jobs and sequence at a date with current execution time interval
+                        * 
+                        * @param day
+                        *            the date in long format
+                        * @param date
+                        *            the date in String format
+                        * 
+                        * @param interval
+                        *           buttom and top bounds of the interval Pair<buttom, top>
+                        * @return DateBean to the controller JobController
+                        **/
+                       public DateBean readJobByDay(long day, String date, Pair<Integer,Integer> interval) {
+                               DateBean res = new DateBean(date);
+                               ResultSet results = CassandraQuery("SELECT * FROM ProteinData WHERE jobtime = " + day + ";");
+                               if (results == null || results.isExhausted())
+                                       return null;
+                               List<Row> rows = results.all();
+                               for (Row r : rows) {
+                                       if (interval.getElement0() <= r.getInt("ExecTime")/1000  && r.getInt("ExecTime")/1000 < interval.getElement1())
+                                                       res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
+                       
+                                       }                       
+                               return res;
+                       }       
+               
+}