1 package compbio.stat.collector;
\r
3 import java.sql.Timestamp;
\r
4 import java.text.DateFormat;
\r
5 import java.text.SimpleDateFormat;
\r
6 import java.util.Comparator;
\r
7 import java.util.Date;
\r
8 import java.util.Locale;
\r
10 import compbio.engine.client.ConfExecutable;
\r
11 import compbio.util.Util;
\r
12 import compbio.ws.client.Services;
\r
14 public class JobStat {
\r
16 static final Comparator<JobStat> RUNTIME = new Comparator<JobStat>() {
\r
18 public int compare(JobStat o1, JobStat o2) {
\r
19 return new Integer(o2.getRuntime()).compareTo(o1.getRuntime());
\r
23 static final Comparator<JobStat> STARTTIME = new Comparator<JobStat>() {
\r
25 public int compare(JobStat o1, JobStat o2) {
\r
26 return new Long(o1.start).compareTo(o2.start);
\r
30 static final Comparator<JobStat> RESULTSIZE = new Comparator<JobStat>() {
\r
32 public int compare(JobStat o1, JobStat o2) {
\r
33 return new Long(o2.resultSize).compareTo(o1.resultSize);
\r
37 private static DateFormat DATE_TIME = SimpleDateFormat.getDateTimeInstance(
\r
38 DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.UK);
\r
40 Services webService;
\r
41 String clusterJobId;
\r
47 boolean isCollected;
\r
48 boolean isCancelled;
\r
50 private JobStat(Services webService, String clusterJobId, String jobname,
\r
51 long start, long finish, long inputSize, long resultSize,
\r
52 boolean isCancelled, boolean isCollected) {
\r
54 this.webService = webService;
\r
55 this.clusterJobId = clusterJobId;
\r
56 this.jobname = jobname;
\r
58 this.finish = finish;
\r
59 this.inputSize = inputSize;
\r
60 this.resultSize = resultSize;
\r
61 this.isCancelled = isCancelled;
\r
62 this.isCollected = isCollected;
\r
66 static JobStat newInstance(Services webService, String clusterJobId,
\r
67 String jobname, long start, long finish, long inputSize,
\r
68 long resultSize, boolean isCancelled, boolean isCollected) {
\r
69 return new JobStat(webService, clusterJobId, jobname, start, finish,
\r
70 inputSize, resultSize, isCancelled, isCollected);
\r
73 static JobStat newInstance(Services webService, String clusterJobId,
\r
74 String jobname, Timestamp start, Timestamp finish, long inputSize,
\r
75 long resultSize, boolean isCancelled, boolean isCollected) {
\r
76 long startm = ExecutionStatCollector.UNDEFINED;
\r
77 long stopm = ExecutionStatCollector.UNDEFINED;
\r
78 if (start != null) {
\r
79 startm = start.getTime();
\r
81 if (finish != null) {
\r
82 stopm = finish.getTime();
\r
84 return new JobStat(webService, clusterJobId, jobname, startm, stopm,
\r
85 inputSize, resultSize, isCancelled, isCollected);
\r
89 if (webService == null) {
\r
90 throw new AssertionError("webService must be defined!:\n " + this);
\r
92 if (Util.isEmpty(jobname)) {
\r
93 throw new AssertionError("jobname must be defined!:\n" + this);
\r
97 private JobStat(String jobId) {
\r
98 assert !Util.isEmpty(jobname);
\r
99 this.jobname = jobId;
\r
102 static JobStat newIncompleteStat(String jobname) {
\r
103 return new JobStat(jobname);
\r
106 public boolean isClusterJob() {
\r
107 return jobname.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX);
\r
111 public int hashCode() {
\r
112 final int prime = 31;
\r
114 result = prime * result + ((jobname == null) ? 0 : jobname.hashCode());
\r
119 public boolean equals(Object obj) {
\r
124 if (getClass() != obj.getClass())
\r
126 JobStat other = (JobStat) obj;
\r
127 if (jobname == null) {
\r
128 if (other.jobname != null)
\r
130 } else if (!jobname.equals(other.jobname))
\r
135 public int getRuntime() {
\r
136 if (start != ExecutionStatCollector.UNDEFINED
\r
137 && finish != ExecutionStatCollector.UNDEFINED) {
\r
138 return (int) (finish - start) / 1000;
\r
140 return ExecutionStatCollector.UNDEFINED;
\r
144 public String toString() {
\r
145 return getJobReport();
\r
148 String getJobReport() {
\r
149 String report = "WS: " + webService + "\n";
\r
150 report += "JOB: " + jobname + "\n";
\r
151 if (start != ExecutionStatCollector.UNDEFINED) {
\r
152 report += "Started " + new Date(start) + "\n";
\r
154 if (finish != ExecutionStatCollector.UNDEFINED) {
\r
155 report += "Finished " + new Date(finish) + "\n";
\r
157 if (start != ExecutionStatCollector.UNDEFINED
\r
158 && finish != ExecutionStatCollector.UNDEFINED) {
\r
159 report += "Runtime " + getRuntime() + "\n";
\r
161 report += "Input size " + inputSize + "\n";
\r
162 report += "Result size " + resultSize + "\n";
\r
163 report += "ClusterJobID " + clusterJobId + "\n";
\r
164 report += "Collected? " + isCollected + "\n";
\r
165 report += "Cancelled? " + isCancelled + "\n";
\r
170 * Header Job Started Finished Runtime Input Result
\r
172 String getJobReportTabulated() {
\r
173 String report = webService + "\t";
\r
174 report += jobname + "\t";
\r
175 if (start != ExecutionStatCollector.UNDEFINED) {
\r
176 report += ExecutionStatCollector.DF.format(new Date(start)) + "\t";
\r
178 report += ExecutionStatCollector.UNDEFINED + "\t";
\r
180 if (finish != ExecutionStatCollector.UNDEFINED) {
\r
181 report += ExecutionStatCollector.DF.format(new Date(finish)) + "\t";
\r
183 report += ExecutionStatCollector.UNDEFINED + "\t";
\r
185 if (start != ExecutionStatCollector.UNDEFINED
\r
186 && finish != ExecutionStatCollector.UNDEFINED) {
\r
187 report += getRuntime() + "\t";
\r
189 report += ExecutionStatCollector.UNDEFINED + "\t";
\r
191 report += inputSize + "\t";
\r
192 report += resultSize + "\t";
\r
193 report += clusterJobId + "\t";
\r
194 report += isCollected + "\t";
\r
195 report += isCancelled + "\t";
\r
199 public Services getWebService() {
\r
203 public String getClusterJobId() {
\r
204 return clusterJobId;
\r
207 public String getJobname() {
\r
211 public String getEscJobname() {
\r
212 String[] parts = jobname.split("#");
\r
213 return parts[0] + "%23" + parts[1];
\r
216 public String getStart() {
\r
217 if (start != ExecutionStatCollector.UNDEFINED) {
\r
218 return DATE_TIME.format(new Date(start));
\r
223 public String getFinish() {
\r
224 if (finish != ExecutionStatCollector.UNDEFINED) {
\r
225 return DATE_TIME.format(new Date(finish));
\r
230 public long getInputSize() {
\r
231 if (inputSize != ExecutionStatCollector.UNDEFINED) {
\r
237 public long getResultSize() {
\r
238 if (resultSize > 0) {
\r
244 public boolean hasResult() {
\r
245 return resultSize > 0;
\r
248 public boolean hasStarted() {
\r
249 return start != ExecutionStatCollector.UNDEFINED;
\r
252 public boolean getIsCollected() {
\r
253 return isCollected;
\r
256 public boolean getIsCancelled() {
\r
257 return isCancelled;
\r
260 public boolean getIsFinished() {
\r
261 return finish != ExecutionStatCollector.UNDEFINED;
\r