Skip to content

Commit

Permalink
created structured json file with url + metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcornier committed Oct 19, 2023
1 parent e7e6fae commit 741b0d6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static ReproVipServiceAsync getInstance() {
void addExecution(Execution execution) throws CoreException;
void updateExecution(String executionID, String newStatus) throws CoreException;
void executionOutputData(String executionID) throws CoreException;
String downloadJsonOutputData(String executionID) throws CoreException;
String downloadJsonOutputData(String executionName, String executionID, String version) throws CoreException;
void createReproVipDirectory(String executionName, String executionID, String version) throws CoreException;
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ReproVipServiceAsync {
void addExecution(Execution execution, AsyncCallback<Void> asyncCallback);
void updateExecution(String executionID, String newStatus, AsyncCallback<Void> asyncCallback);
void executionOutputData(String executionID, AsyncCallback<Void> callback);
void downloadJsonOutputData(String executionID, AsyncCallback<String> callback);
void downloadJsonOutputData(String executionName, String executionID, String version, AsyncCallback<String> callback);
void createReproVipDirectory(String executionName, String execution, String version, AsyncCallback<Void> callback);
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void onClick(MenuItemClickEvent event) {
DownloadOutputDataItem.addClickHandler(new ClickHandler() {
@Override
public void onClick(MenuItemClickEvent event) {
DownloadOutputDataExecution();
DownloadOutputDataExecution(executionName, executionID, version);
}
});

Expand All @@ -63,7 +63,7 @@ public void onSuccess(Void result) {
modal.show("Make execution public", true);
reproVipServiceAsync.updateExecution(executionID, "Public", callback);
}
private void DownloadOutputDataExecution() {
private void DownloadOutputDataExecution(String executionName, String executionID, String version) {
final AsyncCallback<String> callback = new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
Expand All @@ -77,7 +77,7 @@ public void onSuccess(String s) {
}
};
modal.show("Download Outputs", true);
reproVipServiceAsync.downloadJsonOutputData(executionID, callback);
reproVipServiceAsync.downloadJsonOutputData(executionName, executionID, version, callback);
}
public void createReproVipDirectory(String executionName, String executionID, String version) {
reproVipServiceAsync.createReproVipDirectory(executionName, executionID, version, new AsyncCallback<Void>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
Expand Down Expand Up @@ -112,37 +109,51 @@ public ExecutionJobTaskData getExecutionJobTaskData(String executionID) throws B
}
return new ExecutionJobTaskData(jobList);
}

public String createJsonOutputData(String executionID, User currentUser)
public String createJsonOutputData(String executionName, String executionID, String version, User currentUser)
throws ApplicationException, BusinessException {
ExecutionInOutData inOutData = executionOutputData(executionID, currentUser);
//ExecutionJobTaskData jobTaskData = getExecutionJobTaskData(executionID);
List<String> filesToDownload = getFilesToCopyPaths(executionName, executionID, version, currentUser);

Map<String, Object> structuredJson = new HashMap<>();

structuredJson.put("files_to_download", filesToDownload);

Map<String, Object> metadataOuter = new HashMap<>();
Map<String, Object> metadataInner = new HashMap<>();

metadataInner.put("title", "your title");
metadataInner.put("upload_type", "workflow");
metadataInner.put("description", "your description");

//Map<String, Object> combinedData = new HashMap<>();
//combinedData.put("inOutData", inOutData);
//combinedData.put("jobs", jobTaskData.getJobs());
List<Map<String, String>> creators = new ArrayList<>();
Map<String, String> creator = new HashMap<>();
creator.put("name", currentUser.getFullName());
creator.put("affiliation", "your affiliation");
creators.add(creator);

metadataInner.put("creators", creators);
metadataOuter.put("metadata", metadataInner);

structuredJson.put("metadata", metadataOuter);

ObjectMapper objectMapper = new ObjectMapper();
try {
String json = objectMapper.writeValueAsString(inOutData);
//saveJsonToFile(json, executionID);
String json = objectMapper.writeValueAsString(structuredJson);
logger.info(json);

String filePath = "/vip/ReproVip/structuredOutput.json";
saveJsonToFile(json, filePath);

return json;
} catch (JsonProcessingException e) {
throw new ApplicationException(ApplicationException.ApplicationError.valueOf("Failed to convert Output to JSON"), e);
throw new ApplicationException(ApplicationException.ApplicationError.valueOf("Failed to convert structured output to JSON"), e);
} catch (IOException e) {
throw new BusinessException("Failed to save JSON to file", e);
}
}

public void saveJsonToFile(String jsonContent, String executionID) throws IOException {
String filePath = server.getWorkflowsPath() + "/" + executionID + "/inOutPut.json";
File file = new File(filePath);

if (!file.exists()) {
file.createNewFile();
}

try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.write(jsonContent);
public void saveJsonToFile(String jsonContent, String filePath) throws IOException {
try (FileWriter fileWriter = new FileWriter(filePath);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
bufferedWriter.write(jsonContent);
}
}
public void createReproVipDirectory(String executionName, String executionID, String version, User currentUser) {
Expand All @@ -165,7 +176,7 @@ public void createReproVipDirectory(String executionName, String executionID, St
logger.info(outputData.get(0).getPath());

if (outputData != null && !outputData.isEmpty()) {
String outputPath = outputData.get(0).getPath();
String outputPath = "/vip/grida/downloads" + outputData.get(0).getPath();
if (outputPath != null) {
File outputFile = new File(outputPath);
if (outputFile.exists()) {
Expand Down Expand Up @@ -234,4 +245,38 @@ public boolean accept(File dir, String name) {
throw new RuntimeException(e);
}
}

public List<String> getFilesToCopyPaths(String executionName, String executionID, String version, User currentUser) throws BusinessException {
List<String> paths = new ArrayList<>();

List<InOutData> outputData = workflowBusiness.getOutputData(executionID, currentUser.getFolder(), true);
if (outputData != null && !outputData.isEmpty()) {
String outputPath = outputData.get(0).getPath();
if (outputPath != null) {
paths.add(outputPath);
}
}

String workflowPath = String.valueOf(workflowBusiness.getRawApplicationDescriptorPath(currentUser, executionName, version));
if (workflowPath != null && !workflowPath.isEmpty()) {
paths.add(workflowPath);
}

String provenanceDirPath = server.getWorkflowsPath() + "/" + executionID + "/provenance";
File provenanceDir = new File(provenanceDirPath);
if (provenanceDir.exists()) {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".sh.provenance.json");
}
};
File[] matchingFiles = provenanceDir.listFiles(filter);
if (matchingFiles != null && matchingFiles.length > 0) {
paths.add(matchingFiles[0].getAbsolutePath());
}
}

return paths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public String getRawApplicationDescriptorPath(User user, String applicationName,

try {
AppVersion version = applicationDAO.getVersion(applicationName, applicationVersion);
return dataManagerBusiness.getRemoteFile(user, version.getLfn());
return dataManagerBusiness.getRemoteFile(user, version.getJsonLfn());
} catch (DAOException | BusinessException ex) {
throw new BusinessException(WRONG_APPLICATION_DESCRIPTOR, ex, applicationName + "/" + applicationVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public void executionOutputData(String executionID) throws CoreException {
throw new RuntimeException(e);
}
}
public String downloadJsonOutputData(String executionID) throws CoreException {
public String downloadJsonOutputData(String executionName, String executionID, String version) throws CoreException {
try {
User currentUser = getSessionUser();
String json = reproVipBusiness.createJsonOutputData(executionID, currentUser);
String json = reproVipBusiness.createJsonOutputData(executionName, executionID, version, currentUser);
return json;
} catch (BusinessException | ApplicationException e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 741b0d6

Please sign in to comment.