[Updated] Dockerfiles so everything runs using docker compose up, added the benchmark and speed tests

This commit is contained in:
Ninjaws
2024-11-01 19:52:53 +01:00
parent 675bdc7f8e
commit 595b83b364
14 changed files with 143 additions and 17 deletions
+14 -3
View File
@@ -16,11 +16,22 @@ ENV USER=dev
RUN groupadd -g ${GID} ${USER} \
&& useradd -m -u ${UID} -g ${USER} ${USER}
USER ${USER}
EXPOSE 8080
EXPOSE 9090
CMD ["tail","-f","/dev/null"]
#---Building
FROM base AS build
COPY . .
RUN mvn clean package -DskipTests
#--Testing
FROM build AS testing
RUN mvn clean test
#---Production
#---Production
# Pulled again to get rid of maven, to decrease the size
FROM openjdk:17-jdk-slim AS production
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar
EXPOSE 9090
ENTRYPOINT ["java", "-jar", "app.jar"]
@@ -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