Centralize initial examples of job id and IP
[proteocache.git] / server / compbio / controllers / JobController.java
1 package compbio.controllers;
2
3 import java.io.IOException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.log4j.Logger;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestMethod;
14 import org.springframework.web.bind.annotation.RequestParam;
15
16 import compbio.statistic.CassandraRequester;
17 import compbio.cassandra.CassandraNativeConnector;
18 import compbio.cassandra.DataBase;
19 import compbio.engine.archive.ArchivedJob;
20
21 /**
22  * @author Alexander Sherstnev
23  * @author Natasha Sherstneva
24  * @version 1.0 Dec 2013
25  */
26 @Controller
27 public class JobController extends BasicController {
28         private static Logger log = Logger.getLogger(JobController.class);
29
30         /**
31          * form a query page for job execution time statistics. The servlet should
32          * be available for users and admins only. By defaults the report time range
33          * is from the earliest day with jobs to today ("Query for all dates" is
34          * ticked). If the user removes the tick the time range is from today - 3
35          * days to today. Currently, the input model is empty.
36          * 
37          * @param model
38          *            MVC model
39          * @return link to the JSP query page
40          */
41         @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
42         public String initFormExecTime(Map<String, Object> model) {
43                 model.put("username", getPrincipalName());
44                 Calendar cal = Calendar.getInstance();
45                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
46                 cal.add(Calendar.DATE, -3);
47                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
48
49                 model.put("date1", date1);
50                 model.put("date2", date2);
51                 return "query/JobTimeExecution";
52         }
53
54         /**
55          * form a query page for a job. The servlet should no be visible to users at
56          * all.
57          * 
58          * @param model
59          *            MVC model
60          * @return link to the JSP query page
61          */
62         @RequestMapping(value = "/job/query", method = RequestMethod.GET)
63         public String initFindForm(Map<String, Object> model) {
64                 model.put("username", getPrincipalName());
65                 CassandraRequester cr = new CassandraRequester();
66                 model.put("value", cr.getExample("jobid"));
67                 return "query/JobLog";
68         }
69
70         /**
71          * form a report page for a job execution time statistics.
72          * 
73          * @param model
74          *            MVC model
75          * @return link to the JSP query page
76          */
77         @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
78         public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
79                         @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
80                 model.put("username", getPrincipalName());
81                 final long startTime = System.currentTimeMillis();
82
83                 CassandraRequester sp = new CassandraRequester();
84                 if (option.equals("AllDates,off")) {
85                         Calendar cal = Calendar.getInstance();
86                         date1 = DateFormatYYMMDD(sp.earliestDate());
87                         date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
88                 }
89                 model.put("date1", date1);
90                 model.put("date2", date2);
91                 model.put("option", option);
92                 List<DataBase> res = sp.extractExecutionTime(date1, date2);
93                 model.put("result", res);
94                 model.put("ndays", res.size() - 1);
95                 final long endTime = System.currentTimeMillis();
96                 model.put("timeExecution", (endTime - startTime));
97                 return "/reportTimeExecution";
98         }
99
100         /**
101          * form a query page for a job. The servlet should no be visible to users at
102          * all.
103          * 
104          * @param model
105          *            MVC model
106          * @return link to the JSP query page
107          */
108         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
109         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) {
110                 model.put("username", getPrincipalName());
111                 final long startTime = System.currentTimeMillis();
112                 CassandraRequester cr = new CassandraRequester();
113                 model.put("result", cr.readJobLog(jobid));
114                 final long endTime = System.currentTimeMillis();
115                 model.put("timeExecution", (endTime - startTime));
116                 model.put("IdJob", jobid);
117
118                 ArchivedJob aj = new ArchivedJob(jobid);
119                 try {
120                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
121                 } catch (IOException e) {
122                         log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
123                         log.error(e.getLocalizedMessage(), e.getCause());
124                 }
125                 return "reportJobLog";
126         }
127
128         /**
129          * convert date from the standard long representation (milliseconds from 1
130          * Jan 1970) to yyyy/mm/dd
131          * 
132          * @param indate
133          *            date in milliseconds from 1 Jan 1970
134          * @return date 
135          *            in the form of yyyy/mm/dd
136          */
137         private String DateFormatYYMMDD(long indate) {
138                 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
139                 String dateString = datformat.format(new Date(indate));
140                 return dateString;
141         }
142
143 }