125 lines
5.1 KiB
Java
125 lines
5.1 KiB
Java
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;
|
|
import eu.openaire.urls_controller.payloads.requests.WorkerReport;
|
|
import eu.openaire.urls_controller.payloads.responces.AssignmentResponse;
|
|
//import eu.openaire.urls_controller.repositories.AssignmentRepository;
|
|
import eu.openaire.urls_controller.util.ControllerConstants;
|
|
import eu.openaire.urls_controller.util.FileUtils;
|
|
import eu.openaire.urls_controller.util.GenericUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.*;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
@RestController
|
|
@RequestMapping("/urls")
|
|
public class UrlController {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UrlController.class);
|
|
|
|
|
|
private static AtomicLong assignmentCounter = new AtomicLong(); // Just for the "getTestUrls"-endpoint.
|
|
|
|
}
|
|
|
|
|
|
@GetMapping("")
|
|
public ResponseEntity<?> getUrls(@RequestParam String workerId, @RequestParam int workerTasksLimit) {
|
|
|
|
List<Task> tasks = new ArrayList<>();
|
|
|
|
// TODO - Create the Assignment from the id-urls stored in the database up to the tasks-limit.
|
|
Date date = new Date(); // Store it here, in order to have the same date for all current assignments.
|
|
|
|
|
|
int tasksLimitForAssignment = ControllerConstants.ASSIGNMENTS_LIMIT;
|
|
if ( tasksLimitForAssignment > workerTasksLimit )
|
|
tasksLimitForAssignment = workerTasksLimit;
|
|
|
|
List<Assignment> assignments = null; // TODO -> // assignmentRepository.getNewAssignments(tasksLimitForAssignment);
|
|
|
|
//Assignment assignment = new Assignment(assignmentId, tasks, workerId, date);
|
|
|
|
// TODO - Write the Assignment details to the database and then send it to the worker.
|
|
|
|
logger.info("Sending assignment_" + assignmentCounter.get() + " to worker with ID: " + workerId);
|
|
|
|
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignmentCounter.incrementAndGet(), assignments));
|
|
}
|
|
|
|
@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();
|
|
}
|
|
|
|
|
|
@GetMapping("test")
|
|
public ResponseEntity<?> getTestUrls(@RequestParam String workerId, @RequestParam int assignmentsLimit) {
|
|
|
|
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 assignments, was not found.");
|
|
}
|
|
|
|
List<Assignment> assignments = new ArrayList<>();
|
|
HashMultimap<String, String> loadedIdUrlPairs;
|
|
boolean isFirstRun = true;
|
|
boolean assignmentsLimitReached = false;
|
|
Date date = new Date();
|
|
|
|
// Start loading urls.
|
|
while ( true ) {
|
|
loadedIdUrlPairs = FileUtils.getNextIdUrlPairBatchFromJson(); // Take urls from jsonFile.
|
|
|
|
if ( FileUtils.isFinishedLoading(loadedIdUrlPairs.isEmpty(), isFirstRun) ) // Throws RuntimeException which is automatically passed on.
|
|
break;
|
|
else
|
|
isFirstRun = false;
|
|
|
|
Set<Map.Entry<String, String>> pairs = loadedIdUrlPairs.entries();
|
|
|
|
for ( Map.Entry<String,String> pair : pairs )
|
|
{
|
|
if ( assignments.size() >= assignmentsLimit ) {
|
|
assignmentsLimitReached = true;
|
|
break;
|
|
}
|
|
|
|
int randomNum = GenericUtils.getRandomNumber(1, 5);
|
|
assignments.add(new Assignment(pair.getKey(), pair.getValue(), new Datasource("ID_" + randomNum, "NAME_" + randomNum), workerId, date));
|
|
}// end pairs-for-loop
|
|
|
|
if ( assignmentsLimitReached ) {
|
|
logger.debug("Done loading urls from the inputFile as the assignmentsLimit (" + assignmentsLimit + ") was reached.");
|
|
break;
|
|
}
|
|
}// end loading-while-loop
|
|
|
|
if ( FileUtils.inputScanner.get() != null ) // Check if the initial value is null.
|
|
FileUtils.inputScanner.get().close();
|
|
|
|
logger.info("Sending AssignmentResponse_" + assignmentCounter.get() + " with " + assignments.size() + " assignments (" + FileUtils.duplicateIdUrlEntries.get() + " more assignments were discarded as duplicates), to worker with ID: " + workerId);
|
|
|
|
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignmentCounter.incrementAndGet(), assignments));
|
|
}
|
|
|
|
}
|