Add mode comments to the classes
[proteocache.git] / server / compbio / controllers / IPDataController.java
1 package compbio.controllers;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.bind.annotation.RequestMethod;
9 import org.springframework.web.bind.annotation.RequestParam;
10
11 import compbio.beans.TotalByCounterBean;
12 import compbio.beans.UserBean;
13 import compbio.cassandra.readers.CassandraReader;
14 import compbio.cassandra.readers.IpReader;
15 import compbio.cassandra.readers.ReaderByCounter;
16
17 /**
18  * The controller 
19  * 
20  * @author Alexander Sherstnev
21  * @author Natasha Sherstneva
22  * @version 1.0
23  */
24 @Controller
25 public class IPDataController extends BasicController {
26         /**
27          * default minimal jobs to consider a user "heavy user"
28          */
29         private int minimalJobsFromIP = 5;
30         
31         /**
32          * for initial page for request "heavy users"
33          * 
34          */
35         @RequestMapping(value = "/admin/ip/counts/query", method = RequestMethod.GET)
36         public String initStatisticsForm(Map<String, Object> model) {
37                 model.put("username", getPrincipalName());
38                 model.put("value", minimalJobsFromIP);
39                 return "query/IPStatistics";
40         }
41
42         /**
43          * form query page for requesting a single IP
44          * 
45          * @param model - MVC abstract model
46          * @return
47          */
48         @RequestMapping(value = "/admin/ip/query", method = RequestMethod.GET)
49         public String initOneIPForm(Map<String, Object> model) {
50                 model.put("username", getPrincipalName());
51                 CassandraReader cr = new CassandraReader();
52                 model.put("value", cr.getExample("ip"));
53                 return "query/IP";
54         }
55
56         /**
57          * Form output model with statistics on "heavy users"
58          * @param counter - the number of jobs for requested "heavy users"
59          * @param model - abstract MVC model
60          * @return
61          */
62         @RequestMapping(value = "/admin/ip/counts/results", method = RequestMethod.GET)
63         public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map<String, Object> model) {
64                 model.put("username", getPrincipalName());
65                 if (counter.equals("")) {
66                         model.put("error", "The value must not be empty");
67                         model.put("value", counter);
68                         return "query/IPStatistics";
69                 }
70
71                 int realcounter;
72                 try {
73                         realcounter = Integer.parseInt(counter.trim());
74                 } catch (NumberFormatException e) {
75                         model.put("error", "The value must be an integer number");
76                         model.put("value", counter);
77                         return "query/IPStatistics";
78                 }
79
80                 if (realcounter < 1) {
81                         model.put("error", "The value must be greater than 0");
82                         model.put("value", counter);
83                         return "query/IPStatistics";
84                 }
85
86                 final long startTime = System.currentTimeMillis();
87                 ReaderByCounter reader = new ReaderByCounter();
88                 List<TotalByCounterBean> r = reader.readIpByCounter(realcounter);
89                 model.put("results", r);
90                 model.put("njobs", 0);
91                 String csvline = "";
92                 if (null != r) {
93                         model.put("njobs", r.size());
94                         csvline = "\'Job%20 count\', \'IP\'%0A";
95                 }
96                 // form line for CSV file
97                 for (TotalByCounterBean b : r) {
98                         csvline += "\'" + b.getTotaljobs() + "\',\'" + b.getName() + "\'%0A";
99                 }
100                 model.put("csvfile", csvline);
101                 final long endTime = System.currentTimeMillis();
102                 model.put("timeExecution", (endTime - startTime));
103                 model.put("counter", realcounter);
104                 return "reports/IPstatistics";
105         }
106
107         @RequestMapping(value = "/admin/ip/results", method = RequestMethod.GET)
108         public String findIP(@RequestParam("ip") String ip, Map<String, Object> model) {
109                 model.put("username", getPrincipalName());
110                 final long startTime = System.currentTimeMillis();
111                 IpReader reader = new IpReader();
112                 UserBean r = reader.readIp(ip);
113                 model.put("results", r);
114                 model.put("njobs", 0);
115                 if (null != r) {
116                         model.put("njobs", r.getMainInfo().size());
117                 }
118                 final long endTime = System.currentTimeMillis();
119                 model.put("timeExecution", (endTime - startTime));
120                 model.put("ip", ip);
121                 return "reports/IP";
122         }
123 }