Skip to content

Commit

Permalink
BXC-4688 add processingJobs field to list projects cmd (#113)
Browse files Browse the repository at this point in the history
* add processingJobs field to list projects cmd

* add cmd test

* add job_name constant to velocicriptor and use in ProcessSourceFilesCommand

* add constants for processing_jobs and project_properties, use VelocicroptorRemoteJob constants, fix spelling
  • Loading branch information
krwong authored Oct 24, 2024
1 parent b9f001f commit 47892c4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Integer call() throws Exception {
}

private void validateActionName(String actionName) {
if (!actionName.equals("velocicroptor")) {
if (!actionName.equals(VelocicroptorRemoteJob.JOB_NAME)) {
throw new IllegalArgumentException("Invalid action name provided: " + actionName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
*/
public class VelocicroptorRemoteJob {
private static final Logger log = getLogger(VelocicroptorRemoteJob.class);
protected static final String RESULTS_REL_PATH = "processing/results/velocicroptor";
public static final String RESULTS_REL_PATH = "processing/results/velocicroptor";
private static final String JOB_ID_PATTERN_FORMAT = "ddMMyyyyHHmmssSSS";
private static final DateTimeFormatter JOB_ID_FORMATTER = DateTimeFormatter.ofPattern(JOB_ID_PATTERN_FORMAT)
.withZone(ZoneId.systemDefault());
private static final String JOB_FILENAME = "velocicroptor_job.sh";
public static final String JOB_NAME = "velocicroptor";

private SshClientService sshClientService;
private MigrationProject project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import edu.unc.lib.boxc.migration.cdm.exceptions.InvalidProjectStateException;
import edu.unc.lib.boxc.migration.cdm.jobs.VelocicroptorRemoteJob;
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.model.SourceFilesInfo;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -37,6 +38,10 @@ public class ListProjectsService {
public static final String PROJECT_PATH = "projectPath";
public static final String STATUS = "status";
public static final String ALLOWED_ACTIONS = "allowedActions";
public static final String PROJECT_PROPERTIES = "projectProperties";
public static final String PROCESSING_JOBS = "processingJobs";
public static final String PENDING = "pending";
public static final String COMPLETED = "completed";
private static final Set<String> IMAGE_FORMATS = new HashSet<>(Arrays.asList("tif", "tiff", "jpeg", "jpg", "png",
"gif", "pict", "bmp", "psd", "jp2", "nef", "crw", "cr2", "dng", "raf"));

Expand Down Expand Up @@ -80,13 +85,15 @@ private void listProjectsInDirectory(Path directory, ObjectMapper mapper, ArrayN
allowedActions = mapper.valueToTree(allowedActions(project));
}
JsonNode projectProperties = mapper.readTree(project.getProjectPropertiesPath().toFile());
ObjectNode processingJobs = processingJobs(project, mapper);

// add project info to JSON
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put(PROJECT_PATH, projectPath.toString());
objectNode.put(STATUS, projectStatus);
objectNode.putArray(ALLOWED_ACTIONS).addAll(allowedActions);
objectNode.set("projectProperties", projectProperties);
objectNode.set(PROJECT_PROPERTIES, projectProperties);
objectNode.set(PROCESSING_JOBS, processingJobs);
arrayNode.add(objectNode);
} catch (Exception e) {
log.error(e.getMessage());
Expand Down Expand Up @@ -139,6 +146,23 @@ private List<String> allowedActions(MigrationProject project) throws Exception {
return allowedActions;
}

public ObjectNode processingJobs(MigrationProject project, ObjectMapper mapper) throws Exception {
ObjectNode processingJobs = mapper.createObjectNode();

// velocicropter report status
ObjectNode velocicropterStatus = mapper.createObjectNode();
if (Files.exists(project.getProjectPath().resolve(VelocicroptorRemoteJob.RESULTS_REL_PATH
+ "/job_completed"))) {
velocicropterStatus.put(STATUS, COMPLETED);
processingJobs.set(VelocicroptorRemoteJob.JOB_NAME, velocicropterStatus);
} else if (Files.exists(project.getProjectPath().resolve(VelocicroptorRemoteJob.RESULTS_REL_PATH))) {
velocicropterStatus.put(STATUS, PENDING);
processingJobs.set(VelocicroptorRemoteJob.JOB_NAME, velocicropterStatus);
}

return processingJobs;
}

public MigrationProject initializeProject(Path path) throws Exception {
MigrationProject project = MigrationProjectFactory.loadMigrationProject(path);
propertiesService.setProject(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.unc.lib.boxc.migration.cdm;

import edu.unc.lib.boxc.migration.cdm.jobs.VelocicroptorRemoteJob;
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.services.ArchiveProjectsService;
import edu.unc.lib.boxc.migration.cdm.services.ListProjectsService;
Expand All @@ -9,6 +10,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;

Expand All @@ -33,15 +35,24 @@ public void invalidDirectoryTest() throws Exception {

@Test
public void listProjectTest() throws Exception {
File velocicroptorFile = new File(project.getProjectPath() + "/"
+ VelocicroptorRemoteJob.RESULTS_REL_PATH + "/job_completed");
velocicroptorFile.mkdirs();

String[] args = new String[] {
"-w", String.valueOf(baseDir),
"list_projects" };
executeExpectSuccess(args);

assertOutputContains("\"" + ListProjectsService.PROJECT_PATH + "\" : \"" + Path.of(baseDir + "/" + PROJECT_ID) + "\"");
assertOutputContains("\"" + ListProjectsService.PROJECT_PATH + "\" : \"" + Path.of(baseDir
+ "/" + PROJECT_ID) + "\"");
assertOutputContains("\"" + ListProjectsService.STATUS + "\" : \"initialized\"");
assertOutputContains("\"" + ListProjectsService.ALLOWED_ACTIONS + "\" : [ ]");
assertOutputContains("\"name\" : \"" + PROJECT_ID + "\"");
assertOutputContains("\"" + ListProjectsService.PROCESSING_JOBS + "\"");
assertOutputContains("\"" + VelocicroptorRemoteJob.JOB_NAME + "\"");
assertOutputContains("\"" + ListProjectsService.STATUS + "\" : \""
+ ListProjectsService.COMPLETED + "\"");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.unc.lib.boxc.migration.cdm.jobs.VelocicroptorRemoteJob;
import edu.unc.lib.boxc.migration.cdm.model.MigrationProject;
import edu.unc.lib.boxc.migration.cdm.model.SourceFilesInfo;
import edu.unc.lib.boxc.migration.cdm.test.BxcEnvironmentHelper;
Expand All @@ -15,8 +17,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Arrays;
Expand Down Expand Up @@ -173,6 +177,53 @@ public void listProjectsArchivedTest() throws Exception {
assertEquals(PROJECT_NAME, list.findValue("name").asText());
}

@Test
public void listProjectsWithPendingProjectReportsTest() throws Exception {
File velocicroptorFile = new File(project.getProjectPath() + "/" + VelocicroptorRemoteJob.RESULTS_REL_PATH);
velocicroptorFile.mkdirs();

JsonNode list = service.listProjects(tmpFolder, false);

assertEquals(Path.of(tmpFolder + "/" + PROJECT_NAME).toString(),
list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("initialized", list.findValue(ListProjectsService.STATUS).asText());
assertEquals(jsonArray(Arrays.asList()), list.findValue(ListProjectsService.ALLOWED_ACTIONS));
assertEquals(PROJECT_NAME, list.findValue("name").asText());

ObjectMapper mapper = new ObjectMapper();
ObjectNode testStatus = mapper.createObjectNode();
testStatus.put(ListProjectsService.STATUS, ListProjectsService.PENDING);
ObjectNode testVelocicroptor = mapper.createObjectNode();
testVelocicroptor.set(VelocicroptorRemoteJob.JOB_NAME, testStatus);
assertTrue(Files.exists(Path.of(tmpFolder + "/" + PROJECT_NAME + "/"
+ VelocicroptorRemoteJob.RESULTS_REL_PATH)));
assertEquals(testVelocicroptor, list.findValue(ListProjectsService.PROCESSING_JOBS));
}

@Test
public void listProjectsWithCompletedProjectReportsTest() throws Exception {
File velocicroptorFile = new File(project.getProjectPath() + "/"
+ VelocicroptorRemoteJob.RESULTS_REL_PATH + "/job_completed");
velocicroptorFile.mkdirs();

JsonNode list = service.listProjects(tmpFolder, false);

assertEquals(Path.of(tmpFolder + "/" + PROJECT_NAME).toString(),
list.findValue(ListProjectsService.PROJECT_PATH).asText());
assertEquals("initialized", list.findValue(ListProjectsService.STATUS).asText());
assertEquals(jsonArray(Arrays.asList()), list.findValue(ListProjectsService.ALLOWED_ACTIONS));
assertEquals(PROJECT_NAME, list.findValue("name").asText());

ObjectMapper mapper = new ObjectMapper();
ObjectNode testStatus = mapper.createObjectNode();
testStatus.put(ListProjectsService.STATUS, ListProjectsService.COMPLETED);
ObjectNode testVelocicroptor = mapper.createObjectNode();
testVelocicroptor.set(VelocicroptorRemoteJob.JOB_NAME, testStatus);
assertTrue(Files.exists(Path.of(tmpFolder + "/" + PROJECT_NAME + "/"
+ VelocicroptorRemoteJob.RESULTS_REL_PATH + "/job_completed")));
assertEquals(testVelocicroptor, list.findValue(ListProjectsService.PROCESSING_JOBS));
}

@Test
public void listProjectsMultipleProjectsTest() throws Exception {
// project one ingested
Expand Down

0 comments on commit 47892c4

Please sign in to comment.