A new MVC model paramter for "no jobs found"
[proteocache.git] / server / compbio / controllers / JobController.java
1 package compbio.controllers;
2
3 import java.io.IOException;
4 import java.net.HttpURLConnection;
5 import java.net.URL;
6 import java.text.ParseException;
7 import java.util.Calendar;
8 import java.util.Date;
9 import java.util.Map;
10
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;
16
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;
28
29 /**
30  * Spring controller for supporting job queries. This version works in the
31  * servlet style.
32  * 
33  * @author Alexander Sherstnev
34  * @author Natasha Sherstneva
35  * @version 1.0
36  * @since Dec 2013
37  */
38 @Controller
39 public class JobController extends BasicController {
40         private static Logger log = Logger.getLogger(JobController.class);
41
42         /**
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.
48          * 
49          * @param model
50          *            MVC model
51          * @return link to the JSP query page
52          */
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);
60
61                 model.put("date1", date1);
62                 model.put("date2", date2);
63                 return "query/JobTimeExecution";
64         }
65
66         /**
67          * form a query page for a job. The servlet should no be visible to users at
68          * all.
69          * 
70          * @param model
71          *            MVC model
72          * @return link to the JSP query page
73          */
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";
80         }
81
82         /**
83          * form a report page for a job execution time statistics.
84          * 
85          * @param date1
86          *            initial date for requested statistics
87          * @param date2
88          *            final date for requested statistics
89          * @param alldates
90          *            All available jobs are analyzed if alldates="AllDates,off"
91          * @param model
92          *            MVC model
93          * @return link to the JSP query page
94          */
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();
105                 }
106
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);
114                 if (error != null) {
115                         model.put("error", error);
116                         model.put("date1", date1);
117                         model.put("date2", date2);
118                         return "query/JobTimeExecution";
119                 }
120
121                 if (longDate1 < CassandraReader.earliestDate())
122                         longDate1 = CassandraReader.earliestDate();
123                 if (longDate2 > loccal.getTimeInMillis())
124                         longDate2 = loccal.getTimeInMillis();
125
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);
132                 ;
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");
142                         }
143                 }
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";
149         }
150         
151         /**
152          * form a report page for job statistics for one day only.
153          * 
154          * @param model
155          *            MVC model object
156          * @param date
157          *            date for the report
158          * @param status
159          * 
160          * @return link to the report JSP page
161          */
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();
167
168                 String realdate;
169                 long thetime = 0;
170                 try {
171                         thetime = formaterYYMMDD.parse(date).getTime();
172                         if (thetime < 0) {
173                                 realdate = date;
174                         } else {
175                                 realdate = formaterDDMMYY.format(new Date(thetime));
176                         }
177                 } catch (ParseException e) {
178                         realdate = date;
179                         thetime = formaterDDMMYY.parse(realdate).getTime();
180                 }
181
182                 if (null == ExecutionInterval.getExecutionInterval(interval)) 
183                         return "support/Notimplemented";
184
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);
189                 if (r != null)
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";
196         }
197
198         /**
199          * form result page for one job with a given job ID.
200          * 
201          * @param jobid
202          *            job ID
203          * @param model
204          *            MVC model
205          * 
206          * @return link to the JSP query page
207          * @throws IOException
208          */
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);
215                 if (null == job) {
216                         model.put("jobnotfound", "yes");
217                 } else {
218                         model.put("result", job);
219                 }
220                 final long endTime = System.currentTimeMillis();
221                 model.put("timeExecution", (endTime - startTime));
222                 model.put("IdJob", jobid);
223
224                 // prepare archive file for the job for downloading
225                 ArchivedJob aj = new ArchivedJob(jobid);
226                 try {
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());
231                 }
232
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);
239                 }
240                 return "reports/Job";
241         }
242
243 }