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 =
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
}
} }
}.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,31 +59,39 @@ 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 =>
analysisLine(parent, chapter.setup.variant, info) map parent.addChild
} ?? { parentWithNewChildren =>
chapterRepo.setChildren(parentWithNewChildren.children)(chapter, path)
} >> {
import BSONHandlers._
import Node.{ BsonFields => F }
chapterRepo.setNodeValues(
chapter,
path + node,
List(
F.score -> info.eval.score
.ifTrue { .ifTrue {
node.score.isEmpty || node.score.isEmpty ||
advOpt.isDefined && node.comments.findBy(Comment.Author.Lichess).isEmpty advOpt.isDefined && node.comments.findBy(Comment.Author.Lichess).isEmpty
} }
.?? { score => .flatMap(EvalScoreBSONHandler.writeOpt),
chapterRepo.setScore(score.some)(chapter, path + node) >> F.comments -> advOpt
advOpt.?? { adv => .map { adv =>
chapterRepo.setComments(
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),
F.glyphs -> advOpt
.map { adv =>
node.glyphs merge Glyphs.fromList(List(adv.judgment.glyph)) node.glyphs merge Glyphs.fromList(List(adv.judgment.glyph))
)(chapter, path + node) >> {
chapter.root.nodeAt(path).flatMap { parent =>
analysisLine(parent, chapter.setup.variant, info) map parent.addChild
} ?? { parentWithNewChildren =>
chapterRepo.setChildren(parentWithNewChildren.children)(chapter, path)
}
}
} }
.flatMap(GlyphsBSONHandler.writeOpt)
)
)
} inject path + node } inject path + node
} void } void
} >>- { } >>- {