261104_03_HOT: 트랜젝션 누수 관련 긴급 조치 2 #9
@@ -14,6 +14,9 @@ import io.swagger.v3.oas.annotations.media.Content;
|
|||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
@@ -93,13 +96,28 @@ public class ExportController {
|
|||||||
@PostMapping(
|
@PostMapping(
|
||||||
value = "/project/{projectType}/{pkey}/export/data",
|
value = "/project/{projectType}/{pkey}/export/data",
|
||||||
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
||||||
public ResponseEntity<byte[]> data(
|
public void data(
|
||||||
@PathVariable ProjectType projectType,
|
@PathVariable ProjectType projectType,
|
||||||
@PathVariable String pkey,
|
@PathVariable String pkey,
|
||||||
@Valid @RequestBody RawDataExportRequestDto request,
|
@Valid @RequestBody RawDataExportRequestDto request,
|
||||||
@Parameter(hidden = true) RequestUserContext ctx) {
|
@Parameter(hidden = true) RequestUserContext ctx, HttpServletResponse response) throws IOException {
|
||||||
return download(
|
ProjectRefDto project = new ProjectRefDto(projectType, pkey);
|
||||||
exportService.data(new ProjectRefDto(projectType, pkey), ctx.getUserRole(), request));
|
try (var file = exportService.prepareData(project, ctx.getUserRole(), request)) {
|
||||||
|
String fileName = FileUtil.extractFileNameFromPath(file.fileName())
|
||||||
|
.replaceAll("[\\p{Cntrl}\\\\/:*?\"<>|]", "_");
|
||||||
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||||
|
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
|
ContentDisposition.attachment().filename(fileName, StandardCharsets.UTF_8).build().toString());
|
||||||
|
response.setHeader(HttpHeaders.CACHE_CONTROL, "no-store");
|
||||||
|
response.setContentLengthLong(Files.size(file.path()));
|
||||||
|
Files.copy(file.path(), response.getOutputStream());
|
||||||
|
} catch (IOException | RuntimeException error) {
|
||||||
|
// Before any file bytes are committed, preserve the normal JSON error response.
|
||||||
|
if (!response.isCommitted()) {
|
||||||
|
response.reset();
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseEntity<byte[]> download(ExportFileDto file) {
|
private ResponseEntity<byte[]> download(ExportFileDto file) {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.kri.kart.server.dto.export;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
/** Completed download file owned by the caller until close. */
|
||||||
|
public record ExportTempFileDto(String fileName, Path path) implements AutoCloseable {
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,8 @@ public interface DerivedMapper {
|
|||||||
|
|
||||||
String lockProject(@Param("pkey") String pkey);
|
String lockProject(@Param("pkey") String pkey);
|
||||||
|
|
||||||
|
boolean projectExists(@Param("pkey") String pkey);
|
||||||
|
|
||||||
List<DerivedDefinitionDto> definitions(@Param("pkey") String pkey);
|
List<DerivedDefinitionDto> definitions(@Param("pkey") String pkey);
|
||||||
|
|
||||||
DerivedDefinitionDto find(@Param("pkey") String pkey, @Param("qid") String qid);
|
DerivedDefinitionDto find(@Param("pkey") String pkey, @Param("qid") String qid);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import com.kri.kart.server.dto.export.ExportTitleDto;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.session.ResultHandler;
|
||||||
|
|
||||||
public interface ExportMapper {
|
public interface ExportMapper {
|
||||||
String additionalColumns(@Param("pkey") String pkey);
|
String additionalColumns(@Param("pkey") String pkey);
|
||||||
@@ -19,10 +20,11 @@ public interface ExportMapper {
|
|||||||
|
|
||||||
String surveyType(@Param("pkey") String pkey);
|
String surveyType(@Param("pkey") String pkey);
|
||||||
|
|
||||||
List<ExportDataDto> data(
|
void data(
|
||||||
@Param("pkey") String pkey,
|
@Param("pkey") String pkey,
|
||||||
@Param("type") String type,
|
@Param("type") String type,
|
||||||
@Param("startDate") LocalDate startDate,
|
@Param("startDate") LocalDate startDate,
|
||||||
@Param("endDate") LocalDate endDate,
|
@Param("endDate") LocalDate endDate,
|
||||||
@Param("includeInterviewer") Boolean includeInterviewer);
|
@Param("includeInterviewer") Boolean includeInterviewer,
|
||||||
|
ResultHandler<ExportDataDto> handler);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ public class DerivedRepository {
|
|||||||
.lockProject(project.pkey());
|
.lockProject(project.pkey());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean projectExists(ProjectRefDto project) {
|
||||||
|
return surveySessionProvider.mapper(project, DerivedMapper.class).projectExists(project.pkey());
|
||||||
|
}
|
||||||
|
|
||||||
public List<DerivedDefinitionDto> definitions(ProjectRefDto project) {
|
public List<DerivedDefinitionDto> definitions(ProjectRefDto project) {
|
||||||
return surveySessionProvider.mapper(project, DerivedMapper.class)
|
return surveySessionProvider.mapper(project, DerivedMapper.class)
|
||||||
.definitions(project.pkey());
|
.definitions(project.pkey());
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import com.kri.kart.server.dto.project.ProjectRefDto;
|
|||||||
import com.kri.kart.server.persistence.SurveySessionProvider;
|
import com.kri.kart.server.persistence.SurveySessionProvider;
|
||||||
import com.kri.kart.server.persistence.mapper.ExportMapper;
|
import com.kri.kart.server.persistence.mapper.ExportMapper;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
@@ -42,14 +43,14 @@ public class ExportRepository {
|
|||||||
return surveySessionProvider.mapper(project, ExportMapper.class).surveyType(project.pkey());
|
return surveySessionProvider.mapper(project, ExportMapper.class).surveyType(project.pkey());
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ExportDataDto> data(
|
public void data(
|
||||||
ProjectRefDto project, RawDataExportRequestDto request, boolean includeInterviewer) {
|
ProjectRefDto project, RawDataExportRequestDto request, boolean includeInterviewer, Consumer<ExportDataDto> consumer) {
|
||||||
return surveySessionProvider.mapper(project, ExportMapper.class)
|
surveySessionProvider.mapper(project, ExportMapper.class)
|
||||||
.data(
|
.data(
|
||||||
project.pkey(),
|
project.pkey(),
|
||||||
request.getType(),
|
request.getType(),
|
||||||
request.getStartDate(),
|
request.getStartDate(),
|
||||||
request.getEndDate() == null ? request.getStartDate() : request.getEndDate(),
|
request.getEndDate() == null ? request.getStartDate() : request.getEndDate(),
|
||||||
includeInterviewer);
|
includeInterviewer, context -> consumer.accept(context.getResultObject()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,15 @@ import com.kri.common.feature.web.BusinessException;
|
|||||||
import com.kri.common.feature.web.RequestUserContext;
|
import com.kri.common.feature.web.RequestUserContext;
|
||||||
import com.kri.kart.server.common.enums.ProjectType;
|
import com.kri.kart.server.common.enums.ProjectType;
|
||||||
import com.kri.kart.server.dto.derived.DerivedCalculationDto;
|
import com.kri.kart.server.dto.derived.DerivedCalculationDto;
|
||||||
|
import com.kri.kart.server.dto.derived.DerivedDataDto;
|
||||||
import com.kri.kart.server.dto.derived.DerivedDefinitionDto;
|
import com.kri.kart.server.dto.derived.DerivedDefinitionDto;
|
||||||
import com.kri.kart.server.dto.derived.DerivedSaveRequestDto;
|
import com.kri.kart.server.dto.derived.DerivedSaveRequestDto;
|
||||||
import com.kri.kart.server.dto.project.ProjectRefDto;
|
import com.kri.kart.server.dto.project.ProjectRefDto;
|
||||||
|
import com.kri.kart.server.dto.question.QuestionDto;
|
||||||
import com.kri.kart.server.persistence.repository.DerivedRepository;
|
import com.kri.kart.server.persistence.repository.DerivedRepository;
|
||||||
import com.kri.kart.server.service.derived.DerivedComputationService;
|
import com.kri.kart.server.service.derived.DerivedComputationService;
|
||||||
import com.kri.kart.server.service.derived.DerivedRecalculationGuard;
|
import com.kri.kart.server.service.derived.DerivedRecalculationGuard;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
@@ -156,22 +159,61 @@ public class DerivedManagementService {
|
|||||||
if (idkey == null || idkey.isBlank() || idkey.length() > 512) {
|
if (idkey == null || idkey.isBlank() || idkey.length() > 512) {
|
||||||
throw BusinessException.badRequest("응답자 ID를 확인해 주세요.");
|
throw BusinessException.badRequest("응답자 ID를 확인해 주세요.");
|
||||||
}
|
}
|
||||||
return derivedRecalculationGuard.execute(project, () -> transaction(
|
return derivedRecalculationGuard.execute(project, idkey, () -> {
|
||||||
project,
|
if (!derivedRepository.projectExists(project)) {
|
||||||
() -> {
|
throw BusinessException.notFound("PROJECT_NOT_FOUND", "프로젝트를 찾을 수 없습니다.");
|
||||||
requireProject(project);
|
}
|
||||||
// Legacy creates an empty DERIVED_DATA row even if there is no eligible
|
List<DerivedDefinitionDto> definitions = derivedRepository.definitions(project);
|
||||||
// response.
|
List<String> derivedIds = derivedIds(definitions);
|
||||||
|
List<QuestionDto> questions = definitions.isEmpty() ? List.of() : questionService.list(project, 1001);
|
||||||
|
List<DerivedDataDto> data = definitions.isEmpty() ? List.of()
|
||||||
|
: derivedRepository.respondentResponses(project, idkey);
|
||||||
|
if (data.size() > 1) {
|
||||||
|
// Legacy joins can return multiple rows for one ID. Preserve their database reread
|
||||||
|
// semantics rather than assuming uniqueness or collapsing results in memory.
|
||||||
|
return transaction(project, () -> {
|
||||||
derivedRepository.ensureRespondent(project, idkey);
|
derivedRepository.ensureRespondent(project, idkey);
|
||||||
var definitions = derivedRepository.definitions(project);
|
|
||||||
var derivedIds = derivedIds(definitions);
|
|
||||||
int matched = 0;
|
int matched = 0;
|
||||||
for (var definition : definitions) {
|
for (DerivedDefinitionDto definition : definitions) {
|
||||||
matched += calculate(project, definition, idkey, derivedIds);
|
matched += calculate(project, definition, idkey, derivedIds, questions);
|
||||||
}
|
}
|
||||||
return new DerivedCalculationDto(definitions.size(), matched);
|
return new DerivedCalculationDto(definitions.size(), matched);
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
definitions.forEach(definition -> validateId(definition.getQid()));
|
||||||
|
var results = derivedComputationService.calculateRespondent(definitions, questions, data, derivedIds);
|
||||||
|
List<RespondentWrite> writes = new ArrayList<>();
|
||||||
|
int matched = 0;
|
||||||
|
for (int variable = 0; variable < definitions.size(); variable++) {
|
||||||
|
List<String> groups = new ArrayList<>();
|
||||||
|
for (var group : results.get(variable)) {
|
||||||
|
groups.add(group.isEmpty() ? null : JsonUtil.toJson(objectMapper, group));
|
||||||
|
if (!group.isEmpty()) {
|
||||||
|
matched++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writes.add(new RespondentWrite(definitions.get(variable).getQid(), groups));
|
||||||
|
}
|
||||||
|
int matchedRespondents = matched;
|
||||||
|
return transaction(project, () -> {
|
||||||
|
// Keep the original empty-row contract even when there are no definitions or responses.
|
||||||
|
derivedRepository.ensureRespondent(project, idkey);
|
||||||
|
for (RespondentWrite write : writes) {
|
||||||
|
if (write.groups().isEmpty()) {
|
||||||
|
derivedRepository.clearRespondentData(project, write.qid(), idkey);
|
||||||
|
} else {
|
||||||
|
for (String group : write.groups()) {
|
||||||
|
// JSON_MODIFY updates only this variable on the current row; never replace DATJSON.
|
||||||
|
derivedRepository.saveData(project, write.qid(), idkey, group);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new DerivedCalculationDto(definitions.size(), matchedRespondents);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private record RespondentWrite(String qid, List<String> groups) {}
|
||||||
|
|
||||||
/** Stored derived variable IDs; uploaded response columns with these IDs are not imported. */
|
/** Stored derived variable IDs; uploaded response columns with these IDs are not imported. */
|
||||||
public List<String> derivedIds(ProjectRefDto project) {
|
public List<String> derivedIds(ProjectRefDto project) {
|
||||||
@@ -205,6 +247,12 @@ public class DerivedManagementService {
|
|||||||
DerivedDefinitionDto definition,
|
DerivedDefinitionDto definition,
|
||||||
String idkey,
|
String idkey,
|
||||||
List<String> derivedIds) {
|
List<String> derivedIds) {
|
||||||
|
return calculate(project, definition, idkey, derivedIds, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int calculate(
|
||||||
|
ProjectRefDto project, DerivedDefinitionDto definition, String idkey,
|
||||||
|
List<String> derivedIds, List<QuestionDto> questions) {
|
||||||
validateId(definition.getQid());
|
validateId(definition.getQid());
|
||||||
if (idkey == null) {
|
if (idkey == null) {
|
||||||
derivedRepository.clearData(project, definition.getQid());
|
derivedRepository.clearData(project, definition.getQid());
|
||||||
@@ -217,7 +265,8 @@ public class DerivedManagementService {
|
|||||||
: derivedRepository.respondentResponses(project, idkey);
|
: derivedRepository.respondentResponses(project, idkey);
|
||||||
var results =
|
var results =
|
||||||
derivedComputationService.calculate(
|
derivedComputationService.calculate(
|
||||||
definition, questionService.list(project, idkey == null ? 0 : 1001), data, derivedIds);
|
definition, questions == null ? questionService.list(project, idkey == null ? 0 : 1001) : questions,
|
||||||
|
data, derivedIds);
|
||||||
int matched = 0;
|
int matched = 0;
|
||||||
for (int i = 0; i < data.size(); i++) {
|
for (int i = 0; i < data.size(); i++) {
|
||||||
var group = results.get(i);
|
var group = results.get(i);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import com.kri.kart.server.common.consts.KartConstant;
|
|||||||
import com.kri.kart.server.dto.export.ExportDataDto;
|
import com.kri.kart.server.dto.export.ExportDataDto;
|
||||||
import com.kri.kart.server.dto.export.ExportDerivedHeaderDto;
|
import com.kri.kart.server.dto.export.ExportDerivedHeaderDto;
|
||||||
import com.kri.kart.server.dto.export.ExportFileDto;
|
import com.kri.kart.server.dto.export.ExportFileDto;
|
||||||
|
import com.kri.kart.server.dto.export.ExportTempFileDto;
|
||||||
import com.kri.kart.server.dto.export.ExportGuideDto;
|
import com.kri.kart.server.dto.export.ExportGuideDto;
|
||||||
import com.kri.kart.server.dto.export.ExportHeaderDto;
|
import com.kri.kart.server.dto.export.ExportHeaderDto;
|
||||||
import com.kri.kart.server.dto.export.ExportTitleDto;
|
import com.kri.kart.server.dto.export.ExportTitleDto;
|
||||||
@@ -20,6 +21,7 @@ import com.kri.kart.server.persistence.repository.ExportRepository;
|
|||||||
import com.kri.kart.server.persistence.repository.QuestionRepository;
|
import com.kri.kart.server.persistence.repository.QuestionRepository;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
@@ -128,8 +130,26 @@ public class ExportService {
|
|||||||
return rows;
|
return rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExportFileDto data(
|
public ExportTempFileDto prepareData(
|
||||||
ProjectRefDto project, Integer userRole, RawDataExportRequestDto request) {
|
ProjectRefDto project, Integer userRole, RawDataExportRequestDto request) throws IOException {
|
||||||
|
Path path = Files.createTempFile("kart-data-export-", ".xlsx");
|
||||||
|
try {
|
||||||
|
try (OutputStream output = Files.newOutputStream(path)) {
|
||||||
|
data(project, userRole, request, output);
|
||||||
|
}
|
||||||
|
return new ExportTempFileDto(dataFileName(project), path);
|
||||||
|
} catch (IOException | RuntimeException | Error error) {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
} catch (IOException cleanupError) {
|
||||||
|
error.addSuppressed(cleanupError);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void data(
|
||||||
|
ProjectRefDto project, Integer userRole, RawDataExportRequestDto request, OutputStream output) throws IOException {
|
||||||
if (!List.of("COMPLETE", "INCOMPLETE").contains(Objects.toString(request.getType(), ""))
|
if (!List.of("COMPLETE", "INCOMPLETE").contains(Objects.toString(request.getType(), ""))
|
||||||
|| (request.getStartDate() == null && request.getEndDate() != null)
|
|| (request.getStartDate() == null && request.getEndDate() != null)
|
||||||
|| (request.getStartDate() != null
|
|| (request.getStartDate() != null
|
||||||
@@ -138,36 +158,48 @@ public class ExportService {
|
|||||||
throw BusinessException.badRequest("다운로드 유형과 날짜 범위를 확인해 주세요.");
|
throw BusinessException.badRequest("다운로드 유형과 날짜 범위를 확인해 주세요.");
|
||||||
}
|
}
|
||||||
Map<String, String> headers = headers(project, userRole);
|
Map<String, String> headers = headers(project, userRole);
|
||||||
List<Map<String, String>> rows = new ArrayList<>();
|
|
||||||
rows.add(headers); // Row 1 contains storage keys, row 2 contains variable aliases.
|
|
||||||
boolean includeInterviewer = "2".equals(exportRepository.surveyType(project));
|
boolean includeInterviewer = "2".equals(exportRepository.surveyType(project));
|
||||||
for (ExportDataDto response : exportRepository.data(project, request, includeInterviewer)) {
|
try (var workbook = ExcelMapper.createStreamingWorkbook(100)) {
|
||||||
|
try (var writer = ExcelMapper.openStreamingMapWriter(workbook, new ArrayList<>(headers.keySet()), "DATA", 1)) {
|
||||||
|
writer.append(headers);
|
||||||
|
exportRepository.data(project, request, includeInterviewer, response -> {
|
||||||
String dat = response.getDat();
|
String dat = response.getDat();
|
||||||
if (dat == null || dat.isEmpty()) {
|
if (dat == null || dat.isEmpty()) {
|
||||||
continue; // SQL STUFF returns NULL when all response groups are empty.
|
return;
|
||||||
}
|
}
|
||||||
// Preserve legacy ReplaceUnicodeEmpty after SQL UPPER/FOR XML serialization.
|
|
||||||
JsonNode values = jsonObject(dat.replaceAll("\\\\U\\d*", ""));
|
JsonNode values = jsonObject(dat.replaceAll("\\\\U\\d*", ""));
|
||||||
Map<String, String> row = new LinkedHashMap<>();
|
Map<String, String> row = new LinkedHashMap<>();
|
||||||
for (String key : headers.keySet()) {
|
for (String key : headers.keySet()) {
|
||||||
row.put(key, responseValue(response, values, key, includeInterviewer));
|
String value = responseValue(response, values, key, includeInterviewer);
|
||||||
|
if (value != null && !value.isEmpty()) {
|
||||||
|
row.put(key, value);
|
||||||
}
|
}
|
||||||
rows.add(row);
|
|
||||||
}
|
}
|
||||||
Workbook workbook = ExcelMapper.writeFromMapList(rows, "DATA");
|
writer.append(row);
|
||||||
try {
|
});
|
||||||
ExcelMapper.appendSheetToWorkbook(
|
|
||||||
workbook, guideRows(project), ExportGuideDto.class, "GUIDE", 1);
|
|
||||||
return file("DATA", project, workbook);
|
|
||||||
} catch (RuntimeException exception) {
|
|
||||||
try {
|
|
||||||
workbook.close();
|
|
||||||
} catch (IOException closeException) {
|
|
||||||
exception.addSuppressed(closeException);
|
|
||||||
}
|
}
|
||||||
throw exception;
|
// Metadata is bounded by the questionnaire; respondent rows are never accumulated.
|
||||||
|
try (var writer = ExcelMapper.openStreamingMapWriter(workbook,
|
||||||
|
List.of("문항 타입", "문항 / 보기번호", "내용", "VALUE LABEL", "VARIABLE LABEL"), "GUIDE", 1)) {
|
||||||
|
for (ExportGuideDto guide : guideRows(project)) {
|
||||||
|
Map<String, String> row = new LinkedHashMap<>();
|
||||||
|
row.put("문항 타입", guide.questionType());
|
||||||
|
row.put("문항 / 보기번호", guide.variable());
|
||||||
|
row.put("내용", guide.content());
|
||||||
|
row.put("VALUE LABEL", guide.valueLabel());
|
||||||
|
row.put("VARIABLE LABEL", guide.variableLabel());
|
||||||
|
row.values().removeIf(value -> value == null || value.isEmpty());
|
||||||
|
writer.append(row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
workbook.write(output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String dataFileName(ProjectRefDto project) {
|
||||||
|
return "[DATA]" + project.pkey() + "_"
|
||||||
|
+ DateTimeUtil.nowKstLocalDateTime().format(FILE_TIMESTAMP) + ".xlsx";
|
||||||
|
}
|
||||||
|
|
||||||
private String responseValue(
|
private String responseValue(
|
||||||
ExportDataDto response, JsonNode values, String key, boolean includeInterviewer) {
|
ExportDataDto response, JsonNode values, String key, boolean includeInterviewer) {
|
||||||
|
|||||||
@@ -134,6 +134,37 @@ public class DerivedComputationService {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Reuse one respondent snapshot, feeding preceding variable results into later conditions. */
|
||||||
|
public List<List<ObjectNode>> calculateRespondent(
|
||||||
|
List<DerivedDefinitionDto> definitions, List<QuestionDto> questions,
|
||||||
|
List<DerivedDataDto> data, Collection<String> derivedIds) {
|
||||||
|
List<DerivedDataDto> working = new ArrayList<>();
|
||||||
|
for (DerivedDataDto row : data) {
|
||||||
|
DerivedDataDto copy = new DerivedDataDto();
|
||||||
|
copy.setIdkey(row.getIdkey());
|
||||||
|
copy.setDatjson(row.getDatjson());
|
||||||
|
copy.setDerivedJson(row.getDerivedJson());
|
||||||
|
working.add(copy);
|
||||||
|
}
|
||||||
|
List<List<ObjectNode>> prepared = new ArrayList<>();
|
||||||
|
for (DerivedDefinitionDto definition : definitions) {
|
||||||
|
List<ObjectNode> groups = calculate(definition, questions, working, derivedIds);
|
||||||
|
prepared.add(groups);
|
||||||
|
for (int row = 0; row < working.size(); row++) {
|
||||||
|
DerivedDataDto response = working.get(row);
|
||||||
|
ObjectNode derived = response.getDerivedJson() == null
|
||||||
|
? objectMapper.createObjectNode() : object(response.getDerivedJson());
|
||||||
|
if (groups.get(row).isEmpty()) {
|
||||||
|
derived.remove(definition.getQid());
|
||||||
|
} else {
|
||||||
|
derived.set(definition.getQid(), groups.get(row));
|
||||||
|
}
|
||||||
|
response.setDerivedJson(JsonUtil.toJson(objectMapper, derived));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prepared;
|
||||||
|
}
|
||||||
|
|
||||||
private void flatten(ObjectNode grouped, Map<String, String> flat) {
|
private void flatten(ObjectNode grouped, Map<String, String> flat) {
|
||||||
grouped
|
grouped
|
||||||
.fields()
|
.fields()
|
||||||
|
|||||||
@@ -1,56 +1,41 @@
|
|||||||
package com.kri.kart.server.service.derived;
|
package com.kri.kart.server.service.derived;
|
||||||
|
|
||||||
import com.kri.common.feature.web.BusinessException;
|
import com.kri.common.feature.web.BusinessException;
|
||||||
import com.kri.kart.server.common.enums.ProjectType;
|
|
||||||
import com.kri.kart.server.config.KartDatabaseProperties;
|
|
||||||
import com.kri.kart.server.dto.project.ProjectRefDto;
|
import com.kri.kart.server.dto.project.ProjectRefDto;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/** Wait before borrowing JDBC connections; database locks still enforce cross-instance safety. */
|
/** Serialize duplicate respondent recalculations before reading their response snapshots. */
|
||||||
@Component
|
@Component
|
||||||
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
|
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
|
||||||
public class DerivedRecalculationGuard {
|
public class DerivedRecalculationGuard {
|
||||||
private final ConcurrentHashMap<ProjectRefDto, ProjectGate> projects = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<RespondentKey, RespondentGate> respondents = new ConcurrentHashMap<>();
|
||||||
private final Semaphore kon;
|
|
||||||
private final Semaphore konkiwi;
|
|
||||||
private final long waitNanos;
|
private final long waitNanos;
|
||||||
|
|
||||||
@Autowired
|
public DerivedRecalculationGuard() {
|
||||||
public DerivedRecalculationGuard(KartDatabaseProperties properties) {
|
this(TimeUnit.SECONDS.toNanos(10));
|
||||||
this(properties, TimeUnit.SECONDS.toNanos(10));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DerivedRecalculationGuard(KartDatabaseProperties properties, long waitNanos) {
|
DerivedRecalculationGuard(long waitNanos) {
|
||||||
// Reserve one connection for access checks and other operations when pool size permits.
|
|
||||||
this.kon = new Semaphore(Math.max(1, properties.getKon().getMaximumPoolSize() - 1), true);
|
|
||||||
this.konkiwi = new Semaphore(Math.max(1, properties.getKonkiwi().getMaximumPoolSize() - 1), true);
|
|
||||||
this.waitNanos = waitNanos;
|
this.waitNanos = waitNanos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T execute(ProjectRefDto project, Supplier<T> action) {
|
public <T> T execute(ProjectRefDto project, String idkey, Supplier<T> action) {
|
||||||
long deadline = System.nanoTime() + waitNanos;
|
RespondentKey respondent = new RespondentKey(project, idkey);
|
||||||
ProjectGate gate = projects.compute(project, (key, current) -> {
|
RespondentGate gate = respondents.compute(respondent, (key, current) -> {
|
||||||
ProjectGate selected = current == null ? new ProjectGate() : current;
|
RespondentGate selected = current == null ? new RespondentGate() : current;
|
||||||
selected.users++;
|
selected.users++;
|
||||||
return selected;
|
return selected;
|
||||||
});
|
});
|
||||||
Semaphore source = project.projectType() == ProjectType.KON ? kon : konkiwi;
|
boolean acquired = false;
|
||||||
boolean projectAcquired = false;
|
|
||||||
boolean sourceAcquired = false;
|
|
||||||
try {
|
try {
|
||||||
projectAcquired = gate.permit.tryAcquire(remaining(deadline), TimeUnit.NANOSECONDS);
|
acquired = gate.permit.tryAcquire(waitNanos, TimeUnit.NANOSECONDS);
|
||||||
if (!projectAcquired) {
|
if (!acquired) {
|
||||||
throw busy();
|
|
||||||
}
|
|
||||||
sourceAcquired = source.tryAcquire(remaining(deadline), TimeUnit.NANOSECONDS);
|
|
||||||
if (!sourceAcquired) {
|
|
||||||
throw busy();
|
throw busy();
|
||||||
}
|
}
|
||||||
return action.get();
|
return action.get();
|
||||||
@@ -58,29 +43,24 @@ public class DerivedRecalculationGuard {
|
|||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
throw busy();
|
throw busy();
|
||||||
} finally {
|
} finally {
|
||||||
if (sourceAcquired) {
|
if (acquired) {
|
||||||
source.release();
|
|
||||||
}
|
|
||||||
if (projectAcquired) {
|
|
||||||
gate.permit.release();
|
gate.permit.release();
|
||||||
}
|
}
|
||||||
projects.computeIfPresent(project, (key, current) -> --current.users == 0 ? null : current);
|
respondents.computeIfPresent(respondent, (key, current) -> --current.users == 0 ? null : current);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long remaining(long deadline) {
|
|
||||||
return Math.max(0, deadline - System.nanoTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
private BusinessException busy() {
|
private BusinessException busy() {
|
||||||
return BusinessException.of(
|
return BusinessException.of(
|
||||||
HttpStatus.SERVICE_UNAVAILABLE, "DERIVED_RECALCULATION_BUSY",
|
HttpStatus.SERVICE_UNAVAILABLE, "DERIVED_RECALCULATION_BUSY",
|
||||||
"파생변수 재계산 요청이 많습니다. 잠시 후 다시 요청해 주세요.");
|
"파생변수 재계산 요청이 많습니다. 잠시 후 다시 요청해 주세요.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class ProjectGate {
|
private record RespondentKey(ProjectRefDto project, String idkey) {}
|
||||||
|
|
||||||
|
private static final class RespondentGate {
|
||||||
private final Semaphore permit = new Semaphore(1, true);
|
private final Semaphore permit = new Semaphore(1, true);
|
||||||
// Accessed only inside ConcurrentHashMap.compute/computeIfPresent for the project.
|
// Accessed only inside ConcurrentHashMap.compute/computeIfPresent for the respondent.
|
||||||
private int users;
|
private int users;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,9 @@
|
|||||||
<select id="lockProject" databaseId="survey" resultType="string">
|
<select id="lockProject" databaseId="survey" resultType="string">
|
||||||
SELECT PKEY FROM PROJECT WITH(UPDLOCK, HOLDLOCK) WHERE PKEY = #{pkey}
|
SELECT PKEY FROM PROJECT WITH(UPDLOCK, HOLDLOCK) WHERE PKEY = #{pkey}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="projectExists" databaseId="survey" resultType="boolean">
|
||||||
|
SELECT CASE WHEN EXISTS (SELECT 1 FROM PROJECT WHERE PKEY = #{pkey}) THEN 1 ELSE 0 END
|
||||||
|
</select>
|
||||||
<select id="definitions" databaseId="survey" resultType="com.kri.kart.server.dto.derived.DerivedDefinitionDto">
|
<select id="definitions" databaseId="survey" resultType="com.kri.kart.server.dto.derived.DerivedDefinitionDto">
|
||||||
SELECT QID, QUESTIONJSON AS question_json, DERIVEDJSON AS derived_json, Qorder AS q_order
|
SELECT QID, QUESTIONJSON AS question_json, DERIVEDJSON AS derived_json, Qorder AS q_order
|
||||||
FROM DERIVED_VARIABLE WHERE PKEY = #{pkey} ORDER BY Qorder
|
FROM DERIVED_VARIABLE WHERE PKEY = #{pkey} ORDER BY Qorder
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
<select id="surveyType" databaseId="survey" resultType="string">
|
<select id="surveyType" databaseId="survey" resultType="string">
|
||||||
SELECT JSON_VALUE(PROJECTJSON, '$.must.surveytype') FROM PROJECT WHERE PKEY = #{pkey}
|
SELECT JSON_VALUE(PROJECTJSON, '$.must.surveytype') FROM PROJECT WHERE PKEY = #{pkey}
|
||||||
</select>
|
</select>
|
||||||
<select id="data" databaseId="survey" resultType="com.kri.kart.server.dto.export.ExportDataDto">
|
<select id="data" databaseId="survey" resultSetType="FORWARD_ONLY" fetchSize="100" useCache="false" resultType="com.kri.kart.server.dto.export.ExportDataDto">
|
||||||
<![CDATA[
|
<![CDATA[
|
||||||
SELECT IA.Status, DAT.DAT, DAT.start_date, DAT.end_date, DAT.interval_min
|
SELECT IA.Status, DAT.DAT, DAT.start_date, DAT.end_date, DAT.interval_min
|
||||||
]]>
|
]]>
|
||||||
|
|||||||
Reference in New Issue
Block a user