로그조회 쿼리 조정
This commit is contained in:
@@ -7,6 +7,7 @@ import com.caliverse.admin.domain.entity.log.GenericLog;
|
||||
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.global.common.utils.DateUtils;
|
||||
import com.caliverse.admin.logs.Indicatordomain.GenericMongoLog;
|
||||
import com.caliverse.admin.logs.entity.LogAction;
|
||||
import com.caliverse.admin.logs.entity.LogDomain;
|
||||
@@ -278,8 +279,8 @@ public class BusinessLogGenericService extends BusinessLogServiceBase {
|
||||
}
|
||||
|
||||
private List<GenericMongoLog> loadRawLogData(LogGenericRequest logGenericRequest) {
|
||||
String startTime = logGenericRequest.getStartDt().toString().substring(0, 10);
|
||||
String endTime = logGenericRequest.getEndDt().toString().substring(0, 10);
|
||||
String startTime = DateUtils.stringToISODateTime(logGenericRequest.getStartDt());
|
||||
String endTime = DateUtils.stringToISODateTime(logGenericRequest.getEndDt());
|
||||
LogAction logAction = logGenericRequest.getLogAction();
|
||||
LogDomain logDomain = logGenericRequest.getLogDomain();
|
||||
SearchUserType searchUserType = logGenericRequest.getSearchType();
|
||||
|
||||
@@ -2,14 +2,18 @@ package com.caliverse.admin.logs.logservice.indicators;
|
||||
|
||||
import com.caliverse.admin.Indicators.Indicatordomain.IndicatorsLog;
|
||||
import com.caliverse.admin.Indicators.Indicatorsservice.base.IndicatorsLogLoadServiceBase;
|
||||
import com.caliverse.admin.Indicators.entity.CurrencyItemLogInfo;
|
||||
import com.caliverse.admin.Indicators.entity.CurrencyLogInfo;
|
||||
import com.caliverse.admin.Indicators.indicatorrepository.IndicatorCurrencyRepository;
|
||||
import com.caliverse.admin.domain.entity.common.SearchUserType;
|
||||
import com.caliverse.admin.global.common.constants.AdminConstants;
|
||||
import com.caliverse.admin.global.common.utils.DataHelper;
|
||||
import com.caliverse.admin.logs.Indicatordomain.CurrencyMongoLog;
|
||||
import com.caliverse.admin.logs.logservice.businesslogservice.BusinessLogCurrencyService;
|
||||
import com.caliverse.admin.mongodb.dto.MongoPageResult;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -201,8 +205,8 @@ public class IndicatorsCurrencyService extends IndicatorsLogLoadServiceBase {
|
||||
.and("currencies.currencyAmount").as("currencyAmount");
|
||||
|
||||
List<AggregationOperation> baseOperations = new ArrayList<>(List.of(
|
||||
Aggregation.match(criteria),
|
||||
unwindOperation,
|
||||
Aggregation.match(criteria),
|
||||
projection,
|
||||
Aggregation.sort(orderBy.equals("DESC") ? Sort.Direction.DESC : Sort.Direction.ASC, AdminConstants.MONGO_DB_KEY_LOGTIME)
|
||||
));
|
||||
@@ -292,37 +296,100 @@ public class IndicatorsCurrencyService extends IndicatorsLogLoadServiceBase {
|
||||
}
|
||||
}
|
||||
|
||||
Criteria postUnwindCriteria = new Criteria();
|
||||
if(logAction != null && !logAction.isEmpty() && !logAction.equals("None")){
|
||||
postUnwindCriteria.and("currencies.action").is(logAction);
|
||||
}
|
||||
if(currencyType != null && !currencyType.isEmpty() && !currencyType.equals("None")){
|
||||
postUnwindCriteria.and("currencies.currencyType").is(currencyType);
|
||||
}
|
||||
if(amountDeltaType != null && !amountDeltaType.isEmpty() && !amountDeltaType.equals("None")){
|
||||
postUnwindCriteria.and("currencies.amountDeltaType").is(amountDeltaType);
|
||||
}
|
||||
|
||||
int totalCount = 0;
|
||||
|
||||
// 카운트 계산
|
||||
if(page != null && page != 0) {
|
||||
totalCount = getOptimizedCount(criteria, postUnwindCriteria);
|
||||
}
|
||||
|
||||
List<T> items = getDataWithLookup(criteria, postUnwindCriteria, startTime, endTime, orderBy, page, size, clazz);
|
||||
|
||||
return new MongoPageResult<>(items, totalCount);
|
||||
}
|
||||
|
||||
// 최적화된 카운트 메소드
|
||||
private int getOptimizedCount(Criteria criteria, Criteria postUnwindCriteria) {
|
||||
List<AggregationOperation> countOperations = new ArrayList<>();
|
||||
|
||||
countOperations.add(Aggregation.match(criteria));
|
||||
|
||||
countOperations.add(Aggregation.unwind("currencies"));
|
||||
|
||||
if (!postUnwindCriteria.getCriteriaObject().isEmpty()) {
|
||||
countOperations.add(Aggregation.match(postUnwindCriteria));
|
||||
}
|
||||
|
||||
countOperations.add(Aggregation.count().as("total"));
|
||||
|
||||
Aggregation countAggregation = Aggregation.newAggregation(countOperations);
|
||||
Document countResult = mongoTemplate.aggregate(
|
||||
countAggregation.withOptions(AggregationOptions.builder().allowDiskUse(true).build()),
|
||||
AdminConstants.MONGO_DB_COLLECTION_CURRENCY,
|
||||
Document.class
|
||||
).getUniqueMappedResult();
|
||||
|
||||
return countResult != null ? countResult.getInteger("total") : 0;
|
||||
}
|
||||
|
||||
// 데이터 조회 메소드
|
||||
private <T extends IndicatorsLog> List<T> getDataWithLookup(
|
||||
Criteria criteria,
|
||||
Criteria postUnwindCriteria,
|
||||
String startTime,
|
||||
String endTime,
|
||||
String orderBy,
|
||||
Integer page,
|
||||
Integer size,
|
||||
Class<T> clazz) {
|
||||
|
||||
UnwindOperation unwindOperation = Aggregation.unwind("currencies");
|
||||
|
||||
AggregationOperation lookup = context -> new Document("$lookup",
|
||||
new Document("from", "item")
|
||||
.append("let", new Document("tranId", "$currencies.tranId"))
|
||||
.append("pipeline", Arrays.asList(
|
||||
new Document("$unwind", "$itemDetails"),
|
||||
new Document("$match",
|
||||
new Document("$expr",
|
||||
new Document("$eq", Arrays.asList("$itemDetails.tranId", "$$tranId")))),
|
||||
new Document("$unwind", "$itemDetails.items"),
|
||||
new Document("$group",
|
||||
new Document("_id", "$itemDetails.tranId")
|
||||
.append("itemMIDs", new Document("$push", "$itemDetails.items.itemMID"))),
|
||||
new Document("$project",
|
||||
new Document("tranId", "$_id")
|
||||
.append("itemMIDString",
|
||||
new Document("$reduce",
|
||||
new Document("input", "$itemMIDs")
|
||||
.append("initialValue", "")
|
||||
.append("in",
|
||||
new Document("$cond", Arrays.asList(
|
||||
new Document("$eq", Arrays.asList("$$value", "")),
|
||||
new Document("$toString", "$$this"),
|
||||
new Document("$concat", Arrays.asList("$$value", "|", new Document("$toString", "$$this")))
|
||||
))))))
|
||||
))
|
||||
.append("as", "itemInfo"));
|
||||
//자바에서lookup은 성은부하가크다
|
||||
// AggregationOperation lookup = context -> new Document("$lookup",
|
||||
// new Document("from", "item")
|
||||
// .append("let", new Document("tranId", "$currencies.tranId"))
|
||||
// .append("pipeline", Arrays.asList(
|
||||
// new Document("$match",
|
||||
// new Document("logDay",
|
||||
// new Document("$gte", startTime)
|
||||
// .append("$lte", endTime))),
|
||||
// new Document("$unwind", "$itemDetails"),
|
||||
// new Document("$match",
|
||||
// new Document("$expr",
|
||||
// new Document("$eq", Arrays.asList("$itemDetails.tranId", "$$tranId")))),
|
||||
// new Document("$unwind", "$itemDetails.items"),
|
||||
// new Document("$group",
|
||||
// new Document("_id", "$itemDetails.tranId")
|
||||
// .append("itemMIDs", new Document("$addToSet", "$itemDetails.items.itemMID"))),
|
||||
// new Document("$project",
|
||||
// new Document("tranId", "$_id")
|
||||
// .append("itemMIDString",
|
||||
// new Document("$reduce",
|
||||
// new Document("input", "$itemMIDs")
|
||||
// .append("initialValue", "")
|
||||
// .append("in",
|
||||
// new Document("$cond", Arrays.asList(
|
||||
// new Document("$eq", Arrays.asList("$$value", "")),
|
||||
// new Document("$toString", "$$this"),
|
||||
// new Document("$concat", Arrays.asList("$$value", "|", new Document("$toString", "$$this")))
|
||||
// ))))))
|
||||
// ))
|
||||
// .append("as", "itemInfo"));
|
||||
|
||||
ProjectionOperation projection = Aggregation.project()
|
||||
.and("_id").as("id")
|
||||
.andExclude("_id")
|
||||
.and(AdminConstants.MONGO_DB_KEY_LOGDAY).as("logDay")
|
||||
.and(AdminConstants.MONGO_DB_KEY_ACCOUNT_ID).as("accountId")
|
||||
.and(AdminConstants.MONGO_DB_KEY_USER_GUID).as("userGuid")
|
||||
@@ -338,59 +405,34 @@ public class IndicatorsCurrencyService extends IndicatorsLogLoadServiceBase {
|
||||
ArrayOperators.arrayOf("itemInfo.itemMIDString").elementAt(0))
|
||||
.then("")).as("itemIDs");
|
||||
|
||||
List<AggregationOperation> baseOperations = new ArrayList<>(List.of(
|
||||
Aggregation.match(criteria),
|
||||
unwindOperation,
|
||||
lookup,
|
||||
projection,
|
||||
Aggregation.sort(orderBy.equals("DESC") ? Sort.Direction.DESC : Sort.Direction.ASC, AdminConstants.MONGO_DB_KEY_LOGTIME)
|
||||
));
|
||||
List<AggregationOperation> dataOperations = new ArrayList<>();
|
||||
|
||||
Criteria postUnwindCriteria = new Criteria();
|
||||
if(logAction != null && !logAction.isEmpty() && !logAction.equals("None")){
|
||||
postUnwindCriteria.and("action").is(logAction);
|
||||
}
|
||||
dataOperations.add(unwindOperation);
|
||||
|
||||
if(currencyType != null && !currencyType.isEmpty() && !currencyType.equals("None")){
|
||||
postUnwindCriteria.and("currencyType").is(currencyType);
|
||||
}
|
||||
|
||||
if(amountDeltaType != null && !amountDeltaType.isEmpty() && !amountDeltaType.equals("None")){
|
||||
postUnwindCriteria.and("amountDeltaType").is(amountDeltaType);
|
||||
}
|
||||
dataOperations.add(Aggregation.match(criteria));
|
||||
|
||||
if (!postUnwindCriteria.getCriteriaObject().isEmpty()) {
|
||||
baseOperations.add(Aggregation.match(postUnwindCriteria));
|
||||
dataOperations.add(Aggregation.match(postUnwindCriteria));
|
||||
}
|
||||
|
||||
int totalCount = 0;
|
||||
if(page != null && page != 0) {
|
||||
List<AggregationOperation> countOperations = new ArrayList<>(baseOperations);
|
||||
countOperations.add(Aggregation.count().as("total"));
|
||||
// dataOperations.add(lookup);
|
||||
|
||||
Aggregation countAggregation = Aggregation.newAggregation(countOperations);
|
||||
Document countResult = mongoTemplate.aggregate(
|
||||
countAggregation.withOptions(AggregationOptions.builder().allowDiskUse(true).build()),
|
||||
AdminConstants.MONGO_DB_COLLECTION_CURRENCY,
|
||||
Document.class
|
||||
).getUniqueMappedResult();
|
||||
totalCount = countResult != null ? countResult.getInteger("total") : 0;
|
||||
}
|
||||
dataOperations.add(Aggregation.sort(orderBy.equals("DESC") ? Sort.Direction.DESC : Sort.Direction.ASC, "currencies.logTime"));
|
||||
|
||||
List<AggregationOperation> dataOperations = new ArrayList<>(baseOperations);
|
||||
if(page != null && page != 0) {
|
||||
int skip = (page - 1) * size;
|
||||
dataOperations.add(Aggregation.skip((long) skip));
|
||||
dataOperations.add(Aggregation.limit(size));
|
||||
}
|
||||
|
||||
dataOperations.add(projection);
|
||||
|
||||
Aggregation aggregation = Aggregation.newAggregation(dataOperations);
|
||||
List<T> items = mongoTemplate.aggregate(
|
||||
return mongoTemplate.aggregate(
|
||||
aggregation.withOptions(AggregationOptions.builder().allowDiskUse(true).build()),
|
||||
AdminConstants.MONGO_DB_COLLECTION_CURRENCY,
|
||||
clazz
|
||||
).getMappedResults();
|
||||
|
||||
return new MongoPageResult<>(items, totalCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.caliverse.admin.logs.Indicatordomain.ItemMongoLog;
|
||||
import com.caliverse.admin.logs.logservice.businesslogservice.BusinessLogCurrencyService;
|
||||
import com.caliverse.admin.logs.logservice.businesslogservice.BusinessLogItemService;
|
||||
import com.caliverse.admin.mongodb.dto.MongoPageResult;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -22,8 +23,7 @@ import org.springframework.data.mongodb.core.aggregation.*;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -116,6 +116,7 @@ public class IndicatorsItemService extends IndicatorsLogLoadServiceBase {
|
||||
String searchType,
|
||||
String searchData,
|
||||
String tranId,
|
||||
String itemId,
|
||||
String logAction,
|
||||
String itemLargeType,
|
||||
String itemSmallType,
|
||||
@@ -202,6 +203,10 @@ public class IndicatorsItemService extends IndicatorsLogLoadServiceBase {
|
||||
postUnwindCriteria.and("countDeltaType").is(countDeltaType);
|
||||
}
|
||||
|
||||
if(itemId != null && !itemId.isEmpty()){
|
||||
postUnwindCriteria.and("itemId").is(Integer.parseInt(itemId));
|
||||
}
|
||||
|
||||
if (!postUnwindCriteria.getCriteriaObject().isEmpty()) {
|
||||
baseOperations.add(Aggregation.match(postUnwindCriteria));
|
||||
}
|
||||
@@ -236,4 +241,52 @@ public class IndicatorsItemService extends IndicatorsLogLoadServiceBase {
|
||||
|
||||
return new MongoPageResult<>(items, totalCount);
|
||||
}
|
||||
|
||||
public Map<String, String> getCurrencyItemLogData(
|
||||
Set<String> tranIds,
|
||||
String startTime,
|
||||
String endTime
|
||||
) {
|
||||
if (tranIds.isEmpty()) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
MongoCollection<Document> itemCollection = mongoTemplate.getCollection("item");
|
||||
|
||||
List<Document> pipeline = Arrays.asList(
|
||||
new Document("$match", new Document()
|
||||
.append("logDay", new Document("$gte", startTime).append("$lte", endTime))),
|
||||
|
||||
new Document("$unwind", "$itemDetails"),
|
||||
|
||||
new Document("$match", new Document()
|
||||
.append("itemDetails.tranId", new Document("$in", new ArrayList<>(tranIds)))),
|
||||
|
||||
new Document("$unwind", "$itemDetails.items"),
|
||||
|
||||
new Document("$group", new Document()
|
||||
.append("_id", "$itemDetails.tranId")
|
||||
.append("itemMIDs", new Document("$addToSet", "$itemDetails.items.itemMID"))),
|
||||
|
||||
new Document("$project", new Document()
|
||||
.append("tranId", "$_id")
|
||||
.append("itemMIDString", new Document("$reduce", new Document()
|
||||
.append("input", "$itemMIDs")
|
||||
.append("initialValue", "")
|
||||
.append("in", new Document("$cond", Arrays.asList(
|
||||
new Document("$eq", Arrays.asList("$$value", "")),
|
||||
new Document("$toString", "$$this"),
|
||||
new Document("$concat", Arrays.asList("$$value", "|", new Document("$toString", "$$this")))
|
||||
))))))
|
||||
);
|
||||
|
||||
Map<String, String> itemMap = new HashMap<>();
|
||||
itemCollection.aggregate(pipeline).forEach(doc -> {
|
||||
String tranId = doc.getString("tranId");
|
||||
String itemMIDString = doc.getString("itemMIDString");
|
||||
itemMap.put(tranId, itemMIDString != null ? itemMIDString : "");
|
||||
});
|
||||
|
||||
return itemMap;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user