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