Merge branch 'DAO'
[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.DataBase;
18 import compbio.cassandra.readers.JobReader;
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                 String csvline = "";
95                 if (0 < res.size()) {
96                         csvline = "\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A";
97                         for (DataBase entry : res) {
98                                 List<Integer> counts = entry.getTimeRez();
99                                 int total = 0;
100                                 for (int i = 0; i < counts.size(); ++i) {
101                                         total += counts.get(i);
102                                 }
103                                 csvline += "\'" + entry.getDate() + "\',\'" + total;
104                                 for (int i = 0; i < counts.size(); ++i) {
105                                         csvline += "\',\'" + counts.get(i);
106                                 }
107                                 csvline += "\'%0A";
108                         }
109                 }
110                 model.put("csvfile", csvline);
111                 model.put("ndays", res.size() - 1);
112                 final long endTime = System.currentTimeMillis();
113                 model.put("timeExecution", (endTime - startTime));
114                 return "/reportTimeExecution";
115         }
116
117         /**
118          * form a query page for a job. The servlet should no be visible to users at
119          * all.
120          * 
121          * @param model
122          *            MVC model
123          * @return link to the JSP query page
124          */
125         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
126         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) {
127                 model.put("username", getPrincipalName());
128                 final long startTime = System.currentTimeMillis();
129                 JobReader reader = new JobReader();
130                 model.put("result", reader.readJobLog(jobid));
131                 final long endTime = System.currentTimeMillis();
132                 model.put("timeExecution", (endTime - startTime));
133                 model.put("IdJob", jobid);
134
135                 ArchivedJob aj = new ArchivedJob(jobid);
136                 try {
137                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
138                 } catch (IOException e) {
139                         log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
140                         log.error(e.getLocalizedMessage(), e.getCause());
141                 }
142                 return "reportJobLog";
143         }
144
145         /**
146          * convert date from the standard long representation (milliseconds from 1
147          * Jan 1970) to yyyy/mm/dd
148          * 
149          * @param indate
150          *            date in milliseconds from 1 Jan 1970
151          * @return date in the form of yyyy/mm/dd
152          */
153         private String DateFormatYYMMDD(long indate) {
154                 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
155                 String dateString = datformat.format(new Date(indate));
156                 return dateString;
157         }
158
159 }