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, 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
51 public static Map<Date, Totals> getStats(Date fromDate) throws SQLException {
\r
52 Map<Date, Totals> allstats = new TreeMap<Date, Totals>();
\r
54 Calendar fromCal = GregorianCalendar.getInstance();
\r
55 fromCal.setTime(fromDate);
\r
56 fromCal.set(Calendar.DAY_OF_MONTH, 1);
\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
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
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
83 public static StatCollection newStatCollecton(Date startDate, Date endDate)
\r
84 throws SQLException {
\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
92 collection.allStat = new TreeMap<Services, StatProcessor>();
\r
93 for (Services service : Services.values()) {
\r
94 collection.allStat.put(
\r
96 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
101 collection.clusterStat = new TreeMap<Services, StatProcessor>();
\r
102 for (Services service : Services.values()) {
\r
103 collection.clusterStat.put(
\r
105 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
110 collection.localStat = new TreeMap<Services, StatProcessor>();
\r
111 for (Services service : Services.values()) {
\r
112 collection.localStat.put(
\r
114 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
120 public int hashCode() {
\r
121 final int prime = 31;
\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
131 public boolean equals(Object obj) {
\r
136 if (getClass() != obj.getClass())
\r
138 StatCollection other = (StatCollection) obj;
\r
139 if (allStat == null) {
\r
140 if (other.allStat != null)
\r
142 } else if (!allStat.equals(other.allStat))
\r
144 if (clusterStat == null) {
\r
145 if (other.clusterStat != null)
\r
147 } else if (!clusterStat.equals(other.clusterStat))
\r
149 if (localStat == null) {
\r
150 if (other.localStat != null)
\r
152 } else if (!localStat.equals(other.localStat))
\r
157 public String toString() {
\r
159 for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {
\r
160 value += entry.getKey() + ": ";
\r
161 value += entry.getValue() + "\n";
\r
163 for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {
\r
164 value += entry.getKey() + ": ";
\r
165 value += entry.getValue() + "\n";
\r
167 for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {
\r
168 value += entry.getKey() + ": ";
\r
169 value += entry.getValue() + "\n";
\r