Skip to content
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

Log warning when crawler finds new federations #244

Merged
merged 2 commits into from
Jan 31, 2025
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
20 changes: 15 additions & 5 deletions modules/crawler/src/main/scala/Crawler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ object Downloader:

// shamelessly copied (with some minor modificaton) from: https://github.com/lichess-org/lila/blob/8033c4c5a15cf9bb2b36377c3480f3b64074a30f/modules/fide/src/main/FidePlayerSync.scala#L131
def parseLine(line: String)(using Logger[IO]): IO[Option[(NewPlayer, Option[NewFederation])]] =
IO(parse(line))
.handleErrorWith(e => error"Error while parsing line: $line, error: $e".as(none))
parse(line).handleErrorWith(e => error"Error while parsing line: $line, error: $e".as(none))

def parse(line: String): Option[(NewPlayer, Option[NewFederation])] =
def parse(line: String)(using Logger[IO]): IO[Option[(NewPlayer, Option[NewFederation])]] =
def string(start: Int, end: Int): Option[String] = line.substring(start, end).trim.some.filter(_.nonEmpty)

def number(start: Int, end: Int): Option[Int] = string(start, end).flatMap(_.toIntOption)
def rating(start: Int, end: Int): Option[Rating] = string(start, end) >>= Rating.fromString

for
def findFed(id: FederationId, playerId: PlayerId): IO[Option[NewFederation]] =
Federation
.nameById(id)
.fold(warn"cannot find federation: $id for player: $playerId" *> none[NewFederation].pure[IO])(name =>
NewFederation(id, name).some.pure[IO]
)

val x = for
id <- number(0, 15) >>= PlayerId.option
name <- string(15, 76).map(_.filterNot(_.isDigit).trim)
if name.sizeIs > 2
Expand All @@ -99,7 +105,11 @@ object Downloader:
sex = sex,
birthYear = year,
active = inactiveFlag.isEmpty
) -> federationId.flatMap(id => Federation.nameById(id).map(NewFederation(id, _)))
) -> federationId

x.traverse:
case (player, Some(fedId)) => findFed(fedId, player.id).map(fed => (player, fed))
case (player, None) => (player, none).pure[IO]

object Decompressor:

Expand Down
25 changes: 15 additions & 10 deletions modules/crawler/src/test/scala/CrawlerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package fide
package crawler
package test

import cats.effect.IO
import cats.syntax.all.*
import fide.domain.{ NewPlayer, OtherTitle }
import fide.domain.OtherTitle
import org.typelevel.log4cats.Logger
import weaver.*

object CrawlerTest extends FunSuite:
object CrawlerTest extends SimpleIOSuite:

given Logger[IO] = org.typelevel.log4cats.noop.NoOpLogger[IO]

test("player with other titles"):
val x =
parse:
"10001492 Ojok, Patrick UGA M FI,LSI 1638 0 20 1932 0 20 1926 0 20 1974 "
.get
expect(x.otherTitles == List(OtherTitle.FI, OtherTitle.LSI))
parse(
"10001492 Ojok, Patrick UGA M FI,LSI 1638 0 20 1932 0 20 1926 0 20 1974 "
).map(_.get.otherTitles)
.map(x => expect(x == List(OtherTitle.FI, OtherTitle.LSI)))

test("active flag"):
val lines = List(
Expand All @@ -22,7 +25,9 @@ object CrawlerTest extends FunSuite:
"29976634 Aafrin, S F Aja SRI F 2012 w ",
"1478800 Aagaard, Christian DEN M 1999 "
)
val actives = lines.traverse(parse).get.map(_.active)
expect(actives == List(false, false, false, true))
lines
.traverse(parse)
.map(_.flatten.map(_.active))
.map(x => expect(x == List(false, false, false, true)))

private def parse(s: String): Option[NewPlayer] = Downloader.parse(s).map(_._1)
private def parse(s: String) = Downloader.parse(s).map(_.map(_._1))
Loading