new readers for the queries
[proteocache.git] / datadb / compbio / cassandra / readers / DailyStatisticsReader.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.Total;
12 import compbio.beans.TotalJobsStatisticBean;
13 import compbio.cassandra.DateFormatter;
14 import compbio.engine.JobStatus;
15
16 public class DailyStatisticsReader extends CassandraReader {
17
18         public DailyStatisticsReader() {
19                 super();
20         }
21         
22         /**
23          * query: total number of jobs 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 DailyStatisticsController
31          **/
32         public TotalJobsStatisticBean query(long dateStart, long dateEnd) {
33                 Calendar start = Calendar.getInstance();
34                 start.setTime(new Date(dateStart));
35                 Calendar end = Calendar.getInstance();
36                 end.setTime(new Date(dateEnd));
37                 TotalJobsStatisticBean query = new TotalJobsStatisticBean();
38                 Total wholeTotal = new Total(0, 0, 0, 0, 0);
39                 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
40                         ResultSet results = CassandraQuery("SELECT * FROM JobDateInfo WHERE jobday = " + date.getTime() + ";");
41                         if (results == null)
42                                 return null;
43                         if (results.isExhausted())
44                                 continue;
45                         Row therow = results.one();
46                         Total res = new Total(therow.getLong("Total"), therow.getLong("TotalOK"), therow.getLong("TotalStopped"),
47                                         therow.getLong("TotalError"), therow.getLong("TotalTimeOut"));
48                         if (!results.isExhausted()) {
49                                 Date dat = new Date(date.getTime());
50                                 log.warn("CassandraReader.ReadDateTable: date row for " + dat.toString() + " (" + date.getTime() + ") duplicated ");
51                         }
52                         query.setDateTotal(DateFormatter.DateLongToString(date.getTime(), DateFormatter.getFormatDDMMYY()), res);
53                         wholeTotal.setTotal(res.getTotal() + wholeTotal.getTotal());
54                         wholeTotal.setTotalOK(res.getTotalOK() + wholeTotal.getTotalOK());
55                         wholeTotal.setTotalStopped(res.getTotalStopped() + wholeTotal.getTotalStopped());
56                         wholeTotal.setTotalError(res.getTotalError() + wholeTotal.getTotalError());
57                         wholeTotal.setTotalTimeOut(res.getTotalTimeOut() + wholeTotal.getTotalTimeOut());
58                 }
59                 query.setWholeTotal(wholeTotal);
60                 return query;
61         }
62         
63         /**
64          * query: jobs and sequence at a date
65          * 
66          * @param day
67          *           the date in long format
68          * @param date
69          *           the date in String format
70          * 
71          * @param status
72          *           final job status
73          * @return DateBean to the controller DailyStatisticsController
74          **/
75         public DateBean readJobByDay(long day, String date, JobStatus status) { 
76                 DateBean res = new DateBean(date);
77                 if (status == JobStatus.OK) {
78                         ResultSet results = CassandraQuery("SELECT JobID, Protein FROM ProteinData WHERE jobtime = " + day + ";");
79                         if (results == null || results.isExhausted())
80                                 return null;
81                         List<Row> rows = results.all();
82                         for (Row r : rows) {
83                                 res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
84                         }
85                 }
86                 else {
87                         ResultSet results = CassandraQuery("SELECT JobID FROM FailLog WHERE jobtime = " + day + " and FinalStatus = '" + status.name() + "';");
88                         if (results == null || results.isExhausted())
89                                 return null;
90                         List<Row> rows = results.all();
91                         for (Row r : rows) {
92                                 String jobid = r.getString("JobID");
93                                 ResultSet results2 = CassandraQuery("SELECT Protein FROM ProteinLog WHERE JobID = '" + jobid + "';");
94                                 if (results2 == null || results2.isExhausted())
95                                         return null;
96                                 List<Row> jrows = results2.all();
97                                 if (1 == jrows.size()) {
98                                         String protein = jrows.get(0).getString("Protein");
99                                         res.setJobidAndSeq(jobid, protein);
100                                 }
101                         }
102                 }
103                 return res;
104         }
105
106 }