merge JABAWS_Release_2_1 into develop
[jabaws.git] / webservices / compbio / stat / collector / JobStat.java
1 /* Copyright (c) 2011 Peter Troshin
2  *  
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     
4  * 
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
7  * 
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.
11  * 
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
14  * 
15  * Any republication or derived work distributed in source code form
16  * must include this copyright and license notice.
17  */
18 package compbio.stat.collector;
19
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;
26
27 import compbio.engine.client.ConfExecutable;
28 import compbio.util.Util;
29 import compbio.ws.client.Services;
30
31 public class JobStat {
32
33         static final Comparator<JobStat> RUNTIME = new Comparator<JobStat>() {
34                 @Override
35                 public int compare(JobStat o1, JobStat o2) {
36                         return new Integer(o2.getRuntime()).compareTo(o1.getRuntime());
37                 }
38         };
39
40         static final Comparator<JobStat> STARTTIME = new Comparator<JobStat>() {
41                 @Override
42                 public int compare(JobStat o1, JobStat o2) {
43                         return new Long(o1.start).compareTo(o2.start);
44                 }
45         };
46
47         static final Comparator<JobStat> RESULTSIZE = new Comparator<JobStat>() {
48                 @Override
49                 public int compare(JobStat o1, JobStat o2) {
50                         return new Long(o2.resultSize).compareTo(o1.resultSize);
51                 }
52         };
53
54         private static DateFormat DATE_TIME = SimpleDateFormat.getDateTimeInstance(
55                         DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.UK);
56
57         Services webService;
58         String clusterJobId;
59         String jobname;
60         long start;
61         long finish;
62         long inputSize;
63         long resultSize;
64         boolean isCollected;
65         boolean isCancelled;
66
67         private JobStat(Services webService, String clusterJobId, String jobname,
68                         long start, long finish, long inputSize, long resultSize,
69                         boolean isCancelled, boolean isCollected) {
70                 super();
71                 this.webService = webService;
72                 this.clusterJobId = clusterJobId;
73                 this.jobname = jobname;
74                 this.start = start;
75                 this.finish = finish;
76                 this.inputSize = inputSize;
77                 this.resultSize = resultSize;
78                 this.isCancelled = isCancelled;
79                 this.isCollected = isCollected;
80                 validate();
81         }
82
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);
88         }
89
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;
95                 if (start != null) {
96                         startm = start.getTime();
97                 }
98                 if (finish != null) {
99                         stopm = finish.getTime();
100                 }
101                 return new JobStat(webService, clusterJobId, jobname, startm, stopm,
102                                 inputSize, resultSize, isCancelled, isCollected);
103         }
104
105         void validate() {
106                 if (webService == null) {
107                         throw new AssertionError("webService must be defined!:\n " + this);
108                 }
109                 if (Util.isEmpty(jobname)) {
110                         throw new AssertionError("jobname must be defined!:\n" + this);
111                 }
112         }
113
114         private JobStat(String jobId) {
115                 assert !Util.isEmpty(jobname);
116                 this.jobname = jobId;
117         }
118
119         public static JobStat newIncompleteStat(String jobname) {
120                 return new JobStat(jobname);
121         }
122
123         public boolean isClusterJob() {
124                 return jobname.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX);
125         }
126
127         @Override
128         public int hashCode() {
129                 final int prime = 31;
130                 int result = 1;
131                 result = prime * result + ((jobname == null) ? 0 : jobname.hashCode());
132                 return result;
133         }
134
135         @Override
136         public boolean equals(Object obj) {
137                 if (this == obj)
138                         return true;
139                 if (obj == null)
140                         return false;
141                 if (getClass() != obj.getClass())
142                         return false;
143                 JobStat other = (JobStat) obj;
144                 if (jobname == null) {
145                         if (other.jobname != null)
146                                 return false;
147                 } else if (!jobname.equals(other.jobname))
148                         return false;
149                 return true;
150         }
151
152         public int getRuntime() {
153                 if (start != ExecutionStatCollector.UNDEFINED && finish != ExecutionStatCollector.UNDEFINED) {
154                         return (int) (finish - start) / 1000;
155                 }
156                 return ExecutionStatCollector.UNDEFINED;
157         }
158
159         @Override
160         public String toString() {
161                 return getJobReport();
162         }
163
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";
169                 }
170                 if (finish != ExecutionStatCollector.UNDEFINED) {
171                         report += "Finished " + new Date(finish) + "\n";
172                 }
173                 if (start != ExecutionStatCollector.UNDEFINED && finish != ExecutionStatCollector.UNDEFINED) {
174                         report += "Runtime " + getRuntime() + "\n";
175                 }
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";
181                 return report;
182         }
183
184         /**
185          * Header Job Started Finished Runtime Input Result
186          */
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";
192                 } else {
193                         report += ExecutionStatCollector.UNDEFINED + "\t";
194                 }
195                 if (finish != ExecutionStatCollector.UNDEFINED) {
196                         report += ExecutionStatCollector.DF.format(new Date(finish)) + "\t";
197                 } else {
198                         report += ExecutionStatCollector.UNDEFINED + "\t";
199                 }
200                 if (start != ExecutionStatCollector.UNDEFINED && finish != ExecutionStatCollector.UNDEFINED) {
201                         report += getRuntime() + "\t";
202                 } else {
203                         report += ExecutionStatCollector.UNDEFINED + "\t";
204                 }
205                 report += inputSize + "\t";
206                 report += resultSize + "\t";
207                 report += clusterJobId + "\t";
208                 report += isCollected + "\t";
209                 report += isCancelled + "\t";
210                 return report;
211         }
212
213         public Services getWebService() {
214                 return webService;
215         }
216
217         public String getClusterJobId() {
218                 return clusterJobId;
219         }
220
221         public String getJobname() {
222                 return jobname;
223         }
224
225         public String getEscJobname() {
226                 String[] parts = jobname.split("#");
227                 return parts[0] + "%23" + parts[1];
228         }
229
230         public String getStart() {
231                 if (start != ExecutionStatCollector.UNDEFINED) {
232                         return DATE_TIME.format(new Date(start));
233                 }
234                 return "?";
235         }
236
237         public long getNumericalStart() {
238                         return start;
239         }
240
241         public long getNumericalFinish() {
242                 return finish;
243         }
244
245         public String getFinish() {
246                 if (finish != ExecutionStatCollector.UNDEFINED) {
247                         return DATE_TIME.format(new Date(finish));
248                 }
249                 return "?";
250         }
251
252         public long getInputSize() {
253                 if (inputSize != ExecutionStatCollector.UNDEFINED) {
254                         return inputSize;
255                 }
256                 return 0;
257         }
258
259         public long getResultSize() {
260                 if (resultSize > 0) {
261                         return resultSize;
262                 }
263                 return 0;
264         }
265
266         public boolean hasResult() {
267                 return resultSize > 0;
268         }
269
270         public boolean hasStarted() {
271                 return start != ExecutionStatCollector.UNDEFINED;
272         }
273
274         public boolean getIsCollected() {
275                 return isCollected;
276         }
277
278         public boolean getIsCancelled() {
279                 return isCancelled;
280         }
281
282         public boolean getIsFinished() {
283                 return finish != ExecutionStatCollector.UNDEFINED;
284         }
285 }