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