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