1 package compbio.controllers;
3 import java.text.ParseException;
4 import java.util.Calendar;
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;
13 import compbio.engine.JobStatus;
14 import compbio.beans.DateBean;
15 import compbio.beans.TotalJobsStatisticBean;
16 import compbio.cassandra.DateFormatter;
18 import compbio.beans.Total;
19 import compbio.cassandra.readers.CassandraReader;
20 import compbio.cassandra.readers.DailyStatisticsReader;
23 * MVC controller for collecting and showing job statistics
25 * @author Alexander Sherstnev
26 * @author Natasha Sherstneva
32 public class DailyStatisticsController extends BasicController {
35 * form a query page for daily job statistics.
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
45 * @return link to the JSP query page
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";
60 * form a report page for daily job statistics.
65 * initial date for the report (if option is set, date2 = the
66 * earliest date with jobs in DB)
68 * the final date for the report (if option is set, date2 =
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
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();
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);
95 model.put("error", error);
96 model.put("date1", date1);
97 model.put("date2", date2);
98 return "query/JobStatistics";
101 if (longDate1 < CassandraReader.earliestDate())
102 longDate1 = CassandraReader.earliestDate();
103 if (longDate2 > loccal.getTimeInMillis())
104 longDate2 = loccal.getTimeInMillis();
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";
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";
128 * form a report page for job statistics for one day only.
133 * date for the report
136 * @return link to the report JSP page
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();
147 thetime = formaterYYMMDD.parse(date).getTime();
151 realdate = formaterDDMMYY.format(new Date(thetime));
153 } catch (ParseException e) {
155 thetime = formaterDDMMYY.parse(realdate).getTime();
158 if (null == JobStatus.getJobStatus(status))
159 return "support/Notimplemented";
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);
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";