UPDATE FIX BUG_679

This commit is contained in:
PAVEL PALMA
2025-10-27 18:20:19 -06:00
parent 9fb1088f7d
commit 6d68ae16ef
9 changed files with 210 additions and 47 deletions

View File

@@ -9,9 +9,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL) @JsonInclude(Include.NON_NULL)

View File

@@ -18,11 +18,11 @@ public class ConversationMessageMapper {
public Map<String, Object> toMap(ConversationMessageDTO message) { public Map<String, Object> toMap(ConversationMessageDTO message) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("type", message.type().name()); map.put("entidad", message.type().name());
map.put("timestamp", message.timestamp()); map.put("tiempo", message.timestamp());
map.put("text", message.text()); map.put("mensaje", message.text());
if (message.parameters() != null) { if (message.parameters() != null) {
map.put("parameters", message.parameters()); map.put("parametros", message.parameters());
} }
if (message.canal() != null) { if (message.canal() != null) {
map.put("canal", message.canal()); map.put("canal", message.canal());
@@ -32,10 +32,10 @@ public class ConversationMessageMapper {
public ConversationMessageDTO fromMap(Map<String, Object> map) { public ConversationMessageDTO fromMap(Map<String, Object> map) {
return new ConversationMessageDTO( return new ConversationMessageDTO(
MessageType.valueOf((String) map.get("type")), MessageType.valueOf((String) map.get("entidad")),
(Instant) map.get("timestamp"), (Instant) map.get("tiempo"),
(String) map.get("text"), (String) map.get("mensaje"),
(Map<String, Object>) map.get("parameters"), (Map<String, Object>) map.get("parametros"),
(String) map.get("canal") (String) map.get("canal")
); );
} }

View File

@@ -22,8 +22,8 @@ public class FirestoreConversationMapper {
return null; return null;
} }
Timestamp createdAtTimestamp = document.getTimestamp("createdAt"); Timestamp createdAtTimestamp = document.getTimestamp("fechaCreacion");
Timestamp lastModifiedTimestamp = document.getTimestamp("lastModified"); Timestamp lastModifiedTimestamp = document.getTimestamp("ultimaActualizacion");
Instant createdAt = (createdAtTimestamp != null) ? createdAtTimestamp.toDate().toInstant() : null; Instant createdAt = (createdAtTimestamp != null) ? createdAtTimestamp.toDate().toInstant() : null;
Instant lastModified = (lastModifiedTimestamp != null) ? lastModifiedTimestamp.toDate().toInstant() : null; Instant lastModified = (lastModifiedTimestamp != null) ? lastModifiedTimestamp.toDate().toInstant() : null;
@@ -34,7 +34,7 @@ public class FirestoreConversationMapper {
document.getString("telefono"), document.getString("telefono"),
createdAt, createdAt,
lastModified, lastModified,
document.getString("lastMessage"), document.getString("ultimoMensaje"),
document.getString("pantallaContexto") document.getString("pantallaContexto")
); );
} }
@@ -44,9 +44,9 @@ public class FirestoreConversationMapper {
sessionMap.put("sessionId", session.sessionId()); sessionMap.put("sessionId", session.sessionId());
sessionMap.put("userId", session.userId()); sessionMap.put("userId", session.userId());
sessionMap.put("telefono", session.telefono()); sessionMap.put("telefono", session.telefono());
sessionMap.put("createdAt", session.createdAt()); sessionMap.put("fechaCreacion", session.createdAt());
sessionMap.put("lastModified", session.lastModified()); sessionMap.put("ultimaActualizacion", session.lastModified());
sessionMap.put("lastMessage", session.lastMessage()); sessionMap.put("ultimoMensaje", session.lastMessage());
sessionMap.put("pantallaContexto", session.pantallaContexto()); sessionMap.put("pantallaContexto", session.pantallaContexto());
return sessionMap; return sessionMap;
} }

View File

@@ -1,36 +1,79 @@
/*
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
* Your use of it is subject to your agreement with Google.
*/
package com.example.mapper.messagefilter; package com.example.mapper.messagefilter;
import com.example.dto.dialogflow.conversation.ConversationMessageDTO; import com.example.dto.dialogflow.conversation.ConversationMessageDTO;
import com.example.dto.dialogflow.conversation.ConversationSessionDTO; import com.example.dto.dialogflow.conversation.ConversationSessionDTO;
import com.example.dto.dialogflow.conversation.MessageType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component @Component
public class ConversationContextMapper { public class ConversationContextMapper {
private static final int MAX_MESSAGES = 10; @Value("${conversation.context.message.limit:60}")
private int messageLimit;
@Value("${conversation.context.days.limit:30}")
private int daysLimit;
public String toText(ConversationSessionDTO session, List<ConversationMessageDTO> messages) { public String toText(ConversationSessionDTO session, List<ConversationMessageDTO> messages) {
if (messages == null || messages.isEmpty()) {
return "";
}
return toTextFromMessages(messages); return toTextFromMessages(messages);
} }
public String toTextWithLimits(ConversationSessionDTO session, List<ConversationMessageDTO> messages) { public String toTextWithLimits(ConversationSessionDTO session, List<ConversationMessageDTO> messages) {
if (messages.size() > MAX_MESSAGES) { if (messages == null || messages.isEmpty()) {
messages = messages.subList(messages.size() - MAX_MESSAGES, messages.size()); return "";
} }
return toTextFromMessages(messages);
Instant thirtyDaysAgo = Instant.now().minus(daysLimit, ChronoUnit.DAYS);
List<ConversationMessageDTO> recentEntries = messages.stream()
.filter(entry -> entry.timestamp() != null && entry.timestamp().isAfter(thirtyDaysAgo))
.sorted(Comparator.comparing(ConversationMessageDTO::timestamp).reversed())
.limit(messageLimit)
.sorted(Comparator.comparing(ConversationMessageDTO::timestamp))
.collect(Collectors.toList());
return toTextFromMessages(recentEntries);
} }
public String toTextFromMessages(List<ConversationMessageDTO> messages) { public String toTextFromMessages(List<ConversationMessageDTO> messages) {
return messages.stream() return messages.stream()
.map(message -> String.format("%s: %s", message.type(), message.text())) .map(this::formatEntry)
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
} }
private String formatEntry(ConversationMessageDTO entry) {
String prefix = "User: ";
if (entry.type() != null) {
switch (entry.type()) {
case MessageType.AGENT:
prefix = "Agent: ";
break;
case MessageType.SYSTEM:
prefix = "System: ";
break;
case MessageType.USER:
default:
prefix = "User: ";
break;
}
}
String text = prefix + entry.text();
if (entry.parameters() != null && !entry.parameters().isEmpty()) {
text += " " + entry.parameters().toString();
}
return text;
}
} }

View File

@@ -220,4 +220,10 @@ public class FirestoreBaseRepository {
throw new RuntimeException("Error deleting collection", e); throw new RuntimeException("Error deleting collection", e);
} }
} }
public void deleteDocumentAndSubcollections(DocumentReference docRef, String subcollection)
throws ExecutionException, InterruptedException {
deleteCollection(docRef.collection(subcollection).getPath(), 50);
deleteDocument(docRef);
}
} }

View File

@@ -1,106 +1,214 @@
package com.example.service.base; package com.example.service.base;
import com.example.repository.FirestoreBaseRepository; import com.example.repository.FirestoreBaseRepository;
import com.google.cloud.firestore.CollectionReference; import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QueryDocumentSnapshot;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.ReactiveRedisTemplate; import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
@Service @Service
public class DataPurgeService { public class DataPurgeService {
private static final Logger logger = LoggerFactory.getLogger(DataPurgeService.class); private static final Logger logger = LoggerFactory.getLogger(DataPurgeService.class);
private final ReactiveRedisTemplate<String, ?> redisTemplate; private final ReactiveRedisTemplate<String, ?> redisTemplate;
private final FirestoreBaseRepository firestoreBaseRepository; private final FirestoreBaseRepository firestoreBaseRepository;
private final Firestore firestore; private final Firestore firestore;
@Autowired @Autowired
public DataPurgeService( public DataPurgeService(
@Qualifier("reactiveRedisTemplate") ReactiveRedisTemplate<String, ?> redisTemplate, @Qualifier("reactiveRedisTemplate") ReactiveRedisTemplate<String, ?> redisTemplate,
FirestoreBaseRepository firestoreBaseRepository, Firestore firestore) { FirestoreBaseRepository firestoreBaseRepository, Firestore firestore) {
this.redisTemplate = redisTemplate; this.redisTemplate = redisTemplate;
this.firestoreBaseRepository = firestoreBaseRepository; this.firestoreBaseRepository = firestoreBaseRepository;
this.firestore = firestore; this.firestore = firestore;
} }
public Mono<Void> purgeAllData() { public Mono<Void> purgeAllData() {
return purgeRedis() return purgeRedis()
.then(purgeFirestore()); .then(purgeFirestore());
} }
private Mono<Void> purgeRedis() { private Mono<Void> purgeRedis() {
logger.info("Starting Redis data purge."); logger.info("Starting Redis data purge.");
return redisTemplate.getConnectionFactory().getReactiveConnection().serverCommands().flushAll() return redisTemplate.getConnectionFactory().getReactiveConnection().serverCommands().flushAll()
.doOnSuccess(v -> logger.info("Successfully purged all data from Redis.")) .doOnSuccess(v -> logger.info("Successfully purged all data from Redis."))
.doOnError(e -> logger.error("Error purging data from Redis.", e)) .doOnError(e -> logger.error("Error purging data from Redis.", e))
.then(); .then();
} }
private Mono<Void> purgeFirestore() { private Mono<Void> purgeFirestore() {
logger.info("Starting Firestore data purge."); logger.info("Starting Firestore data purge.");
return Mono.fromRunnable(() -> { return Mono.fromRunnable(() -> {
try { try {
String appId = firestoreBaseRepository.getAppId(); String appId = firestoreBaseRepository.getAppId();
String conversationsCollectionPath = String.format("artifacts/%s/conversations", appId); String conversationsCollectionPath = String.format("artifacts/%s/conversations", appId);
String notificationsCollectionPath = String.format("artifacts/%s/notifications", appId); String notificationsCollectionPath = String.format("artifacts/%s/notifications", appId);
// Delete 'messages' sub-collections in 'conversations'
logger.info("Deleting 'messages' sub-collections from '{}'", conversationsCollectionPath);
// Delete 'mensajes' sub-collections in 'conversations'
logger.info("Deleting 'mensajes' sub-collections from '{}'", conversationsCollectionPath);
try { try {
List<QueryDocumentSnapshot> conversationDocuments = firestore.collection(conversationsCollectionPath).get().get().getDocuments(); List<QueryDocumentSnapshot> conversationDocuments = firestore.collection(conversationsCollectionPath).get().get().getDocuments();
for (QueryDocumentSnapshot document : conversationDocuments) { for (QueryDocumentSnapshot document : conversationDocuments) {
String messagesCollectionPath = document.getReference().getPath() + "/messages";
String messagesCollectionPath = document.getReference().getPath() + "/mensajes";
logger.info("Deleting sub-collection: {}", messagesCollectionPath); logger.info("Deleting sub-collection: {}", messagesCollectionPath);
firestoreBaseRepository.deleteCollection(messagesCollectionPath, 50); firestoreBaseRepository.deleteCollection(messagesCollectionPath, 50);
} }
} catch (Exception e) { } catch (Exception e) {
if (e.getMessage().contains("NOT_FOUND")) { if (e.getMessage().contains("NOT_FOUND")) {
logger.warn("Collection '{}' not found, skipping.", conversationsCollectionPath); logger.warn("Collection '{}' not found, skipping.", conversationsCollectionPath);
} else { } else {
throw e; throw e;
} }
} }
// Delete the 'conversations' collection // Delete the 'conversations' collection
logger.info("Deleting collection: {}", conversationsCollectionPath); logger.info("Deleting collection: {}", conversationsCollectionPath);
try { try {
firestoreBaseRepository.deleteCollection(conversationsCollectionPath, 50); firestoreBaseRepository.deleteCollection(conversationsCollectionPath, 50);
} catch (Exception e) { } catch (Exception e) {
if (e.getMessage().contains("NOT_FOUND")) { if (e.getMessage().contains("NOT_FOUND")) {
logger.warn("Collection '{}' not found, skipping.", conversationsCollectionPath); logger.warn("Collection '{}' not found, skipping.", conversationsCollectionPath);
} else {
}
else {
throw e; throw e;
} }
} }
// Delete the 'notifications' collection // Delete the 'notifications' collection
logger.info("Deleting collection: {}", notificationsCollectionPath); logger.info("Deleting collection: {}", notificationsCollectionPath);
try { try {
firestoreBaseRepository.deleteCollection(notificationsCollectionPath, 50); firestoreBaseRepository.deleteCollection(notificationsCollectionPath, 50);
} catch (Exception e) { } catch (Exception e) {
if (e.getMessage().contains("NOT_FOUND")) { if (e.getMessage().contains("NOT_FOUND")) {
logger.warn("Collection '{}' not found, skipping.", notificationsCollectionPath); logger.warn("Collection '{}' not found, skipping.", notificationsCollectionPath);
} else { } else {
throw e; throw e;
}
} }
}
logger.info("Successfully purged Firestore collections."); logger.info("Successfully purged Firestore collections.");
} catch (Exception e) { } catch (Exception e) {
logger.error("Error purging Firestore collections.", e); logger.error("Error purging Firestore collections.", e);
throw new RuntimeException("Failed to purge Firestore collections.", e); throw new RuntimeException("Failed to purge Firestore collections.", e);
} }
}).subscribeOn(Schedulers.boundedElastic()).then(); }).subscribeOn(Schedulers.boundedElastic()).then();
} }
} }

View File

@@ -7,6 +7,7 @@ package com.example.service.base;
import com.example.dto.dialogflow.conversation.ConversationSessionDTO; import com.example.dto.dialogflow.conversation.ConversationSessionDTO;
import com.example.service.conversation.FirestoreConversationService; import com.example.service.conversation.FirestoreConversationService;
import com.example.service.conversation.MemoryStoreConversationService;
import com.example.service.notification.FirestoreNotificationService; import com.example.service.notification.FirestoreNotificationService;
import com.example.service.notification.MemoryStoreNotificationService; import com.example.service.notification.MemoryStoreNotificationService;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -21,10 +22,9 @@ public class SessionPurgeService {
private static final Logger logger = LoggerFactory.getLogger(SessionPurgeService.class); private static final Logger logger = LoggerFactory.getLogger(SessionPurgeService.class);
private static final String SESSION_KEY_PREFIX = "conversation:session:"; private static final String SESSION_KEY_PREFIX = "conversation:session:";
private static final String PHONE_TO_SESSION_KEY_PREFIX = "conversation:phone_to_session:";
private final ReactiveRedisTemplate<String, ConversationSessionDTO> redisTemplate; private final ReactiveRedisTemplate<String, ConversationSessionDTO> redisTemplate;
private final ReactiveRedisTemplate<String, String> stringRedisTemplate; private final MemoryStoreConversationService memoryStoreConversationService;
private final FirestoreConversationService firestoreConversationService; private final FirestoreConversationService firestoreConversationService;
private final MemoryStoreNotificationService memoryStoreNotificationService; private final MemoryStoreNotificationService memoryStoreNotificationService;
private final FirestoreNotificationService firestoreNotificationService; private final FirestoreNotificationService firestoreNotificationService;
@@ -32,12 +32,12 @@ public class SessionPurgeService {
@Autowired @Autowired
public SessionPurgeService( public SessionPurgeService(
ReactiveRedisTemplate<String, ConversationSessionDTO> redisTemplate, ReactiveRedisTemplate<String, ConversationSessionDTO> redisTemplate,
ReactiveRedisTemplate<String, String> stringRedisTemplate, MemoryStoreConversationService memoryStoreConversationService,
FirestoreConversationService firestoreConversationService, FirestoreConversationService firestoreConversationService,
MemoryStoreNotificationService memoryStoreNotificationService, MemoryStoreNotificationService memoryStoreNotificationService,
FirestoreNotificationService firestoreNotificationService) { FirestoreNotificationService firestoreNotificationService) {
this.redisTemplate = redisTemplate; this.redisTemplate = redisTemplate;
this.stringRedisTemplate = stringRedisTemplate; this.memoryStoreConversationService = memoryStoreConversationService;
this.firestoreConversationService = firestoreConversationService; this.firestoreConversationService = firestoreConversationService;
this.memoryStoreNotificationService = memoryStoreNotificationService; this.memoryStoreNotificationService = memoryStoreNotificationService;
this.firestoreNotificationService = firestoreNotificationService; this.firestoreNotificationService = firestoreNotificationService;
@@ -50,14 +50,12 @@ public class SessionPurgeService {
return redisTemplate.opsForValue().get(sessionKey) return redisTemplate.opsForValue().get(sessionKey)
.flatMap(session -> { .flatMap(session -> {
if (session != null && session.telefono() != null) { if (session != null && session.telefono() != null) {
String phoneToSessionKey = PHONE_TO_SESSION_KEY_PREFIX + session.telefono(); return memoryStoreConversationService.deleteSession(sessionId)
return redisTemplate.opsForValue().delete(sessionKey)
.then(stringRedisTemplate.opsForValue().delete(phoneToSessionKey))
.then(firestoreConversationService.deleteSession(sessionId)) .then(firestoreConversationService.deleteSession(sessionId))
.then(memoryStoreNotificationService.deleteNotificationSession(session.telefono())) .then(memoryStoreNotificationService.deleteNotificationSession(session.telefono()))
.then(firestoreNotificationService.deleteNotification(session.telefono())); .then(firestoreNotificationService.deleteNotification(session.telefono()));
} else { } else {
return redisTemplate.opsForValue().delete(sessionKey) return memoryStoreConversationService.deleteSession(sessionId)
.then(firestoreConversationService.deleteSession(sessionId)); .then(firestoreConversationService.deleteSession(sessionId));
} }
}); });

View File

@@ -28,7 +28,7 @@ public class FirestoreConversationService {
private static final Logger logger = LoggerFactory.getLogger(FirestoreConversationService.class); private static final Logger logger = LoggerFactory.getLogger(FirestoreConversationService.class);
private static final String CONVERSATION_COLLECTION_PATH_FORMAT = "artifacts/%s/conversations"; private static final String CONVERSATION_COLLECTION_PATH_FORMAT = "artifacts/%s/conversations";
private static final String MESSAGES_SUBCOLLECTION = "messages"; private static final String MESSAGES_SUBCOLLECTION = "mensajes";
private final FirestoreBaseRepository firestoreBaseRepository; private final FirestoreBaseRepository firestoreBaseRepository;
private final FirestoreConversationMapper firestoreConversationMapper; private final FirestoreConversationMapper firestoreConversationMapper;
private final ConversationMessageMapper conversationMessageMapper; private final ConversationMessageMapper conversationMessageMapper;
@@ -110,7 +110,7 @@ public class FirestoreConversationService {
return Mono.fromRunnable(() -> { return Mono.fromRunnable(() -> {
DocumentReference sessionDocRef = getSessionDocumentReference(sessionId); DocumentReference sessionDocRef = getSessionDocumentReference(sessionId);
try { try {
firestoreBaseRepository.deleteDocument(sessionDocRef); firestoreBaseRepository.deleteDocumentAndSubcollections(sessionDocRef, MESSAGES_SUBCOLLECTION);
logger.info("Successfully deleted conversation session for session {}.", sessionId); logger.info("Successfully deleted conversation session for session {}.", sessionId);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
handleException(e, sessionId); handleException(e, sessionId);

View File

@@ -84,7 +84,18 @@ public class MemoryStoreConversationService {
String sessionKey = SESSION_KEY_PREFIX + sessionId; String sessionKey = SESSION_KEY_PREFIX + sessionId;
String messagesKey = MESSAGES_KEY_PREFIX + sessionId; String messagesKey = MESSAGES_KEY_PREFIX + sessionId;
logger.info("Deleting session {} from Memorystore.", sessionId); logger.info("Deleting session {} from Memorystore.", sessionId);
return redisTemplate.opsForValue().get(sessionKey)
.flatMap(session -> {
if (session != null && session.telefono() != null) {
String phoneToSessionKey = PHONE_TO_SESSION_KEY_PREFIX + session.telefono();
return redisTemplate.opsForValue().delete(sessionKey) return redisTemplate.opsForValue().delete(sessionKey)
.then(messageRedisTemplate.opsForList().delete(messagesKey)).then(); .then(stringRedisTemplate.opsForValue().delete(phoneToSessionKey))
.then(messageRedisTemplate.opsForList().delete(messagesKey));
} else {
return redisTemplate.opsForValue().delete(sessionKey)
.then(messageRedisTemplate.opsForList().delete(messagesKey));
}
}).then();
} }
} }