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