fix study server analysis

pull/8099/head
Thibault Duplessis 2021-02-05 11:39:07 +01:00
parent 15b05b02c9
commit 4864a70877
2 changed files with 59 additions and 38 deletions

View File

@ -119,20 +119,20 @@ final class ChapterRepo(val coll: AsyncColl)(implicit
def setTagsFor(chapter: Chapter) = def setTagsFor(chapter: Chapter) =
coll(_.updateField($id(chapter.id), "tags", chapter.tags)).void coll(_.updateField($id(chapter.id), "tags", chapter.tags)).void
def setShapes(shapes: lila.tree.Node.Shapes) = setNodeValue("h", shapes.value.nonEmpty option shapes) _ def setShapes(shapes: lila.tree.Node.Shapes) =
setNodeValue(Node.BsonFields.shapes, shapes.value.nonEmpty option shapes) _
def setComments(comments: lila.tree.Node.Comments) = def setComments(comments: lila.tree.Node.Comments) =
setNodeValue("co", comments.value.nonEmpty option comments) _ setNodeValue(Node.BsonFields.comments, comments.value.nonEmpty option comments) _
def setGamebook(gamebook: lila.tree.Node.Gamebook) = setNodeValue("ga", gamebook.nonEmpty option gamebook) _ def setGamebook(gamebook: lila.tree.Node.Gamebook) =
setNodeValue(Node.BsonFields.gamebook, gamebook.nonEmpty option gamebook) _
def setGlyphs(glyphs: chess.format.pgn.Glyphs) = setNodeValue("g", glyphs.nonEmpty) _ def setGlyphs(glyphs: chess.format.pgn.Glyphs) = setNodeValue(Node.BsonFields.glyphs, glyphs.nonEmpty) _
def setClock(clock: Option[chess.Centis]) = setNodeValue("l", clock) _ def setClock(clock: Option[chess.Centis]) = setNodeValue(Node.BsonFields.clock, clock) _
def forceVariation(force: Boolean) = setNodeValue("fv", force option true) _ def forceVariation(force: Boolean) = setNodeValue(Node.BsonFields.forceVariation, force option true) _
def setScore(score: Option[lila.tree.Eval.Score]) = setNodeValue("e", score) _
// overrides all children sub-nodes in DB! Make the tree merge beforehand. // overrides all children sub-nodes in DB! Make the tree merge beforehand.
def setChildren(children: Node.Children)(chapter: Chapter, path: Path): Funit = { def setChildren(children: Node.Children)(chapter: Chapter, path: Path): Funit = {
@ -162,15 +162,28 @@ final class ChapterRepo(val coll: AsyncColl)(implicit
$id(chapter.id) ++ $doc(path.toDbField $exists true), $id(chapter.id) ++ $doc(path.toDbField $exists true),
pathToField(path, field), pathToField(path, field),
value value
).map { ).void
case 0 => }
logger.warn(
s"Can't setNodeValue ${chapter.studyId}/${chapter.id} '$path' $field '${path.toDbField}' / no node matched!" private[study] def setNodeValues(
) chapter: Chapter,
false path: Path,
case _ => true values: List[(String, Option[BSONValue])]
} ): Funit =
}.void values.collect { case (field, Some(v)) =>
pathToField(path, field) -> v
} match {
case Nil => funit
case sets =>
coll {
_.update
.one(
$id(chapter.id) ++ $doc(path.toDbField $exists true),
$set($doc(sets))
)
.void
}
}
// root.path.subField // root.path.subField
private def pathToField(path: Path, subField: String): String = s"${path.toDbField}.$subField" private def pathToField(path: Path, subField: String): String = s"${path.toDbField}.$subField"

View File

@ -59,32 +59,40 @@ object ServerEval {
lila.common.Future lila.common.Future
.fold(chapter.root.mainline.zip(analysis.infoAdvices).toList)(Path.root) { .fold(chapter.root.mainline.zip(analysis.infoAdvices).toList)(Path.root) {
case (path, (node, (info, advOpt))) => case (path, (node, (info, advOpt))) =>
info.eval.score chapter.root.nodeAt(path).flatMap { parent =>
.ifTrue { analysisLine(parent, chapter.setup.variant, info) map parent.addChild
node.score.isEmpty || } ?? { parentWithNewChildren =>
advOpt.isDefined && node.comments.findBy(Comment.Author.Lichess).isEmpty chapterRepo.setChildren(parentWithNewChildren.children)(chapter, path)
} } >> {
.?? { score => import BSONHandlers._
chapterRepo.setScore(score.some)(chapter, path + node) >> import Node.{ BsonFields => F }
advOpt.?? { adv => chapterRepo.setNodeValues(
chapterRepo.setComments( chapter,
path + node,
List(
F.score -> info.eval.score
.ifTrue {
node.score.isEmpty ||
advOpt.isDefined && node.comments.findBy(Comment.Author.Lichess).isEmpty
}
.flatMap(EvalScoreBSONHandler.writeOpt),
F.comments -> advOpt
.map { adv =>
node.comments + Comment( node.comments + Comment(
Comment.Id.make, Comment.Id.make,
Comment.Text(adv.makeComment(withEval = false, withBestMove = true)), Comment.Text(adv.makeComment(withEval = false, withBestMove = true)),
Comment.Author.Lichess Comment.Author.Lichess
) )
)(chapter, path + node) >> }
chapterRepo.setGlyphs( .flatMap(CommentsBSONHandler.writeOpt),
node.glyphs merge Glyphs.fromList(List(adv.judgment.glyph)) F.glyphs -> advOpt
)(chapter, path + node) >> { .map { adv =>
chapter.root.nodeAt(path).flatMap { parent => node.glyphs merge Glyphs.fromList(List(adv.judgment.glyph))
analysisLine(parent, chapter.setup.variant, info) map parent.addChild }
} ?? { parentWithNewChildren => .flatMap(GlyphsBSONHandler.writeOpt)
chapterRepo.setChildren(parentWithNewChildren.children)(chapter, path) )
} )
} } inject path + node
}
} inject path + node
} void } void
} >>- { } >>- {
chapterRepo.byId(Chapter.Id(analysis.id)).foreach { chapterRepo.byId(Chapter.Id(analysis.id)).foreach {