0dd39f8fde2376372dd0b7a403e67c64f19ab579
[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.TotalExecutionTime;
21 import compbio.cassandra.DateFormatter;
22 import compbio.cassandra.readers.CassandraReader;
23 import compbio.cassandra.readers.ExecutionTimeReader;
24 import compbio.cassandra.readers.JobReader;
25 import compbio.engine.ExecutionInterval;
26 import compbio.engine.archive.ArchivedJob;
27
28 /**
29  * Spring controller for supporting job queries. This version works in the
30  * servlet style.
31  * 
32  * @author Alexander Sherstnev
33  * @author Natasha Sherstneva
34  * @version 1.0
35  * @since Dec 2013
36  */
37 @Controller
38 public class JobController extends BasicController {
39         private static Logger log = Logger.getLogger(JobController.class);
40
41         /**
42          * form a query page for job execution time statistics. The servlet should
43          * be available for users and admins only. By defaults the report time range
44          * is from the earliest day with jobs to today ("Query for all dates" is
45          * ticked). If the user removes the tick the time range is from today - 3
46          * days to today. Currently, the input model is empty.
47          * 
48          * @param model
49          *            MVC model
50          * @return link to the JSP query page
51          */
52         @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
53         public String initFormExecTime(Map<String, Object> model) {
54                 model.put("username", getPrincipalName());
55                 Calendar cal = Calendar.getInstance();
56                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
57                 cal.add(Calendar.DATE, -3);
58                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
59
60                 model.put("date1", date1);
61                 model.put("date2", date2);
62                 return "query/JobTimeExecution";
63         }
64
65         /**
66          * form a query page for a job. The servlet should no be visible to users at
67          * all.
68          * 
69          * @param model
70          *            MVC model
71          * @return link to the JSP query page
72          */
73         @RequestMapping(value = "/job/query", method = RequestMethod.GET)
74         public String initFindForm(Map<String, Object> model) {
75                 model.put("username", getPrincipalName());
76                 CassandraRequester cr = new CassandraRequester();
77                 model.put("value", cr.getExample("jobid"));
78                 return "query/JobLog";
79         }
80
81         /**
82          * form a report page for a job execution time statistics.
83          * 
84          * @param date1
85          *            initial date for requested statistics
86          * @param date2
87          *            final date for requested statistics
88          * @param alldates
89          *            All available jobs are analyzed if alldates="AllDates,off"
90          * @param model
91          *            MVC model
92          * @return link to the JSP query page
93          */
94         @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
95         public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
96                         @RequestParam(value = "option", required = false) String alldates, Map<String, Object> model) {
97                 model.put("username", getPrincipalName());
98                 final long startTime = System.currentTimeMillis();
99                 Calendar loccal = Calendar.getInstance();
100                 ExecutionTimeReader reader = new ExecutionTimeReader();
101                 if (alldates.equals("AllDates,off")) {
102                         date1 = theEaerlistDate;
103                         date2 = theCurrentDate;
104                 }
105
106                 // dates in string format
107                 String trimmeddate1 = date1.replaceAll("\\s", "");
108                 String trimmeddate2 = date2.replaceAll("\\s", "");
109                 // dates in long format
110                 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
111                 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
112                 String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
113                 if (error != null) {
114                         model.put("error", error);
115                         model.put("date1", date1);
116                         model.put("date2", date2);
117                         return "query/JobTimeExecution";
118                 }
119
120                 if (longDate1 < CassandraReader.earliestDate())
121                         longDate1 = CassandraReader.earliestDate();
122                 if (longDate2 > loccal.getTimeInMillis())
123                         longDate2 = loccal.getTimeInMillis();
124
125                 date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
126                 date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
127                 model.put("date1", date1);
128                 model.put("date2", date2);
129                 model.put("option", alldates);
130                 ExecutionTimeBean res = reader.query(longDate1, longDate2);
131                 ;
132                 model.put("result", res);
133                 Map<String, TotalExecutionTime> results = res.getDateTotal();
134                 StringBuilder csvline = new StringBuilder("");
135                 if (0 < res.getDateTotal().size()) {
136                         csvline.append("\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A");
137                         for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
138                                 csvline.append("\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s()
139                                                 + "\',\'" + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
140                                                 + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A");
141                         }
142                 }
143                 model.put("csvfile", csvline.toString());
144                 model.put("ndays", res.getDateTotal().size() - 1);
145                 final long endTime = System.currentTimeMillis();
146                 model.put("timeExecution", (endTime - startTime));
147                 return "/reports/TimeExecution";
148         }
149         
150         /**
151          * form a report page for job statistics for one day only.
152          * 
153          * @param model
154          *            MVC model object
155          * @param date
156          *            date for the report
157          * @param status
158          * 
159          * @return link to the report JSP page
160          */
161         @RequestMapping(value = "/stat/jobsoneday/executionTime", method = RequestMethod.GET)
162         public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("interval") String interval, Map<String, Object> model)
163                         throws ParseException {
164                 model.put("username", getPrincipalName());
165                 final long startTime = System.currentTimeMillis();
166
167                 String realdate;
168                 long thetime = 0;
169                 try {
170                         thetime = formaterYYMMDD.parse(date).getTime();
171                         if (thetime < 0) {
172                                 realdate = date;
173                         } else {
174                                 realdate = formaterDDMMYY.format(new Date(thetime));
175                         }
176                 } catch (ParseException e) {
177                         realdate = date;
178                         thetime = formaterDDMMYY.parse(realdate).getTime();
179                 }
180
181                 if (null == ExecutionInterval.getExecutionInterval(interval)) 
182                         return "support/Notimplemented";
183
184                 ExecutionTimeReader reader = new ExecutionTimeReader();
185                 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
186                 DateBean r = reader.readJobByDay(thetime, realdate, ExecutionInterval.getBoundsInterval(interval));
187                 model.put("results", r);
188                 if (r != null)
189                         model.put("njobs", r.getJobidAndSeq().size());
190                 model.put("date", realdate);
191                 model.put("status", interval);
192                 final long endTime = System.currentTimeMillis();
193                 model.put("timeExecution", (endTime - startTime));
194                 return "reports/JobStatisticsOneDay";
195         }
196
197         /**
198          * form result page for one job with a given job ID.
199          * 
200          * @param jobid
201          *            job ID
202          * @param model
203          *            MVC model
204          * 
205          * @return link to the JSP query page
206          * @throws IOException
207          */
208         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
209         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
210                 model.put("username", getPrincipalName());
211                 final long startTime = System.currentTimeMillis();
212                 JobReader reader = new JobReader();
213                 model.put("result", reader.readJobLog(jobid));
214                 final long endTime = System.currentTimeMillis();
215                 model.put("timeExecution", (endTime - startTime));
216                 model.put("IdJob", jobid);
217
218                 // prepare archive file for the job for downloading
219                 ArchivedJob aj = new ArchivedJob(jobid);
220                 try {
221                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
222                 } catch (IOException e) {
223                         log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
224                         log.error(e.getLocalizedMessage(), e.getCause());
225                 }
226                 // add a direct link to the job
227                 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
228                 URL remotelinkurl = new URL(remotelink);
229                 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
230                 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
231                         model.put("jobremotelink", remotelink);
232                 }
233                 return "reports/Job";
234         }
235
236 }