1 package compbio.controllers;
3 import java.io.IOException;
4 import java.net.HttpURLConnection;
6 import java.text.ParseException;
7 import java.util.Calendar;
11 import org.apache.log4j.Logger;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RequestMethod;
15 import org.springframework.web.bind.annotation.RequestParam;
17 import compbio.beans.DateBean;
18 import compbio.beans.ExecutionTimeBean;
19 import compbio.beans.JobBean;
20 import compbio.beans.TotalExecutionTime;
21 import compbio.cassandra.DateFormatter;
22 import compbio.cassandra.readers.CassandraReader;
23 import compbio.cassandra.readers.ExecutionTimeReader;
24 import compbio.cassandra.readers.JobReader;
25 import compbio.engine.ExecutionInterval;
26 import compbio.engine.archive.ArchivedJob;
29 * Spring controller for supporting job queries. This version works in the
32 * @author Alexander Sherstnev
33 * @author Natasha Sherstneva
38 public class JobController extends BasicController {
39 private static Logger log = Logger.getLogger(JobController.class);
42 * form a query page for job execution time statistics. The servlet should
43 * be available for users and admins only. By defaults the report time range
44 * is from the earliest day with jobs to today ("Query for all dates" is
45 * ticked). If the user removes the tick the time range is from today - 3
46 * days to today. Currently, the input model is empty.
50 * @return link to the JSP query page
52 @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
53 public String initFormExecTime(Map<String, Object> model) {
54 model.put("username", getPrincipalName());
55 Calendar cal = Calendar.getInstance();
56 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
57 cal.add(Calendar.DATE, -3);
58 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
60 model.put("date1", date1);
61 model.put("date2", date2);
62 return "query/JobTimeExecution";
66 * form a query page for a job. The servlet should no be visible to users at
71 * @return link to the JSP query page
73 @RequestMapping(value = "/job/query", method = RequestMethod.GET)
74 public String initFindForm(Map<String, Object> model) {
75 model.put("username", getPrincipalName());
76 CassandraReader cr = new CassandraReader();
77 model.put("value", cr.getExample("jobid"));
78 return "query/JobLog";
82 * form a report page for a job execution time statistics.
85 * initial date for requested statistics
87 * final date for requested statistics
89 * All available jobs are analyzed if alldates="AllDates,off"
92 * @return link to the JSP query page
94 @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
95 public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
96 @RequestParam(value = "option", required = false) String alldates, Map<String, Object> model) {
97 model.put("username", getPrincipalName());
98 final long startTime = System.currentTimeMillis();
99 Calendar loccal = Calendar.getInstance();
100 ExecutionTimeReader reader = new ExecutionTimeReader();
101 if (alldates.equals("AllDates,off")) {
102 date1 = getEarliestDate();
103 date2 = getCurrentDate();
106 // dates in string format
107 String trimmeddate1 = date1.replaceAll("\\s", "");
108 String trimmeddate2 = date2.replaceAll("\\s", "");
109 // dates in long format
110 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
111 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
112 String error = checkDates(trimmeddate1, trimmeddate2, longDate1, longDate2);
114 model.put("error", error);
115 model.put("date1", date1);
116 model.put("date2", date2);
117 return "query/JobTimeExecution";
120 if (longDate1 < CassandraReader.earliestDate())
121 longDate1 = CassandraReader.earliestDate();
122 if (longDate2 > loccal.getTimeInMillis())
123 longDate2 = loccal.getTimeInMillis();
125 date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
126 date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
127 model.put("date1", date1);
128 model.put("date2", date2);
129 model.put("option", alldates);
130 ExecutionTimeBean res = reader.query(longDate1, longDate2);
132 model.put("result", res);
133 Map<String, TotalExecutionTime> results = res.getDateTotal();
134 StringBuilder csvline = new StringBuilder("");
135 if (0 < res.getDateTotal().size()) {
136 csvline.append("\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A");
137 for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
138 csvline.append("\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s()
139 + "\',\'" + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
140 + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A");
143 model.put("csvfile", csvline.toString());
144 model.put("ndays", res.getDateTotal().size() - 1);
145 final long endTime = System.currentTimeMillis();
146 model.put("timeExecution", (endTime - startTime));
147 return "/reports/TimeExecution";
151 * form a report page for job statistics for one day only.
156 * date for the report
159 * @return link to the report JSP page
161 @RequestMapping(value = "/stat/jobsoneday/executionTime", method = RequestMethod.GET)
162 public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("interval") String interval, Map<String, Object> model)
163 throws ParseException {
164 model.put("username", getPrincipalName());
165 final long startTime = System.currentTimeMillis();
170 thetime = formaterYYMMDD.parse(date).getTime();
174 realdate = formaterDDMMYY.format(new Date(thetime));
176 } catch (ParseException e) {
178 thetime = formaterDDMMYY.parse(realdate).getTime();
181 if (null == ExecutionInterval.getExecutionInterval(interval))
182 return "support/Notimplemented";
184 ExecutionTimeReader reader = new ExecutionTimeReader();
185 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
186 DateBean r = reader.readJobByDay(thetime, realdate, ExecutionInterval.getBoundsInterval(interval));
187 model.put("results", r);
189 model.put("njobs", r.getJobidAndSeq().size());
190 model.put("date", realdate);
191 model.put("status", interval);
192 final long endTime = System.currentTimeMillis();
193 model.put("timeExecution", (endTime - startTime));
194 return "reports/JobStatisticsOneDay";
198 * form result page for one job with a given job ID.
205 * @return link to the JSP query page
206 * @throws IOException
208 @RequestMapping(value = "/job/results", method = RequestMethod.GET)
209 public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
210 model.put("username", getPrincipalName());
211 final long startTime = System.currentTimeMillis();
212 JobReader reader = new JobReader();
213 JobBean job = reader.readJobLog(jobid);
215 model.put("jobnotfound", "yes");
217 model.put("result", job);
219 final long endTime = System.currentTimeMillis();
220 model.put("timeExecution", (endTime - startTime));
221 model.put("IdJob", jobid);
223 // prepare archive file for the job for downloading
224 ArchivedJob aj = new ArchivedJob(jobid);
226 model.put("jobarchive", aj.prepareJobArchiveToWeb());
227 } catch (IOException e) {
228 log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
229 log.error(e.getLocalizedMessage(), e.getCause());
232 // add a direct link to the job
233 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
234 URL remotelinkurl = new URL(remotelink);
235 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
236 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
237 model.put("jobremotelink", remotelink);
239 return "reports/Job";