Skip to content

Commit

Permalink
feat: abtest 편입 관련 api 단일화
Browse files Browse the repository at this point in the history
  • Loading branch information
songsunkook committed Sep 20, 2024
1 parent 691cd22 commit ba0fa5e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,9 @@ ResponseEntity<Void> assignAbtestVariableByAdmin(
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true)))
}
)
@Operation(summary = "(NORMAL) 자신의 실험군 조회")
@GetMapping("/me")
ResponseEntity<String> getMyAbtestVariable(
@RequestHeader("accessHistoryId") Integer accessHistoryId,
@UserAgent UserAgentInfo userAgentInfo,
@UserId Integer userId,
@RequestParam(name = "title") String title
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "400", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true)))
}
)
@Operation(summary = "(NORMAL) 실험군 최초 편입")
@Operation(summary = "(NORMAL) 실험군 편입 정보 확인")
@PostMapping("/assign")
ResponseEntity<AbtestAssignResponse> assignAbtestVariable(
ResponseEntity<AbtestAssignResponse> assignOrGetAbtestVariable(
@RequestHeader("accessHistoryId") Integer accessHistoryId,
@UserAgent UserAgentInfo userAgentInfo,
@UserId Integer userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,14 @@ public ResponseEntity<Void> assignAbtestVariableByAdmin(
return ResponseEntity.ok().build();
}

@GetMapping("/me")
public ResponseEntity<String> getMyAbtestVariable(
@RequestHeader("access_history_id") Integer accessHistoryId,
@UserAgent UserAgentInfo userAgentInfo,
@UserId Integer userId,
@RequestParam(name = "title") String title
) {
String response = abtestService.getMyVariable(accessHistoryId, userAgentInfo, userId, title);
return ResponseEntity.ok(response);
}

@PostMapping("/assign")
public ResponseEntity<AbtestAssignResponse> assignAbtestVariable(
public ResponseEntity<AbtestAssignResponse> assignOrGetAbtestVariable(
@RequestHeader(value = "access_history_id", required = false) Integer accessHistoryId,
@UserAgent UserAgentInfo userAgentInfo,
@UserId Integer userId,
@RequestBody @Valid AbtestAssignRequest abtestAssignRequest
) {
AbtestAssignResponse response = abtestService.assignVariable(accessHistoryId, userAgentInfo, userId, abtestAssignRequest);
AbtestAssignResponse response = abtestService.assignOrGetVariable(accessHistoryId, userAgentInfo, userId, abtestAssignRequest);
return ResponseEntity.ok(response);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import in.koreatech.koin.admin.abtest.dto.response.AbtestUsersResponse;
import in.koreatech.koin.admin.abtest.dto.response.AbtestsResponse;
import in.koreatech.koin.admin.abtest.exception.AbtestAlreadyExistException;
import in.koreatech.koin.admin.abtest.exception.AbtestAssignedUserException;
import in.koreatech.koin.admin.abtest.exception.AbtestDuplicatedVariableException;
import in.koreatech.koin.admin.abtest.exception.AbtestNotAssignedUserException;
import in.koreatech.koin.admin.abtest.exception.AbtestNotInProgressException;
Expand All @@ -38,7 +37,6 @@
import in.koreatech.koin.admin.abtest.repository.AbtestVariableAssignTemplateRepository;
import in.koreatech.koin.admin.abtest.repository.AbtestVariableCountRepository;
import in.koreatech.koin.admin.abtest.repository.AbtestVariableRepository;
import in.koreatech.koin.admin.abtest.repository.AccessHistoryAbtestVariableCustomRepository;
import in.koreatech.koin.admin.abtest.repository.AccessHistoryRepository;
import in.koreatech.koin.admin.abtest.repository.DeviceRepository;
import in.koreatech.koin.domain.user.model.User;
Expand All @@ -63,7 +61,6 @@ public class AbtestService {
private final UserRepository userRepository;
private final AbtestVariableAssignRepository abtestVariableAssignRepository;
private final AbtestVariableAssignTemplateRepository abtestVariableAssignTemplateRepository;
private final AccessHistoryAbtestVariableCustomRepository accessHistoryAbtestVariableCustomRepository;

@Transactional
public AbtestResponse createAbtest(AbtestRequest request) {
Expand Down Expand Up @@ -99,7 +96,6 @@ public AbtestResponse putAbtest(Integer abtestId, AbtestRequest request) {
return AbtestResponse.from(abtest);
}


private void deleteVariableAssignCache(Abtest abtest) {
abtest.getAbtestVariables().forEach(abtestVariable ->
abtestVariableAssignTemplateRepository.deleteAllByVariableId(abtestVariable.getId()));
Expand Down Expand Up @@ -144,15 +140,19 @@ public void closeAbtest(Integer abtestId, AbtestCloseRequest request) {
}

@Transactional
public AbtestAssignResponse assignVariable(Integer accessHistoryId, UserAgentInfo userAgentInfo, Integer userId,
public AbtestAssignResponse assignOrGetVariable(Integer accessHistoryId, UserAgentInfo userAgentInfo,
Integer userId,
AbtestAssignRequest request) {
Abtest abtest = abtestRepository.getByTitle(request.title());
AccessHistory accessHistory = findOrCreateAccessHistory(accessHistoryId);
Optional<AbtestVariable> winnerResponse = returnWinnerIfClosed(abtest);
if (winnerResponse.isPresent()) {
return AbtestAssignResponse.of(winnerResponse.get(), accessHistory);
}
validateAssignedUser(abtest, accessHistory.getId(), userId);
if (isAssignedUser(abtest, accessHistory.getId(), userId)) {
return AbtestAssignResponse.of(
getMyVariable(accessHistory.getId(), userAgentInfo, userId, request.title()), accessHistory);
}
List<AbtestVariableCount> cacheCount = loadCacheCount(abtest);
AbtestVariable variable = abtest.findAssignVariable(cacheCount);
if (userId != null) {
Expand Down Expand Up @@ -193,14 +193,14 @@ private void variableAssignCacheSave(AbtestVariable variable, Integer accessHist
abtestVariableAssignRepository.save(AbtestVariableAssign.of(variable.getId(), accessHistoryId));
}

@Transactional
public String getMyVariable(Integer accessHistoryId, UserAgentInfo userAgentInfo, Integer userId, String title) {
private AbtestVariable getMyVariable(Integer accessHistoryId, UserAgentInfo userAgentInfo, Integer userId,
String title) {
Abtest abtest = abtestRepository.getByTitle(title);
AccessHistory accessHistory = accessHistoryRepository.getById(accessHistoryId);
syncCacheCountToDB(abtest);
Optional<AbtestVariable> winner = returnWinnerIfClosed(abtest);
if (winner.isPresent()) {
return winner.get().getName();
return winner.get();
}
if (userId != null) {
createDeviceIfNotExists(userId, userAgentInfo, accessHistory, abtest);
Expand All @@ -215,23 +215,20 @@ public String getMyVariable(Integer accessHistoryId, UserAgentInfo userAgentInfo
.orElseThrow(() -> AbtestNotAssignedUserException.withDetail("abtestId: " + abtest.getId() + ", "
+ "accessHistoryId: " + accessHistory.getId()));
abtestVariableAssignRepository.save(AbtestVariableAssign.of(dbVariable.getId(), accessHistory.getId()));
return dbVariable.getName();
return dbVariable;
}
accessHistory.updateLastAccessedAt(clock);
return cacheVariable.get().getName();
return cacheVariable.get();
}

private void validateAssignedUser(Abtest abtest, Integer accessHistoryId, Integer userId) {
private boolean isAssignedUser(Abtest abtest, Integer accessHistoryId, Integer userId) {
AccessHistory accessHistory = accessHistoryRepository.getById(accessHistoryId);
if (userId != null && accessHistory.getDevice() != null
&& !Objects.equals(accessHistory.getDevice().getUser().getId(), userId)) {
return;
}
if (abtest.getAbtestVariables().stream()
.anyMatch(abtestVariable -> accessHistory.hasVariable(abtestVariable.getId()))) {
throw AbtestAssignedUserException.withDetail(
"abtestId: " + abtest.getId() + ", accessHistoryId: " + accessHistoryId);
return false;
}
return abtest.getAbtestVariables().stream()
.anyMatch(abtestVariable -> accessHistory.hasVariable(abtestVariable.getId()));
}

@Transactional
Expand Down

0 comments on commit ba0fa5e

Please sign in to comment.