2021-03-16 14:25:15 +01:00
package eu.openaire.urls_controller.controllers ;
2021-05-18 16:23:20 +02:00
import com.google.common.collect.HashMultimap ;
import eu.openaire.urls_controller.models.Assignment ;
2021-05-20 01:50:50 +02:00
import eu.openaire.urls_controller.models.Datasource ;
2021-05-18 16:23:20 +02:00
import eu.openaire.urls_controller.models.Task ;
2021-06-22 04:38:48 +02:00
import eu.openaire.urls_controller.payloads.requests.WorkerReport ;
2021-05-18 16:23:20 +02:00
import eu.openaire.urls_controller.payloads.responces.AssignmentResponse ;
import eu.openaire.urls_controller.util.FileUtils ;
2021-05-20 01:50:50 +02:00
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
2021-05-18 16:23:20 +02: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 ( " " )
2021-05-19 01:32:46 +02:00
public ResponseEntity < ? > getUrls ( @RequestParam String workerId , @RequestParam int tasksLimit ) {
2021-03-16 14:25:15 +01:00
2021-05-18 16:23:20 +02:00
List < Task > tasks = new ArrayList < > ( ) ;
2021-03-16 14:25:15 +01:00
2021-05-18 16:23:20 +02:00
// TODO - Create the Assignment from the id-urls stored in the database up to the tasks-limit.
2021-06-09 04:48:54 +02:00
int assignmentId = 0 ;
2021-03-16 14:25:15 +01:00
2021-06-09 04:48:54 +02:00
Assignment assignment = new Assignment ( assignmentId , tasks , workerId , new Date ( ) ) ;
2021-03-16 14:25:15 +01:00
2021-05-18 16:23:20 +02:00
// TODO - Write the Assignment details to the database and then send it to the worker.
2021-03-16 14:25:15 +01:00
2021-06-10 13:21:39 +02:00
logger . info ( " Sending assignment_ " + assignment . getAssignmentId ( ) + " to worker with ID: " + workerId ) ;
2021-05-18 16:23:20 +02:00
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
2021-06-09 04:48:54 +02:00
private static int assignmentId = - 1 ; // Just for the "getTestUrls"-endpoint.
2021-05-18 16:23:20 +02:00
@GetMapping ( " test " )
2021-05-19 01:32:46 +02:00
public ResponseEntity < ? > getTestUrls ( @RequestParam String workerId , @RequestParam int tasksLimit ) {
2021-03-16 14:25:15 +01:00
2021-06-10 13:21:39 +02: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
2021-06-10 13:21:39 +02:00
List < Task > tasks = new ArrayList < > ( ) ;
2021-05-18 16:23:20 +02:00
HashMultimap < String , String > loadedIdUrlPairs ;
boolean isFirstRun = true ;
boolean tasksLimitReached = false ;
2021-03-16 14:25:15 +01:00
2021-05-18 16:23:20 +02:00
// Start loading urls.
2021-06-10 13:21:39 +02:00
while ( true ) {
2021-05-18 16:23:20 +02:00
loadedIdUrlPairs = FileUtils . getNextIdUrlPairBatchFromJson ( ) ; // Take urls from jsonFile.
2021-03-16 14:25:15 +01:00
2021-05-18 16:23:20 +02: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
2021-05-18 16:23:20 +02:00
Set < Map . Entry < String , String > > pairs = loadedIdUrlPairs . entries ( ) ;
for ( Map . Entry < String , String > pair : pairs )
{
2021-05-19 01:32:46 +02:00
if ( tasks . size ( ) > = tasksLimit ) {
2021-05-18 16:23:20 +02:00
tasksLimitReached = true ;
break ;
}
2021-05-20 01:50:50 +02:00
int randomNum = GenericUtils . getRandomNumber ( 1 , 5 ) ;
tasks . add ( new Task ( pair . getKey ( ) , pair . getValue ( ) , new Datasource ( " ID_ " + randomNum , " NAME_ " + randomNum ) ) ) ;
2021-05-18 16:23:20 +02:00
} // end pairs-for-loop
if ( tasksLimitReached ) {
2021-06-10 13:21:39 +02:00
logger . debug ( " Done loading urls from the inputFile as the tasksLimit ( " + tasksLimit + " ) was reached. " ) ;
2021-05-18 16:23:20 +02:00
break ;
}
} // end loading-while-loop
2021-06-10 19:24:51 +02:00
if ( FileUtils . inputScanner . get ( ) ! = null ) // Check if the initial value is null.
FileUtils . inputScanner . get ( ) . close ( ) ;
2021-06-10 13:21:39 +02:00
2021-06-09 04:48:54 +02:00
Assignment assignment = new Assignment ( ( + + assignmentId ) , tasks , workerId , new Date ( ) ) ;
2021-05-18 16:23:20 +02:00
2021-06-10 19:24:51 +02:00
logger . info ( " Sending assignment_ " + assignment . getAssignmentId ( ) + " with " + tasks . size ( ) + " tasks ( " + FileUtils . duplicateIdUrlEntries . get ( ) + " more tasks were discarded as duplicates), to worker with ID: " + workerId ) ;
2021-06-10 13:21:39 +02:00
2021-05-18 16:23:20 +02:00
return ResponseEntity . status ( 200 ) . header ( " Content-Type " , " application/json " ) . body ( new AssignmentResponse ( assignment ) ) ;
}
2021-03-16 14:25:15 +01:00
}