Add basic security jars to the project
[proteocache.git] / server / compbio / controllers / DailyStatisticsController.java
1 package compbio.controllers;
2
3 import java.text.ParseException;
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
16 import compbio.cassandra.DataBase;
17 import compbio.statistic.CassandraRequester;
18 import compbio.statistic.StatisticsProt;
19
20 /**
21  * @author Alexander Sherstnev
22  * @author Natasha Sherstneva
23  */
24 @Controller
25 public class DailyStatisticsController {
26
27         @RequestMapping(value = "/stat", method = RequestMethod.GET)
28         public String initFindForm(Map<String, Object> model) {
29                 Calendar cal = Calendar.getInstance();
30                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
31                 cal.add(Calendar.DATE, -3);
32                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
33
34                 model.put("date1", date1);
35                 model.put("date2", date2);
36                 
37                 return "queryJobStatistics";
38         }
39
40         @RequestMapping(value = "/stat/query", method = RequestMethod.GET)
41         public String findJobsInPeriod(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
42                         @RequestParam("option") String option, Map<String, Object> model) {
43                 final long startTime = System.currentTimeMillis();
44
45                 CassandraRequester cr = new CassandraRequester();
46                 if (option.equals("AllDates,off")) {
47                         Calendar cal = Calendar.getInstance();
48                         date1 = StatisticsProt.DateFormatYYMMDD(cr.earliestDate());
49                         date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
50                 }
51                 model.put("date1", date1);
52                 model.put("date2", date2);
53                 List<DataBase> res = cr.countJobs(date1, date2);
54                 model.put("result", res);
55                 model.put("ndays", res.size());
56                 final long endTime = System.currentTimeMillis();
57                 model.put("timeExecution", (endTime - startTime));
58                 model.put("option", option);
59                 return "/reportJobStatistics";
60         }
61         
62         @RequestMapping(value = "/stat/oneday", method = RequestMethod.GET)
63         public String findJobsInOneDay(@RequestParam("date") String date, Map<String, Object> model) throws ParseException {
64                 final long startTime = System.currentTimeMillis();
65
66                 String realdate;
67                 final SimpleDateFormat formaterDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
68                 final SimpleDateFormat formaterYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
69                 try {
70                         long thetime = formaterYYMMDD.parse(date).getTime();
71                         if (thetime < 0) {
72                                 realdate = date;
73                         } else {
74                                 realdate = formaterDDMMYY.format(new Date(thetime));
75                         }
76                 } catch (ParseException e) {
77                         realdate = date;
78                 }
79
80                 CassandraRequester cr = new CassandraRequester();
81                 // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
82                 List<DataBase> r = cr.readJobByDay(realdate);
83                 model.put("results", r);
84                 model.put("njobs", r.size());
85                 model.put("date", realdate);
86                 final long endTime = System.currentTimeMillis();
87                 model.put("timeExecution", (endTime - startTime));
88                 return "/reportJobStatisticsOneDay";
89         }
90 }