Add simple statistics to the project home page
[proteocache.git] / server / compbio / controllers / MainController.java
index d555d36..d9e0cf0 100644 (file)
 package compbio.controllers;
 
+import java.util.Calendar;
+
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.ModelMap;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
+import compbio.beans.Total;
+import compbio.beans.TotalJobsStatisticBean;
+import compbio.cassandra.DateFormatter;
+import compbio.cassandra.readers.DailyStatisticsReader;
+
+/**
+ * The main system controller with most general functionality (home page, login
+ * and logout pages)
+ * 
+ * @author Alexander Sherstnev
+ * @author Natasha Sherstneva
+ * 
+ * @version 1.0
+ * @since Dec 2013
+ */
 @Controller
 public class MainController extends BasicController {
 
+       /**
+        * Form the project home page
+        * 
+        * @param model
+        *            MVC model object
+        * 
+        * @return JSP template for the page
+        */
        @RequestMapping(value = "/index", method = RequestMethod.GET)
        public String printPublicHome(ModelMap model) {
                model.addAttribute("username", getPrincipalName());
+               model = getStatistics(model);
                if (isUserRole())
                        return "home";
                return "public";
        }
 
+       /**
+        * Form login page
+        * 
+        * @param model
+        *            MVC model object
+        * 
+        * @return JSP template for the page
+        */
        @RequestMapping(value = "/login", method = RequestMethod.GET)
        public String login(ModelMap model) {
                return "login";
        }
 
+       /**
+        * Form a page with failed login
+        * 
+        * @param model
+        *            MVC model object
+        * 
+        * @return JSP template for the page
+        */
        @RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
        public String loginerror(ModelMap model) {
                model.addAttribute("error", "Wrong user name or password");
                return "login";
        }
 
+       /**
+        * Form a page after log out
+        * 
+        * @param model
+        *            MVC model object
+        * 
+        * @return JSP template for the page
+        */
        @RequestMapping(value = "/logout", method = RequestMethod.GET)
        public String logout(ModelMap model) {
+               model = getStatistics(model);
                return "public";
        }
 
+       /**
+        * Form a page with denial of service
+        * 
+        * @param model
+        *            MVC model object
+        * 
+        * @return JSP template for the page
+        */
        @RequestMapping(value = "/denied", method = RequestMethod.GET)
        public String denied(ModelMap model) {
                model.put("username", getPrincipalName());
                return "support/Denied";
        }
 
+       /**
+        * Form the project home page
+        * 
+        * @param model
+        *            MVC model object
+        * 
+        * @return JSP template for the page
+        */
        @RequestMapping(value = "/home", method = RequestMethod.GET)
        public String printHome(ModelMap model) {
                model.addAttribute("username", getPrincipalName());
+               model = getStatistics(model);
                return "home";
        }
 
+       /**
+        * collect job execution statiustics for the last 7 days.
+        * 
+        * @param model
+        *            MVC model object
+        * 
+        * @return the the same model, but with additional parameters with the job
+        *         statistics
+        */
+       private ModelMap getStatistics(ModelMap model) {
+               int ndays = 7;
+               model.addAttribute("ndays", ndays);
+
+               Calendar cal = Calendar.getInstance();
+               String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
+               long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
+               cal.add(Calendar.DATE, -ndays);
+               String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
+               long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
+
+               DailyStatisticsReader reader = new DailyStatisticsReader();
+               TotalJobsStatisticBean res = reader.query(longDate1, longDate2);
+               Total total = res.getWholeTotal();
+               model.addAttribute("total", total.getTotal());
+               model.addAttribute("totalOK", total.getTotalOK());
+               model.addAttribute("totalTimeout", total.getTotalOK());
+               model.addAttribute("totalError", total.getTotalError());
+               model.addAttribute("totalStopped", total.getTotalStopped());
+               return model;
+       }
+
 }