1 package compbio.controllers;
3 import java.io.IOException;
4 import java.net.HttpURLConnection;
6 import java.text.ParseException;
7 import java.text.SimpleDateFormat;
8 import java.util.Calendar;
10 import java.util.List;
13 import org.apache.log4j.Logger;
14 import org.springframework.stereotype.Controller;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestMethod;
17 import org.springframework.web.bind.annotation.RequestParam;
19 import compbio.beans.DateBean;
20 import compbio.beans.ExecutionTimeBean;
21 import compbio.beans.Total;
22 import compbio.beans.TotalExecutionTime;
23 import compbio.beans.TotalJobsStatisticBean;
24 import compbio.cassandra.DataBase;
25 import compbio.cassandra.DateFormatter;
26 import compbio.cassandra.readers.CassandraReader;
27 import compbio.cassandra.readers.DailyStatisticsReader;
28 import compbio.cassandra.readers.ExecutionTimeReader;
29 import compbio.cassandra.readers.JobReader;
30 import compbio.engine.ExecutionInterval;
31 import compbio.engine.JobStatus;
32 import compbio.engine.archive.ArchivedJob;
35 * @author Alexander Sherstnev
36 * @author Natasha Sherstneva
37 * @version 1.0 Dec 2013
40 public class JobController extends BasicController {
41 private static Logger log = Logger.getLogger(JobController.class);
44 * form a query page for job execution time statistics. The servlet should
45 * be available for users and admins only. By defaults the report time range
46 * is from the earliest day with jobs to today ("Query for all dates" is
47 * ticked). If the user removes the tick the time range is from today - 3
48 * days to today. Currently, the input model is empty.
52 * @return link to the JSP query page
54 @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
55 public String initFormExecTime(Map<String, Object> model) {
56 model.put("username", getPrincipalName());
57 Calendar cal = Calendar.getInstance();
58 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
59 cal.add(Calendar.DATE, -3);
60 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
62 model.put("date1", date1);
63 model.put("date2", date2);
64 return "query/JobTimeExecution";
68 * form a query page for a job. The servlet should no be visible to users at
73 * @return link to the JSP query page
75 @RequestMapping(value = "/job/query", method = RequestMethod.GET)
76 public String initFindForm(Map<String, Object> model) {
77 model.put("username", getPrincipalName());
78 CassandraReader cr = new CassandraReader();
79 model.put("value", cr.getExample("jobid"));
80 return "query/JobLog";
84 * form a report page for a job execution time statistics.
88 * @return link to the JSP query page
90 @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
91 public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
92 @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
93 model.put("username", getPrincipalName());
94 final long startTime = System.currentTimeMillis();
95 Calendar loccal = Calendar.getInstance();
96 ExecutionTimeReader reader = new ExecutionTimeReader();
97 if (option.equals("AllDates,off")) {
98 date1 = theEaerlistDate;
99 date2 = theCurrentDate;
102 // dates in string format
103 String trimmeddate1 = date1.replaceAll("\\s", "");
104 String trimmeddate2 = date2.replaceAll("\\s", "");
105 // dates in long format
106 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
107 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
108 String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
110 model.put("error", error);
111 model.put("date1", date1);
112 model.put("date2", date2);
113 return "query/JobTimeExecution";
116 if (longDate1 < CassandraReader.earliestDate())
117 longDate1 = CassandraReader.earliestDate();
118 if (longDate2 > loccal.getTimeInMillis())
119 longDate2 = loccal.getTimeInMillis();
121 date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
122 date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
123 model.put("date1", date1);
124 model.put("date2", date2);
125 model.put("option", option);
126 ExecutionTimeBean res = reader.query(longDate1, longDate2);;
127 model.put("result", res);
128 Map<String, TotalExecutionTime> results = res.getDateTotal();
130 if (0 < res.getDateTotal().size()) {
131 csvline = "\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A";
132 for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
133 csvline += "\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s() + "\',\'"
134 + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
135 + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A";
138 model.put("csvfile", csvline);
139 model.put("ndays", res.getDateTotal().size() - 1);
140 final long endTime = System.currentTimeMillis();
141 model.put("timeExecution", (endTime - startTime));
142 return "/reports/TimeExecution";
146 * form a report page for job statistics for one day only.
151 * date for the report
154 * @return link to the report JSP page
156 @RequestMapping(value = "/stat/jobsoneday/executionTime", method = RequestMethod.GET)
157 public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("interval") String interval, Map<String, Object> model)
158 throws ParseException {
159 model.put("username", getPrincipalName());
160 final long startTime = System.currentTimeMillis();
165 thetime = formaterYYMMDD.parse(date).getTime();
169 realdate = formaterDDMMYY.format(new Date(thetime));
171 } catch (ParseException e) {
173 thetime = formaterDDMMYY.parse(realdate).getTime();
176 if (null == ExecutionInterval.getExecutionInterval(interval))
177 return "support/Notimplemented";
180 ExecutionTimeReader reader = new ExecutionTimeReader();
181 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
182 DateBean r = reader.readJobByDay(thetime, realdate, ExecutionInterval.getBoundsInterval(interval));
183 model.put("results", r);
185 model.put("njobs", r.getJobidAndSeq().size());
186 model.put("date", realdate);
187 model.put("status", interval);
188 final long endTime = System.currentTimeMillis();
189 model.put("timeExecution", (endTime - startTime));
190 return "reports/JobStatisticsOneDay";
194 * form a query page for a job. The servlet should no be visible to users at
199 * @return link to the JSP query page
200 * @throws IOException
202 @RequestMapping(value = "/job/results", method = RequestMethod.GET)
203 public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
204 model.put("username", getPrincipalName());
205 final long startTime = System.currentTimeMillis();
206 JobReader reader = new JobReader();
207 model.put("result", reader.readJobLog(jobid));
208 final long endTime = System.currentTimeMillis();
209 model.put("timeExecution", (endTime - startTime));
210 model.put("IdJob", jobid);
212 ArchivedJob aj = new ArchivedJob(jobid);
214 model.put("jobarchive", aj.prepareJobArchiveToWeb());
215 } catch (IOException e) {
216 log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
217 log.error(e.getLocalizedMessage(), e.getCause());
219 // add a direct link to the job
220 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
221 URL remotelinkurl = new URL(remotelink);
222 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
223 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
224 model.put("jobremotelink", remotelink);
226 return "reports/Job";
230 * convert date from the standard long representation (milliseconds from 1
231 * Jan 1970) to yyyy/mm/dd
234 * date in milliseconds from 1 Jan 1970
235 * @return date in the form of yyyy/mm/dd
237 private String DateFormatYYMMDD(long indate) {
238 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
239 String dateString = datformat.format(new Date(indate));