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

Use ApplicationStartupEvent to connect to Woken and fill the local db

parent 80161c39
No related branches found
No related tags found
No related merge requests found
package eu.hbp.mip;
import eu.hbp.mip.controllers.DatasetsApi;
import eu.hbp.mip.model.Variable;
import eu.hbp.mip.repositories.VariableRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class StartupTasks implements ApplicationListener<ApplicationReadyEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(StartupTasks.class);
@Autowired
private VariableRepository variableRepository;
@Autowired
private DatasetsApi datasetsApi;
@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()) {
final String code = dataset.dataset().code();
Variable v = variableRepository.findOne(code);
if (v == null) {
v = new Variable(code);
variableRepository.save(v);
}
}
} catch (Exception e) {
LOGGER.error("Cannot initialise the variable repository. Is the connection to Woken working?", e);
}
LOGGER.info("MIP Portal backend is ready!");
}
}
......@@ -7,17 +7,13 @@ package eu.hbp.mip.controllers;
import ch.chuv.lren.woken.messages.datasets.DatasetsQuery;
import ch.chuv.lren.woken.messages.datasets.DatasetsResponse;
import com.google.gson.Gson;
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.Variable;
import eu.hbp.mip.repositories.VariableRepository;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -25,10 +21,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import scala.Option;
import javax.annotation.PostConstruct;
import java.util.Set;
import java.util.stream.Stream;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
......@@ -39,24 +32,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
public class DatasetsApi extends WokenClientController {
private static final Logger LOGGER = LoggerFactory.getLogger(DatasetsApi.class);
private static final Gson gson = new Gson();
@Autowired
private VariableRepository variableRepository;
@PostConstruct
public void init() throws Exception {
// 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) )
for (ch.chuv.lren.woken.messages.datasets.Dataset dataset: fetchDatasets()) {
final String code = dataset.dataset().code();
Variable v = variableRepository.findOne(code);
if (v == null) {
v = new Variable(code);
variableRepository.save(v);
}
}
}
@ApiOperation(value = "Get dataset list", response = Dataset.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET)
......@@ -66,7 +41,7 @@ public class DatasetsApi extends WokenClientController {
try {
JsonArray datasets = new JsonArray();
Stream<JsonObject> values = fetchDatasets().stream().map(d -> {
fetchDatasets().stream().map(d -> {
JsonObject jsObject = new JsonObject();
jsObject.addProperty("code", d.dataset().code());
jsObject.addProperty("label", d.label());
......@@ -83,7 +58,7 @@ public class DatasetsApi extends WokenClientController {
}
private Set<ch.chuv.lren.woken.messages.datasets.Dataset> fetchDatasets() throws Exception {
public Set<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());
}
......
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