Add simple statistics to the project home page
[proteocache.git] / server / compbio / controllers / MainController.java
1 package compbio.controllers;
2
3 import java.util.Calendar;
4
5 import org.springframework.stereotype.Controller;
6 import org.springframework.ui.ModelMap;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestMethod;
9
10 import compbio.beans.Total;
11 import compbio.beans.TotalJobsStatisticBean;
12 import compbio.cassandra.DateFormatter;
13 import compbio.cassandra.readers.DailyStatisticsReader;
14
15 /**
16  * The main system controller with most general functionality (home page, login
17  * and logout pages)
18  * 
19  * @author Alexander Sherstnev
20  * @author Natasha Sherstneva
21  * 
22  * @version 1.0
23  * @since Dec 2013
24  */
25 @Controller
26 public class MainController extends BasicController {
27
28         /**
29          * Form the project home page
30          * 
31          * @param model
32          *            MVC model object
33          * 
34          * @return JSP template for the page
35          */
36         @RequestMapping(value = "/index", method = RequestMethod.GET)
37         public String printPublicHome(ModelMap model) {
38                 model.addAttribute("username", getPrincipalName());
39                 model = getStatistics(model);
40                 if (isUserRole())
41                         return "home";
42                 return "public";
43         }
44
45         /**
46          * Form login page
47          * 
48          * @param model
49          *            MVC model object
50          * 
51          * @return JSP template for the page
52          */
53         @RequestMapping(value = "/login", method = RequestMethod.GET)
54         public String login(ModelMap model) {
55                 return "login";
56         }
57
58         /**
59          * Form a page with failed login
60          * 
61          * @param model
62          *            MVC model object
63          * 
64          * @return JSP template for the page
65          */
66         @RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
67         public String loginerror(ModelMap model) {
68                 model.addAttribute("error", "Wrong user name or password");
69                 return "login";
70         }
71
72         /**
73          * Form a page after log out
74          * 
75          * @param model
76          *            MVC model object
77          * 
78          * @return JSP template for the page
79          */
80         @RequestMapping(value = "/logout", method = RequestMethod.GET)
81         public String logout(ModelMap model) {
82                 model = getStatistics(model);
83                 return "public";
84         }
85
86         /**
87          * Form a page with denial of service
88          * 
89          * @param model
90          *            MVC model object
91          * 
92          * @return JSP template for the page
93          */
94         @RequestMapping(value = "/denied", method = RequestMethod.GET)
95         public String denied(ModelMap model) {
96                 model.put("username", getPrincipalName());
97                 return "support/Denied";
98         }
99
100         /**
101          * Form the project home page
102          * 
103          * @param model
104          *            MVC model object
105          * 
106          * @return JSP template for the page
107          */
108         @RequestMapping(value = "/home", method = RequestMethod.GET)
109         public String printHome(ModelMap model) {
110                 model.addAttribute("username", getPrincipalName());
111                 model = getStatistics(model);
112                 return "home";
113         }
114
115         /**
116          * collect job execution statiustics for the last 7 days.
117          * 
118          * @param model
119          *            MVC model object
120          * 
121          * @return the the same model, but with additional parameters with the job
122          *         statistics
123          */
124         private ModelMap getStatistics(ModelMap model) {
125                 int ndays = 7;
126                 model.addAttribute("ndays", ndays);
127
128                 Calendar cal = Calendar.getInstance();
129                 String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
130                 long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
131                 cal.add(Calendar.DATE, -ndays);
132                 String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
133                 long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
134
135                 DailyStatisticsReader reader = new DailyStatisticsReader();
136                 TotalJobsStatisticBean res = reader.query(longDate1, longDate2);
137                 Total total = res.getWholeTotal();
138                 model.addAttribute("total", total.getTotal());
139                 model.addAttribute("totalOK", total.getTotalOK());
140                 model.addAttribute("totalTimeout", total.getTotalOK());
141                 model.addAttribute("totalError", total.getTotalError());
142                 model.addAttribute("totalStopped", total.getTotalStopped());
143                 return model;
144         }
145
146 }