From 0581579ade23ad680d16a28a5071ec8f06f84218 Mon Sep 17 00:00:00 2001 From: Manuel Spuhler <manuel.spuhler@chuv.ch> Date: Mon, 14 May 2018 18:20:30 +0200 Subject: [PATCH] cleanup exareme code --- .../eu/hbp/mip/controllers/MiningApi.java | 73 ++++--------------- .../java/eu/hbp/mip/model/ExaremeQuery.java | 73 ------------------- 2 files changed, 16 insertions(+), 130 deletions(-) delete mode 100644 src/main/java/eu/hbp/mip/model/ExaremeQuery.java diff --git a/src/main/java/eu/hbp/mip/controllers/MiningApi.java b/src/main/java/eu/hbp/mip/controllers/MiningApi.java index 625b45279..323aea7c0 100644 --- a/src/main/java/eu/hbp/mip/controllers/MiningApi.java +++ b/src/main/java/eu/hbp/mip/controllers/MiningApi.java @@ -3,7 +3,6 @@ package eu.hbp.mip.controllers; import com.google.gson.Gson; import eu.hbp.mip.akka.WokenClientController; import eu.hbp.mip.configuration.SecurityConfiguration; -import eu.hbp.mip.model.ExaremeQuery; import eu.hbp.mip.model.Mining; import eu.hbp.mip.model.User; import eu.hbp.mip.utils.HTTPUtil; @@ -55,57 +54,23 @@ public class MiningApi extends WokenClientController { LOGGER.info("Run an algorithm"); User user = securityConfiguration.getUser(); - if (isExaremeAlgo(query)) { - LOGGER.info("isExaremeAlgo"); - - String algorithm = query.getAlgorithm().getCode(); - String exaremeQuery = ExaremeQuery.query(query); - String url = queryUrl + "/" + algorithm; - - // TODO: Threaded call - try { - StringBuilder results = new StringBuilder(); - int code = HTTPUtil.sendPost(url, exaremeQuery, results); - LOGGER.info("Results " + results); - - if (code >= 500) { - LOGGER.error("Cannot receive algorithm result from exareme"); - return ResponseEntity.status(code).build(); + return askWokenQuery(query.prepareQuery(user.getUsername()), 120, + result -> { + if (result.error().nonEmpty()) { + LOGGER.error(result.error().get()); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"error\":\"" + result.error().get() + "\"}"); + } else { + Mining mining = new Mining( + unwrap(result.jobId()), + result.node(), + unwrap(result.algorithm()), + result.type().mime(), + Date.from(result.timestamp().toInstant()), + result.data().get().compactPrint() + ); + return ResponseEntity.ok(gson.toJson(mining.jsonify())); } - - Mining mining = new Mining( - "exaremeJobId", - "federation", - algorithm, - "application/json", - new java.util.Date(), - results.toString()); - - return ResponseEntity.ok(gson.toJson(mining.jsonify())); - } catch (IOException e) { - LOGGER.error("Cannot receive algorithm result from exareme" + e.getMessage(), e); - return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build(); - } - } else { - - return askWokenQuery(query.prepareQuery(user.getUsername()), 120, - result -> { - if (result.error().nonEmpty()) { - LOGGER.error(result.error().get()); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"error\":\"" + result.error().get() + "\"}"); - } else { - Mining mining = new Mining( - unwrap(result.jobId()), - result.node(), - unwrap(result.algorithm()), - result.type().mime(), - Date.from(result.timestamp().toInstant()), - result.data().get().compactPrint() - ); - return ResponseEntity.ok(gson.toJson(mining.jsonify())); - } - }); - } + }); } private static String unwrap(Option<String> option) { @@ -114,10 +79,4 @@ public class MiningApi extends WokenClientController { else return ""; } - - private static boolean isExaremeAlgo(eu.hbp.mip.model.MiningQuery query) { - return query.getAlgorithm().getCode().length() > 0 && "WP_".equals( - query.getAlgorithm().getCode().substring(0, 3)); - } - } diff --git a/src/main/java/eu/hbp/mip/model/ExaremeQuery.java b/src/main/java/eu/hbp/mip/model/ExaremeQuery.java deleted file mode 100644 index fef81a5ea..000000000 --- a/src/main/java/eu/hbp/mip/model/ExaremeQuery.java +++ /dev/null @@ -1,73 +0,0 @@ -package eu.hbp.mip.model; - -import com.google.gson.Gson; -import org.springframework.beans.factory.annotation.Value; - -import java.util.LinkedList; -import java.util.List; - - -public class ExaremeQuery { - - private static final Gson gson = new Gson(); - - public static String query(MiningQuery query) { - return query(query.getVariables(), query.getCovariables(), query.getGrouping(), query.getDatasets()); - } - - public static String query(Model model) { - Query query = model.getQuery(); - return query(query.getVariables(), query.getCovariables(), query.getGrouping(), null); - } - - private static String query(List<Variable> variables, List<Variable> covariables, List<Variable> groupings, List<Variable> datasets) { - List<ExaremeQueryElement> queryElements = new LinkedList<>(); - for (Variable var : variables) - { - ExaremeQueryElement el = new ExaremeQueryElement(); - el.setName("variable"); - el.setDesc(""); - el.setValue(var.getCode()); - queryElements.add(el); - } - for (Variable var : covariables) - { - ExaremeQueryElement el = new ExaremeQueryElement(); - el.setName("covariables"); - el.setDesc(""); - el.setValue(var.getCode()); - queryElements.add(el); - } - for (Variable var : groupings) - { - ExaremeQueryElement el = new ExaremeQueryElement(); - el.setName("groupings"); - el.setDesc(""); - el.setValue(var.getCode()); - queryElements.add(el); - } - for (Variable var: datasets) - { - ExaremeQueryElement el = new ExaremeQueryElement(); - el.setName("dataset"); - el.setDesc(""); - el.setValue(var.getCode()); - queryElements.add(el); - } - - ExaremeQueryElement tableEl = new ExaremeQueryElement(); - tableEl.setName("showtable"); - tableEl.setDesc(""); - tableEl.setValue("TotalResults"); - queryElements.add(tableEl); - - ExaremeQueryElement formatEl = new ExaremeQueryElement(); - formatEl.setName("format"); - formatEl.setDesc(""); - formatEl.setValue("True"); - queryElements.add(formatEl); - - return gson.toJson(queryElements); - } - -} -- GitLab