new readers for the queries
[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.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestMethod;
13 import org.springframework.web.bind.annotation.RequestParam;
14
15 import compbio.statistic.CassandraRequester;
16 import compbio.cassandra.DataBase;
17 import compbio.cassandra.readers.JobReader;
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
28         /**
29          * form a query page for job execution time statistics. The servlet should be
30          * available for users and admins only. By defaults the report time range is
31          * from the earliest day with jobs to today ("Query for all dates" is
32          * ticked). If the user removes the tick the time range is from today - 3
33          * days to today. Currently, the input model is empty.
34          * 
35          * @param model
36          *            MVC model
37          * @return link to the JSP query page
38          */
39         @RequestMapping(value = "/stat/exectime/query", method = RequestMethod.GET)
40         public String initFormExecTime(Map<String, Object> model) {
41                 model.put("username", getPrincipalName());
42                 Calendar cal = Calendar.getInstance();
43                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
44                 cal.add(Calendar.DATE, -3);
45                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
46
47                 model.put("date1", date1);
48                 model.put("date2", date2);
49                 return "query/JobTimeExecution";
50         }
51
52         /**
53          * form a query page for a job. The servlet should no be visible to users at all. 
54          * 
55          * @param model
56          *            MVC model
57          * @return link to the JSP query page
58          */
59         @RequestMapping(value = "/job/query", method = RequestMethod.GET)
60         public String initFindForm(Map<String, Object> model) {
61                 model.put("username", getPrincipalName());
62                 model.put("value", "jp_NzBOJKo");
63                 return "query/JobLog";
64         }
65
66         @RequestMapping(value = "/stat/exectime/results", method = RequestMethod.GET)
67         public String findExecTimeData(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
68                         @RequestParam(value = "option", required = false) String option, Map<String, Object> model) {
69                 model.put("username", getPrincipalName());
70                 final long startTime = System.currentTimeMillis();
71
72                 CassandraRequester sp = new CassandraRequester();
73                 if (option.equals("AllDates,off")) {
74                         Calendar cal = Calendar.getInstance();
75                         date1 = DateFormatYYMMDD(sp.earliestDate());
76                         date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
77                 }
78                 model.put("date1", date1);
79                 model.put("date2", date2);
80                 model.put("option", option);
81                 List<DataBase> res = sp.extractExecutionTime(date1, date2);
82                 model.put("result", res);
83                 model.put("ndays", res.size() - 1);
84                 final long endTime = System.currentTimeMillis();
85                 model.put("timeExecution", (endTime - startTime));
86                 return "/reportTimeExecution";
87         }
88
89         @RequestMapping(value = "/job/results", method = RequestMethod.GET)
90         public String findJob(@RequestParam("IdJob") String jobid, Map<String, Object> model) {
91                 model.put("username", getPrincipalName());
92                 final long startTime = System.currentTimeMillis();
93                 JobReader reader = new JobReader();
94                 model.put("result", reader.readJobLog(jobid));
95                 final long endTime = System.currentTimeMillis();
96                 model.put("timeExecution", (endTime - startTime));
97                 model.put("IdJob", jobid);
98
99                 ArchivedJob aj = new ArchivedJob(jobid);
100                 try {
101                         model.put("jobarchive", aj.prepareJobArchiveToWeb());
102                 } catch (IOException e) {
103                         //TODO. what should we do if job is not available???
104                 }
105                 return "reportJobLog";
106         }
107
108         /*
109          * convert ???
110          */
111         private String DateFormatYYMMDD(long indate) {
112                 SimpleDateFormat datformat = new SimpleDateFormat("yyyy/MM/dd");
113                 String dateString = datformat.format(new Date(indate));
114                 return dateString;
115         }
116
117 }