261016_01_ENH: 1차 마이그레이션 완료

This commit is contained in:
2026-10-16 12:21:37 +09:00
parent 7ca665397e
commit 26088f6fd0
188 changed files with 10522 additions and 573 deletions

View File

@@ -0,0 +1,343 @@
package com.kri.kart.server.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kri.common.feature.util.StringUtil;
import com.kri.common.feature.web.BusinessException;
import com.kri.common.feature.web.RequestUserContext;
import com.kri.kart.server.common.enums.ProjectType;
import com.kri.kart.server.dto.project.ProjectRefDto;
import com.kri.kart.server.dto.question.QuestionDto;
import com.kri.kart.server.dto.statistics.StatisticsBatchDto;
import com.kri.kart.server.dto.statistics.StatisticsDataDto;
import com.kri.kart.server.dto.statistics.StatisticsFilterDto;
import com.kri.kart.server.dto.statistics.StatisticsRequestDto;
import com.kri.kart.server.dto.statistics.StatisticsResponseDto;
import com.kri.kart.server.dto.statistics.StatisticsStreamEventDto;
import com.kri.kart.server.dto.statistics.SurveyResponseDto;
import com.kri.kart.server.persistence.repository.QuestionRepository;
import com.kri.kart.server.persistence.repository.StatisticsRepository;
import com.kri.kart.server.service.statistics.StatisticsAggregationService;
import com.kri.kart.server.service.statistics.StatisticsDataService;
import com.kri.kart.server.service.statistics.StatisticsDimension;
import com.kri.kart.server.service.statistics.StatisticsDimensionService;
import com.kri.kart.server.service.statistics.StatisticsFilterService;
import com.kri.kart.server.service.statistics.StatisticsTextService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
public class StatisticsService {
private static final String STANDARD_MODE = "STANDARD";
// TABULATION requests have no single index; their rows are stored under the INDEX root.
private static final String TABULATION_STORAGE_ROOT = "INDEX";
private static final String ATTRIBUTE_PROJECTION_SUFFIX = "#A";
private static final int PROFILE_QUESTION_ROLE = 1001;
private static final List<String> PROFILE_ID_PREFIXES = List.of("PF", "PS");
private static final String AUDIT_EVENT = "Get Report Data";
private static final String CALCULATION_FAILED_CODE = "REPORT_CALCULATION_FAILED";
private static final String CALCULATION_FAILED_MESSAGE = "문항 집계 처리에 실패했습니다.";
private final StatisticsRepository statisticsRepository;
private final QuestionRepository questionRepository;
private final QuestionService questionService;
private final StatisticsDataService statisticsDataService;
private final StatisticsDimensionService statisticsDimensionService;
private final StatisticsFilterService statisticsFilterService;
private final StatisticsAggregationService statisticsAggregationService;
private final StatisticsTextService statisticsTextService;
private final LogService logService;
private final ObjectMapper objectMapper;
@FunctionalInterface
public interface StatisticsEventConsumer {
void accept(StatisticsStreamEventDto event) throws IOException;
}
public StatisticsResponseDto result(
ProjectRefDto project,
StatisticsRequestDto request,
RequestUserContext ctx) {
StatisticsBatchDto statisticsBatch = prepare(project, List.of(request), ctx);
return calculate(statisticsBatch, request);
}
/**
* Validates every index, then loads questions and responses once for the whole batch. Every
* general statistics batch is audited once as "Get Report Data".
*/
public StatisticsBatchDto prepare(
ProjectRefDto project,
List<StatisticsRequestDto> requests,
RequestUserContext ctx) {
StatisticsBatchDto statisticsBatch = prepare(project, requests, ctx, false);
logService.writeSiteLog(project, AUDIT_EVENT, objectMapper.valueToTree(requests), ctx);
return statisticsBatch;
}
/**
* Quota tables may use an empty STANDARD index. QuotaService audits them as "QUOTA VIEW", so no
* "Get Report Data" event is written here.
*/
public StatisticsBatchDto prepareQuota(
ProjectRefDto project, StatisticsRequestDto request, RequestUserContext ctx) {
return prepare(project, List.of(request), ctx, true);
}
public void stream(
StatisticsBatchDto statisticsBatch,
BooleanSupplier cancelled,
StatisticsEventConsumer eventConsumer)
throws IOException {
List<StatisticsRequestDto> requests = statisticsBatch.requests();
int succeeded = 0;
int failed = 0;
for (int position = 0; position < requests.size(); position++) {
if (isCancelled(cancelled)) {
return;
}
StatisticsRequestDto request = requests.get(position);
StatisticsStreamEventDto event;
try {
event = resultEvent(position, request, calculate(statisticsBatch, request));
succeeded++;
} catch (BusinessException error) {
int status = error.getStatus().value();
event = errorEvent(position, request, status, error.getCode(), clientMessage(error));
failed++;
} catch (RuntimeException error) {
event =
errorEvent(
position, request, 500, CALCULATION_FAILED_CODE, CALCULATION_FAILED_MESSAGE);
failed++;
}
// A calculation can take a while; never write to a stream cancelled in the meantime.
if (isCancelled(cancelled)) {
return;
}
eventConsumer.accept(event);
}
if (!isCancelled(cancelled)) {
eventConsumer.accept(completeEvent(requests.size(), succeeded, failed));
}
}
private StatisticsBatchDto prepare(
ProjectRefDto project,
List<StatisticsRequestDto> requests,
RequestUserContext ctx,
boolean quotaRequest) {
requests.forEach(request -> validate(request, quotaRequest));
// Banners, filter and date options are shared, so the first request carries them for all.
StatisticsRequestDto sharedRequest = requests.get(0);
List<QuestionDto> questions = loadQuestions(project, ctx);
validateDimensions(requests, sharedRequest, questions);
Predicate<SurveyResponseDto> filter =
statisticsFilterService.compile(sharedRequest.getFilter(), questions);
List<SurveyResponseDto> responses =
loadResponses(project, requests, sharedRequest, questions, filter);
return new StatisticsBatchDto(project, requests, questions, responses);
}
private void validate(StatisticsRequestDto request, boolean quotaRequest) {
boolean standard = isStandard(request);
boolean hasIndex = StringUtil.hasText(request.getIndex());
if (!standard && hasIndex) {
throw BusinessException.badRequest("TABULATION 모드는 index를 빈 문자열로 입력해 주세요.");
}
if (standard && !hasIndex && !quotaRequest) {
throw BusinessException.badRequest("집계할 문항을 입력해 주세요.");
}
if (request.getEndDate() != null
&& (request.getStartDate() == null
|| request.getStartDate().isAfter(request.getEndDate()))) {
throw BusinessException.badRequest("집계 날짜 범위를 확인해 주세요.");
}
}
private List<QuestionDto> loadQuestions(ProjectRefDto project, RequestUserContext ctx) {
boolean includeProfileQuestions =
project.getProjectType() == ProjectType.KIWI
&& Integer.valueOf(PROFILE_QUESTION_ROLE).equals(ctx.getUserRole());
return questionService.organize(questionRepository.list(project, includeProfileQuestions));
}
/** Resolving every index and banner rejects unknown questions before any data is loaded. */
private void validateDimensions(
List<StatisticsRequestDto> requests,
StatisticsRequestDto sharedRequest,
List<QuestionDto> questions) {
for (StatisticsRequestDto request : requests) {
statisticsDimensionService.resolve(request.getIndex(), questions, List.of());
}
for (List<String> bannerGroup : sharedRequest.getBanners()) {
for (String bannerId : bannerGroup) {
statisticsDimensionService.resolve(bannerId, questions, List.of());
}
}
}
private List<SurveyResponseDto> loadResponses(
ProjectRefDto project,
List<StatisticsRequestDto> requests,
StatisticsRequestDto sharedRequest,
List<QuestionDto> questions,
Predicate<SurveyResponseDto> filter) {
boolean includeProfileResponses =
needsProfileResponses(project, requests, sharedRequest, questions);
// Query the union of all storage roots once; calculate() narrows it again for each index.
List<String> storageRoots =
requests.stream().flatMap(request -> storageRoots(request).stream()).distinct().toList();
List<StatisticsDataDto> data =
statisticsRepository.data(project, sharedRequest, includeProfileResponses, storageRoots);
return statisticsDataService.normalize(data).stream().filter(filter).toList();
}
/** KIWI profile answers live in a separate table and are joined only when referenced. */
private boolean needsProfileResponses(
ProjectRefDto project,
List<StatisticsRequestDto> requests,
StatisticsRequestDto sharedRequest,
List<QuestionDto> questions) {
if (project.getProjectType() != ProjectType.KIWI) {
return false;
}
return requests.stream().anyMatch(request -> isProfileId(request.getIndex()))
|| sharedRequest.getBanners().stream().flatMap(List::stream).anyMatch(this::isProfileId)
|| filterUsesProfile(sharedRequest.getFilter(), questions);
}
private boolean filterUsesProfile(StatisticsFilterDto filter, List<QuestionDto> questions) {
if (filter == null) {
return false;
}
if (filter.getDimensionId() != null
&& isProfileId(statisticsFilterService.resolveQid(filter, questions))) {
return true;
}
return filter.getChildren().stream().anyMatch(child -> filterUsesProfile(child, questions));
}
private boolean isProfileId(String id) {
String normalized = StringUtil.normalizeBlank(id);
if (normalized == null) {
return false;
}
String upperCaseId = normalized.toUpperCase(Locale.ROOT);
return PROFILE_ID_PREFIXES.stream().anyMatch(upperCaseId::startsWith);
}
private StatisticsResponseDto calculate(
StatisticsBatchDto statisticsBatch, StatisticsRequestDto request) {
List<SurveyResponseDto> responses =
withinStorageRoots(statisticsBatch.responses(), storageRoots(request));
StatisticsDimension index =
statisticsDimensionService.resolve(
request.getIndex(), statisticsBatch.questions(), responses);
if (isOpenText(index)) {
return textAnalysis(statisticsBatch.project(), index, request, responses);
}
return statisticsAggregationService.aggregate(
request, statisticsBatch.questions(), responses);
}
/** The shared union query must not add respondents that a single-index query would omit. */
private List<SurveyResponseDto> withinStorageRoots(
List<SurveyResponseDto> responses, List<String> storageRoots) {
Predicate<String> storedUnderRoots =
key -> storageRoots.contains(key) || storageRoots.contains(key.split("_")[0]);
return responses.stream()
.filter(response -> response.values().keySet().stream().anyMatch(storedUnderRoots))
.toList();
}
/** Storage root keys selected by the original single-index query: index plus banners. */
private List<String> storageRoots(StatisticsRequestDto request) {
List<String> ids = new ArrayList<>();
ids.add(isStandard(request) ? request.getIndex() : TABULATION_STORAGE_ROOT);
request.getBanners().forEach(ids::addAll);
return ids.stream().map(this::toStorageRoot).distinct().toList();
}
/** e.g. "q1_2#a" -> "Q1": upper-case, drop the attribute projection and keep the root. */
private String toStorageRoot(String id) {
return Objects.toString(StringUtil.normalizeBlank(id), "")
.toUpperCase(Locale.ROOT)
.replace(ATTRIBUTE_PROJECTION_SUFFIX, "")
.split("_")[0];
}
private boolean isOpenText(StatisticsDimension index) {
return "text".equals(index.getQuestion().getQtype()) && !index.isAttributeProjection();
}
private StatisticsResponseDto textAnalysis(
ProjectRefDto project,
StatisticsDimension index,
StatisticsRequestDto request,
List<SurveyResponseDto> responses) {
return StatisticsResponseDto.builder()
.type("TEXT_ANALYSIS")
.questionId(index.getDimensionId())
.title(index.getQuestion().getTitle())
.columns(List.of())
.headerRows(List.of())
.rows(List.of())
.words(statisticsTextService.analyze(project, index.getDimensionId(), responses))
.appliedOptions(request)
.build();
}
private boolean isStandard(StatisticsRequestDto request) {
return STANDARD_MODE.equals(request.getMode());
}
private boolean isCancelled(BooleanSupplier cancelled) {
return cancelled.getAsBoolean() || Thread.currentThread().isInterrupted();
}
/** Server-side failures must not expose internal details to the client. */
private String clientMessage(BusinessException error) {
return error.getStatus().is5xxServerError() ? CALCULATION_FAILED_MESSAGE : error.getMessage();
}
private StatisticsStreamEventDto resultEvent(
int position, StatisticsRequestDto request, StatisticsResponseDto result) {
return StatisticsStreamEventDto.builder()
.type("result")
.position(position)
.index(request.getIndex())
.result(result)
.build();
}
private StatisticsStreamEventDto errorEvent(
int position, StatisticsRequestDto request, int status, String code, String message) {
return StatisticsStreamEventDto.builder()
.type("error")
.position(position)
.index(request.getIndex())
.status(status)
.code(code)
.message(message)
.build();
}
private StatisticsStreamEventDto completeEvent(int total, int succeeded, int failed) {
return StatisticsStreamEventDto.builder()
.type("complete")
.total(total)
.succeeded(succeeded)
.failed(failed)
.build();
}
}