@ -6,6 +6,8 @@ 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 ;
@ -15,6 +17,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.* ;
import java.util.* ;
import java.util.concurrent.atomic.AtomicLong ;
@RestController
@RequestMapping ( "/urls" )
@ -23,26 +26,33 @@ public class UrlController {
private static final Logger logger = LoggerFactory . getLogger ( UrlController . class ) ;
p ublic UrlController ( ) {
p rivate static AtomicLong assignmentCounter = new AtomicLong ( ) ; // Just for the "getTestUrls"-endpoint.
}
@GetMapping ( "" )
public ResponseEntity < ? > getUrls ( @RequestParam String workerId , @RequestParam int t asksLimit) {
public ResponseEntity < ? > getUrls ( @RequestParam String workerId , @RequestParam int workerT asksLimit) {
List < Task > tasks = new ArrayList < > ( ) ;
// TODO - Create the Assignment from the id-urls stored in the database up to the tasks-limit.
int assignmentId = 0 ;
Date date = new Date ( ) ; // Store it here, in order to have the same date for all current assignments.
Assignment assignment = new Assignment ( assignmentId , tasks , workerId , new Date ( ) ) ;
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_" + assignment . getAssignmentId ( ) + " to worker with ID: " + workerId ) ;
logger . info ( "Sending assignment_" + assignment Counter . get ( ) + " to worker with ID: " + workerId ) ;
return ResponseEntity . status ( 200 ) . header ( "Content-Type" , "application/json" ) . body ( new AssignmentResponse ( assignment ) ) ;
return ResponseEntity . status ( 200 ) . header ( "Content-Type" , "application/json" ) . body ( new AssignmentResponse ( assignment Counter. incrementAndGet ( ) , assignments ) ) ;
}
@PostMapping ( "addWorkerReport" )
@ -59,22 +69,21 @@ public class UrlController {
}
private static int assignmentId = - 1 ; // Just for the "getTestUrls"-endpoint.
@GetMapping ( "test" )
public ResponseEntity < ? > getTestUrls ( @RequestParam String workerId , @RequestParam int task sLimit) {
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 tasks was not found.") ;
return ResponseEntity . status ( 500 ) . body ( "The resource file , for the requested assignments, was not found.") ;
}
List < Task> task s = new ArrayList < > ( ) ;
List < Assignment> assignment s = new ArrayList < > ( ) ;
HashMultimap < String , String > loadedIdUrlPairs ;
boolean isFirstRun = true ;
boolean tasksLimitReached = false ;
boolean assignmentsLimitReached = false ;
Date date = new Date ( ) ;
// Start loading urls.
while ( true ) {
@ -89,17 +98,17 @@ public class UrlController {
for ( Map . Entry < String , String > pair : pairs )
{
if ( tasks. size ( ) > = task sLimit ) {
task sLimitReached = true ;
if ( assignments. size ( ) > = assignment sLimit ) {
assignment sLimitReached = true ;
break ;
}
int randomNum = GenericUtils . getRandomNumber ( 1 , 5 ) ;
tasks. add ( new Task ( pair . getKey ( ) , pair . getValue ( ) , new Datasource ( "ID_" + randomNum , "NAME_" + randomNum ) ) ) ;
assignments. add ( new Assignment ( pair . getKey ( ) , pair . getValue ( ) , new Datasource ( "ID_" + randomNum , "NAME_" + randomNum ) , workerId , date ) ) ;
} // end pairs-for-loop
if ( task sLimitReached ) {
logger . debug ( "Done loading urls from the inputFile as the tasksLimit (" + task sLimit + ") was reached." ) ;
if ( assignment sLimitReached ) {
logger . debug ( "Done loading urls from the inputFile as the assignmentsLimit (" + assignment sLimit + ") was reached." ) ;
break ;
}
} // end loading-while-loop
@ -107,11 +116,9 @@ public class UrlController {
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 ) ;
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 ( assignment ) ) ;
return ResponseEntity . status ( 200 ) . header ( "Content-Type" , "application/json" ) . body ( new AssignmentResponse ( assignmentCounter . incrementAndGet ( ) , assignments ) ) ;
}
}