1. Fix bug in SequenceReader
[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.util.Calendar;
7 import java.util.Map;
8
9 import org.apache.log4j.Logger;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestMethod;
13 import org.springframework.web.bind.annotation.RequestParam;
14
15 import compbio.statistic.CassandraRequester;
16 import compbio.beans.ExecutionTimeBean;
17 import compbio.beans.TotalExecutionTime;
18 import compbio.cassandra.DateFormatter;
19 import compbio.cassandra.readers.CassandraReader;
20 import compbio.cassandra.readers.ExecutionTimeReader;
21 import compbio.cassandra.readers.JobReader;
22 import compbio.engine.archive.ArchivedJob;
23
24 /**
25  * Spring controller for supporting job queries. This version works in the
26  * servlet style.
27  * 
28  * @author Alexander Sherstnev
29  * @author Natasha Sherstneva
30  * @version 1.0
31  * @since Dec 2013
32  */
33 @Controller
34 public class JobController extends BasicController {
35         private static Logger log = Logger.getLogger(JobController.class);
36
37         /**
38          * form a query page for job execution time statistics. The servlet should
39          * be available for users and admins only. By defaults the report time range
40          * is from the earliest day with jobs to today ("Query for all dates" is
41          * ticked). If the user removes the tick the time range is from today - 3
42          * days to today. Currently, the input model is empty.
43          * 
44          * @param model
45          *            MVC model
46          * @return link to the JSP query page
47          */
48         @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
49         public String initFormExecTime(Map<String, Object> model) {
50                 model.put("username", getPrincipalName());
51                 Calendar cal = Calendar.getInstance();
52                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
53                 cal.add(Calendar.DATE, -3);
54                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
55
56                 model.put("date1", date1);
57                 model.put("date2", date2);
58                 return "query/JobTimeExecution";
59         }
60
61         /**
62          * form a query page for a job. The servlet should no be visible to users at
63          * all.
64          * 
65          * @param model
66          *            MVC model
67          * @return link to the JSP query page
68          */
69         @RequestMapping(value = "/job/query", method = RequestMethod.GET)
70         public String initFindForm(Map<String, Object> model) {
71                 model.put("username", getPrincipalName());
72                 CassandraRequester cr = new CassandraRequester();
73                 model.put("value", cr.getExample("jobid"));
74                 return "query/JobLog";
75         }
76
77         /**
78          * form a report page for a job execution time statistics.
79          * 
80          * @param date1
81          *            initial date for requested statistics
82          * @param date2
83          *            final date for requested statistics
84          * @param alldates
85          *            All available jobs are analyzed if alldates="AllDates,off"
86          * @param model
87          *            MVC model
88          * @return link to the JSP query page
89          */
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 alldates, 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 (alldates.equals("AllDates,off")) {
98                         date1 = theEaerlistDate;
99                         date2 = theCurrentDate;
100                 }
101
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);
109                 if (error != null) {
110                         model.put("error", error);
111                         model.put("date1", date1);
112                         model.put("date2", date2);
113                         return "query/JobTimeExecution";
114                 }
115
116                 if (longDate1 < CassandraReader.earliestDate())
117                         longDate1 = CassandraReader.earliestDate();
118                 if (longDate2 > loccal.getTimeInMillis())
119                         longDate2 = loccal.getTimeInMillis();
120
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", alldates);
126                 ExecutionTimeBean res = reader.query(longDate1, longDate2);
127                 ;
128                 model.put("result", res);
129                 Map<String, TotalExecutionTime> results = res.getDateTotal();
130                 StringBuilder csvline = new StringBuilder("");
131                 if (0 < res.getDateTotal().size()) {
132                         csvline.append("\'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.append("\'" + 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");
137                         }
138                 }
139                 model.put("csvfile", csvline.toString());
140                 model.put("ndays", res.getDateTotal().size() - 1);
141                 final long endTime = System.currentTimeMillis();
142                 model.put("timeExecution", (endTime - startTime));
143                 return "/reports/TimeExecution";
144         }
145
146         /**
147          * form result page for one job with a given job ID.
148          * 
149          * @param jobid
150          *            job ID
151          * @param model
152          *            MVC model
153          * 
154          * @return link to the JSP query page
155          * @throws IOException
156          */
157         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
158         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
159                 model.put("username", getPrincipalName());
160                 final long startTime = System.currentTimeMillis();
161                 JobReader reader = new JobReader();
162                 model.put("result", reader.readJobLog(jobid));
163                 final long endTime = System.currentTimeMillis();
164                 model.put("timeExecution", (endTime - startTime));
165                 model.put("IdJob", jobid);
166
167                 // prepare archive file for the job for downloading
168                 ArchivedJob aj = new ArchivedJob(jobid);
169                 try {
170                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
171                 } catch (IOException e) {
172                         log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
173                         log.error(e.getLocalizedMessage(), e.getCause());
174                 }
175                 // add a direct link to the job
176                 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
177                 URL remotelinkurl = new URL(remotelink);
178                 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
179                 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
180                         model.put("jobremotelink", remotelink);
181                 }
182                 return "reports/Job";
183         }
184
185 }