Improve stability of Cassandra API calls (commands are checked)
[proteocache.git] / datadb / compbio / cassandra / readers / ExecutionTimeReader.java
1 package compbio.cassandra.readers;
2
3 import java.util.Calendar;
4 import java.util.Date;
5 import java.util.List;
6
7 import com.datastax.driver.core.ResultSet;
8 import com.datastax.driver.core.Row;
9
10 import compbio.beans.DateBean;
11 import compbio.beans.ExecutionTimeBean;
12 import compbio.beans.TotalExecutionTime;
13 import compbio.cassandra.DateFormatter;
14 import compbio.engine.Pair;
15
16 public class ExecutionTimeReader extends CassandraReader {
17
18                 public ExecutionTimeReader() {
19                         super();
20                 }
21
22                 /**
23                  * query: execution time statistics for the period from date1 till date2
24                  * 
25                  * @param dateStart
26                  *            the first date in the period
27                  * @param dateEnd
28                  *            the last date in the period
29                  * 
30                  * @return TotalJobsStatisticBean to the controller
31                  *         JobController
32                  **/
33                 public ExecutionTimeBean query(long dateStart, long dateEnd) {
34                         Calendar start = Calendar.getInstance();
35                         start.setTime(new Date(dateStart));
36                         Calendar end = Calendar.getInstance();
37                         end.setTime(new Date(dateEnd));
38                         ExecutionTimeBean query = new ExecutionTimeBean();
39                         TotalExecutionTime wholeTotal = new TotalExecutionTime(0, 0, 0, 0, 0, 0);
40                                                 
41                         for (Date date = end.getTime(); !end.before(start); end.add(Calendar.DATE, -1), date = end.getTime()) {
42                                 ResultSet results = CassandraQuery("SELECT ExecTime FROM ProteinData WHERE jobtime = " + date.getTime() + ";");
43                                 if (results == null)
44                                         return null;
45                                 if (results.isExhausted())
46                                         continue;
47                                 List<Row> rows = results.all();
48                                 TotalExecutionTime currentTotal = new TotalExecutionTime(0, 0, 0, 0, 0, 0);
49                                 for (Row r : rows) {    
50                                                 long timeExec = r.getInt("ExecTime")/1000;
51                                                 if (timeExec <= 30)
52                                                         currentTotal.setTotal0_30s(currentTotal.getTotal0_30s() + 1);
53                                                 else if (timeExec > 30 && timeExec <= 60)
54                                                         currentTotal.setTotal30_60s(currentTotal.getTotal30_60s() + 1);
55                                                 else if (timeExec > 60 && timeExec <= 120)
56                                                         currentTotal.setTotal1_2m(currentTotal.getTotal1_2m() + 1);
57                                                 else if (timeExec > 120 && timeExec <= 600)
58                                                         currentTotal.setTotal2_10m(currentTotal.getTotal2_10m() + 1);
59                                                 else 
60                                                         currentTotal.setTotal10m(currentTotal.getTotal10m() + 1);
61                                                 
62                                 }
63                                 currentTotal.setTotal(currentTotal.getTotal0_30s() + currentTotal.getTotal30_60s() + currentTotal.getTotal1_2m() + 
64                                                 currentTotal.getTotal2_10m() + currentTotal.getTotal10m());
65                                 query.setDateTotal(DateFormatter.DateLongToString(date.getTime(), DateFormatter.getFormatDDMMYY()), currentTotal);
66                                 wholeTotal.setTotal(currentTotal.getTotal() + wholeTotal.getTotal());
67                                 wholeTotal.setTotal0_30s(currentTotal.getTotal0_30s() + wholeTotal.getTotal0_30s());
68                                 wholeTotal.setTotal30_60s(currentTotal.getTotal30_60s() + wholeTotal.getTotal30_60s());
69                                 wholeTotal.setTotal1_2m(currentTotal.getTotal1_2m() + wholeTotal.getTotal1_2m());
70                                 wholeTotal.setTotal2_10m(currentTotal.getTotal2_10m() + wholeTotal.getTotal2_10m());
71                                 wholeTotal.setTotal10m(currentTotal.getTotal10m() + wholeTotal.getTotal10m());
72                         }
73                         query.setWholeTotal(wholeTotal);
74                         return query;                                                                                                                                   
75                 }
76                         
77                         /**
78                          * query: jobs and sequence at a date with current execution time interval
79                          * 
80                          * @param day
81                          *            the date in long format
82                          * @param date
83                          *            the date in String format
84                          * 
85                          * @param interval
86                          *           buttom and top bounds of the interval Pair<buttom, top>
87                          * @return DateBean to the controller JobController
88                          **/
89                         public DateBean readJobByDay(long day, String date, Pair<Integer,Integer> interval) {
90                                 DateBean res = new DateBean(date);
91                                 ResultSet results = CassandraQuery("SELECT * FROM ProteinData WHERE jobtime = " + day + ";");
92                                 if (results == null || results.isExhausted())
93                                         return null;
94                                 List<Row> rows = results.all();
95                                 for (Row r : rows) {
96                                         if (interval.getElement0() <= r.getInt("ExecTime")/1000  && r.getInt("ExecTime")/1000 < interval.getElement1())
97                                                         res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
98                         
99                                         }                       
100                                 return res;
101                         }       
102                 
103 }