Edit comments
[proteocache.git] / server / compbio / controllers / DailyStatisticsController.java
index b735107..322f74e 100644 (file)
@@ -1,10 +1,8 @@
 package compbio.controllers;
 
 import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
-import java.util.List;
 import java.util.Map;
 
 import org.springframework.stereotype.Controller;
@@ -12,62 +10,137 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import compbio.cassandra.DataBase;
-import compbio.cassandra.DateBean;
-import compbio.cassandra.TotalJobsStatisticBean;
-import compbio.statistic.CassandraRequester;
-import compbio.statistic.StatisticsProt;
+import compbio.engine.JobStatus;
+import compbio.beans.DateBean;
+import compbio.beans.TotalJobsStatisticBean;
+import compbio.cassandra.DateFormatter;
+
+import compbio.beans.Total;
+import compbio.cassandra.readers.CassandraReader;
+import compbio.cassandra.readers.DailyStatisticsReader;
 
 /**
  * @author Alexander Sherstnev
  * @author Natasha Sherstneva
+ * @version 1.0 Dec 2013
  */
 @Controller
-public class DailyStatisticsController {
+public class DailyStatisticsController extends BasicController {
 
-       @RequestMapping(value = "/stat", method = RequestMethod.GET)
+       /**
+        * form a query page for daily job statistics.
+        * 
+        * The servlet should be available for users and admins only. By defaults
+        * the report time range is from the earliest day with jobs to current date
+        * ("Query for all dates" is ticked). If the user removes the tick the time
+        * range is from today - 3 days to today. Currently, the input model is
+        * empty.
+        * 
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
+       @RequestMapping(value = "/stat/jobs/query", method = RequestMethod.GET)
        public String initFindForm(Map<String, Object> model) {
-               Calendar cal = Calendar.getInstance();
-               String date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
-               cal.add(Calendar.DATE, -3);
-               String date1 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
-
+               model.put("username", getPrincipalName());
+               Calendar loccal = Calendar.getInstance();
+               String date2 = loccal.get(Calendar.YEAR) + "/" + (loccal.get(Calendar.MONTH) + 1) + "/" + loccal.get(Calendar.DATE);
+               loccal.add(Calendar.DATE, -3);
+               String date1 = loccal.get(Calendar.YEAR) + "/" + (loccal.get(Calendar.MONTH) + 1) + "/" + loccal.get(Calendar.DATE);
                model.put("date1", date1);
                model.put("date2", date2);
-               
-               return "queryJobStatistics";
+               return "query/JobStatistics";
        }
 
-       @RequestMapping(value = "/stat/query", method = RequestMethod.GET)
+       /**
+        * form a report page for daily job statistics.
+        * 
+        * @param model
+        *            MVC model object
+        * @param date1
+        *            initial date for the report (if option is set, date2 = the
+        *            earliest date with jobs in DB)
+        * @param date2
+        *            the final date for the report (if option is set, date2 =
+        *            today)
+        * @param option
+        *            defined whether the whole time range of jobs is reported (null
+        *            means date1 and date2 are used)
+        * @return link to the report JSP page
+        */
+       @RequestMapping(value = "/stat/jobsdaily/results", method = RequestMethod.GET)
        public String findJobsInPeriod(@RequestParam("date1") String date1, @RequestParam("date2") String date2,
                        @RequestParam("option") String option, Map<String, Object> model) {
+               model.put("username", getPrincipalName());
+               Calendar loccal = Calendar.getInstance();
                final long startTime = System.currentTimeMillis();
-
-               CassandraRequester cr = new CassandraRequester();
+               DailyStatisticsReader reader = new DailyStatisticsReader();
                if (option.equals("AllDates,off")) {
-                       Calendar cal = Calendar.getInstance();
-                       date1 = StatisticsProt.DateFormatYYMMDD(cr.earliestDate());
-                       date2 = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH);
+                       date1 = theEaerlistDate;
+                       date2 = theCurrentDate;
                }
+
+               // dates in string format
+               String trimmeddate1 = date1.replaceAll("\\s", "");
+               String trimmeddate2 = date2.replaceAll("\\s", "");
+               // dates in long format
+               long longDate1 = DateFormatter.DateParsing(date1, formaterYYMMDD);
+               long longDate2 = DateFormatter.DateParsing(date2, formaterYYMMDD);
+               String error = DateChecking(trimmeddate1, trimmeddate2, longDate1, longDate2);
+               if (error != null) {
+                       model.put("error", error);
+                       model.put("date1", date1);
+                       model.put("date2", date2);
+                       return "query/JobStatistics";
+               }
+
+               if (longDate1 < CassandraReader.earliestDate())
+                       longDate1 = CassandraReader.earliestDate();
+               if (longDate2 > loccal.getTimeInMillis())
+                       longDate2 = loccal.getTimeInMillis();
+
+               date1 = DateFormatter.DateLongToString(longDate1, formaterYYMMDD);
+               date2 = DateFormatter.DateLongToString(longDate2, formaterYYMMDD);
                model.put("date1", date1);
                model.put("date2", date2);
-               TotalJobsStatisticBean res = cr.countJobs(date1, date2);
+               TotalJobsStatisticBean res = reader.query(longDate1, longDate2);
                model.put("result", res);
+               Map<String, Total> results = res.getDateTotal();
+               String csvline = "\'Date\',\'Total\',\'OK\',\'Stopped\',\'Error\',\'Timeout\'%0A";
+               for (Map.Entry<String, Total> entry : results.entrySet()) {
+                       csvline += "\'" + entry.getKey() + "\',\'" + entry.getValue().getTotal() + "\',\'" + entry.getValue().getTotalOK() + "\',\'"
+                                       + entry.getValue().getTotalStopped() + "\',\'" + entry.getValue().getTotalError() + "\',\'"
+                                       + entry.getValue().getTotalTimeOut() + "\'%0A";
+               }
+               model.put("csvfile", csvline);
+               model.put("ndays", res.getDateTotal().size());
                final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
                model.put("option", option);
-               return "/reportJobStatistics";
+               return "reports/JobStatistics";
        }
-       
-       @RequestMapping(value = "/stat/oneday", method = RequestMethod.GET)
-       public String findJobsInOneDay(@RequestParam("date") String date, Map<String, Object> model) throws ParseException {
+
+       /**
+        * form a report page for job statistics for one day only.
+        * 
+        * @param model
+        *            MVC model object
+        * @param date
+        *            date for the report
+        * @param status
+        * 
+        * @return link to the report JSP page
+        */
+       @RequestMapping(value = "/stat/jobsoneday/results", method = RequestMethod.GET)
+       public String findJobsInOneDay(@RequestParam("date") String date, @RequestParam("status") String status, Map<String, Object> model)
+                       throws ParseException {
+               model.put("username", getPrincipalName());
                final long startTime = System.currentTimeMillis();
 
                String realdate;
-               final SimpleDateFormat formaterDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
-               final SimpleDateFormat formaterYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
+               long thetime = 0;
                try {
-                       long thetime = formaterYYMMDD.parse(date).getTime();
+                       thetime = formaterYYMMDD.parse(date).getTime();
                        if (thetime < 0) {
                                realdate = date;
                        } else {
@@ -75,17 +148,23 @@ public class DailyStatisticsController {
                        }
                } catch (ParseException e) {
                        realdate = date;
+                       thetime = formaterDDMMYY.parse(realdate).getTime();
+               }
+
+               if (null == JobStatus.getJobStatus(status)) {
+                       return "support/Notimplemented";
                }
 
-               CassandraRequester cr = new CassandraRequester();
+               DailyStatisticsReader reader = new DailyStatisticsReader();
                // IMPORTANT: input should be suppied in the format: DD/MM/YYYY
-               DateBean r = cr.readJobByDay(realdate);
+               DateBean r = reader.readJobByDay(thetime, realdate, JobStatus.getJobStatus(status));
                model.put("results", r);
                if (r != null)
                        model.put("njobs", r.getJobidAndSeq().size());
                model.put("date", realdate);
+               model.put("status", status);
                final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
-               return "/reportJobStatisticsOneDay";
+               return "reports/JobStatisticsOneDay";
        }
 }