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