Adding JABA web services usage statistics web application. Stat database is to follow
[jabaws.git] / webservices / compbio / stat / collector / StatManager.java
1 package compbio.stat.collector;\r
2 \r
3 import java.sql.SQLException;\r
4 import java.sql.Timestamp;\r
5 import java.util.Calendar;\r
6 import java.util.Date;\r
7 import java.util.GregorianCalendar;\r
8 import java.util.Iterator;\r
9 import java.util.Map;\r
10 import java.util.TreeMap;\r
11 \r
12 import compbio.ws.client.Services;\r
13 \r
14 public class StatManager {\r
15 \r
16         static class DateRoller implements Iterator<Date> {\r
17                 final Date initDate;\r
18                 final Calendar calendar;\r
19 \r
20                 public DateRoller(Date date) {\r
21                         this.initDate = date;\r
22                         calendar = GregorianCalendar.getInstance();\r
23                         calendar.setTime(date);\r
24                         calendar.add(Calendar.MONTH, -12);\r
25                 }\r
26 \r
27                 Date getCurrentDate() {\r
28                         return initDate;\r
29                 }\r
30 \r
31                 @Override\r
32                 public boolean hasNext() {\r
33                         return !calendar.getTime().equals(initDate);\r
34                 }\r
35 \r
36                 @Override\r
37                 public Date next() {\r
38                         calendar.add(Calendar.MONTH, 1);\r
39                         return calendar.getTime();\r
40                 }\r
41 \r
42                 @Override\r
43                 public void remove() {\r
44                         throw new UnsupportedOperationException();\r
45                 }\r
46 \r
47         }\r
48 \r
49         void getStats() throws SQLException {\r
50                 Calendar startTime = Calendar.getInstance();\r
51                 startTime.roll(Calendar.YEAR, false);\r
52                 Timestamp startDate = new Timestamp(startTime.getTimeInMillis());\r
53                 Timestamp stopDate = new Timestamp(new Date().getTime());\r
54                 StatDB statdb = null;\r
55 \r
56                 statdb = new StatDB();\r
57 \r
58                 // Total\r
59                 Map<Services, StatProcessor> stats = new TreeMap<Services, StatProcessor>();\r
60                 for (Services service : Services.values()) {\r
61                         stats.put(\r
62                                         service,\r
63                                         new StatProcessor(statdb.readData(startDate, stopDate,\r
64                                                         service, null)));\r
65                 }\r
66 \r
67                 // Cluster\r
68                 Map<Services, StatProcessor> statsCluster = new TreeMap<Services, StatProcessor>();\r
69                 for (Services service : Services.values()) {\r
70                         statsCluster.put(\r
71                                         service,\r
72                                         new StatProcessor(statdb.readData(startDate, stopDate,\r
73                                                         service, true)));\r
74                 }\r
75                 // Local\r
76                 Map<Services, StatProcessor> statsLocal = new TreeMap<Services, StatProcessor>();\r
77                 for (Services service : Services.values()) {\r
78                         statsLocal.put(\r
79                                         service,\r
80                                         new StatProcessor(statdb.readData(startDate, stopDate,\r
81                                                         service, false)));\r
82                 }\r
83 \r
84         }\r
85 }\r