6f688454d9ef3df89cbf3005d95c23471efe2879
[jabaws.git] / webservices / compbio / stat / collector / JobStat.java
1 package compbio.stat.collector;\r
2 \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
9 \r
10 import compbio.engine.client.ConfExecutable;\r
11 import compbio.util.Util;\r
12 import compbio.ws.client.Services;\r
13 \r
14 public class JobStat {\r
15 \r
16         static final Comparator<JobStat> RUNTIME = new Comparator<JobStat>() {\r
17                 @Override\r
18                 public int compare(JobStat o1, JobStat o2) {\r
19                         return new Integer(o2.getRuntime()).compareTo(o1.getRuntime());\r
20                 }\r
21         };\r
22 \r
23         static final Comparator<JobStat> STARTTIME = new Comparator<JobStat>() {\r
24                 @Override\r
25                 public int compare(JobStat o1, JobStat o2) {\r
26                         return new Long(o1.start).compareTo(o2.start);\r
27                 }\r
28         };\r
29 \r
30         static final Comparator<JobStat> RESULTSIZE = new Comparator<JobStat>() {\r
31                 @Override\r
32                 public int compare(JobStat o1, JobStat o2) {\r
33                         return new Long(o2.resultSize).compareTo(o1.resultSize);\r
34                 }\r
35         };\r
36 \r
37         private static DateFormat DATE_TIME = SimpleDateFormat.getDateTimeInstance(\r
38                         DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.UK);\r
39 \r
40         Services webService;\r
41         String clusterJobId;\r
42         String jobname;\r
43         long start;\r
44         long finish;\r
45         long inputSize;\r
46         long resultSize;\r
47         boolean isCollected;\r
48         boolean isCancelled;\r
49 \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
53                 super();\r
54                 this.webService = webService;\r
55                 this.clusterJobId = clusterJobId;\r
56                 this.jobname = jobname;\r
57                 this.start = start;\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
63                 validate();\r
64         }\r
65 \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
71         }\r
72 \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
80                 }\r
81                 if (finish != null) {\r
82                         stopm = finish.getTime();\r
83                 }\r
84                 return new JobStat(webService, clusterJobId, jobname, startm, stopm,\r
85                                 inputSize, resultSize, isCancelled, isCollected);\r
86         }\r
87 \r
88         void validate() {\r
89                 if (webService == null) {\r
90                         throw new AssertionError("webService must be defined!:\n " + this);\r
91                 }\r
92                 if (Util.isEmpty(jobname)) {\r
93                         throw new AssertionError("jobname must be defined!:\n" + this);\r
94                 }\r
95         }\r
96 \r
97         private JobStat(String jobId) {\r
98                 assert !Util.isEmpty(jobname);\r
99                 this.jobname = jobId;\r
100         }\r
101 \r
102         static JobStat newIncompleteStat(String jobname) {\r
103                 return new JobStat(jobname);\r
104         }\r
105 \r
106         public boolean isClusterJob() {\r
107                 return jobname.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX);\r
108         }\r
109 \r
110         @Override\r
111         public int hashCode() {\r
112                 final int prime = 31;\r
113                 int result = 1;\r
114                 result = prime * result + ((jobname == null) ? 0 : jobname.hashCode());\r
115                 return result;\r
116         }\r
117 \r
118         @Override\r
119         public boolean equals(Object obj) {\r
120                 if (this == obj)\r
121                         return true;\r
122                 if (obj == null)\r
123                         return false;\r
124                 if (getClass() != obj.getClass())\r
125                         return false;\r
126                 JobStat other = (JobStat) obj;\r
127                 if (jobname == null) {\r
128                         if (other.jobname != null)\r
129                                 return false;\r
130                 } else if (!jobname.equals(other.jobname))\r
131                         return false;\r
132                 return true;\r
133         }\r
134 \r
135         public int getRuntime() {\r
136                 if (start != ExecutionStatCollector.UNDEFINED\r
137                                 && finish != ExecutionStatCollector.UNDEFINED) {\r
138                         return (int) (finish - start) / 1000;\r
139                 }\r
140                 return ExecutionStatCollector.UNDEFINED;\r
141         }\r
142 \r
143         @Override\r
144         public String toString() {\r
145                 return getJobReport();\r
146         }\r
147 \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
153                 }\r
154                 if (finish != ExecutionStatCollector.UNDEFINED) {\r
155                         report += "Finished " + new Date(finish) + "\n";\r
156                 }\r
157                 if (start != ExecutionStatCollector.UNDEFINED\r
158                                 && finish != ExecutionStatCollector.UNDEFINED) {\r
159                         report += "Runtime " + getRuntime() + "\n";\r
160                 }\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
166                 return report;\r
167         }\r
168 \r
169         /**\r
170          * Header Job Started Finished Runtime Input Result\r
171          */\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
177                 } else {\r
178                         report += ExecutionStatCollector.UNDEFINED + "\t";\r
179                 }\r
180                 if (finish != ExecutionStatCollector.UNDEFINED) {\r
181                         report += ExecutionStatCollector.DF.format(new Date(finish)) + "\t";\r
182                 } else {\r
183                         report += ExecutionStatCollector.UNDEFINED + "\t";\r
184                 }\r
185                 if (start != ExecutionStatCollector.UNDEFINED\r
186                                 && finish != ExecutionStatCollector.UNDEFINED) {\r
187                         report += getRuntime() + "\t";\r
188                 } else {\r
189                         report += ExecutionStatCollector.UNDEFINED + "\t";\r
190                 }\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
196                 return report;\r
197         }\r
198 \r
199         public Services getWebService() {\r
200                 return webService;\r
201         }\r
202 \r
203         public String getClusterJobId() {\r
204                 return clusterJobId;\r
205         }\r
206 \r
207         public String getJobname() {\r
208                 return jobname;\r
209         }\r
210 \r
211         public String getEscJobname() {\r
212                 String[] parts = jobname.split("#");\r
213                 return parts[0] + "%23" + parts[1];\r
214         }\r
215 \r
216         public String getStart() {\r
217                 if (start != ExecutionStatCollector.UNDEFINED) {\r
218                         return DATE_TIME.format(new Date(start));\r
219                 }\r
220                 return "?";\r
221         }\r
222 \r
223         public String getFinish() {\r
224                 if (finish != ExecutionStatCollector.UNDEFINED) {\r
225                         return DATE_TIME.format(new Date(finish));\r
226                 }\r
227                 return "?";\r
228         }\r
229 \r
230         public long getInputSize() {\r
231                 if (inputSize != ExecutionStatCollector.UNDEFINED) {\r
232                         return inputSize;\r
233                 }\r
234                 return 0;\r
235         }\r
236 \r
237         public long getResultSize() {\r
238                 if (resultSize > 0) {\r
239                         return resultSize;\r
240                 }\r
241                 return 0;\r
242         }\r
243 \r
244         public boolean hasResult() {\r
245                 return resultSize > 0;\r
246         }\r
247 \r
248         public boolean hasStarted() {\r
249                 return start != ExecutionStatCollector.UNDEFINED;\r
250         }\r
251 \r
252         public boolean getIsCollected() {\r
253                 return isCollected;\r
254         }\r
255 \r
256         public boolean getIsCancelled() {\r
257                 return isCancelled;\r
258         }\r
259 \r
260         public boolean getIsFinished() {\r
261                 return finish != ExecutionStatCollector.UNDEFINED;\r
262         }\r
263 \r
264 }