Centralize initial examples of job id and IP
[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.cassandra.TotalByCounterBean;
12 import compbio.cassandra.UserBean;
13 import compbio.statistic.CassandraRequester;
14
15 /**
16  * @author Alexander Sherstnev
17  * @author Natasha Sherstneva
18  */
19 @Controller
20 public class IPDataController extends BasicController {
21
22         @RequestMapping(value = "/admin/ip/counts/query", method = RequestMethod.GET)
23         public String initStatisticsForm(Map<String, Object> model) {
24                 model.put("username", getPrincipalName());
25                 model.put("value", 5);
26                 return "query/IPStatistics";
27         }
28
29         @RequestMapping(value = "/admin/ip/query", method = RequestMethod.GET)
30         public String initOneIPForm(Map<String, Object> model) {
31                 model.put("username", getPrincipalName());
32                 CassandraRequester cr = new CassandraRequester();
33                 model.put("value", cr.getExample("ip"));
34                 return "query/IP";
35         }
36
37         @RequestMapping(value = "/admin/ip/counts/results", method = RequestMethod.GET)
38         public String findIPwithCounter(@RequestParam("JobCounter") String counter, Map<String, Object> model) {
39                 model.put("username", getPrincipalName());
40                 if (counter.equals("")) {
41                         model.put("error", "The value must not be empty");
42                         model.put("value", counter);
43                         return "query/IPStatistics";
44                 }
45
46                 int realcounter;
47                 try {
48                         realcounter = Integer.parseInt(counter.trim());
49                 } catch (NumberFormatException e) {
50                         model.put("error", "The value must be an integer number");
51                         model.put("value", counter);
52                         return "query/IPStatistics";
53                 }
54
55                 if (realcounter < 1) {
56                         model.put("error", "The value must be greater than 0");
57                         model.put("value", counter);
58                         return "query/IPStatistics";
59                 }
60
61                 final long startTime = System.currentTimeMillis();
62                 CassandraRequester cr = new CassandraRequester();
63                 List<TotalByCounterBean> r = cr.readIpByCounter(realcounter);
64                 model.put("results", r);
65                 model.put("njobs", 0);
66                 if (null != r) {
67                         model.put("njobs", r.size());
68                 }
69                 final long endTime = System.currentTimeMillis();
70                 model.put("timeExecution", (endTime - startTime));
71                 model.put("counter", realcounter);
72                 return "reportIPstatistics";
73         }
74
75         @RequestMapping(value = "/admin/ip/results", method = RequestMethod.GET)
76         public String findIP(@RequestParam("ip") String ip, Map<String, Object> model) {
77                 model.put("username", getPrincipalName());
78                 final long startTime = System.currentTimeMillis();
79                 CassandraRequester cr = new CassandraRequester();
80                 UserBean r = cr.readIp(ip);
81                 model.put("results", r);
82                 model.put("njobs", 0);
83                 if (null != r) {
84                         model.put("njobs", r.getMainInfo().size());
85                 }
86                 final long endTime = System.currentTimeMillis();
87                 model.put("timeExecution", (endTime - startTime));
88                 model.put("ip", ip);
89                 return "reportIP";
90         }
91 }