Merge branch 'DAO' of https://source.jalview.org/git/proteocache into DAO
[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.text.SimpleDateFormat;
8 import java.util.Calendar;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.Map;
12
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;
18
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;
34
35 /**
36  * @author Alexander Sherstnev
37  * @author Natasha Sherstneva
38  * @version 1.0 Dec 2013
39  */
40 @Controller
41 public class JobController extends BasicController {
42         private static Logger log = Logger.getLogger(JobController.class);
43
44         /**
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.
50          * 
51          * @param model
52          *            MVC model
53          * @return link to the JSP query page
54          */
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);
62
63                 model.put("date1", date1);
64                 model.put("date2", date2);
65                 return "query/JobTimeExecution";
66         }
67
68         /**
69          * form a query page for a job. The servlet should no be visible to users at
70          * all.
71          * 
72          * @param model
73          *            MVC model
74          * @return link to the JSP query page
75          */
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";
82         }
83
84         /**
85          * form a report page for a job execution time statistics.
86          * 
87          * @param model
88          *            MVC model
89          * @return link to the JSP query page
90          */
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;
101                 }
102                 
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);
110                 if (error != null) {
111                         model.put("error", error);
112                         model.put("date1", date1);
113                         model.put("date2", date2);
114                         return "query/JobTimeExecution";
115                 }
116                 
117                 if (longDate1 < CassandraReader.earliestDate())
118                         longDate1 = CassandraReader.earliestDate();
119                 if (longDate2 > loccal.getTimeInMillis())
120                         longDate2 = loccal.getTimeInMillis();
121
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();
130                 String csvline = "";
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";
137                         }
138                 }
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";
144         }
145         
146         /**
147          * form a report page for job statistics for one day only.
148          * 
149          * @param model
150          *            MVC model object
151          * @param date
152          *            date for the report
153          * @param status
154          * 
155          * @return link to the report JSP page
156          */
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();
162
163                 String realdate;
164                 long thetime = 0;
165                 try {
166                         thetime = formaterYYMMDD.parse(date).getTime();
167                         if (thetime < 0) {
168                                 realdate = date;
169                         } else {
170                                 realdate = formaterDDMMYY.format(new Date(thetime));
171                         }
172                 } catch (ParseException e) {
173                         realdate = date;
174                         thetime = formaterDDMMYY.parse(realdate).getTime();
175                 }
176
177                 if (null == ExecutionInterval.getExecutionInterval(interval)) 
178                         return "support/Notimplemented";
179
180
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);
185                 if (r != null)
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";
192         }
193
194         /**
195          * form a query page for a job. The servlet should no be visible to users at
196          * all.
197          * 
198          * @param model
199          *            MVC model
200          * @return link to the JSP query page
201          * @throws IOException
202          */
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);
212
213                 ArchivedJob aj = new ArchivedJob(jobid);
214                 try {
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());
219                 }
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);
226                 }
227                 return "reports/Job";
228         }
229
230         /**
231          * convert date from the standard long representation (milliseconds from 1
232          * Jan 1970) to yyyy/mm/dd
233          * 
234          * @param indate
235          *            date in milliseconds from 1 Jan 1970
236          * @return date in the form of yyyy/mm/dd
237          */
238         private String DateFormatYYMMDD(long indate) {
239                 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
240                 String dateString = datformat.format(new Date(indate));
241                 return dateString;
242         }
243
244 }