fixed Execution time report
[proteocache.git] / datadb / compbio / cassandra / readers / ExecutionTimeReader.java
1 package compbio.cassandra.readers;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.List;
9
10 import com.datastax.driver.core.ResultSet;
11 import com.datastax.driver.core.Row;
12
13 import compbio.beans.ExecutionTimeBean;
14 import compbio.beans.Total;
15 import compbio.beans.TotalExecutionTime;
16 import compbio.beans.TotalJobsStatisticBean;
17 import compbio.cassandra.DataBase;
18 import compbio.cassandra.DateFormatter;
19 import compbio.cassandra.Pair;
20
21 public class ExecutionTimeReader extends CassandraReader {
22
23                 public ExecutionTimeReader() {
24                         super();
25                 }
26
27                 /**
28                  * query: execution time statistics for the period from date1 till date2
29                  * 
30                  * @param dateStart
31                  *            the first date in the period
32                  * @param dateEnd
33                  *            the last date in the period
34                  * 
35                  * @return TotalJobsStatisticBean to the controller
36                  *         JobController
37                  **/
38                 public ExecutionTimeBean query(long dateStart, long dateEnd) {
39                         Calendar start = Calendar.getInstance();
40                         start.setTime(new Date(dateStart));
41                         Calendar end = Calendar.getInstance();
42                         end.setTime(new Date(dateEnd));
43                         ExecutionTimeBean query = new ExecutionTimeBean();
44                         TotalExecutionTime wholeTotal = new TotalExecutionTime(0, 0, 0, 0, 0, 0);
45                                                 
46                         for (Date date = end.getTime(); !end.before(start); end.add(Calendar.DATE, -1), date = end.getTime()) {
47                                 ResultSet results = CassandraQuery("SELECT ExecTime FROM ProteinData WHERE jobtime = " + date.getTime() + ";");
48                                 if (results == null)
49                                         return null;
50                                 if (results.isExhausted())
51                                         continue;
52                                 List<Row> rows = results.all();
53                                 TotalExecutionTime currentTotal = new TotalExecutionTime(0, 0, 0, 0, 0, 0);
54                                 for (Row r : rows) {    
55                                                 long timeExec = r.getInt("ExecTime")/1000;
56                                                 if (timeExec <= 30)
57                                                         currentTotal.setTotal0_30s(currentTotal.getTotal0_30s() + 1);
58                                                 else if (timeExec > 30 && timeExec <= 60)
59                                                         currentTotal.setTotal30_60s(currentTotal.getTotal30_60s() + 1);
60                                                 else if (timeExec > 60 && timeExec <= 120)
61                                                         currentTotal.setTotal1_2m(currentTotal.getTotal1_2m() + 1);
62                                                 else if (timeExec > 120 && timeExec <= 600)
63                                                         currentTotal.setTotal2_10m(currentTotal.getTotal2_10m() + 1);
64                                                 else 
65                                                         currentTotal.setTotal10m(currentTotal.getTotal10m() + 1);
66                                                 
67                                 }
68                                 currentTotal.setTotal(currentTotal.getTotal0_30s() + currentTotal.getTotal30_60s() + currentTotal.getTotal1_2m() + 
69                                                 currentTotal.getTotal2_10m() + currentTotal.getTotal10m());
70                                 query.setDateTotal(DateFormatter.DateLongToString(date.getTime(), DateFormatter.getFormatDDMMYY()), currentTotal);
71                                 wholeTotal.setTotal(currentTotal.getTotal() + wholeTotal.getTotal());
72                                 wholeTotal.setTotal0_30s(currentTotal.getTotal0_30s() + wholeTotal.getTotal0_30s());
73                                 wholeTotal.setTotal30_60s(currentTotal.getTotal30_60s() + wholeTotal.getTotal30_60s());
74                                 wholeTotal.setTotal1_2m(currentTotal.getTotal1_2m() + wholeTotal.getTotal1_2m());
75                                 wholeTotal.setTotal2_10m(currentTotal.getTotal2_10m() + wholeTotal.getTotal2_10m());
76                                 wholeTotal.setTotal10m(currentTotal.getTotal10m() + wholeTotal.getTotal10m());
77                         }
78                         query.setWholeTotal(wholeTotal);
79                         return query;                                                                                                                                   
80                         
81                 }
82 }