1 /* Copyright (c) 2011 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.stat.servlet.util;
\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
28 import org.apache.log4j.Logger;
\r
30 import compbio.stat.collector.StatDB;
\r
31 import compbio.stat.collector.StatProcessor;
\r
32 import compbio.ws.client.Services;
\r
34 public class StatCollection {
\r
37 * Total number of requests
\r
39 * incomplete abandoned cancelled
\r
45 public enum Stattype {
\r
49 private Map<Services, StatProcessor> allStat;
\r
50 private Map<Services, StatProcessor> clusterStat;
\r
51 private Map<Services, StatProcessor> localStat;
\r
53 private final static Logger log = Logger.getLogger(StatCollection.class);
\r
55 public Map<Services, StatProcessor> getAllStat() {
\r
58 public Map<Services, StatProcessor> getClusterStat() {
\r
61 public Map<Services, StatProcessor> getLocalStat() {
\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
72 public static Map<Date, Totals> getStats(Date fromDate) throws SQLException {
\r
73 Map<Date, Totals> allstats = new TreeMap<Date, Totals>();
\r
75 Calendar fromCal = GregorianCalendar.getInstance();
\r
76 fromCal.setTime(fromDate);
\r
77 fromCal.set(Calendar.DAY_OF_MONTH, 1);
\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
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
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
106 public static StatCollection newStatCollecton(Date startDate, Date endDate)
\r
107 throws SQLException {
\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
115 collection.allStat = new TreeMap<Services, StatProcessor>();
\r
116 for (Services service : Services.values()) {
\r
117 collection.allStat.put(
\r
119 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
124 collection.clusterStat = new TreeMap<Services, StatProcessor>();
\r
125 for (Services service : Services.values()) {
\r
126 collection.clusterStat.put(
\r
128 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
133 collection.localStat = new TreeMap<Services, StatProcessor>();
\r
134 for (Services service : Services.values()) {
\r
135 collection.localStat.put(
\r
137 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
143 public int hashCode() {
\r
144 final int prime = 31;
\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
154 public boolean equals(Object obj) {
\r
159 if (getClass() != obj.getClass())
\r
161 StatCollection other = (StatCollection) obj;
\r
162 if (allStat == null) {
\r
163 if (other.allStat != null)
\r
165 } else if (!allStat.equals(other.allStat))
\r
167 if (clusterStat == null) {
\r
168 if (other.clusterStat != null)
\r
170 } else if (!clusterStat.equals(other.clusterStat))
\r
172 if (localStat == null) {
\r
173 if (other.localStat != null)
\r
175 } else if (!localStat.equals(other.localStat))
\r
180 public String toString() {
\r
182 for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {
\r
183 value += entry.getKey() + ": ";
\r
184 value += entry.getValue() + "\n";
\r
186 for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {
\r
187 value += entry.getKey() + ": ";
\r
188 value += entry.getValue() + "\n";
\r
190 for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {
\r
191 value += entry.getKey() + ": ";
\r
192 value += entry.getValue() + "\n";
\r