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 compbio.stat.collector.StatDB;
\r
29 import compbio.stat.collector.StatProcessor;
\r
30 import compbio.ws.client.Services;
\r
32 public class StatCollection {
\r
35 * Total number of requests
\r
37 * incomplete abandoned cancelled
\r
43 public enum Stattype {
\r
47 private Map<Services, StatProcessor> allStat;
\r
48 private Map<Services, StatProcessor> clusterStat;
\r
49 private Map<Services, StatProcessor> localStat;
\r
51 public Map<Services, StatProcessor> getAllStat() {
\r
54 public Map<Services, StatProcessor> getClusterStat() {
\r
57 public Map<Services, StatProcessor> getLocalStat() {
\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
68 public static Map<Date, Totals> getStats(Date fromDate) throws SQLException {
\r
69 Map<Date, Totals> allstats = new TreeMap<Date, Totals>();
\r
71 Calendar fromCal = GregorianCalendar.getInstance();
\r
72 fromCal.setTime(fromDate);
\r
73 fromCal.set(Calendar.DAY_OF_MONTH, 1);
\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
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
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
100 public static StatCollection newStatCollecton(Date startDate, Date endDate)
\r
101 throws SQLException {
\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
109 collection.allStat = new TreeMap<Services, StatProcessor>();
\r
110 for (Services service : Services.values()) {
\r
111 collection.allStat.put(
\r
113 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
118 collection.clusterStat = new TreeMap<Services, StatProcessor>();
\r
119 for (Services service : Services.values()) {
\r
120 collection.clusterStat.put(
\r
122 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
127 collection.localStat = new TreeMap<Services, StatProcessor>();
\r
128 for (Services service : Services.values()) {
\r
129 collection.localStat.put(
\r
131 new StatProcessor(statdb.readData(startStamp, stopStamp,
\r
137 public int hashCode() {
\r
138 final int prime = 31;
\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
148 public boolean equals(Object obj) {
\r
153 if (getClass() != obj.getClass())
\r
155 StatCollection other = (StatCollection) obj;
\r
156 if (allStat == null) {
\r
157 if (other.allStat != null)
\r
159 } else if (!allStat.equals(other.allStat))
\r
161 if (clusterStat == null) {
\r
162 if (other.clusterStat != null)
\r
164 } else if (!clusterStat.equals(other.clusterStat))
\r
166 if (localStat == null) {
\r
167 if (other.localStat != null)
\r
169 } else if (!localStat.equals(other.localStat))
\r
174 public String toString() {
\r
176 for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {
\r
177 value += entry.getKey() + ": ";
\r
178 value += entry.getValue() + "\n";
\r
180 for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {
\r
181 value += entry.getKey() + ": ";
\r
182 value += entry.getValue() + "\n";
\r
184 for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {
\r
185 value += entry.getKey() + ": ";
\r
186 value += entry.getValue() + "\n";
\r