Adding JABA web services usage statistics web application. Stat database is to follow
[jabaws.git] / webservices / compbio / stat / collector / JobStat.java
diff --git a/webservices/compbio/stat/collector/JobStat.java b/webservices/compbio/stat/collector/JobStat.java
new file mode 100644 (file)
index 0000000..67767dc
--- /dev/null
@@ -0,0 +1,261 @@
+package compbio.stat.collector;\r
+\r
+import java.sql.Timestamp;\r
+import java.text.SimpleDateFormat;\r
+import java.util.Comparator;\r
+import java.util.Date;\r
+\r
+import compbio.engine.client.ConfExecutable;\r
+import compbio.util.Util;\r
+import compbio.ws.client.Services;\r
+\r
+public class JobStat {\r
+\r
+       static final Comparator<JobStat> RUNTIME = new Comparator<JobStat>() {\r
+               @Override\r
+               public int compare(JobStat o1, JobStat o2) {\r
+                       return new Integer(o2.getRuntime()).compareTo(o1.getRuntime());\r
+               }\r
+       };\r
+\r
+       static final Comparator<JobStat> STARTTIME = new Comparator<JobStat>() {\r
+               @Override\r
+               public int compare(JobStat o1, JobStat o2) {\r
+                       return new Long(o1.start).compareTo(o2.start);\r
+               }\r
+       };\r
+\r
+       static final Comparator<JobStat> RESULTSIZE = new Comparator<JobStat>() {\r
+               @Override\r
+               public int compare(JobStat o1, JobStat o2) {\r
+                       return new Long(o2.resultSize).compareTo(o1.resultSize);\r
+               }\r
+       };\r
+\r
+       Services webService;\r
+       String clusterJobId;\r
+       String jobname;\r
+       long start;\r
+       long finish;\r
+       long inputSize;\r
+       long resultSize;\r
+       boolean isCollected;\r
+       boolean isCancelled;\r
+\r
+       private JobStat(Services webService, String clusterJobId, String jobname,\r
+                       long start, long finish, long inputSize, long resultSize,\r
+                       boolean isCancelled, boolean isCollected) {\r
+               super();\r
+               this.webService = webService;\r
+               this.clusterJobId = clusterJobId;\r
+               this.jobname = jobname;\r
+               this.start = start;\r
+               this.finish = finish;\r
+               this.inputSize = inputSize;\r
+               this.resultSize = resultSize;\r
+               this.isCancelled = isCancelled;\r
+               this.isCollected = isCollected;\r
+               validate();\r
+       }\r
+\r
+       static JobStat newInstance(Services webService, String clusterJobId,\r
+                       String jobname, long start, long finish, long inputSize,\r
+                       long resultSize, boolean isCancelled, boolean isCollected) {\r
+               return new JobStat(webService, clusterJobId, jobname, start, finish,\r
+                               inputSize, resultSize, isCancelled, isCollected);\r
+       }\r
+\r
+       static JobStat newInstance(Services webService, String clusterJobId,\r
+                       String jobname, Timestamp start, Timestamp finish, long inputSize,\r
+                       long resultSize, boolean isCancelled, boolean isCollected) {\r
+               long startm = ExecutionStatCollector.UNDEFINED;\r
+               long stopm = ExecutionStatCollector.UNDEFINED;\r
+               if (start != null) {\r
+                       startm = start.getTime();\r
+               }\r
+               if (finish != null) {\r
+                       stopm = finish.getTime();\r
+               }\r
+               return new JobStat(webService, clusterJobId, jobname, startm, stopm,\r
+                               inputSize, resultSize, isCancelled, isCollected);\r
+       }\r
+\r
+       void validate() {\r
+               if (webService == null) {\r
+                       throw new AssertionError("webService must be defined!:\n " + this);\r
+               }\r
+               if (Util.isEmpty(jobname)) {\r
+                       throw new AssertionError("jobname must be defined!:\n" + this);\r
+               }\r
+       }\r
+\r
+       private JobStat(String jobId) {\r
+               assert !Util.isEmpty(jobname);\r
+               this.jobname = jobId;\r
+       }\r
+\r
+       static JobStat newIncompleteStat(String jobname) {\r
+               return new JobStat(jobname);\r
+       }\r
+\r
+       public boolean isClusterJob() {\r
+               return jobname.startsWith(ConfExecutable.CLUSTER_TASK_ID_PREFIX);\r
+       }\r
+\r
+       @Override\r
+       public int hashCode() {\r
+               final int prime = 31;\r
+               int result = 1;\r
+               result = prime * result + ((jobname == null) ? 0 : jobname.hashCode());\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (this == obj)\r
+                       return true;\r
+               if (obj == null)\r
+                       return false;\r
+               if (getClass() != obj.getClass())\r
+                       return false;\r
+               JobStat other = (JobStat) obj;\r
+               if (jobname == null) {\r
+                       if (other.jobname != null)\r
+                               return false;\r
+               } else if (!jobname.equals(other.jobname))\r
+                       return false;\r
+               return true;\r
+       }\r
+\r
+       public int getRuntime() {\r
+               if (start != ExecutionStatCollector.UNDEFINED\r
+                               && finish != ExecutionStatCollector.UNDEFINED) {\r
+                       return (int) (finish - start) / 1000;\r
+               }\r
+               return ExecutionStatCollector.UNDEFINED;\r
+       }\r
+\r
+       @Override\r
+       public String toString() {\r
+               return getJobReport();\r
+       }\r
+\r
+       String getJobReport() {\r
+               String report = "WS: " + webService + "\n";\r
+               report += "JOB: " + jobname + "\n";\r
+               if (start != ExecutionStatCollector.UNDEFINED) {\r
+                       report += "Started " + new Date(start) + "\n";\r
+               }\r
+               if (finish != ExecutionStatCollector.UNDEFINED) {\r
+                       report += "Finished " + new Date(finish) + "\n";\r
+               }\r
+               if (start != ExecutionStatCollector.UNDEFINED\r
+                               && finish != ExecutionStatCollector.UNDEFINED) {\r
+                       report += "Runtime " + getRuntime() + "\n";\r
+               }\r
+               report += "Input size " + inputSize + "\n";\r
+               report += "Result size " + resultSize + "\n";\r
+               report += "ClusterJobID " + clusterJobId + "\n";\r
+               report += "Collected? " + isCollected + "\n";\r
+               report += "Cancelled? " + isCancelled + "\n";\r
+               return report;\r
+       }\r
+\r
+       /**\r
+        * Header Job Started Finished Runtime Input Result\r
+        */\r
+       String getJobReportTabulated() {\r
+               String report = webService + "\t";\r
+               report += jobname + "\t";\r
+               if (start != ExecutionStatCollector.UNDEFINED) {\r
+                       report += ExecutionStatCollector.DF.format(new Date(start)) + "\t";\r
+               } else {\r
+                       report += ExecutionStatCollector.UNDEFINED + "\t";\r
+               }\r
+               if (finish != ExecutionStatCollector.UNDEFINED) {\r
+                       report += ExecutionStatCollector.DF.format(new Date(finish)) + "\t";\r
+               } else {\r
+                       report += ExecutionStatCollector.UNDEFINED + "\t";\r
+               }\r
+               if (start != ExecutionStatCollector.UNDEFINED\r
+                               && finish != ExecutionStatCollector.UNDEFINED) {\r
+                       report += getRuntime() + "\t";\r
+               } else {\r
+                       report += ExecutionStatCollector.UNDEFINED + "\t";\r
+               }\r
+               report += inputSize + "\t";\r
+               report += resultSize + "\t";\r
+               report += clusterJobId + "\t";\r
+               report += isCollected + "\t";\r
+               report += isCancelled + "\t";\r
+               return report;\r
+       }\r
+\r
+       public Services getWebService() {\r
+               return webService;\r
+       }\r
+\r
+       public String getClusterJobId() {\r
+               return clusterJobId;\r
+       }\r
+\r
+       public String getJobname() {\r
+               return jobname;\r
+       }\r
+\r
+       public String getEscJobname() {\r
+               String[] parts = jobname.split("#");\r
+               return parts[0] + "%23" + parts[1];\r
+       }\r
+\r
+       public String getStart() {\r
+               if (start != ExecutionStatCollector.UNDEFINED) {\r
+                       return SimpleDateFormat.getDateTimeInstance().format(\r
+                                       new Date(start));\r
+               }\r
+               return "?";\r
+       }\r
+\r
+       public String getFinish() {\r
+               if (finish != ExecutionStatCollector.UNDEFINED) {\r
+                       return SimpleDateFormat.getDateTimeInstance().format(\r
+                                       new Date(finish));\r
+               }\r
+               return "?";\r
+       }\r
+\r
+       public long getInputSize() {\r
+               if (inputSize != ExecutionStatCollector.UNDEFINED) {\r
+                       return inputSize / 1000;\r
+               }\r
+               return 0;\r
+       }\r
+\r
+       public long getResultSize() {\r
+               if (resultSize != ExecutionStatCollector.UNDEFINED) {\r
+                       return resultSize / 1000;\r
+               }\r
+               return 0;\r
+       }\r
+\r
+       public boolean hasResult() {\r
+               return resultSize != ExecutionStatCollector.UNDEFINED;\r
+       }\r
+\r
+       public boolean hasStarted() {\r
+               return start != ExecutionStatCollector.UNDEFINED;\r
+       }\r
+\r
+       public boolean getIsCollected() {\r
+               return isCollected;\r
+       }\r
+\r
+       public boolean getIsCancelled() {\r
+               return isCancelled;\r
+       }\r
+\r
+       public boolean getIsFinished() {\r
+               return finish != ExecutionStatCollector.UNDEFINED;\r
+       }\r
+\r
+}
\ No newline at end of file