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