File: StatCollection.java

package info (click to toggle)
libjaba-client-java 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,052 kB
  • sloc: java: 17,308; makefile: 12
file content (197 lines) | stat: -rw-r--r-- 6,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Copyright (c) 2011 Peter Troshin
 *  
 *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     
 * 
 *  This library is free software; you can redistribute it and/or modify it under the terms of the
 *  Apache License version 2 as published by the Apache Software Foundation
 * 
 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache 
 *  License for more details.
 * 
 *  A copy of the license is in apache_license.txt. It is also available here:
 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
 * 
 * Any republication or derived work distributed in source code form
 * must include this copyright and license notice.
 */
package compbio.stat.servlet.util;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import compbio.stat.collector.StatDB;
import compbio.stat.collector.StatProcessor;
import compbio.ws.client.Services;

public class StatCollection {

	/**
	 * Total number of requests
	 * 
	 * incomplete abandoned cancelled
	 * 
	 * @author pvtroshin
	 * 
	 */

	public enum Stattype {
		CLUSTER, LOCAL, ALL
	}

	private Map<Services, StatProcessor> allStat;
	private Map<Services, StatProcessor> clusterStat;
	private Map<Services, StatProcessor> localStat;
	
	private final static Logger log = Logger.getLogger(StatCollection.class);

	public Map<Services, StatProcessor> getAllStat() {
		return allStat;
	}
	public Map<Services, StatProcessor> getClusterStat() {
		return clusterStat;
	}
	public Map<Services, StatProcessor> getLocalStat() {
		return localStat;
	}

	public static Map<Date, Totals> getStats(int monthsToReport)
			throws SQLException {
		Calendar fromCal = GregorianCalendar.getInstance();
		fromCal.add(Calendar.MONTH, -monthsToReport);
		return getStats(fromCal.getTime());
	}

	public static Map<Date, Totals> getStats(Date fromDate) throws SQLException {
		Map<Date, Totals> allstats = new TreeMap<Date, Totals>();

		Calendar fromCal = GregorianCalendar.getInstance();
		fromCal.setTime(fromDate);
		fromCal.set(Calendar.DAY_OF_MONTH, 1);

		Calendar toCal = GregorianCalendar.getInstance();
		toCal.setTime(new Date());
		if (fromCal.after(toCal)) {
			throw new AssertionError("From Date must be before ToDate! ");
		}
		while (fromCal.before(toCal)) {
			Date from = fromCal.getTime();
			fromCal.add(Calendar.MONTH, 1);
			allstats.put(from, getJobCounts(from, fromCal.getTime()));
		}
		return allstats;
	}

	private static Totals getJobCounts(Date from, Date to) throws SQLException {
		StatDB db = new StatDB();
		Totals t = new Totals();
		Timestamp fromTime = new Timestamp(from.getTime());
		Timestamp toTime = new Timestamp(to.getTime());
		t.total = db.getTotalJobsCount(fromTime, toTime);
		t.incomplete = db.getIncompleteCount(fromTime, toTime);
		t.abandoned = db.getAbandonedCount(fromTime, toTime);
		t.cancelled = db.getCancelledCount(fromTime, toTime);
		log.trace("Job counts: total = " + t.total + ", incomplete = " + 
		t.incomplete + ", abandoned = " + t.abandoned + ", cancelled = " + t.cancelled );
		return t;
	}

	public static StatCollection newStatCollecton(Date startDate, Date endDate)
			throws SQLException {

		Timestamp startStamp = new Timestamp(startDate.getTime());
		Timestamp stopStamp = new Timestamp(endDate.getTime());
		StatCollection collection = new StatCollection();
		StatDB statdb = new StatDB();

		// Total
		collection.allStat = new TreeMap<Services, StatProcessor>();
		for (Services service : Services.values()) {
			collection.allStat.put(
					service,
					new StatProcessor(statdb.readData(startStamp, stopStamp,
							service, null)));
		}

		// Cluster
		collection.clusterStat = new TreeMap<Services, StatProcessor>();
		for (Services service : Services.values()) {
			collection.clusterStat.put(
					service,
					new StatProcessor(statdb.readData(startStamp, stopStamp,
							service, true)));
		}

		// Local
		collection.localStat = new TreeMap<Services, StatProcessor>();
		for (Services service : Services.values()) {
			collection.localStat.put(
					service,
					new StatProcessor(statdb.readData(startStamp, stopStamp,
							service, false)));
		}
		return collection;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((allStat == null) ? 0 : allStat.hashCode());
		result = prime * result
				+ ((clusterStat == null) ? 0 : clusterStat.hashCode());
		result = prime * result
				+ ((localStat == null) ? 0 : localStat.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StatCollection other = (StatCollection) obj;
		if (allStat == null) {
			if (other.allStat != null)
				return false;
		} else if (!allStat.equals(other.allStat))
			return false;
		if (clusterStat == null) {
			if (other.clusterStat != null)
				return false;
		} else if (!clusterStat.equals(other.clusterStat))
			return false;
		if (localStat == null) {
			if (other.localStat != null)
				return false;
		} else if (!localStat.equals(other.localStat))
			return false;
		return true;
	}
	@Override
	public String toString() {
		String value = "";
		for (Map.Entry<Services, StatProcessor> entry : allStat.entrySet()) {
			value += entry.getKey() + ": ";
			value += entry.getValue() + "\n";
		}
		for (Map.Entry<Services, StatProcessor> entry : clusterStat.entrySet()) {
			value += entry.getKey() + ": ";
			value += entry.getValue() + "\n";
		}
		for (Map.Entry<Services, StatProcessor> entry : localStat.entrySet()) {
			value += entry.getKey() + ": ";
			value += entry.getValue() + "\n";
		}
		return value;
	}

}