From 03cb32eb33a53066b00562c14d548f3a181bf29e Mon Sep 17 00:00:00 2001
From: Petr Bainar <petr.bainar@hotmail.com>
Date: Wed, 31 Jan 2018 09:48:39 +0100
Subject: [PATCH] iss1011: committing preliminary implementation of the
 getState method

---
 .../core/BenchmarkJobManager.java             |  4 +-
 .../core/SPIMComputationAccessor.java         |  2 +-
 .../core/TaskComputation.java                 | 44 +++++++++++++++----
 3 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
index e0480df0..3f636d93 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/BenchmarkJobManager.java
@@ -68,8 +68,8 @@ public class BenchmarkJobManager {
 			}
 			
 			@Override
-			public boolean fileExists(Path filePath) {
-				File f = new File(filePath.toString());
+			public boolean fileExists(String filePath) {
+				File f = new File(filePath);
 				return f.exists() && !f.isDirectory();
 			}
 		};
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java
index 39151ca4..5c01528d 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/SPIMComputationAccessor.java
@@ -5,5 +5,5 @@ import java.nio.file.Path;
 import cz.it4i.fiji.haas.HaaSOutputHolder;
 
 public interface SPIMComputationAccessor extends HaaSOutputHolder {
-	boolean fileExists(Path fileName);
+	boolean fileExists(String fileName);
 }
diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/TaskComputation.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/TaskComputation.java
index af0bec07..8d7969e0 100644
--- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/TaskComputation.java
+++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/TaskComputation.java
@@ -24,6 +24,7 @@ public class TaskComputation {
 		}
 	}
 
+	private final SPIMComputationAccessor computationAccessor;
 	private final Task task;
 	private final int timepoint;
 	private final Collection<String> inputs;
@@ -34,14 +35,16 @@ public class TaskComputation {
 	//TASK 1011 what states will be defined and how it will be defined
 	private JobState state = JobState.Unknown;
 	
-	public TaskComputation(SPIMComputationAccessor outputHolder, Task task, int timepoint) {
+	public TaskComputation(SPIMComputationAccessor computationAccessor, Task task, int timepoint) {
+		this.computationAccessor = computationAccessor;
 		this.task = task;
-		this.timepoint = timepoint;		
-		ParsedTaskComputationValues parsedValues = parseStuff(outputHolder);
+		this.timepoint = timepoint;
+		ParsedTaskComputationValues parsedValues = parseStuff(computationAccessor);
 		this.inputs = parsedValues.inputs;
 		this.outputs = parsedValues.outputs;
 		this.logs = parsedValues.logs;
 		this.id = parsedValues.id;
+		updateState();
 	}
 
 	public JobState getState() {
@@ -52,14 +55,39 @@ public class TaskComputation {
 	private void updateState() {
 		//TASK 1011 This should never happen, add some error handling to resolveId()
 		if (id == null) {
+			state = JobState.Unknown;
 			return;
 		}
 		
-		//String snakeOutput = outputHolder.getActualOutput();
-
-		//TASK 1011 
-		//resolve if job is queued (defined id), started (exists log file), finished (in log is Finished job 10.) or
-		//or failed (some error in log)
+		state = JobState.Queued;
+		
+		// Check whether a log file exists
+		if (!logs.stream().anyMatch(logFile -> computationAccessor.fileExists(logFile))) {
+			return;
+		}
+		
+		state = JobState.Running;
+		
+		// Check whether the corresponding job has finished
+		final String OUTPUT_PARSING_FINISHED_JOB = "Finished job ";
+		final String desiredPatternFinishedJob = OUTPUT_PARSING_FINISHED_JOB + id.toString();
+		final String OUTPUT_PARSING_ERRONEOUS_JOB = "Error job ";
+		final String desiredPatternErroneousJob = OUTPUT_PARSING_ERRONEOUS_JOB + id.toString();
+		String currentLine;
+		Scanner scanner = new Scanner(computationAccessor.getActualOutput());
+		while (scanner.hasNextLine()) {
+			currentLine = scanner.nextLine();
+			if (currentLine.contains(desiredPatternErroneousJob)) {
+				state = JobState.Failed;
+				break;
+			} else if (currentLine.contains(desiredPatternFinishedJob)) {
+				state = JobState.Finished;
+				break;
+			}
+		}
+		scanner.close();
+		
+		return;
 	}
 
 	private Long getId() {
-- 
GitLab