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