Stat collection and display done
[jabaws.git] / webservices / compbio / stat / collector / ExecutionStatCollector.java
index bf3130a..31a5747 100644 (file)
@@ -16,6 +16,7 @@ import java.util.Set;
 import org.apache.log4j.Logger;\r
 \r
 import compbio.engine.client.Executable;\r
+import compbio.engine.client.PathValidator;\r
 import compbio.metadata.JobStatus;\r
 import compbio.util.FileUtil;\r
 import compbio.ws.client.Services;\r
@@ -57,6 +58,7 @@ public class ExecutionStatCollector implements Runnable {
 \r
        static SimpleDateFormat DF = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");\r
 \r
+       final private File workDirectory;\r
        final private List<JobStat> stats;\r
        /**\r
         * Consider the job that has been working for longer than timeOutInHours\r
@@ -73,22 +75,18 @@ public class ExecutionStatCollector implements Runnable {
        public ExecutionStatCollector(String workDirectory, int timeOutInHours) {\r
                log.info("Starting stat collector for directory: " + workDirectory);\r
                log.info("Maximum allowed runtime(h): " + timeOutInHours);\r
-               File[] files = FileUtil.getFiles(workDirectory, directories);\r
+               if (!PathValidator.isValidDirectory(workDirectory)) {\r
+                       throw new IllegalArgumentException("workDirectory '"\r
+                                       + workDirectory + "' does not exist!");\r
+               }\r
+               this.workDirectory = new File(workDirectory);\r
                stats = new ArrayList<JobStat>();\r
-               assert timeOutInHours > 0;\r
-               this.timeOutInHours = timeOutInHours;\r
-               for (File file : files) {\r
-                       JobDirectory jd = new JobDirectory(file);\r
-                       JobStat jstat = jd.getJobStat();\r
-                       // Do not record stats on the job that has not completed yet\r
-                       if (hasCompleted(jd)) {\r
-                               stats.add(jstat);\r
-                       } else {\r
-                               log.debug("Skipping the job: " + jstat);\r
-                               log.debug("As it has not completed yet");\r
-                       }\r
-                       // System.out.println(jd.getJobStat().getJobReportTabulated());\r
+               if (timeOutInHours <= 0) {\r
+                       throw new IllegalArgumentException(\r
+                                       "Timeout value must be greater than 0! Given value: "\r
+                                                       + timeOutInHours);\r
                }\r
+               this.timeOutInHours = timeOutInHours;\r
        }\r
 \r
        boolean hasCompleted(JobDirectory jd) {\r
@@ -104,11 +102,11 @@ public class ExecutionStatCollector implements Runnable {
                return ((System.currentTimeMillis() - jd.jobdir.lastModified()) / (1000 * 60 * 60)) > timeOutInHours;\r
        }\r
 \r
-       public StatProcessor getStats() {\r
+       StatProcessor getStats() {\r
                return new StatProcessor(stats);\r
        }\r
 \r
-       public void writeStatToDB() throws SQLException {\r
+       void writeStatToDB() throws SQLException {\r
                Set<JobStat> rjobs = new HashSet<JobStat>(stats);\r
                StatDB statdb = new StatDB();\r
                log.debug("Removing records that has already been recorded");\r
@@ -125,10 +123,7 @@ public class ExecutionStatCollector implements Runnable {
         */\r
 \r
        /**\r
-        * \r
-        * @param args\r
-        * @throws IOException\r
-        * @throws SQLException\r
+        * Not in use\r
         */\r
        public static void main(String[] args) throws IOException, SQLException {\r
 \r
@@ -168,14 +163,14 @@ public class ExecutionStatCollector implements Runnable {
                File jobdir;\r
                Map<String, File> files = new HashMap<String, File>();\r
 \r
-               public JobDirectory(File directory) {\r
+               JobDirectory(File directory) {\r
                        this.jobdir = directory;\r
                        for (File f : jobdir.listFiles()) {\r
                                files.put(f.getName(), f);\r
                        }\r
                }\r
 \r
-               public boolean hasStatus(JobStatus status) {\r
+               boolean hasStatus(JobStatus status) {\r
                        return files.containsKey(status.toString());\r
                }\r
 \r
@@ -211,8 +206,9 @@ public class ExecutionStatCollector implements Runnable {
                                        clustjobId = FileUtil.readFileToString(jobid);\r
                                }\r
                        } catch (IOException ioe) {\r
-                               ioe.printStackTrace();\r
-                               // TODO LOG\r
+                               log.error(\r
+                                               "IO Exception while reading the content of JOBID file for job "\r
+                                                               + jobid, ioe);\r
                        }\r
                        return clustjobId.trim();\r
                }\r
@@ -304,13 +300,30 @@ public class ExecutionStatCollector implements Runnable {
                                return false;\r
                        return true;\r
                }\r
+       }\r
 \r
+       private void collectStatistics() {\r
+               File[] files = workDirectory.listFiles(directories);\r
+               for (File file : files) {\r
+                       JobDirectory jd = new JobDirectory(file);\r
+                       JobStat jstat = jd.getJobStat();\r
+                       // Do not record stats on the job that has not completed yet\r
+                       if (hasCompleted(jd)) {\r
+                               stats.add(jstat);\r
+                       } else {\r
+                               log.debug("Skipping the job: " + jstat);\r
+                               log.debug("As it has not completed yet");\r
+                       }\r
+                       // System.out.println(jd.getJobStat().getJobReportTabulated());\r
+               }\r
        }\r
 \r
        @Override\r
        public void run() {\r
                log.info("Started updating statistics at " + new Date());\r
 \r
+               collectStatistics();\r
+\r
                StatProcessor local_stats = getStats();\r
                log.info("Found " + local_stats.getJobNumber() + " jobs!");\r
                try {\r
@@ -321,5 +334,4 @@ public class ExecutionStatCollector implements Runnable {
                }\r
                log.info("Finished updating statistics at " + new Date());\r
        }\r
-\r
 }\r