Clean up logging system
[jabaws.git] / webservices / compbio / stat / servlet / util / StatCollection.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.stat.servlet.util;\r
19 \r
20 import java.sql.SQLException;\r
21 import java.sql.Timestamp;\r
22 import java.util.Calendar;\r
23 import java.util.Date;\r
24 import java.util.GregorianCalendar;\r
25 import java.util.Map;\r
26 import java.util.TreeMap;\r
27 \r
28 import org.apache.log4j.Logger;\r
29 \r
30 import compbio.stat.collector.StatDB;\r
31 import compbio.stat.collector.StatProcessor;\r
32 import compbio.ws.client.Services;\r
33 \r
34 public class StatCollection {\r
35 \r
36         /**\r
37          * Total number of requests\r
38          * \r
39          * incomplete abandoned cancelled\r
40          * \r
41          * @author pvtroshin\r
42          * \r
43          */\r
44 \r
45         public enum Stattype {\r
46                 CLUSTER, LOCAL, ALL\r
47         }\r
48 \r
49         private Map<Services, StatProcessor> allStat;\r
50         private Map<Services, StatProcessor> clusterStat;\r
51         private Map<Services, StatProcessor> localStat;\r
52         \r
53         private final static Logger log = Logger.getLogger(StatCollection.class);\r
54 \r
55         public Map<Services, StatProcessor> getAllStat() {\r
56                 return allStat;\r
57         }\r
58         public Map<Services, StatProcessor> getClusterStat() {\r
59                 return clusterStat;\r
60         }\r
61         public Map<Services, StatProcessor> getLocalStat() {\r
62                 return localStat;\r
63         }\r
64 \r
65         public static Map<Date, Totals> getStats(int monthsToReport)\r
66                         throws SQLException {\r
67                 Calendar fromCal = GregorianCalendar.getInstance();\r
68                 fromCal.add(Calendar.MONTH, -monthsToReport);\r
69                 return getStats(fromCal.getTime());\r
70         }\r
71 \r
72         public static Map<Date, Totals> getStats(Date fromDate) throws SQLException {\r
73                 Map<Date, Totals> allstats = new TreeMap<Date, Totals>();\r
74 \r
75                 Calendar fromCal = GregorianCalendar.getInstance();\r
76                 fromCal.setTime(fromDate);\r
77                 fromCal.set(Calendar.DAY_OF_MONTH, 1);\r
78 \r
79                 Calendar toCal = GregorianCalendar.getInstance();\r
80                 toCal.setTime(new Date());\r
81                 if (fromCal.after(toCal)) {\r
82                         throw new AssertionError("From Date must be before ToDate! ");\r
83                 }\r
84                 while (fromCal.before(toCal)) {\r
85                         Date from = fromCal.getTime();\r
86                         fromCal.add(Calendar.MONTH, 1);\r
87                         allstats.put(from, getJobCounts(from, fromCal.getTime()));\r
88                 }\r
89                 return allstats;\r
90         }\r
91 \r
92         private static Totals getJobCounts(Date from, Date to) throws SQLException {\r
93                 StatDB db = new StatDB();\r
94                 Totals t = new Totals();\r
95                 Timestamp fromTime = new Timestamp(from.getTime());\r
96                 Timestamp toTime = new Timestamp(to.getTime());\r
97                 t.total = db.getTotalJobsCount(fromTime, toTime);\r
98                 t.incomplete = db.getIncompleteCount(fromTime, toTime);\r
99                 t.abandoned = db.getAbandonedCount(fromTime, toTime);\r
100                 t.cancelled = db.getCancelledCount(fromTime, toTime);\r
101                 log.trace("Job counts: total = " + t.total + ", incomplete = " + \r
102                 t.incomplete + ", abandoned = " + t.abandoned + ", cancelled = " + t.cancelled );\r
103                 return t;\r
104         }\r
105 \r
106         public static StatCollection newStatCollecton(Date startDate, Date endDate)\r
107                         throws SQLException {\r
108 \r
109                 Timestamp startStamp = new Timestamp(startDate.getTime());\r
110                 Timestamp stopStamp = new Timestamp(endDate.getTime());\r
111                 StatCollection collection = new StatCollection();\r
112                 StatDB statdb = new StatDB();\r
113 \r
114                 // Total\r
115                 collection.allStat = new TreeMap<Services, StatProcessor>();\r
116                 for (Services service : Services.values()) {\r
117                         collection.allStat.put(\r
118                                         service,\r
119                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
120                                                         service, null)));\r
121                 }\r
122 \r
123                 // Cluster\r
124                 collection.clusterStat = new TreeMap<Services, StatProcessor>();\r
125                 for (Services service : Services.values()) {\r
126                         collection.clusterStat.put(\r
127                                         service,\r
128                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
129                                                         service, true)));\r
130                 }\r
131 \r
132                 // Local\r
133                 collection.localStat = new TreeMap<Services, StatProcessor>();\r
134                 for (Services service : Services.values()) {\r
135                         collection.localStat.put(\r
136                                         service,\r
137                                         new StatProcessor(statdb.readData(startStamp, stopStamp,\r
138                                                         service, false)));\r
139                 }\r
140                 return collection;\r
141         }\r
142         @Override\r
143         public int hashCode() {\r
144                 final int prime = 31;\r
145                 int result = 1;\r
146                 result = prime * result + ((allStat == null) ? 0 : allStat.hashCode());\r
147                 result = prime * result\r
148                                 + ((clusterStat == null) ? 0 : clusterStat.hashCode());\r
149                 result = prime * result\r
150                                 + ((localStat == null) ? 0 : localStat.hashCode());\r
151                 return result;\r
152         }\r
153         @Override\r
154         public boolean equals(Object obj) {\r
155                 if (this == obj)\r
156                         return true;\r
157                 if (obj == null)\r
158                         return false;\r
159                 if (getClass() != obj.getClass())\r
160                         return false;\r
161                 StatCollection other = (StatCollection) obj;\r
162                 if (allStat == null) {\r
163                         if (other.allStat != null)\r
164                                 return false;\r
165                 } else if (!allStat.equals(other.allStat))\r
166                         return false;\r
167                 if (clusterStat == null) {\r
168                         if (other.clusterStat != null)\r
169                                 return false;\r
170                 } else if (!clusterStat.equals(other.clusterStat))\r
171                         return false;\r
172                 if (localStat == null) {\r
173                         if (other.localStat != null)\r
174                                 return false;\r
175                 } else if (!localStat.equals(other.localStat))\r
176                         return false;\r
177                 return true;\r
178         }\r
179         @Override\r
180         public String toString() {\r
181                 String value = "";\r
182                 for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {\r
183                         value += entry.getKey() + ": ";\r
184                         value += entry.getValue() + "\n";\r
185                 }\r
186                 for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {\r
187                         value += entry.getKey() + ": ";\r
188                         value += entry.getValue() + "\n";\r
189                 }\r
190                 for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {\r
191                         value += entry.getKey() + ": ";\r
192                         value += entry.getValue() + "\n";\r
193                 }\r
194                 return value;\r
195         }\r
196 \r
197 }\r