Further work on statistics display
[jabaws.git] / webservices / compbio / stat / servlet / util / StatCollection.java
1 package compbio.stat.servlet.util;\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.Map;\r
9 import java.util.TreeMap;\r
10 \r
11 import compbio.stat.collector.StatDB;\r
12 import compbio.stat.collector.StatProcessor;\r
13 import compbio.ws.client.Services;\r
14 \r
15 public class StatCollection {\r
16 \r
17         /**\r
18          * Total number of requests\r
19          * \r
20          * incomplete abandoned cancelled\r
21          * \r
22          * @author pvtroshin\r
23          * \r
24          */\r
25 \r
26         public enum Stattype {\r
27                 CLUSTER, LOCAL, ALL\r
28         }\r
29 \r
30         private Map<Services, StatProcessor> allStat;\r
31         private Map<Services, StatProcessor> clusterStat;\r
32         private Map<Services, StatProcessor> localStat;\r
33 \r
34         public Map<Services, StatProcessor> getAllStat() {\r
35                 return allStat;\r
36         }\r
37         public Map<Services, StatProcessor> getClusterStat() {\r
38                 return clusterStat;\r
39         }\r
40         public Map<Services, StatProcessor> getLocalStat() {\r
41                 return localStat;\r
42         }\r
43 \r
44         public static Map<Date, StatCollection> getStats(int monthsToReport)\r
45                         throws SQLException {\r
46                 Calendar fromCal = GregorianCalendar.getInstance();\r
47                 fromCal.add(Calendar.MONTH, -monthsToReport);\r
48                 return getStats(fromCal.getTime());\r
49         }\r
50 \r
51         public static Map<Date, StatCollection> getStats(Date fromDate)\r
52                         throws SQLException {\r
53                 Map<Date, StatCollection> allstats = new TreeMap<Date, StatCollection>();\r
54 \r
55                 Calendar fromCal = GregorianCalendar.getInstance();\r
56                 fromCal.setTime(fromDate);\r
57                 fromCal.set(Calendar.DAY_OF_MONTH, 1);\r
58 \r
59                 Calendar toCal = GregorianCalendar.getInstance();\r
60                 toCal.setTime(new Date());\r
61 \r
62                 if (fromCal.after(toCal)) {\r
63                         throw new AssertionError("From Date must be before ToDate! ");\r
64                 }\r
65 \r
66                 while (true) {\r
67                         Date from = fromCal.getTime();\r
68                         fromCal.add(Calendar.MONTH, 1);\r
69                         if (toCal.before(fromCal)) {\r
70                                 allstats.put(toCal.getTime(),\r
71                                                 StatCollection.newStatCollecton(from, toCal.getTime()));\r
72                                 break;\r
73                         }\r
74                         // System.out.println("!" + from + " !!! " + fromCal.getTime());\r
75                         allstats.put(from,\r
76                                         StatCollection.newStatCollecton(from, fromCal.getTime()));\r
77                 }\r
78                 return allstats;\r
79         }\r
80         public static Map<Date, Totals> getTotalStats(\r
81                         Map<Date, StatCollection> detailedStats) {\r
82                 Map<Date, Totals> totals = new TreeMap<Date, Totals>();\r
83                 for (Map.Entry<Date, StatCollection> stat : detailedStats.entrySet()) {\r
84                         totals.put(stat.getKey(), Totals.sumStats(stat.getValue().allStat));\r
85                 }\r
86                 return totals;\r
87         }\r
88 \r
89         static StatCollection newStatCollecton(Date startDate, Date endDate)\r
90                         throws SQLException {\r
91 \r
92                 Timestamp startStamp = new Timestamp(startDate.getTime());\r
93                 Timestamp stopStamp = new Timestamp(endDate.getTime());\r
94                 StatCollection collection = new StatCollection();\r
95                 StatDB statdb = new StatDB();\r
96 \r
97                 // Total\r
98                 collection.allStat = new TreeMap<Services, StatProcessor>();\r
99                 for (Services service : Services.values()) {\r
100                         collection.allStat.put(\r
101                                         service,\r
102                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
103                                                         service, null)));\r
104                 }\r
105 \r
106                 // Cluster\r
107                 collection.clusterStat = new TreeMap<Services, StatProcessor>();\r
108                 for (Services service : Services.values()) {\r
109                         collection.clusterStat.put(\r
110                                         service,\r
111                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
112                                                         service, true)));\r
113                 }\r
114 \r
115                 // Local\r
116                 collection.localStat = new TreeMap<Services, StatProcessor>();\r
117                 for (Services service : Services.values()) {\r
118                         collection.localStat.put(\r
119                                         service,\r
120                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
121                                                         service, false)));\r
122                 }\r
123                 return collection;\r
124         }\r
125         @Override\r
126         public int hashCode() {\r
127                 final int prime = 31;\r
128                 int result = 1;\r
129                 result = prime * result + ((allStat == null) ? 0 : allStat.hashCode());\r
130                 result = prime * result\r
131                                 + ((clusterStat == null) ? 0 : clusterStat.hashCode());\r
132                 result = prime * result\r
133                                 + ((localStat == null) ? 0 : localStat.hashCode());\r
134                 return result;\r
135         }\r
136         @Override\r
137         public boolean equals(Object obj) {\r
138                 if (this == obj)\r
139                         return true;\r
140                 if (obj == null)\r
141                         return false;\r
142                 if (getClass() != obj.getClass())\r
143                         return false;\r
144                 StatCollection other = (StatCollection) obj;\r
145                 if (allStat == null) {\r
146                         if (other.allStat != null)\r
147                                 return false;\r
148                 } else if (!allStat.equals(other.allStat))\r
149                         return false;\r
150                 if (clusterStat == null) {\r
151                         if (other.clusterStat != null)\r
152                                 return false;\r
153                 } else if (!clusterStat.equals(other.clusterStat))\r
154                         return false;\r
155                 if (localStat == null) {\r
156                         if (other.localStat != null)\r
157                                 return false;\r
158                 } else if (!localStat.equals(other.localStat))\r
159                         return false;\r
160                 return true;\r
161         }\r
162         @Override\r
163         public String toString() {\r
164                 String value = "";\r
165                 for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {\r
166                         value += entry.getKey() + ": ";\r
167                         value += entry.getValue() + "\n";\r
168                 }\r
169                 for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {\r
170                         value += entry.getKey() + ": ";\r
171                         value += entry.getValue() + "\n";\r
172                 }\r
173                 for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {\r
174                         value += entry.getKey() + ": ";\r
175                         value += entry.getValue() + "\n";\r
176                 }\r
177                 return value;\r
178         }\r
179 \r
180 }\r