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 * @author Alexander Sherstnev
24 * @author Natasha Sherstneva
25 * @version 1.0 Dec 2013
28 public class DailyStatisticsController extends BasicController {
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.
39 * @return link to the JSP query page
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";
54 * form a report page for daily job statistics.
59 * initial date for the report (if option is set, date2 = the
60 * earliest date with jobs in DB)
62 * the final date for the report (if option is set, date2 =
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
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;
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);
89 model.put("error", error);
90 model.put("date1", date1);
91 model.put("date2", date2);
92 return "query/JobStatistics";
95 if (longDate1 < CassandraReader.earliestDate())
96 longDate1 = CassandraReader.earliestDate();
97 if (longDate2 > loccal.getTimeInMillis())
98 longDate2 = loccal.getTimeInMillis();
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";
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";
122 * form a report page for job statistics for one day only.
127 * date for the report
130 * @return link to the report JSP page
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();
141 thetime = formaterYYMMDD.parse(date).getTime();
145 realdate = formaterDDMMYY.format(new Date(thetime));
147 } catch (ParseException e) {
149 thetime = formaterDDMMYY.parse(realdate).getTime();
152 if (null == JobStatus.getJobStatus(status)) {
153 return "support/Notimplemented";
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);
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";