lila/modules/explorer/src/main/ExplorerIndexer.scala

175 lines
6.4 KiB
Scala
Raw Normal View History

2016-02-08 08:42:33 -07:00
package lila.explorer
import scala.util.Random.nextFloat
2016-02-09 03:07:35 -07:00
import scala.util.{ Try, Success, Failure }
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
2016-02-08 08:42:33 -07:00
import play.api.libs.iteratee._
import play.api.libs.ws.WS
import play.api.Play.current
2017-11-03 12:37:33 -06:00
import chess.format.pgn.Tag
2016-02-08 08:42:33 -07:00
2016-04-01 11:50:57 -06:00
import lila.db.dsl._
2016-02-08 08:42:33 -07:00
import lila.game.BSONHandlers.gameBSONHandler
import lila.game.{ Game, GameRepo, Query, PgnDump, Player }
2016-02-11 07:56:53 -07:00
import lila.user.UserRepo
2016-02-08 08:42:33 -07:00
private final class ExplorerIndexer(
2016-04-01 11:50:57 -06:00
gameColl: Coll,
internalEndpoint: String
) {
2016-02-08 08:42:33 -07:00
private val maxGames = Int.MaxValue
private val batchSize = 50
private val separator = "\n\n\n"
2016-02-09 03:07:35 -07:00
private val datePattern = "yyyy-MM-dd"
2016-02-11 07:56:53 -07:00
private val dateFormatter = DateTimeFormat forPattern datePattern
private val dateTimeFormatter = DateTimeFormat forPattern s"$datePattern HH:mm"
private val pgnDateFormat = DateTimeFormat forPattern "yyyy.MM.dd";
private val internalEndPointUrl = s"$internalEndpoint/import/lichess"
2016-02-08 08:42:33 -07:00
2016-02-09 03:27:12 -07:00
private def parseDate(str: String): Option[DateTime] =
Try(dateFormatter parseDateTime str).toOption
2016-02-09 03:07:35 -07:00
type GamePGN = (Game, String)
def apply(sinceStr: String): Funit =
parseDate(sinceStr).fold(fufail[Unit](s"Invalid date $sinceStr")) { since =>
logger.info(s"Start indexing since $since")
2016-04-01 11:50:57 -06:00
val query =
2016-02-11 07:56:53 -07:00
Query.createdSince(since) ++
2016-02-09 03:07:35 -07:00
Query.rated ++
2016-02-08 08:42:33 -07:00
Query.finished ++
2018-01-25 08:10:01 -07:00
Query.turnsGt(8) ++
2016-02-09 20:41:21 -07:00
Query.noProvisional ++
2016-02-23 20:36:37 -07:00
Query.bothRatingsGreaterThan(1501)
2016-03-04 22:25:10 -07:00
import reactivemongo.api._
import reactivemongo.play.iteratees.cursorProducer
2016-08-05 04:24:18 -06:00
gameColl.find(query)
2016-02-08 08:42:33 -07:00
.sort(Query.sortChronological)
.cursor[Game](ReadPreference.secondary)
.enumerator(maxGames) &>
Enumeratee.mapM[Game].apply[Option[GamePGN]] { game =>
2016-02-09 03:27:12 -07:00
makeFastPgn(game) map {
_ map { game -> _ }
}
} &>
Enumeratee.collect { case Some(el) => el } &>
2016-02-08 08:42:33 -07:00
Enumeratee.grouped(Iteratee takeUpTo batchSize) |>>>
Iteratee.foldM[Seq[GamePGN], Long](nowMillis) {
case (millis, pairs) =>
WS.url(internalEndPointUrl).put(pairs.map(_._2) mkString separator).flatMap {
case res if res.status == 200 =>
2016-02-09 03:27:12 -07:00
val date = pairs.headOption.map(_._1.createdAt) ?? dateTimeFormatter.print
val nb = pairs.size
val gameMs = (nowMillis - millis) / nb.toDouble
logger.info(s"$date $nb ${gameMs.toInt} ms/game ${(1000 / gameMs).toInt} games/s")
funit
case res => fufail(s"Stop import because of status ${res.status}")
} >> {
pairs.headOption match {
case None => fufail(s"No games left, import complete!")
case Some((g, _)) if (g.createdAt.isAfter(DateTime.now.minusMinutes(10))) =>
fufail(s"Found a recent game, import complete!")
case _ => funit
}
2016-02-09 03:27:12 -07:00
} inject nowMillis
2016-02-08 08:42:33 -07:00
} void
2016-02-09 03:07:35 -07:00
}
def apply(game: Game): Funit = makeFastPgn(game) map {
_ foreach flowBuffer.apply
}
private object flowBuffer {
2016-02-22 22:24:32 -07:00
private val max = 30
private val buf = scala.collection.mutable.ArrayBuffer.empty[String]
2017-10-21 14:06:14 -06:00
def apply(pgn: String): Unit = {
buf += pgn
2016-02-18 22:39:25 -07:00
val startAt = nowMillis
if (buf.size >= max) {
WS.url(internalEndPointUrl).put(buf mkString separator) andThen {
case Success(res) if res.status == 200 =>
2016-03-10 11:21:04 -07:00
lila.mon.explorer.index.time(((nowMillis - startAt) / max).toInt)
2016-03-11 03:18:22 -07:00
lila.mon.explorer.index.success(max)
2016-03-10 11:21:04 -07:00
case Success(res) =>
logger.warn(s"[${res.status}]")
2016-03-11 03:18:22 -07:00
lila.mon.explorer.index.failure(max)
2016-03-10 11:21:04 -07:00
case Failure(err) =>
logger.warn(s"$err", err)
2016-03-11 03:18:22 -07:00
lila.mon.explorer.index.failure(max)
}
buf.clear
}
}
}
private def valid(game: Game) =
game.finished &&
game.rated &&
game.turns >= 10 &&
game.variant != chess.variant.FromPosition &&
2016-09-04 11:07:21 -06:00
!Game.isOldHorde(game)
private def stableRating(player: Player) = player.rating ifFalse player.provisional
// probability of the game being indexed, between 0 and 1
private def probability(game: Game, rating: Int) = {
import lila.rating.PerfType._
game.perfType ?? {
case Correspondence => 1
2017-11-29 10:58:08 -07:00
case Rapid | Classical if rating >= 2000 => 1
case Rapid | Classical if rating >= 1800 => 2 / 5f
case Rapid | Classical => 1 / 8f
case Blitz if rating >= 2000 => 1
case Blitz if rating >= 1800 => 1 / 4f
2017-05-02 13:46:16 -06:00
case Blitz => 1 / 15f
case Bullet if rating >= 2300 => 1
case Bullet if rating >= 2200 => 4 / 5f
case Bullet if rating >= 2000 => 1 / 4f
case Bullet if rating >= 1800 => 1 / 7f
2017-05-02 13:46:16 -06:00
case Bullet => 1 / 20f
case _ if rating >= 1600 => 1 // variant games
case _ => 1 / 2f // noob variant games
}
}
private def makeFastPgn(game: Game): Fu[Option[String]] = ~(for {
whiteRating <- stableRating(game.whitePlayer)
blackRating <- stableRating(game.blackPlayer)
minPlayerRating = if (game.variant.exotic) 1400 else 1500
minAverageRating = if (game.variant.exotic) 1520 else 1600
if whiteRating >= minPlayerRating
if blackRating >= minPlayerRating
averageRating = (whiteRating + blackRating) / 2
if averageRating >= minAverageRating
if probability(game, averageRating) > nextFloat
if valid(game)
2016-02-11 07:56:53 -07:00
} yield GameRepo initialFen game flatMap { initialFen =>
UserRepo.usernamesByIds(game.userIds) map { usernames =>
def username(color: chess.Color) = game.player(color).userId flatMap { id =>
usernames.find(_.toLowerCase == id)
} orElse game.player(color).userId getOrElse "?"
val fenTags = initialFen.?? { fen => List(s"[FEN $fen]") }
2017-11-03 12:37:33 -06:00
val timeControl = Tag.timeControl(game.clock.map(_.config)).value
2016-02-11 07:56:53 -07:00
val otherTags = List(
s"[LichessID ${game.id}]",
s"[Variant ${game.variant.name}]",
s"[TimeControl $timeControl]",
2016-02-11 07:56:53 -07:00
s"[White ${username(chess.White)}]",
s"[Black ${username(chess.Black)}]",
s"[WhiteElo $whiteRating]",
s"[BlackElo $blackRating]",
s"[Result ${PgnDump.result(game)}]",
s"[Date ${pgnDateFormat.print(game.createdAt)}]"
)
2016-02-11 07:56:53 -07:00
val allTags = fenTags ::: otherTags
s"${allTags.mkString("\n")}\n\n${game.pgnMoves.take(maxPlies).mkString(" ")}".some
2016-02-11 07:56:53 -07:00
}
})
2016-03-20 03:31:09 -06:00
private val logger = lila.log("explorer")
2016-02-08 08:42:33 -07:00
}