Add the "Reload" and "CSV" buttons
[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.engine.archive.ArchivedJob;
19
20 /**
21  * @author Alexander Sherstnev
22  * @author Natasha Sherstneva
23  * @version 1.0 Dec 2013
24  */
25 @Controller
26 public class JobController extends BasicController {
27         private static Logger log = Logger.getLogger(JobController.class);
28
29         /**
30          * form a query page for job execution time statistics. The servlet should
31          * be available for users and admins only. By defaults the report time range
32          * is from the earliest day with jobs to today ("Query for all dates" is
33          * ticked). If the user removes the tick the time range is from today - 3
34          * days to today. Currently, the input model is empty.
35          * 
36          * @param model
37          *            MVC model
38          * @return link to the JSP query page
39          */
40         @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
41         public String initFormExecTime(Map<String, Object> model) {
42                 model.put("username", getPrincipalName());
43                 Calendar cal = Calendar.getInstance();
44                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
45                 cal.add(Calendar.DATE, -3);
46                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
47
48                 model.put("date1", date1);
49                 model.put("date2", date2);
50                 return "query/JobTimeExecution";
51         }
52
53         /**
54          * form a query page for a job. The servlet should no be visible to users at
55          * all.
56          * 
57          * @param model
58          *            MVC model
59          * @return link to the JSP query page
60          */
61         @RequestMapping(value = "/job/query", method = RequestMethod.GET)
62         public String initFindForm(Map<String, Object> model) {
63                 model.put("username", getPrincipalName());
64                 CassandraRequester cr = new CassandraRequester();
65                 model.put("value", cr.getExample("jobid"));
66                 return "query/JobLog";
67         }
68
69         /**
70          * form a report page for a job execution time statistics.
71          * 
72          * @param model
73          *            MVC model
74          * @return link to the JSP query page
75          */
76         @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
77         public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
78                         @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
79                 model.put("username", getPrincipalName());
80                 final long startTime = System.currentTimeMillis();
81
82                 CassandraRequester sp = new CassandraRequester();
83                 if (option.equals("AllDates,off")) {
84                         Calendar cal = Calendar.getInstance();
85                         date1 = DateFormatYYMMDD(sp.earliestDate());
86                         date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
87                 }
88                 model.put("date1", date1);
89                 model.put("date2", date2);
90                 model.put("option", option);
91                 List<DataBase> res = sp.extractExecutionTime(date1, date2);
92                 model.put("result", res);
93                 String csvline = "";
94                 if (0 < res.size()) {
95                         csvline = "\'Date\',\'Total\',\'0-30 sec\',\'30-60 sec\',\'1-2 min\',\'2-10 min\',\'more 10 min\'%0A";
96                         for (DataBase entry : res) {
97                                 List<Integer> counts = entry.getTimeRez();
98                                 int total = 0;
99                                 for (int i = 0; i < counts.size(); ++i) {
100                                         total += counts.get(i);
101                                 }
102                                 csvline += "\'" + entry.getDate() + "\',\'" + total;
103                                 for (int i = 0; i < counts.size(); ++i) {
104                                         csvline += "\',\'" + counts.get(i);
105                                 }
106                                 csvline += "\'%0A";
107                         }
108                 }
109                 model.put("csvfile", csvline);
110                 model.put("ndays", res.size() - 1);
111                 final long endTime = System.currentTimeMillis();
112                 model.put("timeExecution", (endTime - startTime));
113                 return "/reportTimeExecution";
114         }
115
116         /**
117          * form a query page for a job. The servlet should no be visible to users at
118          * all.
119          * 
120          * @param model
121          *            MVC model
122          * @return link to the JSP query page
123          */
124         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
125         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) {
126                 model.put("username", getPrincipalName());
127                 final long startTime = System.currentTimeMillis();
128                 CassandraRequester cr = new CassandraRequester();
129                 model.put("result", cr.readJobLog(jobid));
130                 final long endTime = System.currentTimeMillis();
131                 model.put("timeExecution", (endTime - startTime));
132                 model.put("IdJob", jobid);
133
134                 ArchivedJob aj = new ArchivedJob(jobid);
135                 try {
136                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
137                 } catch (IOException e) {
138                         log.error("JobController.prepareJobArchiveToWeb: IO exception with job archive file");
139                         log.error(e.getLocalizedMessage(), e.getCause());
140                 }
141                 return "reportJobLog";
142         }
143
144         /**
145          * convert date from the standard long representation (milliseconds from 1
146          * Jan 1970) to yyyy/mm/dd
147          * 
148          * @param indate
149          *            date in milliseconds from 1 Jan 1970
150          * @return date in the form of yyyy/mm/dd
151          */
152         private String DateFormatYYMMDD(long indate) {
153                 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
154                 String dateString = datformat.format(new Date(indate));
155                 return dateString;
156         }
157
158 }