create equal number of captchas for each parameter combination in background task

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2022-02-28 00:17:13 +05:30
parent e44a1df22e
commit 8316084ee7
3 changed files with 33 additions and 22 deletions

View File

@ -18,22 +18,33 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
challengeGCPstmt.executeUpdate()
val imageNumResult = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
val imageNum = if (imageNumResult.next()) {
imageNumResult.getInt("total")
} else {
0
}
for (param <- allParameterCombinations()) {
val imageNum = captchaManager.getCount(param).getOrElse(0)
val countCreate = (config.throttle * 1.1).toInt - imageNum
if (countCreate > 0) {
println(s"Creating $countCreate captchas for $param")
val throttle = (config.throttle * 1.1).toInt - imageNum
for (i <- 0 until throttle) {
captchaManager.generateChallenge(getRandomParam())
for (i <- 0 until countCreate) {
captchaManager.generateChallenge(param)
}
}
}
} catch { case exception: Exception => println(exception) }
}
}
private def allParameterCombinations(): List[Parameters] = {
(config.captchaConfig).flatMap {captcha =>
(captcha.allowedLevels).flatMap {level =>
(captcha.allowedMedia).flatMap {media =>
(captcha.allowedInputType).map {inputType =>
Parameters(level, media, inputType, Some(Size(0, 0)))
}
}
}
}
}
private def getRandomParam(): Parameters = {
val captcha = pickRandom(config.captchaConfig)
val level = pickRandom(captcha.allowedLevels)

View File

@ -105,8 +105,8 @@ class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
}
}
private def getCount(param: Parameters): Option[Int] = {
val countPstmt = Statements.tlStmts.get.countPstmt
def getCount(param: Parameters): Option[Int] = {
val countPstmt = Statements.tlStmts.get.countForParameterPstmt
countPstmt.setString(1, param.level)
countPstmt.setString(2, param.media)
countPstmt.setString(3, param.input_type)
@ -123,16 +123,16 @@ class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
if (count == 0) {
None
} else {
val tokenPstmt = Statements.tlStmts.get.tokenPstmt
tokenPstmt.setString(1, param.level)
tokenPstmt.setString(2, param.media)
tokenPstmt.setString(3, param.input_type)
val tokenPstmt = Statements.tlStmts.get.tokenPstmt
tokenPstmt.setString(1, param.level)
tokenPstmt.setString(2, param.media)
tokenPstmt.setString(3, param.input_type)
tokenPstmt.setInt(4, count)
val rs = tokenPstmt.executeQuery()
if (rs.next()) {
Some(rs.getInt("token"))
} else {
None
val rs = tokenPstmt.executeQuery()
if (rs.next()) {
Some(rs.getInt("token"))
} else {
None
}
}
}

View File

@ -70,7 +70,7 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
"WHERE token = ?;"
)
val countPstmt: PreparedStatement = dbConn.con.prepareStatement(
val countForParameterPstmt: PreparedStatement = dbConn.con.prepareStatement(
s"""
SELECT count(*) as count
FROM challenge