Deleted CassandraRequest.java and CassandraReaderOld.java
[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.beans.DateBean;
20 import compbio.beans.ExecutionTimeBean;
21 import compbio.beans.Total;
22 import compbio.beans.TotalExecutionTime;
23 import compbio.beans.TotalJobsStatisticBean;
24 import compbio.cassandra.DataBase;
25 import compbio.cassandra.DateFormatter;
26 import compbio.cassandra.readers.CassandraReader;
27 import compbio.cassandra.readers.DailyStatisticsReader;
28 import compbio.cassandra.readers.ExecutionTimeReader;
29 import compbio.cassandra.readers.JobReader;
30 import compbio.engine.ExecutionInterval;
31 import compbio.engine.JobStatus;
32 import compbio.engine.archive.ArchivedJob;
33
34 /**
35  * @author Alexander Sherstnev
36  * @author Natasha Sherstneva
37  * @version 1.0 Dec 2013
38  */
39 @Controller
40 public class JobController extends BasicController {
41         private static Logger log = Logger.getLogger(JobController.class);
42
43         /**
44          * form a query page for job execution time statistics. The servlet should
45          * be available for users and admins only. By defaults the report time range
46          * is from the earliest day with jobs to today ("Query for all dates" is
47          * ticked). If the user removes the tick the time range is from today - 3
48          * days to today. Currently, the input model is empty.
49          * 
50          * @param model
51          *            MVC model
52          * @return link to the JSP query page
53          */
54         @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
55         public String initFormExecTime(Map<String, Object> model) {
56                 model.put("username", getPrincipalName());
57                 Calendar cal = Calendar.getInstance();
58                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
59                 cal.add(Calendar.DATE, -3);
60                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
61
62                 model.put("date1", date1);
63                 model.put("date2", date2);
64                 return "query/JobTimeExecution";
65         }
66
67         /**
68          * form a query page for a job. The servlet should no be visible to users at
69          * all.
70          * 
71          * @param model
72          *            MVC model
73          * @return link to the JSP query page
74          */
75         @RequestMapping(value = "/job/query", method = RequestMethod.GET)
76         public String initFindForm(Map<String, Object> model) {
77                 model.put("username", getPrincipalName());
78                 CassandraReader cr = new CassandraReader();
79                 model.put("value", cr.getExample("jobid"));
80                 return "query/JobLog";
81         }
82
83         /**
84          * form a report page for a job execution time statistics.
85          * 
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 option, 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 (option.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", option);
126                 ExecutionTimeBean res = reader.query(longDate1, longDate2);;
127                 model.put("result", res);
128                 Map<String, TotalExecutionTime> results = res.getDateTotal();
129                 String csvline = "";
130                 if (0 < res.getDateTotal().size()) {
131                         csvline = "\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A";
132                         for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
133                                 csvline += "\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s() + "\',\'"
134                                                 + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
135                                                 + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A";
136                         }
137                 }
138                 model.put("csvfile", csvline);
139                 model.put("ndays", res.getDateTotal().size() - 1);
140                 final long endTime = System.currentTimeMillis();
141                 model.put("timeExecution", (endTime - startTime));
142                 return "/reports/TimeExecution";
143         }
144         
145         /**
146          * form a report page for job statistics for one day only.
147          * 
148          * @param model
149          *            MVC model object
150          * @param date
151          *            date for the report
152          * @param status
153          * 
154          * @return link to the report JSP page
155          */
156         @RequestMapping(value = "/stat/jobsoneday/executionTime", method = RequestMethod.GET)
157         public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("interval") String interval, Map<String, Object> model)
158                         throws ParseException {
159                 model.put("username", getPrincipalName());
160                 final long startTime = System.currentTimeMillis();
161
162                 String realdate;
163                 long thetime = 0;
164                 try {
165                         thetime = formaterYYMMDD.parse(date).getTime();
166                         if (thetime < 0) {
167                                 realdate = date;
168                         } else {
169                                 realdate = formaterDDMMYY.format(new Date(thetime));
170                         }
171                 } catch (ParseException e) {
172                         realdate = date;
173                         thetime = formaterDDMMYY.parse(realdate).getTime();
174                 }
175
176                 if (null == ExecutionInterval.getExecutionInterval(interval)) 
177                         return "support/Notimplemented";
178
179
180                 ExecutionTimeReader reader = new ExecutionTimeReader();
181                 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
182                 DateBean r = reader.readJobByDay(thetime, realdate, ExecutionInterval.getBoundsInterval(interval));
183                 model.put("results", r);
184                 if (r != null)
185                         model.put("njobs", r.getJobidAndSeq().size());
186                 model.put("date", realdate);
187                 model.put("status", interval);
188                 final long endTime = System.currentTimeMillis();
189                 model.put("timeExecution", (endTime - startTime));
190                 return "reports/JobStatisticsOneDay";
191         }
192
193         /**
194          * form a query page for a job. The servlet should no be visible to users at
195          * all.
196          * 
197          * @param model
198          *            MVC model
199          * @return link to the JSP query page
200          * @throws IOException
201          */
202         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
203         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
204                 model.put("username", getPrincipalName());
205                 final long startTime = System.currentTimeMillis();
206                 JobReader reader = new JobReader();
207                 model.put("result", reader.readJobLog(jobid));
208                 final long endTime = System.currentTimeMillis();
209                 model.put("timeExecution", (endTime - startTime));
210                 model.put("IdJob", jobid);
211
212                 ArchivedJob aj = new ArchivedJob(jobid);
213                 try {
214                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
215                 } catch (IOException e) {
216                         log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
217                         log.error(e.getLocalizedMessage(), e.getCause());
218                 }
219                 // add a direct link to the job
220                 String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
221                 URL remotelinkurl = new URL(remotelink);
222                 HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
223                 if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
224                         model.put("jobremotelink", remotelink);
225                 }
226                 return "reports/Job";
227         }
228
229         /**
230          * convert date from the standard long representation (milliseconds from 1
231          * Jan 1970) to yyyy/mm/dd
232          * 
233          * @param indate
234          *            date in milliseconds from 1 Jan 1970
235          * @return date in the form of yyyy/mm/dd
236          */
237         private String DateFormatYYMMDD(long indate) {
238                 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
239                 String dateString = datformat.format(new Date(indate));
240                 return dateString;
241         }
242
243 }