Add mode comments to the classes
[proteocache.git] / server / compbio / controllers / IPDataController.java
index 7a7e298..a9d25b9 100644 (file)
@@ -8,79 +8,116 @@ 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.statistic.CassandraRequester;
+import compbio.beans.TotalByCounterBean;
+import compbio.beans.UserBean;
+import compbio.cassandra.readers.CassandraReader;
+import compbio.cassandra.readers.IpReader;
+import compbio.cassandra.readers.ReaderByCounter;
 
 /**
+ * The controller 
+ * 
  * @author Alexander Sherstnev
  * @author Natasha Sherstneva
+ * @version 1.0
  */
 @Controller
-public class IPDataController {
-
-       @RequestMapping(value = "/ip/stat", method = RequestMethod.GET)
+public class IPDataController extends BasicController {
+       /**
+        * default minimal jobs to consider a user "heavy user"
+        */
+       private int minimalJobsFromIP = 5;
+       
+       /**
+        * for initial page for request "heavy users"
+        * 
+        */
+       @RequestMapping(value = "/admin/ip/counts/query", method = RequestMethod.GET)
        public String initStatisticsForm(Map<String, Object> model) {
-               model.put("value", 5);
-               return "queryIPStatistics";
+               model.put("username", getPrincipalName());
+               model.put("value", minimalJobsFromIP);
+               return "query/IPStatistics";
        }
 
-       @RequestMapping(value = "/ip", method = RequestMethod.GET)
+       /**
+        * form query page for requesting a single IP
+        * 
+        * @param model - MVC abstract model
+        * @return
+        */
+       @RequestMapping(value = "/admin/ip/query", method = RequestMethod.GET)
        public String initOneIPForm(Map<String, Object> model) {
-               model.put("value", "127.0.0.1");
-               return "queryIP";
+               model.put("username", getPrincipalName());
+               CassandraReader cr = new CassandraReader();
+               model.put("value", cr.getExample("ip"));
+               return "query/IP";
        }
-       
-       @RequestMapping(value = "/ip/stat/querycounter", method = RequestMethod.GET)
-       public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map<String, Object> model) {
 
+       /**
+        * Form output model with statistics on "heavy users"
+        * @param counter - the number of jobs for requested "heavy users"
+        * @param model - abstract MVC model
+        * @return
+        */
+       @RequestMapping(value = "/admin/ip/counts/results", method = RequestMethod.GET)
+       public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map<String, Object> model) {
+               model.put("username", getPrincipalName());
                if (counter.equals("")) {
                        model.put("error", "The value must not be empty");
                        model.put("value", counter);
-                       return "queryIPStatistics";
+                       return "query/IPStatistics";
                }
-       
+
                int realcounter;
                try {
                        realcounter = Integer.parseInt(counter.trim());
                } catch (NumberFormatException e) {
                        model.put("error", "The value must be an integer number");
                        model.put("value", counter);
-                       return "queryIPStatistics";
+                       return "query/IPStatistics";
                }
 
                if (realcounter < 1) {
                        model.put("error", "The value must be greater than 0");
                        model.put("value", counter);
-                       return "queryIPStatistics";
+                       return "query/IPStatistics";
                }
 
                final long startTime = System.currentTimeMillis();
-               CassandraRequester cr = new CassandraRequester();
-               List<DataBase> r = cr.readIpByCounter(realcounter);
-               model.put("results", r);
+               ReaderByCounter reader = new ReaderByCounter();
+               List<TotalByCounterBean> r = reader.readIpByCounter(realcounter);
+               model.put("results", r);
                model.put("njobs", 0);
+               String csvline = "";
                if (null != r) {
                        model.put("njobs", r.size());
+                       csvline = "\'Job%20 count\', \'IP\'%0A";
                }
-               final long endTime = System.currentTimeMillis();
+               // form line for CSV file
+               for (TotalByCounterBean b : r) {
+                       csvline += "\'" + b.getTotaljobs() + "\',\'" + b.getName() + "\'%0A";
+               }
+               model.put("csvfile", csvline);
+               final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
                model.put("counter", realcounter);
-               return "reportIPstatistics";
+               return "reports/IPstatistics";
        }
 
-       @RequestMapping(value = "/ip/query", method = RequestMethod.GET)
+       @RequestMapping(value = "/admin/ip/results", method = RequestMethod.GET)
        public String findIP(@RequestParam("ip") String ip, Map<String, Object> model) {
+               model.put("username", getPrincipalName());
                final long startTime = System.currentTimeMillis();
-               CassandraRequester cr = new CassandraRequester();
-               List<DataBase> r = cr.readIp(ip);
-               model.put("results", r);
+               IpReader reader = new IpReader();
+               UserBean r = reader.readIp(ip);
+               model.put("results", r);
                model.put("njobs", 0);
                if (null != r) {
-                       model.put("njobs", r.size());
+                       model.put("njobs", r.getMainInfo().size());
                }
-               final long endTime = System.currentTimeMillis();
+               final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
                model.put("ip", ip);
-               return "reportIP";
+               return "reports/IP";
        }
 }