1 package compbio.controllers;
3 import java.io.IOException;
4 import java.net.HttpURLConnection;
6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
12 import org.apache.log4j.Logger;
13 import org.springframework.stereotype.Controller;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.RequestMethod;
16 import org.springframework.web.bind.annotation.RequestParam;
18 import compbio.statistic.CassandraRequester;
19 import compbio.cassandra.DataBase;
20 import compbio.cassandra.readers.JobReader;
21 import compbio.engine.archive.ArchivedJob;
24 * @author Alexander Sherstnev
25 * @author Natasha Sherstneva
26 * @version 1.0 Dec 2013
29 public class JobController extends BasicController {
30 private static Logger log = Logger.getLogger(JobController.class);
33 * form a query page for job execution time statistics. The servlet should
34 * be available for users and admins only. By defaults the report time range
35 * is from the earliest day with jobs to today ("Query for all dates" is
36 * ticked). If the user removes the tick the time range is from today - 3
37 * days to today. Currently, the input model is empty.
41 * @return link to the JSP query page
43 @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
44 public String initFormExecTime(Map<String, Object> model) {
45 model.put("username", getPrincipalName());
46 Calendar cal = Calendar.getInstance();
47 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
48 cal.add(Calendar.DATE, -3);
49 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
51 model.put("date1", date1);
52 model.put("date2", date2);
53 return "query/JobTimeExecution";
57 * form a query page for a job. The servlet should no be visible to users at
62 * @return link to the JSP query page
64 @RequestMapping(value = "/job/query", method = RequestMethod.GET)
65 public String initFindForm(Map<String, Object> model) {
66 model.put("username", getPrincipalName());
67 CassandraRequester cr = new CassandraRequester();
68 model.put("value", cr.getExample("jobid"));
69 return "query/JobLog";
73 * form a report page for a job execution time statistics.
77 * @return link to the JSP query page
79 @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
80 public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
81 @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
82 model.put("username", getPrincipalName());
83 final long startTime = System.currentTimeMillis();
85 CassandraRequester sp = new CassandraRequester();
86 if (option.equals("AllDates,off")) {
87 Calendar cal = Calendar.getInstance();
88 date1 = DateFormatYYMMDD(sp.earliestDate());
89 date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
91 model.put("date1", date1);
92 model.put("date2", date2);
93 model.put("option", option);
94 List<DataBase> res = sp.extractExecutionTime(date1, date2);
95 model.put("result", res);
98 csvline = "\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A";
99 for (DataBase entry : res) {
100 List<Integer> counts = entry.getTimeRez();
102 for (int i = 0; i < counts.size(); ++i) {
103 total += counts.get(i);
105 csvline += "\'" + entry.getDate() + "\',\'" + total;
106 for (int i = 0; i < counts.size(); ++i) {
107 csvline += "\',\'" + counts.get(i);
112 model.put("csvfile", csvline);
113 model.put("ndays", res.size() - 1);
114 final long endTime = System.currentTimeMillis();
115 model.put("timeExecution", (endTime - startTime));
116 return "/reports/TimeExecution";
120 * form a query page for a job. The servlet should no be visible to users at
125 * @return link to the JSP query page
126 * @throws IOException
128 @RequestMapping(value = "/job/results", method = RequestMethod.GET)
129 public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
130 model.put("username", getPrincipalName());
131 final long startTime = System.currentTimeMillis();
132 JobReader reader = new JobReader();
133 model.put("result", reader.readJobLog(jobid));
134 final long endTime = System.currentTimeMillis();
135 model.put("timeExecution", (endTime - startTime));
136 model.put("IdJob", jobid);
138 ArchivedJob aj = new ArchivedJob(jobid);
140 model.put("jobarchive", aj.prepareJobArchiveToWeb());
141 } catch (IOException e) {
142 log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
143 log.error(e.getLocalizedMessage(), e.getCause());
145 // add a direct link to the job
146 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
147 URL remotelinkurl = new URL(remotelink);
148 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
149 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
150 model.put("jobremotelink", remotelink);
152 return "reports/Job";
156 * convert date from the standard long representation (milliseconds from 1
157 * Jan 1970) to yyyy/mm/dd
160 * date in milliseconds from 1 Jan 1970
161 * @return date in the form of yyyy/mm/dd
163 private String DateFormatYYMMDD(long indate) {
164 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
165 String dateString = datformat.format(new Date(indate));