Skip to content
Snippets Groups Projects
Commit 18c7073a authored by Manuel Spuhler's avatar Manuel Spuhler
Browse files

Exareme mining method

parent fe10a291
No related branches found
No related tags found
No related merge requests found
...@@ -3,8 +3,10 @@ package eu.hbp.mip.controllers; ...@@ -3,8 +3,10 @@ package eu.hbp.mip.controllers;
import com.google.gson.Gson; import com.google.gson.Gson;
import eu.hbp.mip.akka.WokenClientController; import eu.hbp.mip.akka.WokenClientController;
import eu.hbp.mip.configuration.SecurityConfiguration; import eu.hbp.mip.configuration.SecurityConfiguration;
import eu.hbp.mip.model.ExaremeQuery;
import eu.hbp.mip.model.Mining; import eu.hbp.mip.model.Mining;
import eu.hbp.mip.model.User; import eu.hbp.mip.model.User;
import eu.hbp.mip.utils.HTTPUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -18,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -18,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.sql.Date; import java.sql.Date;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
...@@ -43,6 +46,39 @@ public class MiningApi extends WokenClientController { ...@@ -43,6 +46,39 @@ public class MiningApi extends WokenClientController {
LOGGER.info("Run an algorithm"); LOGGER.info("Run an algorithm");
User user = securityConfiguration.getUser(); User user = securityConfiguration.getUser();
if (isExaremeAlgo(query)) {
LOGGER.info("isExaremeAlgo");
String algorithm = query.getAlgorithm().getCode();
String exaremeQuery = ExaremeQuery.query(query);
String url = ExaremeQuery.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();
}
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();
}
}
return askWokenQuery(query.prepareQuery(user.getUsername()), 120, return askWokenQuery(query.prepareQuery(user.getUsername()), 120,
result -> { result -> {
if (result.error().nonEmpty()) { if (result.error().nonEmpty()) {
...@@ -62,4 +98,9 @@ public class MiningApi extends WokenClientController { ...@@ -62,4 +98,9 @@ public class MiningApi extends WokenClientController {
}); });
} }
private static boolean isExaremeAlgo(eu.hbp.mip.model.MiningQuery query) {
return query.getAlgorithm().getCode().length() > 0 && "WP_".equals(
query.getAlgorithm().getCode().substring(0, 3));
}
} }
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 {
@Value("#{'${services.query.miningExaremeUrl:http://hbps2.chuv.ch:9090/mining/query}'}")
public static String queryUrl;
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);
}
}
...@@ -21,6 +21,7 @@ public class MiningQuery { ...@@ -21,6 +21,7 @@ public class MiningQuery {
private List<Variable> variables; private List<Variable> variables;
private List<Variable> covariables; private List<Variable> covariables;
private List<Variable> grouping; private List<Variable> grouping;
private List<Variable> datasets;
private String filters; private String filters;
private Algorithm algorithm; private Algorithm algorithm;
...@@ -59,6 +60,14 @@ public class MiningQuery { ...@@ -59,6 +60,14 @@ public class MiningQuery {
this.grouping = grouping; this.grouping = grouping;
} }
public List<Variable> getDatasets() { return datasets; }
public void setDataset(List<Variable> datasets) {
this.datasets = datasets;
}
public void addDataset(Variable variable) { this.datasets.add(variable); }
public void addGrouping(Variable variable) { this.grouping.add(variable); } public void addGrouping(Variable variable) { this.grouping.add(variable); }
public String getFilters() { public String getFilters() {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment