Attempt to resolve the problem with java on cluster...
[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.Total;
18 import compbio.cassandra.TotalJobsStatisticBean;
19 import compbio.statistic.CassandraRequester;
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 (null
65          *            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                 CassandraRequester cr = new CassandraRequester();
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 = cr.countJobs(date1, date2);
104                 model.put("result", res);
105                 Map<String, Total> results = res.getDateTotal();
106                 String csvline = "\'Date\',\'Total\',\'OK\',\'Stopped\',\'Error\',\'Timeout\'%0A";
107                 for (Map.Entry<String, Total> entry : results.entrySet()) {
108                         csvline += "\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotalOK() + "\',\'"
109                                         + entry.getValue().getTotalStopped() + "\',\'" + entry.getValue().getTotalError() + "\',\'"
110                                         + entry.getValue().getTotalTimeOut() + "\'%0A";
111                 }
112                 model.put("csvfile", csvline);
113                 model.put("ndays", res.getDateTotal().size());
114                 final long endTime = System.currentTimeMillis();
115                 model.put("timeExecution", (endTime - startTime));
116                 model.put("option", option);
117                 return "/reportJobStatistics";
118         }
119
120         /**
121          * form a report page for job statistics for one day only.
122          * 
123          * @param model
124          *            MVC model object
125          * @param date
126          *            date for the report
127          * @param status
128          * 
129          * @return link to the report JSP page
130          */
131         @RequestMapping(value = "/stat/jobsoneday/results", method = RequestMethod.GET)
132         public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map<String, Object> model)
133                         throws ParseException {
134                 model.put("username", getPrincipalName());
135                 final long startTime = System.currentTimeMillis();
136
137                 String realdate;
138                 try {
139                         long thetime = formaterYYMMDD.parse(date).getTime();
140                         if (thetime < 0) {
141                                 realdate = date;
142                         } else {
143                                 realdate = formaterDDMMYY.format(new Date(thetime));
144                         }
145                 } catch (ParseException e) {
146                         realdate = date;
147                 }
148
149                 if (null == JobStatus.getJobStatus(status)) {
150                         return "support/Notimplemented";
151                 }
152
153                 CassandraRequester cr = new CassandraRequester();
154                 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
155                 DateBean r = cr.readJobByDay(realdate, JobStatus.getJobStatus(status));
156                 model.put("results", r);
157                 if (r != null)
158                         model.put("njobs", r.getJobidAndSeq().size());
159                 model.put("date", realdate);
160                 model.put("status", status);
161                 final long endTime = System.currentTimeMillis();
162                 model.put("timeExecution", (endTime - startTime));
163                 return "/reportJobStatisticsOneDay";
164         }
165 }