mongodb 설정 분기 처리

비즈니스로그 조회 수정
This commit is contained in:
2025-04-09 15:48:14 +09:00
parent a5122cfefc
commit a50f703eb0
3 changed files with 136 additions and 15 deletions

View File

@@ -30,13 +30,28 @@ public class MongoBusinessLogConfig {
String password;
@Value("${mongodb.business-log.db}")
String businessLogdb;
@Value("${spring.profiles.active}")
private String activeProfile;
// instance 방식
// @Bean(name = "mongoBusinessLogClient")
// public MongoClient mongoBusinessLogClient() {
// String encodePassword = URLEncoder.encode(password, StandardCharsets.UTF_8);
// String auth = username.isEmpty() ? "" : String.format("%s:%s@",username, encodePassword);
// String connection = String.format("mongodb://%s%s/?authSource=%s", auth, businessLogHost, businessLogdb);
// return MongoClients.create(connection);
// }
@Bean(name = "mongoBusinessLogClient")
public MongoClient mongoBusinessLogClient() {
String encodePassword = URLEncoder.encode(password, StandardCharsets.UTF_8);
String auth = username.isEmpty() ? "" : String.format("%s:%s@",username, encodePassword);
String connection = String.format("mongodb://%s%s/?authSource=%s", auth, businessLogHost, businessLogdb);
String connection;
// if(activeProfile.equals("local") || activeProfile.equals("dev")) {
if(!activeProfile.equals("qa")) {
connection = String.format("mongodb://%s%s/?authSource=%s", auth, businessLogHost, businessLogdb);
}else{
connection = String.format("mongodb+srv://%s%s/%s", auth, businessLogHost, businessLogdb);
}
return MongoClients.create(connection);
}

View File

@@ -31,12 +31,28 @@ public class MongoIndicatorConfig {
String password;
@Value("${mongodb.indicator.db}")
String db;
@Value("${spring.profiles.active}")
private String activeProfile;
// @Bean(name = "mongoIndicatorClient")
// public MongoClient mongoStatClient() {
// String encodePassword = URLEncoder.encode(password, StandardCharsets.UTF_8);
// String auth = username.isEmpty() ? "" : String.format("%s:%s@",username, encodePassword);
// String connection = String.format("mongodb://%s%s/?authSource=%s", auth, businessLogHost, db);
// return MongoClients.create(connection);
// }
@Bean(name = "mongoIndicatorClient")
public MongoClient mongoStatClient() {
String encodePassword = URLEncoder.encode(password, StandardCharsets.UTF_8);
String auth = username.isEmpty() ? "" : String.format("%s:%s@",username, encodePassword);
String connection = String.format("mongodb://%s%s/?authSource=%s", auth, businessLogHost, db);
String connection;
// if(activeProfile.equals("local") || activeProfile.equals("dev")) {
if(!activeProfile.equals("qa")) {
connection = String.format("mongodb://%s%s/?authSource=%s", auth, businessLogHost, db);
}else{
connection = String.format("mongodb+srv://%s%s/%s", auth, businessLogHost, db);
}
return MongoClients.create(connection);
}

View File

@@ -3,12 +3,14 @@ package com.caliverse.admin.logs.logservice.businesslogservice;
import com.caliverse.admin.domain.entity.common.SearchUserType;
import com.caliverse.admin.domain.request.LogGenericRequest;
import com.caliverse.admin.global.common.constants.AdminConstants;
import com.caliverse.admin.global.common.utils.CommonUtils;
import com.caliverse.admin.logs.entity.LogAction;
import com.caliverse.admin.logs.entity.LogDomain;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
@@ -23,6 +25,9 @@ import java.util.*;
@Service
public class BusinessLogGenericService extends BusinessLogServiceBase {
@Value("${spring.profiles.active}")
private String activeProfile;
public BusinessLogGenericService(@Qualifier("mongoBusinessLogTemplate") MongoTemplate mongoTemplate) {
super(mongoTemplate);
@@ -38,22 +43,40 @@ public class BusinessLogGenericService extends BusinessLogServiceBase {
SearchUserType searchUserType = logGenericRequest.getSearchType();
String searchData = logGenericRequest.getSearchData();
String tranId = logGenericRequest.getTranId();
List<LogGenericRequest.LogFilter> filters = logGenericRequest.getFilters();
Criteria criteria = makeCriteria(startTime, endTime);
List<AggregationOperation> operations = setDefaultOperation(criteria);
//message json parsing
operations.add(context ->
new Document("$addFields",
new Document("parseMessage",
new Document("$function",
new Document("body", "function(jsonString) { try { return JSON.parse(jsonString); } catch(e) { return {}; } }")
.append("args", Arrays.asList("$message"))
.append("lang", "js")
)
)
)
);
// if(activeProfile.equals("local") || activeProfile.equals("dev")) {
if(!activeProfile.equals("qa")) {
//message json parsing
operations.add(context ->
new Document("$addFields",
new Document("parseMessage",
new Document("$function",
new Document("body", "function(jsonString) { try { return JSON.parse(jsonString); } catch(e) { return {}; } }")
.append("args", Arrays.asList("$message"))
.append("lang", "js")
)
)
)
);
}else {
operations.add(context ->
new Document("$addFields",
new Document("parseMessage",
new Document("$convert",
new Document("input", "$message")
.append("to", "object")
.append("onError", new Document())
)
)
)
);
}
operations.add(context ->
new Document("$addFields",
@@ -112,6 +135,73 @@ public class BusinessLogGenericService extends BusinessLogServiceBase {
);
}
if(filters != null && !filters.isEmpty()) {
List<Document> filterConditions = new ArrayList<>();
for (LogGenericRequest.LogFilter filter : filters) {
if (filter.getFieldName() != null && !filter.getFieldName().isEmpty() &&
filter.getValue() != null) {
Object convertedValue = null;
// 필드 타입에 따른 값 변환만 수행
switch (filter.getFieldType()) {
case Number:
try {
if (filter.getValue().contains(".")) {
convertedValue = Double.parseDouble(filter.getValue());
} else {
convertedValue = Long.parseLong(filter.getValue());
}
} catch (NumberFormatException e) {
log.warn("Failed to convert value to number: {}", filter.getValue(), e);
convertedValue = filter.getValue();
}
break;
case Boolean:
convertedValue = Boolean.parseBoolean(filter.getValue());
break;
case String:
case None:
default:
convertedValue = filter.getValue();
break;
}
// 중첩된 JSON 구조에 맞게 $regex를 사용한 패턴 매칭
String regexPattern = "\"" + filter.getFieldName() + "\":" + CommonUtils.formatValueForRegex(convertedValue);
Document filterDoc = new Document("message", new Document("$regex", regexPattern));
filterConditions.add(filterDoc);
}
}
if (!filterConditions.isEmpty()) {
operations.add(context -> new Document("$match", new Document("$and", filterConditions)));
}
}
// if(filters != null && !filters.isEmpty()) {
// List<Document> filterDocuments = new ArrayList<>();
//
// for (LogGenericRequest.LogFilter filter : filters) {
// if (filter.getFieldName() != null && !filter.getFieldName().isEmpty() &&
// filter.getValue() != null) {
// Document filterDoc = new Document(filter.getFieldName(), filter.getValue());
// filterDocuments.add(filterDoc);
// }
// }
//
// if (!filterDocuments.isEmpty()) {
// operations.add(context ->
// new Document("$match",
// new Document("$and", filterDocuments)
// )
// );
// }
// }
// 최종 출력 형식
operations.add(context ->
new Document("$sort",