UrlsController/src/main/java/eu/openaire/urls_controller/controllers/UrlController.java

118 lines
4.4 KiB
Java
Raw Normal View History

2021-03-16 14:25:15 +01:00
package eu.openaire.urls_controller.controllers;
import com.google.common.collect.HashMultimap;
import eu.openaire.urls_controller.models.Assignment;
import eu.openaire.urls_controller.models.Datasource;
import eu.openaire.urls_controller.models.Task;
2021-06-22 04:38:48 +02:00
import eu.openaire.urls_controller.payloads.requests.WorkerReport;
import eu.openaire.urls_controller.payloads.responces.AssignmentResponse;
import eu.openaire.urls_controller.util.FileUtils;
import eu.openaire.urls_controller.util.GenericUtils;
2021-03-16 14:25:15 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-06-22 04:38:48 +02:00
import org.springframework.http.HttpStatus;
2021-03-16 14:25:15 +01:00
import org.springframework.http.ResponseEntity;
2021-06-22 04:38:48 +02:00
import org.springframework.web.bind.annotation.*;
2021-03-16 14:25:15 +01:00
import java.util.*;
2021-03-16 14:25:15 +01:00
@RestController
@RequestMapping("/urls")
public class UrlController {
private static final Logger logger = LoggerFactory.getLogger(UrlController.class);
public UrlController() {
}
@GetMapping("")
public ResponseEntity<?> getUrls(@RequestParam String workerId, @RequestParam int tasksLimit) {
2021-03-16 14:25:15 +01:00
List<Task> tasks = new ArrayList<>();
2021-03-16 14:25:15 +01:00
// TODO - Create the Assignment from the id-urls stored in the database up to the tasks-limit.
int assignmentId = 0;
2021-03-16 14:25:15 +01:00
Assignment assignment = new Assignment(assignmentId, tasks, workerId, new Date());
2021-03-16 14:25:15 +01:00
// TODO - Write the Assignment details to the database and then send it to the worker.
2021-03-16 14:25:15 +01:00
logger.info("Sending assignment_" + assignment.getAssignmentId() + " to worker with ID: " + workerId);
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment));
2021-03-16 14:25:15 +01:00
}
2021-06-22 04:38:48 +02:00
@PostMapping("addWorkerReport")
public ResponseEntity<?> addWorkerReport(@RequestBody WorkerReport workerReport) {
if ( workerReport == null )
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
logger.debug("Received the WorkerReport:\n" + workerReport.toString());
// TODO - Store the workerReport into the database.
return ResponseEntity.status(HttpStatus.OK).build();
}
2021-03-16 14:25:15 +01:00
private static int assignmentId = -1; // Just for the "getTestUrls"-endpoint.
@GetMapping("test")
public ResponseEntity<?> getTestUrls(@RequestParam String workerId, @RequestParam int tasksLimit) {
2021-03-16 14:25:15 +01:00
try {
new FileUtils(); // Find the input file.
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(500).body("The resource file for the requested tasks was not found.");
}
2021-03-16 14:25:15 +01:00
List<Task> tasks = new ArrayList<>();
HashMultimap<String, String> loadedIdUrlPairs;
boolean isFirstRun = true;
boolean tasksLimitReached = false;
2021-03-16 14:25:15 +01:00
// Start loading urls.
while ( true ) {
loadedIdUrlPairs = FileUtils.getNextIdUrlPairBatchFromJson(); // Take urls from jsonFile.
2021-03-16 14:25:15 +01:00
if ( FileUtils.isFinishedLoading(loadedIdUrlPairs.isEmpty(), isFirstRun) ) // Throws RuntimeException which is automatically passed on.
break;
else
isFirstRun = false;
2021-03-16 14:25:15 +01:00
Set<Map.Entry<String, String>> pairs = loadedIdUrlPairs.entries();
for ( Map.Entry<String,String> pair : pairs )
{
if ( tasks.size() >= tasksLimit ) {
tasksLimitReached = true;
break;
}
int randomNum = GenericUtils.getRandomNumber(1, 5);
tasks.add(new Task(pair.getKey(), pair.getValue(), new Datasource("ID_" + randomNum, "NAME_" + randomNum)));
}// end pairs-for-loop
if ( tasksLimitReached ) {
logger.debug("Done loading urls from the inputFile as the tasksLimit (" + tasksLimit + ") was reached.");
break;
}
}// end loading-while-loop
if ( FileUtils.inputScanner.get() != null ) // Check if the initial value is null.
FileUtils.inputScanner.get().close();
Assignment assignment = new Assignment((++assignmentId), tasks, workerId, new Date());
logger.info("Sending assignment_" + assignment.getAssignmentId() + " with " + tasks.size() + " tasks (" + FileUtils.duplicateIdUrlEntries.get() + " more tasks were discarded as duplicates), to worker with ID: " + workerId);
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment));
}
2021-03-16 14:25:15 +01:00
}