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

Exareme mining

parent c70f20cb
No related branches found
No related tags found
No related merge requests found
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) {
......
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