.
This commit is contained in:
@@ -355,22 +355,21 @@ public class MarketingAnalysisService {
|
||||
|
||||
/**
|
||||
* Очистка JSON-ответа от markdown-тегов
|
||||
* Использует поиск первой и последней фигурных скобок для надежного извлечения
|
||||
* JSON
|
||||
*/
|
||||
private String cleanJsonFromMarkdown(String jsonResponse) {
|
||||
if (jsonResponse == null || jsonResponse.trim().isEmpty()) {
|
||||
return jsonResponse;
|
||||
}
|
||||
String cleanedJson = jsonResponse.trim();
|
||||
if (cleanedJson.startsWith("```json")) {
|
||||
cleanedJson = cleanedJson.substring(7);
|
||||
int firstBrace = jsonResponse.indexOf("{");
|
||||
int lastBrace = jsonResponse.lastIndexOf("}");
|
||||
|
||||
if (firstBrace != -1 && lastBrace != -1 && firstBrace < lastBrace) {
|
||||
return jsonResponse.substring(firstBrace, lastBrace + 1);
|
||||
}
|
||||
if (cleanedJson.startsWith("```")) {
|
||||
cleanedJson = cleanedJson.substring(3);
|
||||
}
|
||||
if (cleanedJson.endsWith("```")) {
|
||||
cleanedJson = cleanedJson.substring(0, cleanedJson.length() - 3);
|
||||
}
|
||||
return cleanedJson.trim();
|
||||
// Fallback очистка
|
||||
return jsonResponse.replace("```json", "").replace("```", "").trim();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2728,6 +2727,7 @@ public class MarketingAnalysisService {
|
||||
for (Map.Entry<String, ChartMapping> entry : placeholderMap.entrySet()) {
|
||||
String placeholder = entry.getKey();
|
||||
ChartMapping mapping = entry.getValue();
|
||||
String regex = java.util.regex.Pattern.quote(placeholder);
|
||||
|
||||
if (result.contains(placeholder)) {
|
||||
processedPlaceholders.add(placeholder);
|
||||
@@ -2737,15 +2737,19 @@ public class MarketingAnalysisService {
|
||||
try {
|
||||
String jsonStr = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(chartData);
|
||||
String jsonBlock = "\n```json:" + mapping.chartKey + "\n" + jsonStr + "\n```\n";
|
||||
result = result.replace(placeholder, jsonBlock);
|
||||
String replacement = java.util.regex.Matcher.quoteReplacement(jsonBlock);
|
||||
result = result.replaceAll(regex, replacement);
|
||||
logger.info("Inserted chart data for placeholder: {}", placeholder);
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.warn("Failed to serialize chart data for {}: {}", mapping.dataKey, e.getMessage());
|
||||
logger.error("Failed to serialize data for placeholder {}: {}", placeholder, e.getMessage());
|
||||
// Удаляем плейсхолдер при ошибке сериализации
|
||||
result = result.replace(placeholder, "");
|
||||
result = result.replaceAll(regex, "");
|
||||
}
|
||||
} else {
|
||||
// Данных нет - удаляем плейсхолдер
|
||||
result = result.replace(placeholder, "");
|
||||
// ВОТ ЗДЕСЬ ВАЖНЫЙ ЛОГ - когда данных нет
|
||||
logger.warn("MISSING_CHART: No data found for placeholder {} (key: {})",
|
||||
placeholder, mapping.dataKey);
|
||||
result = result.replaceAll(regex, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user