From a42b976a397c116be052cc79914a8bcbe3c96077 Mon Sep 17 00:00:00 2001 From: Vedran Kasalica Date: Wed, 16 Oct 2024 12:25:24 +0200 Subject: [PATCH] Add code that calls pubmetric --- .github/workflows/publish.yaml | 2 + .../restape/ToolBenchmarkingAPIs.java | 77 ++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 09c2134..abc8f17 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -4,6 +4,8 @@ on: workflow_dispatch: # This line adds the manual trigger push: branches: [ "main" ] + pull_request: + branches: [ "main" ] env: REGISTRY: ghcr.io diff --git a/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java b/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java index 855d099..a15d19d 100644 --- a/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java +++ b/src/main/java/nl/esciencecenter/restape/ToolBenchmarkingAPIs.java @@ -7,6 +7,14 @@ import java.util.List; import okhttp3.OkHttpClient; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -23,6 +31,7 @@ import nl.esciencecenter.models.benchmarks.BenchmarkBase; import nl.uu.cs.ape.solver.solutionStructure.SolutionWorkflow; import nl.uu.cs.ape.solver.solutionStructure.SolutionsList; +import nl.uu.cs.ape.solver.solutionStructure.cwl.DefaultCWLCreator; import nl.uu.cs.ape.utils.APEFiles; /** @@ -63,7 +72,14 @@ static boolean computeBenchmarks(SolutionsList candidateSolutions, String runID) * Compute the benchmarks for the workflow and save them in a JSON file. */ JSONObject workflowBenchmarks = computeWorkflowSpecificFields(workflow, runID); - workflowBenchmarks.put("benchmarks", computeWorkflowBenchmarks(workflow)); + JSONArray benchmarks = computeWorkflowBenchmarks(workflow); + JSONObject additionalBenchmarks = getPubmetricBenchmarks(workflow); + + if (!additionalBenchmarks.isEmpty() && additionalBenchmarks.has("benchmarks")) { + additionalBenchmarks.getJSONArray("benchmarks").forEach(benchmark -> benchmarks.put(benchmark)); + } + + workflowBenchmarks.put("benchmarks", benchmarks); String titleBenchmark = workflow.getFileName() + ".json"; Path solFolder = candidateSolutions.getRunConfiguration().getSolutionDirPath2CWL(); @@ -79,6 +95,65 @@ static boolean computeBenchmarks(SolutionsList candidateSolutions, String runID) return true; } + /** + * Get the Pubmetric benchmarks for the workflow. + * @param workflow + * @return + */ + public static JSONObject getPubmetricBenchmarks(SolutionWorkflow workflow) { + // Generate the CWL file content + DefaultCWLCreator cwlCreator = new DefaultCWLCreator(workflow); + String cwlFileContent = cwlCreator.generate(); + + // Convert the CWL content to a byte array + byte[] cwlFileBytes = cwlFileContent.getBytes(); + + return sendPostToPubmetric(cwlFileBytes); + } + + + /** + * Send a POST request to the Pubmetric API to get the benchmarks for the CWL file. + * @param cwlFileBytes + * @return + */ + public static JSONObject sendPostToPubmetric(byte[] cwlFileBytes) { + // Create the HTTP client + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost uploadFile = new HttpPost("http://pubmetric:8000/score_workflow/"); + + // Create a multipart entity with the CWL file + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); + builder.addBinaryBody("cwl_file", cwlFileBytes, org.apache.http.entity.ContentType.DEFAULT_BINARY, + "workflow.cwl"); + + HttpEntity multipart = builder.build(); + uploadFile.setEntity(multipart); + + // Execute the request + CloseableHttpResponse response; + try { + response = httpClient.execute(uploadFile); + HttpEntity responseEntity = response.getEntity(); + + // Print the response + if (responseEntity != null) { + String responseString = EntityUtils.toString(responseEntity); + return new JSONObject(responseString); + } + + // Close resources + response.close(); + httpClient.close(); + } catch (IOException e) { + log.error("Error while fetching the Pubmetric benchmarks"); + } catch (JSONException e) { + log.error("Error while parsing the Pubmetric benchmarks"); + } + + return new JSONObject(); + } + /** * Compute the benchmarks (based on bio.tools and OpenEBench APIs) for the * workflows and return it in JSON format.