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