diff --git a/src/main/java/eu/hbp/mip/controllers/MiningApi.java b/src/main/java/eu/hbp/mip/controllers/MiningApi.java index 24356234ea7467c873b902830f37fa46b78a5c17..5e402eb937d373daf547615fdce05ec41dd8c7d0 100644 --- a/src/main/java/eu/hbp/mip/controllers/MiningApi.java +++ b/src/main/java/eu/hbp/mip/controllers/MiningApi.java @@ -1,5 +1,7 @@ package eu.hbp.mip.controllers; +import eu.hbp.mip.utils.HTTPUtil; + import com.google.gson.Gson; import eu.hbp.mip.akka.WokenClientController; import eu.hbp.mip.model.Mining; @@ -21,6 +23,8 @@ import org.springframework.web.bind.annotation.RestController; import scala.Option; import java.sql.Date; +import java.util.*; +import java.io.IOException; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @@ -28,7 +32,7 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; * Created by mirco on 06.01.17. */ @RestController -@RequestMapping(value = "/mining", produces = {APPLICATION_JSON_VALUE}) +@RequestMapping(value = "/mining", produces = { APPLICATION_JSON_VALUE }) @Api(value = "/mining", description = "the mining API") public class MiningApi extends WokenClientController { @@ -38,33 +42,54 @@ public class MiningApi extends WokenClientController { @Autowired private UserInfo userInfo; + @Value("#{'${services.exareme.miningExaremeUrl:http://localhost:9090/mining/query}'}") + public String miningExaremeQueryUrl; + @ApiOperation(value = "Run an algorithm", response = String.class) - @Cacheable(value = "mining", - condition = "#query != null and (#query.getAlgorithm().getCode() == 'histograms' or #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"); User user = userInfo.getUser(); - 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( - 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())); - } - }); + 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(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())); + } + }); + } + + @ApiOperation(value = "Create an experiment on Exareme", response = String.class) + @RequestMapping(value = "/exareme", method = RequestMethod.POST) + public ResponseEntity runExaremeMining(@RequestBody List<HashMap<String, String>> queryList) { + LOGGER.info("Run an histogram"); + // Mining mining = new Mining(); + + String query = gson.toJson(queryList); + String url = miningExaremeQueryUrl + "/" + "HISTOGRAMS"; + + try { + StringBuilder results = new StringBuilder(); + int code = HTTPUtil.sendPost(url, query, results); + if (code >= 500) { + return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR); + } + if (code >= 400) { + return new ResponseEntity<>("Bad request", HttpStatus.BAD_REQUEST); + } + + return ResponseEntity.ok(gson.toJson(results.toString())); + } catch (IOException e) { + return new ResponseEntity<>("Not found", HttpStatus.BAD_REQUEST); + } + } private static String unwrap(Option<String> option) {