Fix problem with different date formats
[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                 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                 List<DataBase> r = cr.readJobByDay(realdate);
82                 model.put("results", r);
83                 model.put("njobs", r.size());
84                 model.put("date", realdate);
85                 final long endTime = System.currentTimeMillis();
86                 model.put("timeExecution", (endTime - startTime));
87                 return "/reportJobStatisticsOneDay";
88         }
89 }