Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
}
Comment on lines +1 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

에러 기록 클래스 추가는 좋은 접근입니다만, 로깅 개선이 필요합니다.

에러 발생 위치와 횟수를 추적하는 ErrorRecorder 클래스 추가는 좋은 접근입니다. 그러나 몇 가지 개선이 필요합니다:

  1. KotlinLogging 로거를 초기화했지만 사용하지 않고 있습니다. println 대신 로거를 사용하는 것이 좋습니다.
  2. 에러 카운트를 재설정하거나 프로그래밍 방식으로 검색하는 메서드가 없습니다.

다음과 같이 개선해 보세요:

 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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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")
}
}
}
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) ->
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()
}
}

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)
+        }
     }
 }

Committable suggestion skipped: line range outside the PR's diff.


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.

Loading