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.
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
41 * @return link to the JSP query page
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";
56 * form a report page for daily job statistics.
61 * initial date for the report (if option is set, date2 = the
62 * earliest date with jobs in DB)
64 * the final date for the report (if option is set, date2 =
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
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;
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);
91 model.put("error", error);
92 model.put("date1", date1);
93 model.put("date2", date2);
94 return "query/JobStatistics";
97 if (longDate1 < CassandraReader.earliestDate())
98 longDate1 = CassandraReader.earliestDate();
99 if (longDate2 > loccal.getTimeInMillis())
100 longDate2 = loccal.getTimeInMillis();
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";
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";
124 * form a report page for job statistics for one day only.
129 * date for the report
132 * @return link to the report JSP page
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();
143 thetime = formaterYYMMDD.parse(date).getTime();
147 realdate = formaterDDMMYY.format(new Date(thetime));
149 } catch (ParseException e) {
151 thetime = formaterDDMMYY.parse(realdate).getTime();
154 if (null == JobStatus.getJobStatus(status)) {
155 return "support/Notimplemented";
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);
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";