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.statistic.CassandraRequester;
20 import compbio.beans.DateBean;
21 import compbio.beans.ExecutionTimeBean;
22 import compbio.beans.Total;
23 import compbio.beans.TotalExecutionTime;
24 import compbio.beans.TotalJobsStatisticBean;
25 import compbio.cassandra.DataBase;
26 import compbio.cassandra.DateFormatter;
27 import compbio.cassandra.readers.CassandraReader;
28 import compbio.cassandra.readers.DailyStatisticsReader;
29 import compbio.cassandra.readers.ExecutionTimeReader;
30 import compbio.cassandra.readers.JobReader;
31 import compbio.engine.ExecutionInterval;
32 import compbio.engine.JobStatus;
33 import compbio.engine.archive.ArchivedJob;
36 * @author Alexander Sherstnev
37 * @author Natasha Sherstneva
38 * @version 1.0 Dec 2013
41 public class JobController extends BasicController {
42 private static Logger log = Logger.getLogger(JobController.class);
45 * form a query page for job execution time statistics. The servlet should
46 * be available for users and admins only. By defaults the report time range
47 * is from the earliest day with jobs to today ("Query for all dates" is
48 * ticked). If the user removes the tick the time range is from today - 3
49 * days to today. Currently, the input model is empty.
53 * @return link to the JSP query page
55 @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
56 public String initFormExecTime(Map<String, Object> model) {
57 model.put("username", getPrincipalName());
58 Calendar cal = Calendar.getInstance();
59 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
60 cal.add(Calendar.DATE, -3);
61 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
63 model.put("date1", date1);
64 model.put("date2", date2);
65 return "query/JobTimeExecution";
69 * form a query page for a job. The servlet should no be visible to users at
74 * @return link to the JSP query page
76 @RequestMapping(value = "/job/query", method = RequestMethod.GET)
77 public String initFindForm(Map<String, Object> model) {
78 model.put("username", getPrincipalName());
79 CassandraRequester cr = new CassandraRequester();
80 model.put("value", cr.getExample("jobid"));
81 return "query/JobLog";
85 * form a report page for a job execution time statistics.
89 * @return link to the JSP query page
91 @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
92 public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
93 @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
94 model.put("username", getPrincipalName());
95 final long startTime = System.currentTimeMillis();
96 Calendar loccal = Calendar.getInstance();
97 ExecutionTimeReader reader = new ExecutionTimeReader();
98 if (option.equals("AllDates,off")) {
99 date1 = theEaerlistDate;
100 date2 = theCurrentDate;
103 // dates in string format
104 String trimmeddate1 = date1.replaceAll("\\s", "");
105 String trimmeddate2 = date2.replaceAll("\\s", "");
106 // dates in long format
107 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
108 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
109 String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
111 model.put("error", error);
112 model.put("date1", date1);
113 model.put("date2", date2);
114 return "query/JobTimeExecution";
117 if (longDate1 < CassandraReader.earliestDate())
118 longDate1 = CassandraReader.earliestDate();
119 if (longDate2 > loccal.getTimeInMillis())
120 longDate2 = loccal.getTimeInMillis();
122 date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
123 date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
124 model.put("date1", date1);
125 model.put("date2", date2);
126 model.put("option", option);
127 ExecutionTimeBean res = reader.query(longDate1, longDate2);;
128 model.put("result", res);
129 Map<String, TotalExecutionTime> results = res.getDateTotal();
131 if (0 < res.getDateTotal().size()) {
132 csvline = "\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A";
133 for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
134 csvline += "\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s() + "\',\'"
135 + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
136 + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A";
139 model.put("csvfile", csvline);
140 model.put("ndays", res.getDateTotal().size() - 1);
141 final long endTime = System.currentTimeMillis();
142 model.put("timeExecution", (endTime - startTime));
143 return "/reports/TimeExecution";
147 * form a report page for job statistics for one day only.
152 * date for the report
155 * @return link to the report JSP page
157 @RequestMapping(value = "/stat/jobsoneday/executionTime", method = RequestMethod.GET)
158 public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("interval") String interval, Map<String, Object> model)
159 throws ParseException {
160 model.put("username", getPrincipalName());
161 final long startTime = System.currentTimeMillis();
166 thetime = formaterYYMMDD.parse(date).getTime();
170 realdate = formaterDDMMYY.format(new Date(thetime));
172 } catch (ParseException e) {
174 thetime = formaterDDMMYY.parse(realdate).getTime();
177 if (null == ExecutionInterval.getExecutionInterval(interval))
178 return "support/Notimplemented";
181 ExecutionTimeReader reader = new ExecutionTimeReader();
182 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
183 DateBean r = reader.readJobByDay(thetime, realdate, ExecutionInterval.getBoundsInterval(interval));
184 model.put("results", r);
186 model.put("njobs", r.getJobidAndSeq().size());
187 model.put("date", realdate);
188 model.put("status", interval);
189 final long endTime = System.currentTimeMillis();
190 model.put("timeExecution", (endTime - startTime));
191 return "reports/JobStatisticsOneDay";
195 * form a query page for a job. The servlet should no be visible to users at
200 * @return link to the JSP query page
201 * @throws IOException
203 @RequestMapping(value = "/job/results", method = RequestMethod.GET)
204 public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
205 model.put("username", getPrincipalName());
206 final long startTime = System.currentTimeMillis();
207 JobReader reader = new JobReader();
208 model.put("result", reader.readJobLog(jobid));
209 final long endTime = System.currentTimeMillis();
210 model.put("timeExecution", (endTime - startTime));
211 model.put("IdJob", jobid);
213 ArchivedJob aj = new ArchivedJob(jobid);
215 model.put("jobarchive", aj.prepareJobArchiveToWeb());
216 } catch (IOException e) {
217 log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
218 log.error(e.getLocalizedMessage(), e.getCause());
220 // add a direct link to the job
221 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
222 URL remotelinkurl = new URL(remotelink);
223 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
224 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
225 model.put("jobremotelink", remotelink);
227 return "reports/Job";
231 * convert date from the standard long representation (milliseconds from 1
232 * Jan 1970) to yyyy/mm/dd
235 * date in milliseconds from 1 Jan 1970
236 * @return date in the form of yyyy/mm/dd
238 private String DateFormatYYMMDD(long indate) {
239 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
240 String dateString = datformat.format(new Date(indate));