1 /* Copyright (c) 2011 Peter Troshin
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
6 * Apache License version 2 as published by the Apache Software Foundation
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
10 * License for more details.
12 * A copy of the license is in apache_license.txt. It is also available here:
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
15 * Any republication or derived work distributed in source code form
16 * must include this copyright and license notice.
18 package compbio.stat.collector;
20 import java.sql.Timestamp;
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Comparator;
24 import java.util.Date;
25 import java.util.Locale;
27 import compbio.engine.client.ConfExecutable;
28 import compbio.util.Util;
29 import compbio.ws.client.Services;
31 public class JobStat {
33 static final Comparator<JobStat> RUNTIME = new Comparator<JobStat>() {
35 public int compare(JobStat o1, JobStat o2) {
36 return new Integer(o2.getRuntime()).compareTo(o1.getRuntime());
40 static final Comparator<JobStat> STARTTIME = new Comparator<JobStat>() {
42 public int compare(JobStat o1, JobStat o2) {
43 return new Long(o1.start).compareTo(o2.start);
47 static final Comparator<JobStat> RESULTSIZE = new Comparator<JobStat>() {
49 public int compare(JobStat o1, JobStat o2) {
50 return new Long(o2.resultSize).compareTo(o1.resultSize);
54 private static DateFormat DATE_TIME = SimpleDateFormat.getDateTimeInstance(
55 DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.UK);
67 private JobStat(Services webService, String clusterJobId, String jobname,
68 long start, long finish, long inputSize, long resultSize,
69 boolean isCancelled, boolean isCollected) {
71 this.webService = webService;
72 this.clusterJobId = clusterJobId;
73 this.jobname = jobname;
76 this.inputSize = inputSize;
77 this.resultSize = resultSize;
78 this.isCancelled = isCancelled;
79 this.isCollected = isCollected;
83 public static JobStat newInstance(Services webService, String clusterJobId,
84 String jobname, long start, long finish, long inputSize,
85 long resultSize, boolean isCancelled, boolean isCollected) {
86 return new JobStat(webService, clusterJobId, jobname, start, finish,
87 inputSize, resultSize, isCancelled, isCollected);
90 public static JobStat newInstance(Services webService, String clusterJobId,
91 String jobname, Timestamp start, Timestamp finish, long inputSize,
92 long resultSize, boolean isCancelled, boolean isCollected) {
93 long startm = ExecutionStatCollector.UNDEFINED;
94 long stopm = ExecutionStatCollector.UNDEFINED;
96 startm = start.getTime();
99 stopm = finish.getTime();
101 return new JobStat(webService, clusterJobId, jobname, startm, stopm,
102 inputSize, resultSize, isCancelled, isCollected);
106 if (webService == null) {
107 throw new AssertionError("webService must be defined!:\n " + this);
109 if (Util.isEmpty(jobname)) {
110 throw new AssertionError("jobname must be defined!:\n" + this);
114 private JobStat(String jobId) {
115 assert !Util.isEmpty(jobname);
116 this.jobname = jobId;
119 public static JobStat newIncompleteStat(String jobname) {
120 return new JobStat(jobname);
123 public boolean isClusterJob() {
124 return jobname.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX);
128 public int hashCode() {
129 final int prime = 31;
131 result = prime * result + ((jobname == null) ? 0 : jobname.hashCode());
136 public boolean equals(Object obj) {
141 if (getClass() != obj.getClass())
143 JobStat other = (JobStat) obj;
144 if (jobname == null) {
145 if (other.jobname != null)
147 } else if (!jobname.equals(other.jobname))
152 public int getRuntime() {
153 if (start != ExecutionStatCollector.UNDEFINED && finish != ExecutionStatCollector.UNDEFINED) {
154 return (int) (finish - start) / 1000;
156 return ExecutionStatCollector.UNDEFINED;
160 public String toString() {
161 return getJobReport();
164 String getJobReport() {
165 String report = "WS: " + webService + "\n";
166 report += "JOB: " + jobname + "\n";
167 if (start != ExecutionStatCollector.UNDEFINED) {
168 report += "Started " + new Date(start) + "\n";
170 if (finish != ExecutionStatCollector.UNDEFINED) {
171 report += "Finished " + new Date(finish) + "\n";
173 if (start != ExecutionStatCollector.UNDEFINED && finish != ExecutionStatCollector.UNDEFINED) {
174 report += "Runtime " + getRuntime() + "\n";
176 report += "Input size " + inputSize + "\n";
177 report += "Result size " + resultSize + "\n";
178 report += "ClusterJobID " + clusterJobId + "\n";
179 report += "Collected? " + isCollected + "\n";
180 report += "Cancelled? " + isCancelled + "\n";
185 * Header Job Started Finished Runtime Input Result
187 String getJobReportTabulated() {
188 String report = webService + "\t";
189 report += jobname + "\t";
190 if (start != ExecutionStatCollector.UNDEFINED) {
191 report += ExecutionStatCollector.DF.format(new Date(start)) + "\t";
193 report += ExecutionStatCollector.UNDEFINED + "\t";
195 if (finish != ExecutionStatCollector.UNDEFINED) {
196 report += ExecutionStatCollector.DF.format(new Date(finish)) + "\t";
198 report += ExecutionStatCollector.UNDEFINED + "\t";
200 if (start != ExecutionStatCollector.UNDEFINED && finish != ExecutionStatCollector.UNDEFINED) {
201 report += getRuntime() + "\t";
203 report += ExecutionStatCollector.UNDEFINED + "\t";
205 report += inputSize + "\t";
206 report += resultSize + "\t";
207 report += clusterJobId + "\t";
208 report += isCollected + "\t";
209 report += isCancelled + "\t";
213 public Services getWebService() {
217 public String getClusterJobId() {
221 public String getJobname() {
225 public String getEscJobname() {
226 String[] parts = jobname.split("#");
227 return parts[0] + "%23" + parts[1];
230 public String getStart() {
231 if (start != ExecutionStatCollector.UNDEFINED) {
232 return DATE_TIME.format(new Date(start));
237 public long getNumericalStart() {
241 public long getNumericalFinish() {
245 public String getFinish() {
246 if (finish != ExecutionStatCollector.UNDEFINED) {
247 return DATE_TIME.format(new Date(finish));
252 public long getInputSize() {
253 if (inputSize != ExecutionStatCollector.UNDEFINED) {
259 public long getResultSize() {
260 if (resultSize > 0) {
266 public boolean hasResult() {
267 return resultSize > 0;
270 public boolean hasStarted() {
271 return start != ExecutionStatCollector.UNDEFINED;
274 public boolean getIsCollected() {
278 public boolean getIsCancelled() {
282 public boolean getIsFinished() {
283 return finish != ExecutionStatCollector.UNDEFINED;