9fdc128c9624fa5a77efcf4412a24c4b933507a3
[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.SimpleDateFormat;
7 import java.util.Calendar;
8 import java.util.Date;
9 import java.util.List;
10 import java.util.Map;
11
12 import org.apache.log4j.Logger;
13 import org.springframework.stereotype.Controller;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.RequestMethod;
16 import org.springframework.web.bind.annotation.RequestParam;
17
18 import compbio.statistic.CassandraRequester;
19 import compbio.beans.ExecutionTimeBean;
20 import compbio.beans.Total;
21 import compbio.beans.TotalExecutionTime;
22 import compbio.beans.TotalJobsStatisticBean;
23 import compbio.cassandra.DataBase;
24 import compbio.cassandra.DateFormatter;
25 import compbio.cassandra.readers.CassandraReader;
26 import compbio.cassandra.readers.ExecutionTimeReader;
27 import compbio.cassandra.readers.JobReader;
28 import compbio.engine.archive.ArchivedJob;
29
30 /**
31  * @author Alexander Sherstnev
32  * @author Natasha Sherstneva
33  * @version 1.0 Dec 2013
34  */
35 @Controller
36 public class JobController extends BasicController {
37         private static Logger log = Logger.getLogger(JobController.class);
38
39         /**
40          * form a query page for job execution time statistics. The servlet should
41          * be available for users and admins only. By defaults the report time range
42          * is from the earliest day with jobs to today ("Query for all dates" is
43          * ticked). If the user removes the tick the time range is from today - 3
44          * days to today. Currently, the input model is empty.
45          * 
46          * @param model
47          *            MVC model
48          * @return link to the JSP query page
49          */
50         @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
51         public String initFormExecTime(Map<String, Object> model) {
52                 model.put("username", getPrincipalName());
53                 Calendar cal = Calendar.getInstance();
54                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
55                 cal.add(Calendar.DATE, -3);
56                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
57
58                 model.put("date1", date1);
59                 model.put("date2", date2);
60                 return "query/JobTimeExecution";
61         }
62
63         /**
64          * form a query page for a job. The servlet should no be visible to users at
65          * all.
66          * 
67          * @param model
68          *            MVC model
69          * @return link to the JSP query page
70          */
71         @RequestMapping(value = "/job/query", method = RequestMethod.GET)
72         public String initFindForm(Map<String, Object> model) {
73                 model.put("username", getPrincipalName());
74                 CassandraRequester cr = new CassandraRequester();
75                 model.put("value", cr.getExample("jobid"));
76                 return "query/JobLog";
77         }
78
79         /**
80          * form a report page for a job execution time statistics.
81          * 
82          * @param model
83          *            MVC model
84          * @return link to the JSP query page
85          */
86         @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
87         public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
88                         @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
89                 model.put("username", getPrincipalName());
90                 final long startTime = System.currentTimeMillis();
91                 Calendar loccal = Calendar.getInstance();
92                 ExecutionTimeReader reader = new ExecutionTimeReader();
93                 if (option.equals("AllDates,off")) {
94                         date1 = theEaerlistDate;
95                         date2 = theCurrentDate;
96                 }
97                 
98                 // dates in string format
99                 String trimmeddate1 = date1.replaceAll("\\s", "");
100                 String trimmeddate2 = date2.replaceAll("\\s", "");
101                 // dates in long format
102                 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
103                 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
104                 String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
105                 if (error != null) {
106                         model.put("error", error);
107                         model.put("date1", date1);
108                         model.put("date2", date2);
109                         return "query/JobTimeExecution";
110                 }
111                 
112                 if (longDate1 < CassandraReader.earliestDate())
113                         longDate1 = CassandraReader.earliestDate();
114                 if (longDate2 > loccal.getTimeInMillis())
115                         longDate2 = loccal.getTimeInMillis();
116
117                 date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
118                 date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
119                 model.put("date1", date1);
120                 model.put("date2", date2);
121                 model.put("option", option);
122                 ExecutionTimeBean res = reader.query(longDate1, longDate2);;
123                 model.put("result", res);
124                 Map<String, TotalExecutionTime> results = res.getDateTotal();
125                 String csvline = "";
126                 if (0 < res.getDateTotal().size()) {
127                         csvline = "\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A";
128                         for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
129                                 csvline += "\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s() + "\',\'"
130                                                 + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
131                                                 + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A";
132                         }
133                 }
134                 model.put("csvfile", csvline);
135                 model.put("ndays", res.getDateTotal().size() - 1);
136                 final long endTime = System.currentTimeMillis();
137                 model.put("timeExecution", (endTime - startTime));
138                 return "/reports/TimeExecution";
139         }
140
141         /**
142          * form a query page for a job. The servlet should no be visible to users at
143          * all.
144          * 
145          * @param model
146          *            MVC model
147          * @return link to the JSP query page
148          * @throws IOException
149          */
150         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
151         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
152                 model.put("username", getPrincipalName());
153                 final long startTime = System.currentTimeMillis();
154                 JobReader reader = new JobReader();
155                 model.put("result", reader.readJobLog(jobid));
156                 final long endTime = System.currentTimeMillis();
157                 model.put("timeExecution", (endTime - startTime));
158                 model.put("IdJob", jobid);
159
160                 ArchivedJob aj = new ArchivedJob(jobid);
161                 try {
162                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
163                 } catch (IOException e) {
164                         log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
165                         log.error(e.getLocalizedMessage(), e.getCause());
166                 }
167                 // add a direct link to the job
168                 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
169                 URL remotelinkurl = new URL(remotelink);
170                 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
171                 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
172                         model.put("jobremotelink", remotelink);
173                 }
174                 return "reports/Job";
175         }
176
177         /**
178          * convert date from the standard long representation (milliseconds from 1
179          * Jan 1970) to yyyy/mm/dd
180          * 
181          * @param indate
182          *            date in milliseconds from 1 Jan 1970
183          * @return date in the form of yyyy/mm/dd
184          */
185         private String DateFormatYYMMDD(long indate) {
186                 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
187                 String dateString = datformat.format(new Date(indate));
188                 return dateString;
189         }
190
191 }