Skip to content
Snippets Groups Projects
Commit da6382f9 authored by Ludovic Claude's avatar Ludovic Claude
Browse files

Fix /datasets service

parent a793f38a
No related branches found
No related tags found
No related merge requests found
package eu.hbp.mip;
import ch.chuv.lren.woken.messages.datasets.Dataset;
import com.google.gson.Gson;
import eu.hbp.mip.controllers.DatasetsApi;
import eu.hbp.mip.controllers.MiningApi;
import eu.hbp.mip.controllers.VariablesApi;
import eu.hbp.mip.model.Algorithm;
import eu.hbp.mip.model.MiningQuery;
import eu.hbp.mip.model.Variable;
import eu.hbp.mip.repositories.VariableRepository;
import org.slf4j.Logger;
......@@ -10,10 +16,13 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.util.Collections;
@Component
public class StartupTasks implements ApplicationListener<ApplicationReadyEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(StartupTasks.class);
private static final Gson gson = new Gson();
@Autowired
private VariableRepository variableRepository;
......@@ -21,12 +30,18 @@ public class StartupTasks implements ApplicationListener<ApplicationReadyEvent>
@Autowired
private DatasetsApi datasetsApi;
@Autowired
private VariablesApi variablesApi;
@Autowired
private MiningApi miningApi;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// Pre-fill the local variable repository with the list of datasets, interpreted here as variables
// (a bit like a categorical variable can be split into a set of variables (a.k.a one hot encoding in Data science) )
try {
for (ch.chuv.lren.woken.messages.datasets.Dataset dataset: datasetsApi.fetchDatasets()) {
for (Dataset dataset: datasetsApi.fetchDatasets()) {
final String code = dataset.dataset().code();
Variable v = variableRepository.findOne(code);
if (v == null) {
......@@ -38,6 +53,18 @@ public class StartupTasks implements ApplicationListener<ApplicationReadyEvent>
LOGGER.error("Cannot initialise the variable repository. Is the connection to Woken working?", e);
}
/*
for (String variableJson: variablesApi.loadVariables()) {
String code = gson.fromJson(variableJson, Variable.class).getCode();
MiningQuery histogram = new MiningQuery();
histogram.setAlgorithm(new Algorithm("histogram", "histogram", false));
histogram.setVariables(Collections.singletonList(new Variable(code)));
histogram.setCovariables(Collections.emptyList());
histogram.setGrouping(Collections.emptyList());
// TODO: need to get groupings from Woken
}
*/
LOGGER.info("MIP Portal backend is ready!");
}
}
......@@ -5,15 +5,17 @@
package eu.hbp.mip.controllers;
import ch.chuv.lren.woken.messages.datasets.Dataset;
import ch.chuv.lren.woken.messages.datasets.DatasetsQuery;
import ch.chuv.lren.woken.messages.datasets.DatasetsResponse;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import eu.hbp.mip.akka.WokenClientController;
import eu.hbp.mip.model.Dataset;
import eu.hbp.mip.model.DatasetDescription;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -21,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import scala.Option;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
......@@ -35,19 +39,22 @@ public class DatasetsApi extends WokenClientController {
@ApiOperation(value = "Get dataset list", response = Dataset.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET)
@Cacheable(value = "datasets")
public ResponseEntity getDatasets(
) {
LOGGER.info("Get dataset list");
LOGGER.info("Get list of datasets");
try {
JsonArray datasets = new JsonArray();
fetchDatasets().stream().map(d -> {
JsonObject jsObject = new JsonObject();
jsObject.addProperty("code", d.dataset().code());
jsObject.addProperty("label", d.label());
datasets.add(jsObject);
return jsObject;
});
List<DatasetDescription> datasets = new ArrayList<>();
for (Dataset d: fetchDatasets()) {
DatasetDescription dataset = new DatasetDescription();
LOGGER.info("Dataset {}", d);
dataset.setCode(d.dataset().code());
dataset.setLabel(d.label());
dataset.setDescription(d.description());
dataset.setAnonymisationLevel(d.anonymisationLevel().toString());
datasets.add(dataset);
}
return ResponseEntity.ok(datasets);
} catch (Exception e) {
......@@ -58,8 +65,8 @@ public class DatasetsApi extends WokenClientController {
}
public Set<ch.chuv.lren.woken.messages.datasets.Dataset> fetchDatasets() throws Exception {
public List<ch.chuv.lren.woken.messages.datasets.Dataset> fetchDatasets() throws Exception {
DatasetsResponse result = askWoken(new DatasetsQuery(Option.empty()), 30);
return scala.collection.JavaConversions.setAsJavaSet(result.datasets());
return new ArrayList<>(scala.collection.JavaConversions.asJavaCollection(result.datasets()));
}
}
......@@ -44,7 +44,10 @@ public class MiningApi extends WokenClientController {
public String queryUrl;
@ApiOperation(value = "Run an algorithm", response = String.class)
@Cacheable(value = "mining", condition = "#query != null and #query.getAlgorithm().getCode() == 'histograms'", key = "#query.toString()", unless = "#result.getStatusCode().value()!=200")
@Cacheable(value = "mining",
condition = "#query != null and (#query.getAlgorithm().getCode() == 'histograms' or #query.getAlgorithm().getCode() == 'histograms')",
key = "#query.toString()",
unless = "#result.getStatusCode().value()!=200")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity runAlgorithm(@RequestBody eu.hbp.mip.model.MiningQuery query) {
LOGGER.info("Run an algorithm");
......
......@@ -166,7 +166,7 @@ public class VariablesApi {
}
private List<String> loadVariables() {
public List<String> loadVariables() {
String sqlQuery = String.format(
"SELECT * FROM meta_variables where upper(target_table)='%s'", featuresMainTable.toUpperCase());
SqlRowSet data = metaJdbcTemplate.queryForRowSet(sqlQuery);
......
package eu.hbp.mip.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
@ApiModel
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DatasetDescription {
private String code;
private String label;
private String description;
private String anonymisationLevel;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAnonymisationLevel() {
return anonymisationLevel;
}
public void setAnonymisationLevel(String anonymisationLevel) {
this.anonymisationLevel = anonymisationLevel;
}
}
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