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.consts.KartConstant; 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.StatisticsSourceService; import com.kri.kart.server.service.statistics.StatisticsTextService; import java.io.IOException; import java.util.List; 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 final StatisticsRepository statisticsRepository; private final QuestionRepository questionRepository; private final QuestionService questionService; private final StatisticsDataService statisticsDataService; private final StatisticsSourceService statisticsSourceService; 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 requests, RequestUserContext ctx) { StatisticsBatchDto statisticsBatch = prepare(project, requests, ctx, false); logService.writeSiteLog(project, KartConstant.REPORT_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 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, KartConstant.CALCULATION_FAILED_CODE, KartConstant.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 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 questions = loadQuestions(project, ctx); validateDimensions(requests, sharedRequest, questions); Predicate filter = statisticsFilterService.compile(sharedRequest.getFilter(), questions); List 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 loadQuestions(ProjectRefDto project, RequestUserContext ctx) { boolean includeProfileQuestions = project.projectType() == ProjectType.KIWI && Integer.valueOf(KartConstant.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 requests, StatisticsRequestDto sharedRequest, List questions) { for (StatisticsRequestDto request : requests) { statisticsDimensionService.resolve(request.getIndex(), questions, List.of()); } for (List bannerGroup : sharedRequest.getBanners()) { for (String bannerId : bannerGroup) { statisticsDimensionService.resolve(bannerId, questions, List.of()); } } } private List loadResponses( ProjectRefDto project, List requests, StatisticsRequestDto sharedRequest, List questions, Predicate filter) { boolean includeProfileResponses = needsProfileResponses(project, requests, sharedRequest, questions); // Query the union of all storage roots once; calculate() narrows it again for each index. List storageRoots = requests.stream().flatMap(request -> statisticsSourceService.storageRoots(request).stream()).distinct().toList(); List 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 requests, StatisticsRequestDto sharedRequest, List questions) { if (project.projectType() != ProjectType.KIWI) { return false; } return requests.stream().anyMatch(request -> statisticsSourceService.isProfileId(request.getIndex())) || sharedRequest.getBanners().stream().flatMap(List::stream).anyMatch(statisticsSourceService::isProfileId) || filterUsesProfile(sharedRequest.getFilter(), questions); } private boolean filterUsesProfile(StatisticsFilterDto filter, List questions) { if (filter == null) { return false; } if (filter.getDimensionId() != null && statisticsSourceService.isProfileId(statisticsFilterService.resolveQid(filter, questions))) { return true; } return filter.getChildren().stream().anyMatch(child -> filterUsesProfile(child, questions)); } private StatisticsResponseDto calculate( StatisticsBatchDto statisticsBatch, StatisticsRequestDto request) { List responses = statisticsSourceService.withinStorageRoots( statisticsBatch.responses(), statisticsSourceService.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); } private boolean isOpenText(StatisticsDimension index) { return "text".equals(index.getQuestionType()) && !index.isAttributeProjection(); } private StatisticsResponseDto textAnalysis( ProjectRefDto project, StatisticsDimension index, StatisticsRequestDto request, List responses) { return StatisticsResponseDto.builder() .type("TEXT_ANALYSIS") .questionId(index.getDimensionId()) .title(index.getTitle()) .columns(List.of()) .headerRows(List.of()) .rows(List.of()) .words( statisticsTextService.analyze( project, index.getDimensionId(), responses, request.getWordMergeMode(), request.getCompoundNounMaxTokens())) .appliedOptions(request) .build(); } private boolean isStandard(StatisticsRequestDto request) { return KartConstant.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() ? KartConstant.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(); } }