[Updated] Dockerfiles so everything runs using docker compose up, added the benchmark and speed tests
This commit is contained in:
@@ -23,28 +23,46 @@ public class QuizController {
|
||||
@Autowired
|
||||
private QuizService quizService;
|
||||
|
||||
/**
|
||||
* Start a session
|
||||
* @param amount The amount of questsions
|
||||
* @param category The desired category (or all)
|
||||
* @param difficulty The desired difficulty (or all)
|
||||
* @param type The desired type (or both)
|
||||
* @return The sessionId, or statusCode 5 when the queue is full
|
||||
*/
|
||||
@GetMapping("/questions")
|
||||
public String startSession(
|
||||
@RequestParam(required = true) Integer amount,
|
||||
@RequestParam(required = false) Integer category,
|
||||
@RequestParam(required = false) String difficulty,
|
||||
@RequestParam(required = false) String type
|
||||
@RequestParam(required = true) Integer amount,
|
||||
@RequestParam(required = false) Integer category,
|
||||
@RequestParam(required = false) String difficulty,
|
||||
@RequestParam(required = false) String type
|
||||
) {
|
||||
QuizSettings quizSettings = new QuizSettings(amount, category, difficulty, type);
|
||||
Optional<String> result = quizService.createSession(quizSettings);
|
||||
if(result.isPresent()) {
|
||||
if (result.isPresent()) {
|
||||
String jsonString = String.format("{\"statusCode\":0,\"sessionId\":\"%s\"}", result.get());
|
||||
return jsonString;
|
||||
}
|
||||
String jsonString = String.format("{\"statusCode\":5}");
|
||||
return jsonString;
|
||||
}
|
||||
String jsonString = String.format("{\"statusCode\":5}");
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the questions have been retrieved
|
||||
* @param sessionId The session that holds the questions
|
||||
* @return The Session, or nothing if they are not ready yet
|
||||
*/
|
||||
@GetMapping("/status/{sessionId}")
|
||||
public Optional<Session> checkStatus(@PathVariable String sessionId) {
|
||||
return quizService.checkSession(sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive the chosen answers, calculate the score, and return it
|
||||
* @param answers The chosen answers
|
||||
* @return The score
|
||||
*/
|
||||
@PostMapping("/score")
|
||||
public String checkAnswers(@RequestBody Answers answers) {
|
||||
String jsonString = String.format("{\"score\":\"%s\"}", quizService.getScore(answers));
|
||||
|
||||
@@ -48,4 +48,21 @@ public class ApiService {
|
||||
logger.log(System.Logger.Level.ERROR, "Critical failure", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect a chuck of 50 items
|
||||
* Store them in the DataService
|
||||
*/
|
||||
public void collectBulk() {
|
||||
String url = apiUrl + "?amount=50";
|
||||
try {
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
String json = response.getBody();
|
||||
ApiResponse apiResponse = cleanerService.extractApiResponse(json);
|
||||
responseHandler.handleResponse(apiResponse);
|
||||
|
||||
} catch (RestClientException | IOException e) {
|
||||
logger.log(System.Logger.Level.ERROR, "Critical failure", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ResponseHandlingService {
|
||||
|
||||
switch (response.getResponseCode()) {
|
||||
case 0:
|
||||
// Nothing
|
||||
// Success
|
||||
updateSessionInCache(session, response);
|
||||
break;
|
||||
case 1:
|
||||
@@ -58,7 +58,7 @@ public class ResponseHandlingService {
|
||||
/**
|
||||
* If the request was made by the scheduler during queue-downtime, there is no session
|
||||
*/
|
||||
public void switchInternal(ApiResponse response) {
|
||||
public void handleResponse(ApiResponse response) {
|
||||
switch (response.getResponseCode()) {
|
||||
case 0:
|
||||
// TODO: Store in DB
|
||||
|
||||
@@ -30,7 +30,7 @@ public class ApiServiceTest {
|
||||
@Test
|
||||
public void testRequestQuestions() throws Exception {
|
||||
QuizSettings settings = new QuizSettings();
|
||||
String jsonResponse = apiService.requestQuestions(settings);
|
||||
String jsonResponse = apiService.handleRequest(settings);
|
||||
JsonNode jsonNode = objectMapper.readTree(jsonResponse);
|
||||
|
||||
// Check if the response code is as expected
|
||||
|
||||
Reference in New Issue
Block a user