Skip to content

Commit

Permalink
FEAT : CDE-302 accéder aux logs de l'algo vest-ppn sur la prod :
Browse files Browse the repository at this point in the history
     - modification d'un paramètre dans PpnDto.java
     - modification d'un paramètre dans PpnControllerDto.java
     - adaptation de BestPpnService.java et ajout des lignes de logs
     - adaptation du BestPpnController.java et renommage de variable
     - adaptation des TU
  • Loading branch information
EryneKL committed Dec 12, 2023
1 parent cddcae5 commit d907e9e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 36 deletions.
14 changes: 9 additions & 5 deletions src/main/java/fr/abes/bestppn/controller/BestPpnController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "Calcul du meilleur PPN", description = "API de calcul du meilleur PPN pour une ligne tsv")
@CrossOrigin(origins = "*")
Expand Down Expand Up @@ -44,7 +46,7 @@ public PpnControllerDto bestPpn(@RequestParam(name = "provider") String provider
@RequestParam(name = "print_identifier", required = false) String printIdentifier, @RequestParam(name = "titleUrl", required = false) String titleUrl,
@RequestParam(name = "date_monograph_published_online", required = false) String dateMonographPublishedOnline, @RequestParam(name = "date_monograph_published_print", required = false) String dateMonographPublishedPrint,
@RequestParam(name = "first_author", required = false) String firstAuthor, @RequestParam(name = "force", required = false) Boolean force,
@RequestParam(name = "log", required = false) Boolean errors) throws IOException {
@RequestParam(name = "log", required = false) Boolean log) throws IOException {
try {
LigneKbartDto ligneKbartDto = new LigneKbartDto();
ligneKbartDto.setPublicationType(publicationType);
Expand All @@ -56,13 +58,15 @@ public PpnControllerDto bestPpn(@RequestParam(name = "provider") String provider
ligneKbartDto.setDateMonographPublishedOnline((dateMonographPublishedOnline != null) ? dateMonographPublishedOnline : "");
ligneKbartDto.setFirstAuthor((firstAuthor != null) ? firstAuthor : "");
boolean injectKafka = (force != null) ? force : false;
boolean sendErrors = (errors != null) ? errors : false;
PpnDto ppnDto = service.getBestPpn(ligneKbartDto, provider, injectKafka, sendErrors);
return new PpnControllerDto(ppnDto.getPpn(), ppnDto.getTypeSupport(), ppnDto.getLog());
boolean sendLog = (log != null) ? log : false;
PpnDto ppnDto = service.getBestPpn(ligneKbartDto, provider, injectKafka, sendLog);
return new PpnControllerDto(ppnDto.getPpn(), ppnDto.getTypeSupport(), ppnDto.getLogs());
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Une url dans le champ title_url du kbart n'est pas correcte");
} catch (BestPpnException | RestClientException | IllegalArgumentException | IllegalDoiException e) {
return new PpnControllerDto(e.getMessage());
List<String> logs = new ArrayList<>();
logs.add(e.getMessage());
return new PpnControllerDto(logs);
}
}
}
12 changes: 7 additions & 5 deletions src/main/java/fr/abes/bestppn/dto/PpnControllerDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Setter;
import org.springframework.lang.Nullable;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
Expand All @@ -21,15 +23,15 @@ public class PpnControllerDto {

@Nullable
@JsonInclude(JsonInclude.Include.NON_NULL)
private String log;
private List<String> logs;

public PpnControllerDto (String ppn, TYPE_SUPPORT typeSupport, String log) {
public PpnControllerDto (String ppn, TYPE_SUPPORT typeSupport, List<String> logs) {
this.ppn = ppn;
this.typeSupport = typeSupport;
this.log = log;
this.logs = logs;
}

public PpnControllerDto (String log) {
this.log = log;
public PpnControllerDto (List<String> logs) {
this.logs = logs;
}
}
12 changes: 7 additions & 5 deletions src/main/java/fr/abes/bestppn/dto/kafka/PpnDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Setter;
import org.springframework.lang.Nullable;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
Expand All @@ -22,7 +24,7 @@ public class PpnDto {
private DESTINATION_TOPIC destination;

@Nullable
private String log;
private List<String> logs;

public PpnDto (String ppn, DESTINATION_TOPIC destination) {
this.ppn = ppn;
Expand All @@ -35,16 +37,16 @@ public PpnDto (String ppn, DESTINATION_TOPIC destination, TYPE_SUPPORT typeSuppo
this.typeSupport = typeSupport;
}

public PpnDto (String ppn, DESTINATION_TOPIC destination, String log) {
public PpnDto (String ppn, DESTINATION_TOPIC destination, List<String> logs) {
this.ppn = ppn;
this.destination = destination;
this.log = log;
this.logs = logs;
}

public PpnDto (String ppn, DESTINATION_TOPIC destination, TYPE_SUPPORT typeSupport, String log) {
public PpnDto (String ppn, DESTINATION_TOPIC destination, TYPE_SUPPORT typeSupport, List<String> logs) {
this.ppn = ppn;
this.destination = destination;
this.typeSupport = typeSupport;
this.log = log;
this.logs = logs;
}
}
59 changes: 42 additions & 17 deletions src/main/java/fr/abes/bestppn/service/BestPpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
import fr.abes.bestppn.kafka.TopicProducer;
import fr.abes.bestppn.utils.*;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

@Service
@Getter
Expand Down Expand Up @@ -49,6 +47,10 @@ public class BestPpnService {

private final CheckUrlService checkUrlService;

private List<String> kbartLineLogs;

@Setter
private Boolean sendLogs;

public BestPpnService(WsService service, NoticeService noticeService, TopicProducer topicProducer, CheckUrlService checkUrlService) {
this.service = service;
Expand All @@ -57,18 +59,22 @@ public BestPpnService(WsService service, NoticeService noticeService, TopicProdu
this.checkUrlService = checkUrlService;
}

public PpnDto getBestPpn(LigneKbartDto kbart, String provider, boolean injectKafka, boolean sendErrors) throws IOException, BestPpnException, URISyntaxException, RestClientException, IllegalArgumentException, IllegalDoiException {
public PpnDto getBestPpn(LigneKbartDto kbart, String provider, boolean injectKafka, boolean sendLogs) throws IOException, BestPpnException, URISyntaxException, RestClientException, IllegalArgumentException, IllegalDoiException {

kbartLineLogs = new ArrayList<>();
this.sendLogs = sendLogs;
Map<String, Integer> ppnElecScoredList = new HashMap<>();
Set<String> ppnPrintResultList = new HashSet<>();

if (!kbart.getPublicationType().isEmpty()) {
provider = kbart.getPublicationType().equals(PUBLICATION_TYPE.serial.toString()) ? "" : provider;
if (kbart.getOnlineIdentifier() != null && !kbart.getOnlineIdentifier().isEmpty()) {
if (sendLogs) kbartLineLogs.add("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getOnlineIdentifier() + " / provider : " + provider);
log.debug("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getOnlineIdentifier() + " / provider : " + provider);
feedPpnListFromOnline(kbart, provider, ppnElecScoredList, ppnPrintResultList);
}
if (kbart.getPrintIdentifier() != null && !kbart.getPrintIdentifier().isEmpty()) {
if (sendLogs) kbartLineLogs.add("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getPrintIdentifier() + " / provider : " + provider);
log.debug("paramètres en entrée : type : " + kbart.getPublicationType() + " / id : " + kbart.getPrintIdentifier() + " / provider : " + provider);
feedPpnListFromPrint(kbart, provider, ppnElecScoredList, ppnPrintResultList);
}
Expand All @@ -82,10 +88,11 @@ public PpnDto getBestPpn(LigneKbartDto kbart, String provider, boolean injectKaf
feedPpnListFromDat(kbart, ppnElecScoredList, ppnPrintResultList);
}

return getBestPpnByScore(kbart, ppnElecScoredList, ppnPrintResultList, injectKafka, sendErrors);
return getBestPpnByScore(kbart, ppnElecScoredList, ppnPrintResultList, injectKafka);
}

private void feedPpnListFromOnline(LigneKbartDto kbart, String provider, Map<String, Integer> ppnElecScoredList, Set<String> ppnPrintResultList) throws IOException, URISyntaxException, IllegalArgumentException, BestPpnException {
if (sendLogs) kbartLineLogs.add("Entrée dans onlineId2Ppn");
log.debug("Entrée dans onlineId2Ppn");
try {
setScoreToEveryPpnFromResultWS(service.callOnlineId2Ppn(kbart.getPublicationType(), kbart.getOnlineIdentifier(), provider), kbart.getTitleUrl(), this.scoreOnlineId2PpnElect, ppnElecScoredList, ppnPrintResultList);
Expand All @@ -95,6 +102,7 @@ private void feedPpnListFromOnline(LigneKbartDto kbart, String provider, Map<Str
}

private void feedPpnListFromPrint(LigneKbartDto kbart, String provider, Map<String, Integer> ppnElecScoredList, Set<String> ppnPrintResultList) throws IOException, URISyntaxException, IllegalArgumentException, BestPpnException {
if (sendLogs) kbartLineLogs.add("Entrée dans printId2Ppn");
log.debug("Entrée dans printId2Ppn");
try {
ResultWsSudocDto resultCallWs = service.callPrintId2Ppn(kbart.getPublicationType(), kbart.getPrintIdentifier(), provider);
Expand All @@ -114,14 +122,17 @@ private void feedPpnListFromPrint(LigneKbartDto kbart, String provider, Map<Stri
private void feedPpnListFromDat(LigneKbartDto kbart, Map<String, Integer> ppnElecScoredList, Set<String> ppnPrintResultList) throws IOException {
ResultDat2PpnWebDto resultDat2PpnWeb = null;
if (!kbart.getAnneeFromDate_monograph_published_online().isEmpty()) {
if (sendLogs) kbartLineLogs.add("Appel dat2ppn : date_monograph_published_online : " + kbart.getAnneeFromDate_monograph_published_online() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor());
log.debug("Appel dat2ppn : date_monograph_published_online : " + kbart.getAnneeFromDate_monograph_published_online() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor());
resultDat2PpnWeb = service.callDat2Ppn(kbart.getAnneeFromDate_monograph_published_online(), kbart.getAuthor(), kbart.getPublicationTitle());
} else if (ppnElecScoredList.isEmpty() && !kbart.getAnneeFromDate_monograph_published_print().isEmpty()) {
if (sendLogs) kbartLineLogs.add("Appel dat2ppn : date_monograph_published_print : " + kbart.getAnneeFromDate_monograph_published_print() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor());
log.debug("Appel dat2ppn : date_monograph_published_print : " + kbart.getAnneeFromDate_monograph_published_print() + " / publication_title : " + kbart.getPublicationTitle() + " auteur : " + kbart.getAuthor());
resultDat2PpnWeb = service.callDat2Ppn(kbart.getAnneeFromDate_monograph_published_print(), kbart.getAuthor(), kbart.getPublicationTitle());
}
if(resultDat2PpnWeb != null && !resultDat2PpnWeb.getPpns().isEmpty()) {
for (String ppn : resultDat2PpnWeb.getPpns()) {
if (sendLogs) kbartLineLogs.add("résultat : ppn " + ppn);
log.debug("résultat : ppn " + ppn);
NoticeXml notice = noticeService.getNoticeByPpn(ppn);
if (notice.isNoticeElectronique()) {
Expand All @@ -140,6 +151,7 @@ private void feedPpnListFromDoi(String doi, String provider, Map<String, Integer
if(ppn.getTypeSupport().equals(TYPE_SUPPORT.ELECTRONIQUE)){
setScoreToPpnElect(scoreDoi2Ppn,ppnElecScoredList,nbPpnElec,ppn);
} else {
if (sendLogs) kbartLineLogs.add("PPN Imprimé : " + ppn);
log.info("PPN Imprimé : " + ppn);
ppnPrintResultList.add(ppn.getPpn());
}
Expand All @@ -151,11 +163,13 @@ private void setScoreToEveryPpnFromResultWS(ResultWsSudocDto resultCallWs, Strin
int nbPpnElec = (int) resultCallWs.getPpns().stream().filter(ppnWithTypeDto -> ppnWithTypeDto.getTypeSupport().equals(TYPE_SUPPORT.ELECTRONIQUE)).count();
for (PpnWithTypeDto ppn : resultCallWs.getPpns()) {
if(ppn.getTypeSupport().equals(TYPE_SUPPORT.IMPRIME)) {
if (sendLogs) kbartLineLogs.add("PPN Imprimé : " + ppn);
log.info("PPN Imprimé : " + ppn);
ppnPrintResultList.add(ppn.getPpn());
} else if (ppn.getTypeDocument() != TYPE_DOCUMENT.MONOGRAPHIE || ppn.isProviderPresent() || checkUrlService.checkUrlInNotice(ppn.getPpn(), titleUrl)){
setScoreToPpnElect(score, ppnElecResultList, nbPpnElec, ppn);
} else {
if (sendLogs) kbartLineLogs.add("Le PPN " + ppn + " n'a pas de provider trouvé");
log.error("Le PPN " + ppn + " n'a pas de provider trouvé");
}
}
Expand All @@ -169,28 +183,31 @@ private void setScoreToPpnElect(int score, Map<String, Integer> ppnElecScoredLis
} else {
ppnElecScoredList.put(ppn.getPpn(), (score / nbPpnElec));
}
if (sendLogs) kbartLineLogs.add("PPN Electronique : " + ppn + " / score : " + ppnElecScoredList.get(ppn.getPpn()));
log.info("PPN Electronique : " + ppn + " / score : " + ppnElecScoredList.get(ppn.getPpn()));
}

public PpnDto getBestPpnByScore(LigneKbartDto kbart, Map<String, Integer> ppnElecResultList, Set<String> ppnPrintResultList, boolean isForced, boolean sendErrors) throws BestPpnException {
public PpnDto getBestPpnByScore(LigneKbartDto kbart, Map<String, Integer> ppnElecResultList, Set<String> ppnPrintResultList, boolean isForced) throws BestPpnException {
Map<String, Integer> ppnElecScore = Utils.getMaxValuesFromMap(ppnElecResultList);
return switch (ppnElecScore.size()) {
case 0 -> {
log.info("Aucun ppn électronique trouvé." + kbart);
if (sendLogs) kbartLineLogs.add("Aucun ppn électronique trouvé. " + kbart);
log.info("Aucun ppn électronique trouvé. " + kbart);
yield switch (ppnPrintResultList.size()) {
case 0 -> {
kbart.setErrorType("Aucun ppn trouvé");
if(sendErrors) {
yield new PpnDto(null, DESTINATION_TOPIC.NO_PPN_FOUND_SUDOC, "Aucun ppn électronique trouvé.");
if(sendLogs) {
yield new PpnDto(null, DESTINATION_TOPIC.NO_PPN_FOUND_SUDOC, kbartLineLogs);
} else {
yield new PpnDto(null, DESTINATION_TOPIC.NO_PPN_FOUND_SUDOC);
}
}

case 1 -> {
kbart.setErrorType("Ppn imprimé trouvé : " + ppnPrintResultList.stream().toList().get(0));
if(sendErrors) {
yield new PpnDto(ppnPrintResultList.stream().toList().get(0),DESTINATION_TOPIC.PRINT_PPN_SUDOC, TYPE_SUPPORT.IMPRIME, "Ppn imprimé trouvé à la place d'un ppn électronique : " + ppnPrintResultList.stream().toList().get(0));
if(sendLogs) {
kbartLineLogs.add("Ppn imprimé trouvé : " + ppnPrintResultList.stream().toList().get(0));
yield new PpnDto(ppnPrintResultList.stream().toList().get(0),DESTINATION_TOPIC.PRINT_PPN_SUDOC, TYPE_SUPPORT.IMPRIME, kbartLineLogs);
} else {
yield new PpnDto(ppnPrintResultList.stream().toList().get(0),DESTINATION_TOPIC.PRINT_PPN_SUDOC, TYPE_SUPPORT.IMPRIME);
}
Expand All @@ -202,8 +219,9 @@ yield switch (ppnPrintResultList.size()) {
// vérification du forçage
if (isForced) {
log.error(errorString);
if (sendErrors) {
yield new PpnDto("",DESTINATION_TOPIC.BEST_PPN_BACON, "Plusieurs ppn imprimés (" + String.join(", ", ppnPrintResultList) + ") ont été trouvés.");
if (sendLogs) {
kbartLineLogs.add("Plusieurs ppn imprimés (" + String.join(", ", ppnPrintResultList) + ") ont été trouvés.");
yield new PpnDto("",DESTINATION_TOPIC.BEST_PPN_BACON, kbartLineLogs);
} else {
yield new PpnDto("",DESTINATION_TOPIC.BEST_PPN_BACON);
}
Expand All @@ -213,7 +231,13 @@ yield switch (ppnPrintResultList.size()) {
}
};
}
case 1 -> new PpnDto(ppnElecScore.keySet().stream().findFirst().get(), DESTINATION_TOPIC.BEST_PPN_BACON, TYPE_SUPPORT.ELECTRONIQUE);
case 1 -> {
if (sendLogs) {
yield new PpnDto(ppnElecScore.keySet().stream().findFirst().get(), DESTINATION_TOPIC.BEST_PPN_BACON, TYPE_SUPPORT.ELECTRONIQUE, kbartLineLogs);
} else {
yield new PpnDto(ppnElecScore.keySet().stream().findFirst().get(), DESTINATION_TOPIC.BEST_PPN_BACON, TYPE_SUPPORT.ELECTRONIQUE);
}
}

default -> {
String listPpn = String.join(", ", ppnElecScore.keySet());
Expand All @@ -222,8 +246,9 @@ yield switch (ppnPrintResultList.size()) {
// vérification du forçage
if (isForced) {
log.error(errorString);
if (sendErrors) {
yield new PpnDto("", DESTINATION_TOPIC.BEST_PPN_BACON, errorString);
if (sendLogs) {
kbartLineLogs.add(errorString);
yield new PpnDto("", DESTINATION_TOPIC.BEST_PPN_BACON, kbartLineLogs);
} else {
yield new PpnDto("", DESTINATION_TOPIC.BEST_PPN_BACON);
}
Expand Down
Loading

0 comments on commit d907e9e

Please sign in to comment.