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