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