forked from lsmyrnaios/UrlsController
96 lines
3.2 KiB
Java
96 lines
3.2 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.Task;
|
|
import eu.openaire.urls_controller.payloads.requests.WorkerRequest;
|
|
import eu.openaire.urls_controller.payloads.responces.AssignmentResponse;
|
|
import eu.openaire.urls_controller.util.FileUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.util.*;
|
|
|
|
@RestController
|
|
@RequestMapping("/urls")
|
|
public class UrlController {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UrlController.class);
|
|
|
|
|
|
public UrlController() {
|
|
|
|
}
|
|
|
|
|
|
@GetMapping("")
|
|
public ResponseEntity<?> getUrls(WorkerRequest workerRequest) {
|
|
|
|
String workerId = workerRequest.getWorkerId();
|
|
int tasksLimit = workerRequest.getTasksLimit();
|
|
|
|
List<Task> tasks = new ArrayList<>();
|
|
|
|
// TODO - Create the Assignment from the id-urls stored in the database up to the tasks-limit.
|
|
|
|
Assignment assignment = new Assignment(tasks, workerId, new Date());
|
|
|
|
// TODO - Write the Assignment details to the database and then send it to the worker.
|
|
|
|
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment));
|
|
}
|
|
|
|
|
|
@GetMapping("test")
|
|
public ResponseEntity<?> getTestUrls(WorkerRequest workerRequest) {
|
|
|
|
String workerId = workerRequest.getWorkerId();
|
|
int tasksLimit = workerRequest.getTasksLimit();
|
|
|
|
List<Task> tasks = new ArrayList<>();
|
|
new FileUtils(); // Find the input file.
|
|
|
|
HashMultimap<String, String> loadedIdUrlPairs;
|
|
boolean isFirstRun = true;
|
|
boolean tasksLimitReached = false;
|
|
|
|
// 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 ( tasks.size() > tasksLimit ) {
|
|
tasksLimitReached = true;
|
|
break;
|
|
}
|
|
|
|
tasks.add(new Task(pair.getKey(), pair.getValue()));
|
|
}// end pairs-for-loop
|
|
|
|
if ( tasksLimitReached ) {
|
|
logger.debug("Done loading tasksLimit (" + tasksLimit + ") urls from the inputFile.");
|
|
break;
|
|
}
|
|
|
|
}// end loading-while-loop
|
|
|
|
Assignment assignment = new Assignment(tasks, workerId, new Date());
|
|
|
|
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment));
|
|
}
|
|
|
|
}
|