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.statistic.CassandraRequester;
18 import compbio.beans.DateBean;
19 import compbio.beans.ExecutionTimeBean;
20 import compbio.beans.JobBean;
21 import compbio.beans.TotalExecutionTime;
22 import compbio.cassandra.DateFormatter;
23 import compbio.cassandra.readers.CassandraReader;
24 import compbio.cassandra.readers.ExecutionTimeReader;
25 import compbio.cassandra.readers.JobReader;
26 import compbio.engine.ExecutionInterval;
27 import compbio.engine.archive.ArchivedJob;
30 * Spring controller for supporting job queries. This version works in the
33 * @author Alexander Sherstnev
34 * @author Natasha Sherstneva
39 public class JobController extends BasicController {
40 private static Logger log = Logger.getLogger(JobController.class);
43 * form a query page for job execution time statistics. The servlet should
44 * be available for users and admins only. By defaults the report time range
45 * is from the earliest day with jobs to today ("Query for all dates" is
46 * ticked). If the user removes the tick the time range is from today - 3
47 * days to today. Currently, the input model is empty.
51 * @return link to the JSP query page
53 @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
54 public String initFormExecTime(Map<String, Object> model) {
55 model.put("username", getPrincipalName());
56 Calendar cal = Calendar.getInstance();
57 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
58 cal.add(Calendar.DATE, -3);
59 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
61 model.put("date1", date1);
62 model.put("date2", date2);
63 return "query/JobTimeExecution";
67 * form a query page for a job. The servlet should no be visible to users at
72 * @return link to the JSP query page
74 @RequestMapping(value = "/job/query", method = RequestMethod.GET)
75 public String initFindForm(Map<String, Object> model) {
76 model.put("username", getPrincipalName());
77 CassandraRequester cr = new CassandraRequester();
78 model.put("value", cr.getExample("jobid"));
79 return "query/JobLog";
83 * form a report page for a job execution time statistics.
86 * initial date for requested statistics
88 * final date for requested statistics
90 * All available jobs are analyzed if alldates="AllDates,off"
93 * @return link to the JSP query page
95 @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
96 public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
97 @RequestParam(value = "option", required = false) String alldates, Map<String, Object> model) {
98 model.put("username", getPrincipalName());
99 final long startTime = System.currentTimeMillis();
100 Calendar loccal = Calendar.getInstance();
101 ExecutionTimeReader reader = new ExecutionTimeReader();
102 if (alldates.equals("AllDates,off")) {
103 date1 = getEarliestDate();
104 date2 = getCurrentDate();
107 // dates in string format
108 String trimmeddate1 = date1.replaceAll("\\s", "");
109 String trimmeddate2 = date2.replaceAll("\\s", "");
110 // dates in long format
111 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
112 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
113 String error = checkDates(trimmeddate1, trimmeddate2, longDate1, longDate2);
115 model.put("error", error);
116 model.put("date1", date1);
117 model.put("date2", date2);
118 return "query/JobTimeExecution";
121 if (longDate1 < CassandraReader.earliestDate())
122 longDate1 = CassandraReader.earliestDate();
123 if (longDate2 > loccal.getTimeInMillis())
124 longDate2 = loccal.getTimeInMillis();
126 date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
127 date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
128 model.put("date1", date1);
129 model.put("date2", date2);
130 model.put("option", alldates);
131 ExecutionTimeBean res = reader.query(longDate1, longDate2);
133 model.put("result", res);
134 Map<String, TotalExecutionTime> results = res.getDateTotal();
135 StringBuilder csvline = new StringBuilder("");
136 if (0 < res.getDateTotal().size()) {
137 csvline.append("\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A");
138 for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
139 csvline.append("\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s()
140 + "\',\'" + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
141 + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A");
144 model.put("csvfile", csvline.toString());
145 model.put("ndays", res.getDateTotal().size() - 1);
146 final long endTime = System.currentTimeMillis();
147 model.put("timeExecution", (endTime - startTime));
148 return "/reports/TimeExecution";
152 * form a report page for job statistics for one day only.
157 * date for the report
160 * @return link to the report JSP page
162 @RequestMapping(value = "/stat/jobsoneday/executionTime", method = RequestMethod.GET)
163 public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("interval") String interval, Map<String, Object> model)
164 throws ParseException {
165 model.put("username", getPrincipalName());
166 final long startTime = System.currentTimeMillis();
171 thetime = formaterYYMMDD.parse(date).getTime();
175 realdate = formaterDDMMYY.format(new Date(thetime));
177 } catch (ParseException e) {
179 thetime = formaterDDMMYY.parse(realdate).getTime();
182 if (null == ExecutionInterval.getExecutionInterval(interval))
183 return "support/Notimplemented";
185 ExecutionTimeReader reader = new ExecutionTimeReader();
186 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
187 DateBean r = reader.readJobByDay(thetime, realdate, ExecutionInterval.getBoundsInterval(interval));
188 model.put("results", r);
190 model.put("njobs", r.getJobidAndSeq().size());
191 model.put("date", realdate);
192 model.put("status", interval);
193 final long endTime = System.currentTimeMillis();
194 model.put("timeExecution", (endTime - startTime));
195 return "reports/JobStatisticsOneDay";
199 * form result page for one job with a given job ID.
206 * @return link to the JSP query page
207 * @throws IOException
209 @RequestMapping(value = "/job/results", method = RequestMethod.GET)
210 public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
211 model.put("username", getPrincipalName());
212 final long startTime = System.currentTimeMillis();
213 JobReader reader = new JobReader();
214 JobBean job = reader.readJobLog(jobid);
216 model.put("jobnotfound", "yes");
218 model.put("result", job);
220 final long endTime = System.currentTimeMillis();
221 model.put("timeExecution", (endTime - startTime));
222 model.put("IdJob", jobid);
224 // prepare archive file for the job for downloading
225 ArchivedJob aj = new ArchivedJob(jobid);
227 model.put("jobarchive", aj.prepareJobArchiveToWeb());
228 } catch (IOException e) {
229 log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
230 log.error(e.getLocalizedMessage(), e.getCause());
233 // add a direct link to the job
234 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
235 URL remotelinkurl = new URL(remotelink);
236 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
237 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
238 model.put("jobremotelink", remotelink);
240 return "reports/Job";