new readers for the queries
[proteocache.git] / server / compbio / controllers / DailyStatisticsController.java
1 package compbio.controllers;
2
3 import java.text.ParseException;
4 import java.util.Calendar;
5 import java.util.Date;
6 import java.util.Map;
7
8 import org.springframework.stereotype.Controller;
9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12
13 import compbio.engine.JobStatus;
14 import compbio.beans.DateBean;
15 import compbio.beans.TotalJobsStatisticBean;
16 import compbio.cassandra.DateFormatter;
17 import compbio.cassandra.readers.CassandraReader;
18 import compbio.cassandra.readers.DailyStatisticsReader;
19 import compbio.cassandra.readers.ProteoDAO;
20
21 /**
22  * @author Alexander Sherstnev
23  * @author Natasha Sherstneva
24  * @version 1.0 Dec 2013
25  */
26 @Controller
27 public class DailyStatisticsController extends BasicController {
28
29         /**
30          * form a query page for daily job statistics. The servlet should be
31          * available for users and admins only. By defaults the report time range is
32          * from the earliest day with jobs to today ("Query for all dates" is
33          * ticked). If the user removes the tick the time range is from today - 3
34          * days to today. Currently, the input model is empty.
35          * 
36          * @param model
37          *            MVC model
38          * @return link to the JSP query page
39          */
40         @RequestMapping(value = "/stat/jobs/query", method = RequestMethod.GET)
41         public String initFindForm(Map<String, Object> model) {
42                 model.put("username", getPrincipalName());
43                 Calendar loccal = Calendar.getInstance();
44                 String date2 = loccal.get(Calendar.YEAR) + "/" + (loccal.get(Calendar.MONTH) + 1) + "/" + loccal.get(Calendar.DATE);
45                 loccal.add(Calendar.DATE, -3);
46                 String date1 = loccal.get(Calendar.YEAR) + "/" + (loccal.get(Calendar.MONTH) + 1) + "/" + loccal.get(Calendar.DATE);
47                 model.put("date1", date1);
48                 model.put("date2", date2);
49                 return "query/JobStatistics";
50         }
51
52         /**
53          * form a report page for daily job statistics.
54          * 
55          * @param model
56          *            MVC model object
57          * @param date1
58          *            initial date for the report (if option is set, date2 = the
59          *            earliest date with jobs in DB)
60          * @param date2
61          *            the final date for the report (if option is set, date2 =
62          *            today)
63          * @param option
64          *            defined whether the whole time range of jobs is reported
65          *            (null means date1 and date2 are used)
66          * @return link to the report JSP page
67          */
68         @RequestMapping(value = "/stat/jobsdaily/results", method = RequestMethod.GET)
69         public String findJobsInPeriod(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
70                         @RequestParam("option") String option, Map<String, Object> model) {
71                 model.put("username", getPrincipalName());
72                 Calendar loccal = Calendar.getInstance();
73                 final long startTime = System.currentTimeMillis();
74                 DailyStatisticsReader reader = new DailyStatisticsReader();
75                 if (option.equals("AllDates,off")) {
76                         date1 = theEaerlistDate;
77                         date2 = theCurrentDate;
78                 }
79
80                 // dates in string format
81                 String trimmeddate1 = date1.replaceAll("\\s", "");
82                 String trimmeddate2 = date2.replaceAll("\\s", "");
83                 // dates in long format
84                 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
85                 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
86                 String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
87                 if (error != null) {
88                         model.put("error", error);
89                         model.put("date1", date1);
90                         model.put("date2", date2);
91                         return "query/JobStatistics";
92                 }
93
94                 if (longDate1 < CassandraReader.earliestDate())
95                         longDate1 = CassandraReader.earliestDate();
96                 if (longDate2 > loccal.getTimeInMillis())
97                         longDate2 = loccal.getTimeInMillis();
98
99                 date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
100                 date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
101                 model.put("date1", date1);
102                 model.put("date2", date2);
103                 TotalJobsStatisticBean res = reader.query(longDate1, longDate2);
104                 model.put("result", res);
105                 model.put("ndays", res.getDateTotal().size());
106                 final long endTime = System.currentTimeMillis();
107                 model.put("timeExecution", (endTime - startTime));
108                 model.put("option", option);
109                 return "/reportJobStatistics";
110         }
111
112         /**
113          * form a report page for job statistics for one day only.
114          * 
115          * @param model
116          *            MVC model object
117          * @param date
118          *            date for the report
119          * @param status
120          * 
121          * @return link to the report JSP page
122          */
123         @RequestMapping(value = "/stat/jobsoneday/results", method = RequestMethod.GET)
124         public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map<String, Object> model)
125                         throws ParseException {
126                 model.put("username", getPrincipalName());
127                 final long startTime = System.currentTimeMillis();
128
129                 String realdate;
130                 long thetime = 0;
131                 try {
132                         thetime = formaterYYMMDD.parse(date).getTime();
133                         if (thetime < 0) {
134                                 realdate = date;
135                         } else {
136                                 realdate = formaterDDMMYY.format(new Date(thetime));
137                         }
138                 } catch (ParseException e) {
139                         realdate = date;
140                         thetime = formaterDDMMYY.parse(realdate).getTime();
141                 }
142                 
143                 if (null == JobStatus.getJobStatus(status)) {
144                         return "support/Notimplemented";
145                 }
146
147                 DailyStatisticsReader reader = new DailyStatisticsReader();
148                 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
149                 DateBean r = reader.readJobByDay(thetime, realdate, JobStatus.getJobStatus(status));
150                 model.put("results", r);
151                 if (r != null)
152                         model.put("njobs", r.getJobidAndSeq().size());
153                 model.put("date", realdate);
154                 model.put("status", status);
155                 final long endTime = System.currentTimeMillis();
156                 model.put("timeExecution", (endTime - startTime));
157                 return "/reportJobStatisticsOneDay";
158         }
159 }