A new MVC model paramter for "no jobs found"
[proteocache.git] / server / compbio / controllers / JobController.java
index a2b952c..0a00d9b 100644 (file)
@@ -1,33 +1,48 @@
 package compbio.controllers;
 
 import java.io.IOException;
-import java.text.SimpleDateFormat;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.text.ParseException;
 import java.util.Calendar;
 import java.util.Date;
-import java.util.List;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
 import compbio.statistic.CassandraRequester;
-import compbio.cassandra.DataBase;
+import compbio.beans.DateBean;
+import compbio.beans.ExecutionTimeBean;
+import compbio.beans.JobBean;
+import compbio.beans.TotalExecutionTime;
+import compbio.cassandra.DateFormatter;
+import compbio.cassandra.readers.CassandraReader;
+import compbio.cassandra.readers.ExecutionTimeReader;
+import compbio.cassandra.readers.JobReader;
+import compbio.engine.ExecutionInterval;
 import compbio.engine.archive.ArchivedJob;
 
 /**
+ * Spring controller for supporting job queries. This version works in the
+ * servlet style.
+ * 
  * @author Alexander Sherstnev
  * @author Natasha Sherstneva
- * @version 1.0 Dec 2013
+ * @version 1.0
+ * @since Dec 2013
  */
 @Controller
 public class JobController extends BasicController {
+       private static Logger log = Logger.getLogger(JobController.class);
 
        /**
-        * form a query page for job execution time statistics. The servlet should be
-        * available for users and admins only. By defaults the report time range is
-        * from the earliest day with jobs to today ("Query for all dates" is
+        * form a query page for job execution time statistics. The servlet should
+        * be available for users and admins only. By defaults the report time range
+        * is from the earliest day with jobs to today ("Query for all dates" is
         * ticked). If the user removes the tick the time range is from today - 3
         * days to today. Currently, the input model is empty.
         * 
@@ -49,7 +64,8 @@ public class JobController extends BasicController {
        }
 
        /**
-        * form a query page for a job. The servlet should no be visible to users at all. 
+        * form a query page for a job. The servlet should no be visible to users at
+        * all.
         * 
         * @param model
         *            MVC model
@@ -58,59 +74,170 @@ public class JobController extends BasicController {
        @RequestMapping(value = "/job/query", method = RequestMethod.GET)
        public String initFindForm(Map<String, Object> model) {
                model.put("username", getPrincipalName());
-               model.put("value", "jp_NzBOJKo");
+               CassandraRequester cr = new CassandraRequester();
+               model.put("value", cr.getExample("jobid"));
                return "query/JobLog";
        }
 
+       /**
+        * form a report page for a job execution time statistics.
+        * 
+        * @param date1
+        *            initial date for requested statistics
+        * @param date2
+        *            final date for requested statistics
+        * @param alldates
+        *            All available jobs are analyzed if alldates="AllDates,off"
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
        @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
        public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
-                       @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
+                       @RequestParam(value = "option", required = false) String alldates, Map<String, Object> model) {
                model.put("username", getPrincipalName());
                final long startTime = System.currentTimeMillis();
+               Calendar loccal = Calendar.getInstance();
+               ExecutionTimeReader reader = new ExecutionTimeReader();
+               if (alldates.equals("AllDates,off")) {
+                       date1 = getEarliestDate();
+                       date2 = getCurrentDate();
+               }
 
-               CassandraRequester sp = new CassandraRequester();
-               if (option.equals("AllDates,off")) {
-                       Calendar cal = Calendar.getInstance();
-                       date1 = DateFormatYYMMDD(sp.earliestDate());
-                       date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
+               // dates in string format
+               String trimmeddate1 = date1.replaceAll("\\s", "");
+               String trimmeddate2 = date2.replaceAll("\\s", "");
+               // dates in long format
+               long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
+               long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
+               String error = checkDates(trimmeddate1, trimmeddate2, longDate1, longDate2);
+               if (error != null) {
+                       model.put("error", error);
+                       model.put("date1", date1);
+                       model.put("date2", date2);
+                       return "query/JobTimeExecution";
                }
+
+               if (longDate1 < CassandraReader.earliestDate())
+                       longDate1 = CassandraReader.earliestDate();
+               if (longDate2 > loccal.getTimeInMillis())
+                       longDate2 = loccal.getTimeInMillis();
+
+               date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
+               date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
                model.put("date1", date1);
                model.put("date2", date2);
-               model.put("option", option);
-               List<DataBase> res = sp.extractExecutionTime(date1, date2);
+               model.put("option", alldates);
+               ExecutionTimeBean res = reader.query(longDate1, longDate2);
+               ;
                model.put("result", res);
-               model.put("ndays", res.size() - 1);
+               Map<String, TotalExecutionTime> results = res.getDateTotal();
+               StringBuilder csvline = new StringBuilder("");
+               if (0 < res.getDateTotal().size()) {
+                       csvline.append("\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A");
+                       for (Map.Entry<String, TotalExecutionTime> entry : results.entrySet()) {
+                               csvline.append("\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotal0_30s()
+                                               + "\',\'" + entry.getValue().getTotal30_60s() + "\',\'" + entry.getValue().getTotal1_2m() + "\',\'"
+                                               + entry.getValue().getTotal2_10m() + "\',\'" + entry.getValue().getTotal10m() + "\'%0A");
+                       }
+               }
+               model.put("csvfile", csvline.toString());
+               model.put("ndays", res.getDateTotal().size() - 1);
+               final long endTime = System.currentTimeMillis();
+               model.put("timeExecution", (endTime - startTime));
+               return "/reports/TimeExecution";
+       }
+       
+       /**
+        * form a report page for job statistics for one day only.
+        * 
+        * @param model
+        *            MVC model object
+        * @param date
+        *            date for the report
+        * @param status
+        * 
+        * @return link to the report JSP page
+        */
+       @RequestMapping(value = "/stat/jobsoneday/executionTime", method = RequestMethod.GET)
+       public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("interval") String interval, Map<String, Object> model)
+                       throws ParseException {
+               model.put("username", getPrincipalName());
+               final long startTime = System.currentTimeMillis();
+
+               String realdate;
+               long thetime = 0;
+               try {
+                       thetime = formaterYYMMDD.parse(date).getTime();
+                       if (thetime < 0) {
+                               realdate = date;
+                       } else {
+                               realdate = formaterDDMMYY.format(new Date(thetime));
+                       }
+               } catch (ParseException e) {
+                       realdate = date;
+                       thetime = formaterDDMMYY.parse(realdate).getTime();
+               }
+
+               if (null == ExecutionInterval.getExecutionInterval(interval)) 
+                       return "support/Notimplemented";
+
+               ExecutionTimeReader reader = new ExecutionTimeReader();
+               // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
+               DateBean r = reader.readJobByDay(thetime, realdate, ExecutionInterval.getBoundsInterval(interval));
+               model.put("results", r);
+               if (r != null)
+                       model.put("njobs", r.getJobidAndSeq().size());
+               model.put("date", realdate);
+               model.put("status", interval);
                final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
-               return "/reportTimeExecution";
+               return "reports/JobStatisticsOneDay";
        }
 
+       /**
+        * form result page for one job with a given job ID.
+        * 
+        * @param jobid
+        *            job ID
+        * @param model
+        *            MVC model
+        * 
+        * @return link to the JSP query page
+        * @throws IOException
+        */
        @RequestMapping(value = "/job/results", method = RequestMethod.GET)
-       public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) {
+       public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) throws IOException {
                model.put("username", getPrincipalName());
                final long startTime = System.currentTimeMillis();
-               CassandraRequester cr = new CassandraRequester();
-               model.put("result", cr.readJobLog(jobid));
+               JobReader reader = new JobReader();
+               JobBean job = reader.readJobLog(jobid);
+               if (null == job) {
+                       model.put("jobnotfound", "yes");
+               } else {
+                       model.put("result", job);
+               }
                final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
                model.put("IdJob", jobid);
 
+               // prepare archive file for the job for downloading
                ArchivedJob aj = new ArchivedJob(jobid);
                try {
                        model.put("jobarchive", aj.prepareJobArchiveToWeb());
                } catch (IOException e) {
-                       //TODO. what should we do if job is not available???
+                       log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
+                       log.error(e.getLocalizedMessage(), e.getCause());
                }
-               return "reportJobLog";
-       }
 
-       /*
-        * convert ???
-        */
-       private String DateFormatYYMMDD(long indate) {
-               SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
-               String dateString = datformat.format(new Date(indate));
-               return dateString;
+               // add a direct link to the job
+               String remotelink = "http://www.compbio.dundee.ac.uk/www-jpred/results/" + jobid + "/" + jobid + ".results.html";
+               URL remotelinkurl = new URL(remotelink);
+               HttpURLConnection httpConnection_remotelinkurl = (HttpURLConnection) remotelinkurl.openConnection();
+               if (199 < httpConnection_remotelinkurl.getResponseCode() && httpConnection_remotelinkurl.getResponseCode() < 300) {
+                       model.put("jobremotelink", remotelink);
+               }
+               return "reports/Job";
        }
 
 }