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