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