1 package compbio.stat.servlet.util;
\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
11 import compbio.stat.collector.StatDB;
\r
12 import compbio.stat.collector.StatProcessor;
\r
13 import compbio.ws.client.Services;
\r
15 public class StatCollection {
\r
18 * Total number of requests
\r
20 * incomplete abandoned cancelled
\r
26 public enum Stattype {
\r
30 private Map<Services, StatProcessor> allStat;
\r
31 private Map<Services, StatProcessor> clusterStat;
\r
32 private Map<Services, StatProcessor> localStat;
\r
34 public Map<Services, StatProcessor> getAllStat() {
\r
37 public Map<Services, StatProcessor> getClusterStat() {
\r
40 public Map<Services, StatProcessor> getLocalStat() {
\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
51 public static Map<Date, StatCollection> getStats(Date fromDate)
\r
52 throws SQLException {
\r
53 Map<Date, StatCollection> allstats = new TreeMap<Date, StatCollection>();
\r
55 Calendar fromCal = GregorianCalendar.getInstance();
\r
56 fromCal.setTime(fromDate);
\r
57 fromCal.set(Calendar.DAY_OF_MONTH, 1);
\r
59 Calendar toCal = GregorianCalendar.getInstance();
\r
60 toCal.setTime(new Date());
\r
62 if (fromCal.after(toCal)) {
\r
63 throw new AssertionError("From Date must be before ToDate! ");
\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
74 // System.out.println("!" + from + " !!! " + fromCal.getTime());
\r
76 StatCollection.newStatCollecton(from, fromCal.getTime()));
\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
89 static StatCollection newStatCollecton(Date startDate, Date endDate)
\r
90 throws SQLException {
\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
98 collection.allStat = new TreeMap<Services, StatProcessor>();
\r
99 for (Services service : Services.values()) {
\r
100 collection.allStat.put(
\r
102 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
107 collection.clusterStat = new TreeMap<Services, StatProcessor>();
\r
108 for (Services service : Services.values()) {
\r
109 collection.clusterStat.put(
\r
111 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
116 collection.localStat = new TreeMap<Services, StatProcessor>();
\r
117 for (Services service : Services.values()) {
\r
118 collection.localStat.put(
\r
120 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
126 public int hashCode() {
\r
127 final int prime = 31;
\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
137 public boolean equals(Object obj) {
\r
142 if (getClass() != obj.getClass())
\r
144 StatCollection other = (StatCollection) obj;
\r
145 if (allStat == null) {
\r
146 if (other.allStat != null)
\r
148 } else if (!allStat.equals(other.allStat))
\r
150 if (clusterStat == null) {
\r
151 if (other.clusterStat != null)
\r
153 } else if (!clusterStat.equals(other.clusterStat))
\r
155 if (localStat == null) {
\r
156 if (other.localStat != null)
\r
158 } else if (!localStat.equals(other.localStat))
\r
163 public String toString() {
\r
165 for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {
\r
166 value += entry.getKey() + ": ";
\r
167 value += entry.getValue() + "\n";
\r
169 for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {
\r
170 value += entry.getKey() + ": ";
\r
171 value += entry.getValue() + "\n";
\r
173 for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {
\r
174 value += entry.getKey() + ": ";
\r
175 value += entry.getValue() + "\n";
\r