-
Notifications
You must be signed in to change notification settings - Fork 1
[IDLE-567] 배치 pageIndex * 채팅 목록 버그 수정 #271
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.swm.idle.batch.crawler | ||
|
|
||
| import io.github.oshai.kotlinlogging.KotlinLogging | ||
|
|
||
| class ErrorRecorder { | ||
|
|
||
| private val errorCounts: MutableMap<String, Int> = mutableMapOf() | ||
| private val logger = KotlinLogging.logger {} | ||
|
|
||
| fun recordError(location: String) { | ||
| errorCounts[location] = errorCounts.getOrDefault(location, 0) + 1 | ||
| } | ||
|
|
||
| fun printErrors() { | ||
| errorCounts.forEach { (location, count) -> | ||
| println("Error at $location: $count occurrences") | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package com.swm.idle.batch.crawler | ||
|
|
||
| import io.github.oshai.kotlinlogging.KotlinLogging | ||
| import org.openqa.selenium.By | ||
| import org.openqa.selenium.WebDriver | ||
| import org.openqa.selenium.support.ui.ExpectedConditions | ||
| import org.openqa.selenium.support.ui.WebDriverWait | ||
| import java.time.Duration | ||
| import java.time.LocalDate | ||
| import java.time.format.DateTimeFormatter | ||
|
|
||
| class WorknetPageParser { | ||
| private var postingCount = 0 | ||
| private var crawlingUrl: String = "" | ||
| private var lastPageJobPostingCount: Int = 1 | ||
| private var pageCount: Int = 1 | ||
|
|
||
| private val logger = KotlinLogging.logger { } | ||
|
|
||
| init { | ||
| val driver = DriverInitializer.init() | ||
| driver.safeUse { | ||
| getCrawlingURL() | ||
| moveToPage(driver) | ||
| getPostingCount(driver) | ||
| calculatePageInfo() | ||
| LoggingPageResult() | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+29
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. 🛠️ Refactor suggestion 초기화 블록에서의 상태 설정은 예외 처리가 필요합니다. 초기화 블록에서 WebDriver를 생성하고 페이지 정보를 가져오는 로직은 간결하지만, 예외 처리가 없어 초기화 과정에서 문제가 발생하면 클래스 인스턴스 생성이 실패할 수 있습니다. 예외 처리를 통해 더 견고한 초기화 과정을 구현하는 것이 좋습니다. init {
val driver = DriverInitializer.init()
- driver.safeUse {
- getCrawlingURL()
- moveToPage(driver)
- getPostingCount(driver)
- calculatePageInfo()
- LoggingPageResult()
+ driver.safeUse {
+ try {
+ getCrawlingURL()
+ moveToPage(driver)
+ getPostingCount(driver)
+ calculatePageInfo()
+ loggingPageResult() // 메서드명 소문자로 수정
+ } catch (e: Exception) {
+ logger.error(e) { "Failed to initialize WorknetPageParser" }
+ throw IllegalStateException("Failed to initialize WorknetPageParser", e)
+ }
}
}
|
||
|
|
||
| fun isOverPage(currentPage: Int): Boolean { | ||
| return currentPage > pageCount | ||
| } | ||
|
|
||
| fun getFetchCount(currentPage: Int): Int { | ||
| if (currentPage == pageCount && lastPageJobPostingCount > 0) { | ||
| return lastPageJobPostingCount | ||
| } | ||
| return CrawlerConsts.JOB_POSTING_COUNT_PER_PAGE.getIntValue() | ||
| } | ||
|
|
||
| fun getAccessURL(currentPage: Int): String { | ||
| return crawlingUrl.replace( | ||
| Regex("pageIndex=\\d+"), | ||
| "pageIndex=$currentPage") | ||
| } | ||
|
|
||
| private fun getCrawlingURL() { | ||
| crawlingUrl = CrawlerConsts.CRAWLING_TARGET_URL_FORMAT.value | ||
| .replace("{yesterday}", LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))) | ||
| .replace("{pageIndex}", "1") | ||
| } | ||
|
|
||
| private fun moveToPage(driver: WebDriver) { | ||
| driver.get(crawlingUrl) | ||
| WebDriverWait(driver, Duration.ofSeconds(10)) | ||
| .until(ExpectedConditions.visibilityOfElementLocated( | ||
| By.xpath( | ||
| CrawlerConsts.JOB_POSTING_COUNT.value | ||
| ))) | ||
| } | ||
|
|
||
| private fun getPostingCount(driver: WebDriver) { | ||
| postingCount = driver | ||
| .findElement(By.xpath(CrawlerConsts.JOB_POSTING_COUNT.value)) | ||
| .text | ||
| .replace(",", "") | ||
| .toInt() | ||
| .takeIf { it > 0 } | ||
| ?: run { | ||
| throw Exception("크롤링 할 공고가 없습니다.") | ||
| } | ||
| } | ||
|
|
||
| private fun calculatePageInfo() { | ||
| pageCount = | ||
| (postingCount + CrawlerConsts.JOB_POSTING_COUNT_PER_PAGE.getIntValue() - 1) / | ||
| CrawlerConsts.JOB_POSTING_COUNT_PER_PAGE.getIntValue() | ||
| lastPageJobPostingCount = | ||
| postingCount % CrawlerConsts.JOB_POSTING_COUNT_PER_PAGE.getIntValue() | ||
| } | ||
|
|
||
| private fun LoggingPageResult() { | ||
| logger.info { "PageCount : ${pageCount}" } | ||
| logger.info { "PostingCount : ${postingCount}" } | ||
| logger.info { "LastPage JobCount : ${lastPageJobPostingCount}" } | ||
| } | ||
|
|
||
| private fun <T> WebDriver.safeUse(block: (WebDriver) -> T): T { | ||
| try { | ||
| return block(this) | ||
| } finally { | ||
| this.quit() | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
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.
🛠️ Refactor suggestion
에러 기록 클래스 추가는 좋은 접근입니다만, 로깅 개선이 필요합니다.
에러 발생 위치와 횟수를 추적하는
ErrorRecorder클래스 추가는 좋은 접근입니다. 그러나 몇 가지 개선이 필요합니다:println대신 로거를 사용하는 것이 좋습니다.다음과 같이 개선해 보세요:
class ErrorRecorder { private val errorCounts: MutableMap<String, Int> = mutableMapOf() private val logger = KotlinLogging.logger {} fun recordError(location: String) { errorCounts[location] = errorCounts.getOrDefault(location, 0) + 1 } fun printErrors() { errorCounts.forEach { (location, count) -> - println("Error at $location: $count occurrences") + logger.error { "Error at $location: $count occurrences" } } } + + fun resetErrors() { + errorCounts.clear() + } + + fun getErrorCount(location: String): Int { + return errorCounts.getOrDefault(location, 0) + } + + fun getTotalErrorCount(): Int { + return errorCounts.values.sum() + } }📝 Committable suggestion