-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 변경 감지 정책 도입 #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: 변경 감지 정책 도입 #200
Changes from all commits
9b7d2ed
b302ca4
7a15eb0
0001840
2fb6a43
c69f6f4
b0a8b41
ac0f755
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package kr.allcll.backend.admin.seat; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import kr.allcll.backend.admin.seat.dto.ChangeSubjectsResponse; | ||
|
|
@@ -20,7 +22,9 @@ public class ChangeDetector { | |
| private final SjptProperties sjptProperties; | ||
|
|
||
| public boolean isRemainSeatChanged(CrawlerSubject crawlerSubject, CrawlerSeat renewedCrawlerSeat) { | ||
| Optional<CrawlerSeat> previousSeat = crawlerSeatRepository.findByCrawlerSubject(crawlerSubject); | ||
| LocalDate today = LocalDate.now(); | ||
|
|
||
| Optional<CrawlerSeat> previousSeat = crawlerSeatRepository.findByCrawlerSubjectAndCreatedDate(crawlerSubject, today); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. db에서 매번 해당 과목의 여석 정보를 가져오는 것 보다, 모든 과목의 여석 정보를 담고있는 캐시에서 가져와서 비교하는 식으로 방법을 바꾸는건 어떨까요? 매번 db를 들락날락하면 병목이 생길 수 있을 것 같은데, 관리포인트가 너무 늘어나지 않는 선에서 방법을 고민해보면 좋을 것 같아요. |
||
| if (previousSeat.isEmpty()) { | ||
| return true; | ||
| } | ||
|
|
@@ -41,7 +45,7 @@ public void saveChangeToBuffer(CrawlerSubject crawlerSubject, CrawlerSeat renewe | |
| } else { | ||
| changedSubjectBuffer.add( | ||
| ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.UPDATE, SeatUtils.getRemainSeat( | ||
| renewedCrawlerSeat))); | ||
| renewedCrawlerSeat), LocalDateTime.now())); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package kr.allcll.backend.admin.seat; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -27,25 +28,25 @@ public LiveBoards() { | |
| public List<ChangeSubjectsResponse> checkStatus(CrawlerSubject crawlerSubject, Integer remainSeat) { | ||
| if (canOnlyIn(crawlerSubject, remainSeat)) { | ||
| liveBoardSubjects.put(crawlerSubject, remainSeat); | ||
| return List.of(ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.IN, remainSeat)); | ||
| return List.of(ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.IN, remainSeat, LocalDateTime.now())); | ||
| } | ||
| CrawlerSubject maxCrawlerSubject = findMaxRemainSeatSubject(); | ||
| Integer maxSubjectRemainSeat = liveBoardSubjects.get(maxCrawlerSubject); | ||
| if (canInAndOut(crawlerSubject, maxCrawlerSubject, remainSeat)) { | ||
| liveBoardSubjects.put(crawlerSubject, remainSeat); | ||
| liveBoardSubjects.remove(maxCrawlerSubject); | ||
| return List.of( | ||
| ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.IN, remainSeat), | ||
| ChangeSubjectsResponse.of(maxCrawlerSubject.getId(), ChangeStatus.OUT, maxSubjectRemainSeat) | ||
| ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.IN, remainSeat, LocalDateTime.now()), | ||
| ChangeSubjectsResponse.of(maxCrawlerSubject.getId(), ChangeStatus.OUT, maxSubjectRemainSeat, LocalDateTime.now()) | ||
| ); | ||
| } | ||
| if (canOut(crawlerSubject, remainSeat)) { | ||
| liveBoardSubjects.remove(crawlerSubject); | ||
| return List.of(ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.OUT, remainSeat)); | ||
| return List.of(ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.OUT, remainSeat, LocalDateTime.now())); | ||
| } | ||
|
Comment on lines
43
to
46
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. out 된 경우에는 전광판의 과목의 개수가 19개가 되니 21번째 전광판 과목이 20번째로 올라와야할 것 같은데 그 로직이 빠져있는 것 같아요!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 또한 checkStatus 메서드에 대해 과 같이 초반에 isRemainSeatBecomeEmpty(크롤링 한 과목의 여석이 0인지) 를 기준으로 분기처리를 해 로직을 수정하는 것은 어떤가요? 여석이 0인 경우에도 불필요하게 많은 로직들을 거치게 되는 것 같아서요! |
||
| if (canUpdate(crawlerSubject, remainSeat)) { | ||
| liveBoardSubjects.put(crawlerSubject, remainSeat); | ||
| return List.of(ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.UPDATE, remainSeat)); | ||
| return List.of(ChangeSubjectsResponse.of(crawlerSubject.getId(), ChangeStatus.UPDATE, remainSeat, LocalDateTime.now())); | ||
| } | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package kr.allcll.backend.admin.seat; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.Optional; | ||
| import kr.allcll.crawler.seat.CrawlerSeat; | ||
| import kr.allcll.crawler.seat.CrawlerSeatRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
@@ -15,6 +17,17 @@ public class SeatPersistenceService { | |
|
|
||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void saveSeat(CrawlerSeat crawlerSeat) { | ||
| crawlerSeatRepository.save(crawlerSeat); | ||
| LocalDate today = LocalDate.now(); | ||
|
|
||
| Optional<CrawlerSeat> existingSeatOpt = crawlerSeatRepository.findByCrawlerSubjectAndCreatedDate( | ||
| crawlerSeat.getCrawlerSubject(), | ||
| today | ||
| ); | ||
| if (existingSeatOpt.isPresent()) { | ||
| CrawlerSeat existingCrawlerSeat = existingSeatOpt.get(); | ||
| existingCrawlerSeat.merge(crawlerSeat); | ||
|
Comment on lines
+26
to
+28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기존에는 과목을 크롤링할 때 마다 계속 save만 했던 로직을, 과목 여석 정보를 Update하는 방식으로 변경했는데요, 하지만 이는 매초 15번 일어나는 로직이기에
요 경우(2) == 에도 db에 부하를 너무 많이 주는 로직인 것 같습니다. 따라서 변경이 필요해보여요! 위에 링크 남겨놓은 부분이 db를 거치지 않는 방식으로 수정된다면, 해당 부분은 원래대로 save만 하는 방법으로 수정할 수 있을 것 같은데 어떻게 생각하시나요? |
||
| } else { | ||
| crawlerSeatRepository.save(crawlerSeat); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,21 @@ | ||
| package kr.allcll.backend.domain.seat.dto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import kr.allcll.backend.admin.seat.ChangeStatus; | ||
|
|
||
| public record SeatResponse( | ||
| Long subjectId, | ||
| Integer seatCount, | ||
| LocalDateTime queryTime | ||
| LocalDateTime queryTime, | ||
| ChangeStatus changeStatus | ||
| ) { | ||
|
|
||
| public static SeatResponse from(SeatDto seatDto) { | ||
| return new SeatResponse( | ||
| seatDto.getSubject().getId(), | ||
| seatDto.getSeatCount(), | ||
| seatDto.getQueryTime() | ||
| seatDto.getQueryTime(), | ||
| seatDto.getChangeStatus() | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changedSubjectBuffer.addAll(response); 에 걸리는 경우는 교양과목인 경우 중, 대시보드에 변화가 생긴 경우입니다.
else에 해당하는 changedSubjectBuffer.add()…. 가 실행되는 경우는 교양 과목이 아닌 경우 즉 pin 과목인 경우를 의도하고 작성된 부분인 것 같은데, 교양 과목인데 pin 과목인 경우에는 어느곳에도 해당하지 않아 여석이 업데이트 되지 않을 것 같습니다.
따라서
다음과 같이 로직을 변경해야하지 않나 하는 생각이 드는데 어떻게 생각하시나요?