0409e2c353672040e95534332e2d58a3605dd24a
[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, Totals> 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, Totals> getStats(Date fromDate) throws SQLException {\r
52                 Map<Date, Totals> allstats = new TreeMap<Date, Totals>();\r
53 \r
54                 Calendar fromCal = GregorianCalendar.getInstance();\r
55                 fromCal.setTime(fromDate);\r
56                 fromCal.set(Calendar.DAY_OF_MONTH, 1);\r
57 \r
58                 Calendar toCal = GregorianCalendar.getInstance();\r
59                 toCal.setTime(new Date());\r
60                 if (fromCal.after(toCal)) {\r
61                         throw new AssertionError("From Date must be before ToDate! ");\r
62                 }\r
63                 while (fromCal.before(toCal)) {\r
64                         Date from = fromCal.getTime();\r
65                         fromCal.add(Calendar.MONTH, 1);\r
66                         allstats.put(from, getJobCounts(from, fromCal.getTime()));\r
67                 }\r
68                 return allstats;\r
69         }\r
70 \r
71         private static Totals getJobCounts(Date from, Date to) throws SQLException {\r
72                 StatDB db = new StatDB();\r
73                 Totals t = new Totals();\r
74                 Timestamp fromTime = new Timestamp(from.getTime());\r
75                 Timestamp toTime = new Timestamp(to.getTime());\r
76                 t.total = db.getTotalJobsCount(fromTime, toTime);\r
77                 t.incomplete = db.getIncompleteCount(fromTime, toTime);\r
78                 t.abandoned = db.getAbandonedCount(fromTime, toTime);\r
79                 t.cancelled = db.getCancelledCount(fromTime, toTime);\r
80                 return t;\r
81         }\r
82 \r
83         public static StatCollection newStatCollecton(Date startDate, Date endDate)\r
84                         throws SQLException {\r
85 \r
86                 Timestamp startStamp = new Timestamp(startDate.getTime());\r
87                 Timestamp stopStamp = new Timestamp(endDate.getTime());\r
88                 StatCollection collection = new StatCollection();\r
89                 StatDB statdb = new StatDB();\r
90 \r
91                 // Total\r
92                 collection.allStat = new TreeMap<Services, StatProcessor>();\r
93                 for (Services service : Services.values()) {\r
94                         collection.allStat.put(\r
95                                         service,\r
96                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
97                                                         service, null)));\r
98                 }\r
99 \r
100                 // Cluster\r
101                 collection.clusterStat = new TreeMap<Services, StatProcessor>();\r
102                 for (Services service : Services.values()) {\r
103                         collection.clusterStat.put(\r
104                                         service,\r
105                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
106                                                         service, true)));\r
107                 }\r
108 \r
109                 // Local\r
110                 collection.localStat = new TreeMap<Services, StatProcessor>();\r
111                 for (Services service : Services.values()) {\r
112                         collection.localStat.put(\r
113                                         service,\r
114                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
115                                                         service, false)));\r
116                 }\r
117                 return collection;\r
118         }\r
119         @Override\r
120         public int hashCode() {\r
121                 final int prime = 31;\r
122                 int result = 1;\r
123                 result = prime * result + ((allStat == null) ? 0 : allStat.hashCode());\r
124                 result = prime * result\r
125                                 + ((clusterStat == null) ? 0 : clusterStat.hashCode());\r
126                 result = prime * result\r
127                                 + ((localStat == null) ? 0 : localStat.hashCode());\r
128                 return result;\r
129         }\r
130         @Override\r
131         public boolean equals(Object obj) {\r
132                 if (this == obj)\r
133                         return true;\r
134                 if (obj == null)\r
135                         return false;\r
136                 if (getClass() != obj.getClass())\r
137                         return false;\r
138                 StatCollection other = (StatCollection) obj;\r
139                 if (allStat == null) {\r
140                         if (other.allStat != null)\r
141                                 return false;\r
142                 } else if (!allStat.equals(other.allStat))\r
143                         return false;\r
144                 if (clusterStat == null) {\r
145                         if (other.clusterStat != null)\r
146                                 return false;\r
147                 } else if (!clusterStat.equals(other.clusterStat))\r
148                         return false;\r
149                 if (localStat == null) {\r
150                         if (other.localStat != null)\r
151                                 return false;\r
152                 } else if (!localStat.equals(other.localStat))\r
153                         return false;\r
154                 return true;\r
155         }\r
156         @Override\r
157         public String toString() {\r
158                 String value = "";\r
159                 for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {\r
160                         value += entry.getKey() + ": ";\r
161                         value += entry.getValue() + "\n";\r
162                 }\r
163                 for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {\r
164                         value += entry.getKey() + ": ";\r
165                         value += entry.getValue() + "\n";\r
166                 }\r
167                 for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {\r
168                         value += entry.getKey() + ": ";\r
169                         value += entry.getValue() + "\n";\r
170                 }\r
171                 return value;\r
172         }\r
173 \r
174 }\r