Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge develop dans main #11

Merged
merged 12 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.3.0.0</version>
<version>21.9.0.0</version>
</dependency>
<!-- === Logging === -->
<dependency>
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/fr/abes/logskbart/entity/bacon/LigneKbart.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Date;

@Entity
@Table(name = "LIGNE_KBART_CONVERGENCE")
@Table(name = "LIGNE_KBART")
@Setter @Getter
public class LigneKbart implements Serializable {
@Id
Expand Down Expand Up @@ -65,13 +65,15 @@ public class LigneKbart implements Serializable {
private String precedeingPublicationTitleId;
@Column(name = "ACCESS_TYPE")
private String accessType;
@ManyToOne(targetEntity = ProviderPackage.class, cascade = CascadeType.REMOVE, optional = false)
@JoinColumns({
@JoinColumn(name = "PROVIDER_PACKAGE_PACKAGE", referencedColumnName = "PACKAGE"),
@JoinColumn(name = "PROVIDER_PACKAGE_DATE_P", referencedColumnName = "DATE_P"),
@JoinColumn(name = "PROVIDER_PACKAGE_IDT_PROVIDER", referencedColumnName = "PROVIDER_IDT_PROVIDER"
)})
private ProviderPackage providerPackage;
@Column(name = "PACKAGE")
private String packageName;
@Column(name = "DATE_P")
private Date dateP;
@Column(name = "PROVIDER_IDT_PROVIDER")
private Integer providerIdtProvider;
@Column(name = "BEST_PPN")
private String bestPpn;
@ManyToOne(targetEntity = ProviderPackage.class, cascade = CascadeType.REMOVE, optional = false)
@JoinColumn(name = "ID_PROVIDER_PACKAGE", referencedColumnName = "ID_PROVIDER_PACKAGE")
private ProviderPackage providerPackage;
}
19 changes: 15 additions & 4 deletions src/main/java/fr/abes/logskbart/entity/bacon/ProviderPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;

@Entity
@Table(name = "PROVIDER_PACKAGE")
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class ProviderPackage implements Serializable {
@EmbeddedId
private ProviderPackageId providerPackageId;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_PROVIDER_PACKAGE")
private Integer providerPackageId;
@Column(name = "PACKAGE")
private String packageName;
@Column(name = "DATE_P")
private Date dateP;
@Column(name = "PROVIDER_IDT_PROVIDER")
private Integer providerIdtProvider;

@Column(name = "LABEL_ABES")
private char labelAbes;
Expand All @@ -24,8 +33,10 @@ public class ProviderPackage implements Serializable {
@JoinColumn(referencedColumnName = "IDT_PROVIDER", insertable = false, updatable = false)
private Provider provider;

public ProviderPackage(ProviderPackageId providerPackageId, char labelAbes) {
this.providerPackageId = providerPackageId;
public ProviderPackage(String packageName, Date dateP, Integer providerIdtProvider, char labelAbes) {
this.packageName = packageName;
this.dateP = dateP;
this.providerIdtProvider = providerIdtProvider;
this.labelAbes = labelAbes;
}
}

This file was deleted.

48 changes: 0 additions & 48 deletions src/main/java/fr/abes/logskbart/kafka/KbartListener.java

This file was deleted.

93 changes: 84 additions & 9 deletions src/main/java/fr/abes/logskbart/kafka/LogsListener.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package fr.abes.logskbart.kafka;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.abes.logskbart.dto.Kbart2KafkaDto;
import fr.abes.logskbart.entity.logs.LogKbart;
import fr.abes.logskbart.repository.logs.LogKbartRepository;
import fr.abes.logskbart.utils.UtilsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.header.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service
public class LogsListener {

Expand All @@ -26,13 +34,80 @@ public class LogsListener {
@Autowired
private LogKbartRepository repository;

@KafkaListener(topics = {"infokbart2kafka", "errorkbart2kafka"}, groupId = "logskbart", containerFactory = "kafkaLogsListenerContainerFactory")
public void listenInfoKbart2KafkaAndErrorKbart2Kafka(ConsumerRecord<String, String> message) throws JsonProcessingException {
Kbart2KafkaDto dto = mapper.readValue(message.value(), Kbart2KafkaDto.class);
LogKbart entity = logsMapper.map(dto, LogKbart.class);
Timestamp timestamp = new Timestamp(message.timestamp());
entity.setTimestamp(new Date(timestamp.getTime()));
entity.setPackageName(message.key());
repository.save(entity);
/**
* Ecoute les topic de log d'erreurs et de fin de traitement bestPpn et génère un fichier err pour chaque fichier kbart
* @param message le message kafka
* @throws IOException exception levée
*/
@KafkaListener(topics = {"errorkbart2kafka", "bestppn.endoftraitment"}, groupId = "logskbart", containerFactory = "kafkaLogsListenerContainerFactory")
public void listenInfoKbart2KafkaAndErrorKbart2Kafka(ConsumerRecord<String, String> message) throws IOException {

if (message.topic().equals("errorkbart2kafka")) {
Kbart2KafkaDto dto = mapper.readValue(message.value(), Kbart2KafkaDto.class);
LogKbart entity = logsMapper.map(dto, LogKbart.class);
Timestamp timestamp = new Timestamp(message.timestamp());
entity.setTimestamp(new Date(timestamp.getTime()));
entity.setPackageName(message.key().replaceAll("\\[line\\s:\\s\\d+\\]", ""));

// Si la ligne de log sur le topic errorkbart2kafka est de type ERROR
if (entity.getLevel().toString().equals("ERROR")) {
String nbrLine = message.key().substring(message.key().indexOf(".tsv")+4).replaceAll("\\[line\\s:\\s", "").replaceAll("]", "");
String fileName = message.key().replaceAll(".tsv\\[line\\s:\\s\\d+\\]", ".err");
String line = nbrLine + "\t" + dto.getMessage();

// Vérifie qu'un fichier portant le nom du kbart en cours existe
Path of = Path.of(fileName);
if (Files.exists(of)) {
// Inscrit la ligne dedans
Files.write(of, (line + System.lineSeparator()).getBytes(), StandardOpenOption.APPEND);
} else if (!Files.exists(of)) {
try {
// Créer le fichier et inscrit la ligne dedans
Files.createFile(of);
// Créer la ligne d'en-tête
Files.write(of, ("LINE\tMESSAGE" + System.lineSeparator()).getBytes(), StandardOpenOption.APPEND);
// Inscrit les informations sur la ligne
Files.write(of, (line + System.lineSeparator()).getBytes(), StandardOpenOption.APPEND);
log.info("Fichier temporaire créé.");
} catch (SecurityException | IOException e) {
log.error("Erreur lors de la création du fichier temporaire. " + e);
throw new RuntimeException(e);
}
}
}

// Inscrit l'entity en BDD
repository.save(entity);

} else { // Si la ligne sur le topic bestppn.endoftraitment contient OK

// Créer un nouveau Path avec le FileName (en remplaçant l'extension par .err)
Path source = null;
for (Header header : message.headers().toArray()) {
if (header.key().equals("FileName")) {
source = Path.of(new String(header.value()).replaceAll(".tsv", ".err"));
break;
}
}

// Copie le fichier existant vers le répertoire temporaire en ajoutant sa date de création
if (source != null && Files.exists(source)) {
LocalDateTime time = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss", Locale.FRANCE);
String date = format.format(time);

// Vérification du chemin et création si inexistant
String tempLog = "tempLog/";
File chemin = new File("tempLog/");
if (!chemin.isDirectory()) {
Files.createDirectory(Paths.get(tempLog));
}
Path target = Path.of("tempLog\\" + date + "_" + source);

// Déplacement du fichier
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
log.info("Fichier de log transféré dans le dossier temporaire.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import fr.abes.logskbart.configuration.BaconDbConfiguration;
import fr.abes.logskbart.entity.bacon.ProviderPackage;
import fr.abes.logskbart.entity.bacon.ProviderPackageId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -11,6 +10,6 @@

@Repository
@BaconDbConfiguration
public interface ProviderPackageRepository extends JpaRepository<ProviderPackage, ProviderPackageId> {
Optional<ProviderPackage> findByProviderPackageId(ProviderPackageId providerPackageId);
public interface ProviderPackageRepository extends JpaRepository<ProviderPackage, Integer> {
Optional<ProviderPackage> findByPackageNameAndDatePAndProviderIdtProvider(String packageName, Date dateP, Integer providerIdtProvider);
}
7 changes: 0 additions & 7 deletions src/main/java/fr/abes/logskbart/service/KbartLoader.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/fr/abes/logskbart/service/KbartLoaderFactory.java

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/fr/abes/logskbart/service/KbartService.java
Original file line number Diff line number Diff line change
@@ -1,23 +0,0 @@
package fr.abes.logskbart.service;

import fr.abes.logskbart.dto.PackageKbartDto;
import fr.abes.logskbart.utils.LoaderType;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class KbartService {
private final KbartLoaderFactory factory;

private final Environment env;

public KbartService(KbartLoaderFactory factory, Environment env) {
this.factory = factory;
this.env = env;
}

public void chargerKbart(PackageKbartDto packageKbart) {
KbartLoader loader = factory.getLoader(LoaderType.valueOf(env.getProperty("LOADERTYPE")));
loader.chargerPackageKbart(packageKbart);
}
}
Loading
Loading