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