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