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.collector;
\r
20 import java.sql.Timestamp;
\r
21 import java.text.DateFormat;
\r
22 import java.text.SimpleDateFormat;
\r
23 import java.util.Comparator;
\r
24 import java.util.Date;
\r
25 import java.util.Locale;
\r
27 import compbio.engine.client.ConfExecutable;
\r
28 import compbio.util.Util;
\r
29 import compbio.ws.client.Services;
\r
31 public class JobStat {
\r
33 static final Comparator<JobStat> RUNTIME = new Comparator<JobStat>() {
\r
35 public int compare(JobStat o1, JobStat o2) {
\r
36 return new Integer(o2.getRuntime()).compareTo(o1.getRuntime());
\r
40 static final Comparator<JobStat> STARTTIME = new Comparator<JobStat>() {
\r
42 public int compare(JobStat o1, JobStat o2) {
\r
43 return new Long(o1.start).compareTo(o2.start);
\r
47 static final Comparator<JobStat> RESULTSIZE = new Comparator<JobStat>() {
\r
49 public int compare(JobStat o1, JobStat o2) {
\r
50 return new Long(o2.resultSize).compareTo(o1.resultSize);
\r
54 private static DateFormat DATE_TIME = SimpleDateFormat.getDateTimeInstance(
\r
55 DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.UK);
\r
57 Services webService;
\r
58 String clusterJobId;
\r
64 boolean isCollected;
\r
65 boolean isCancelled;
\r
67 private JobStat(Services webService, String clusterJobId, String jobname,
\r
68 long start, long finish, long inputSize, long resultSize,
\r
69 boolean isCancelled, boolean isCollected) {
\r
71 this.webService = webService;
\r
72 this.clusterJobId = clusterJobId;
\r
73 this.jobname = jobname;
\r
75 this.finish = finish;
\r
76 this.inputSize = inputSize;
\r
77 this.resultSize = resultSize;
\r
78 this.isCancelled = isCancelled;
\r
79 this.isCollected = isCollected;
\r
83 static JobStat newInstance(Services webService, String clusterJobId,
\r
84 String jobname, long start, long finish, long inputSize,
\r
85 long resultSize, boolean isCancelled, boolean isCollected) {
\r
86 return new JobStat(webService, clusterJobId, jobname, start, finish,
\r
87 inputSize, resultSize, isCancelled, isCollected);
\r
90 static JobStat newInstance(Services webService, String clusterJobId,
\r
91 String jobname, Timestamp start, Timestamp finish, long inputSize,
\r
92 long resultSize, boolean isCancelled, boolean isCollected) {
\r
93 long startm = ExecutionStatCollector.UNDEFINED;
\r
94 long stopm = ExecutionStatCollector.UNDEFINED;
\r
95 if (start != null) {
\r
96 startm = start.getTime();
\r
98 if (finish != null) {
\r
99 stopm = finish.getTime();
\r
101 return new JobStat(webService, clusterJobId, jobname, startm, stopm,
\r
102 inputSize, resultSize, isCancelled, isCollected);
\r
106 if (webService == null) {
\r
107 throw new AssertionError("webService must be defined!:\n " + this);
\r
109 if (Util.isEmpty(jobname)) {
\r
110 throw new AssertionError("jobname must be defined!:\n" + this);
\r
114 private JobStat(String jobId) {
\r
115 assert !Util.isEmpty(jobname);
\r
116 this.jobname = jobId;
\r
119 static JobStat newIncompleteStat(String jobname) {
\r
120 return new JobStat(jobname);
\r
123 public boolean isClusterJob() {
\r
124 return jobname.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX);
\r
128 public int hashCode() {
\r
129 final int prime = 31;
\r
131 result = prime * result + ((jobname == null) ? 0 : jobname.hashCode());
\r
136 public boolean equals(Object obj) {
\r
141 if (getClass() != obj.getClass())
\r
143 JobStat other = (JobStat) obj;
\r
144 if (jobname == null) {
\r
145 if (other.jobname != null)
\r
147 } else if (!jobname.equals(other.jobname))
\r
152 public int getRuntime() {
\r
153 if (start != ExecutionStatCollector.UNDEFINED
\r
154 && finish != ExecutionStatCollector.UNDEFINED) {
\r
155 return (int) (finish - start) / 1000;
\r
157 return ExecutionStatCollector.UNDEFINED;
\r
161 public String toString() {
\r
162 return getJobReport();
\r
165 String getJobReport() {
\r
166 String report = "WS: " + webService + "\n";
\r
167 report += "JOB: " + jobname + "\n";
\r
168 if (start != ExecutionStatCollector.UNDEFINED) {
\r
169 report += "Started " + new Date(start) + "\n";
\r
171 if (finish != ExecutionStatCollector.UNDEFINED) {
\r
172 report += "Finished " + new Date(finish) + "\n";
\r
174 if (start != ExecutionStatCollector.UNDEFINED
\r
175 && finish != ExecutionStatCollector.UNDEFINED) {
\r
176 report += "Runtime " + getRuntime() + "\n";
\r
178 report += "Input size " + inputSize + "\n";
\r
179 report += "Result size " + resultSize + "\n";
\r
180 report += "ClusterJobID " + clusterJobId + "\n";
\r
181 report += "Collected? " + isCollected + "\n";
\r
182 report += "Cancelled? " + isCancelled + "\n";
\r
187 * Header Job Started Finished Runtime Input Result
\r
189 String getJobReportTabulated() {
\r
190 String report = webService + "\t";
\r
191 report += jobname + "\t";
\r
192 if (start != ExecutionStatCollector.UNDEFINED) {
\r
193 report += ExecutionStatCollector.DF.format(new Date(start)) + "\t";
\r
195 report += ExecutionStatCollector.UNDEFINED + "\t";
\r
197 if (finish != ExecutionStatCollector.UNDEFINED) {
\r
198 report += ExecutionStatCollector.DF.format(new Date(finish)) + "\t";
\r
200 report += ExecutionStatCollector.UNDEFINED + "\t";
\r
202 if (start != ExecutionStatCollector.UNDEFINED
\r
203 && finish != ExecutionStatCollector.UNDEFINED) {
\r
204 report += getRuntime() + "\t";
\r
206 report += ExecutionStatCollector.UNDEFINED + "\t";
\r
208 report += inputSize + "\t";
\r
209 report += resultSize + "\t";
\r
210 report += clusterJobId + "\t";
\r
211 report += isCollected + "\t";
\r
212 report += isCancelled + "\t";
\r
216 public Services getWebService() {
\r
220 public String getClusterJobId() {
\r
221 return clusterJobId;
\r
224 public String getJobname() {
\r
228 public String getEscJobname() {
\r
229 String[] parts = jobname.split("#");
\r
230 return parts[0] + "%23" + parts[1];
\r
233 public String getStart() {
\r
234 if (start != ExecutionStatCollector.UNDEFINED) {
\r
235 return DATE_TIME.format(new Date(start));
\r
240 public String getFinish() {
\r
241 if (finish != ExecutionStatCollector.UNDEFINED) {
\r
242 return DATE_TIME.format(new Date(finish));
\r
247 public long getInputSize() {
\r
248 if (inputSize != ExecutionStatCollector.UNDEFINED) {
\r
254 public long getResultSize() {
\r
255 if (resultSize > 0) {
\r
261 public boolean hasResult() {
\r
262 return resultSize > 0;
\r
265 public boolean hasStarted() {
\r
266 return start != ExecutionStatCollector.UNDEFINED;
\r
269 public boolean getIsCollected() {
\r
270 return isCollected;
\r
273 public boolean getIsCancelled() {
\r
274 return isCancelled;
\r
277 public boolean getIsFinished() {
\r
278 return finish != ExecutionStatCollector.UNDEFINED;
\r