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