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