2016-03-11 10:09:59 -07:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import play.api.libs.json._
|
|
|
|
import play.api.mvc._
|
2019-12-13 07:30:20 -07:00
|
|
|
import scala.util.{ Failure, Success }
|
2016-03-11 10:09:59 -07:00
|
|
|
|
|
|
|
import lila.app._
|
2016-03-20 10:01:03 -06:00
|
|
|
import lila.common.HTTPRequest
|
2016-03-12 00:23:20 -07:00
|
|
|
import lila.fishnet.JsonApi.readers._
|
|
|
|
import lila.fishnet.JsonApi.writers._
|
|
|
|
import lila.fishnet.{ JsonApi, Work }
|
2016-03-11 10:09:59 -07:00
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
final class Fishnet(env: Env) extends LilaController(env) {
|
2016-03-11 10:09:59 -07:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
private def api = env.fishnet.api
|
2016-11-02 06:15:41 -06:00
|
|
|
private val logger = lila.log("fishnet")
|
2016-03-11 10:09:59 -07:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def acquire(slow: Boolean = false) =
|
|
|
|
ClientAction[JsonApi.Request.Acquire] { _ => client =>
|
|
|
|
api.acquire(client, slow) addEffect { jobOpt =>
|
2020-09-28 15:35:17 -06:00
|
|
|
lila.mon.fishnet.http.request(jobOpt.isDefined).increment().unit
|
2020-05-05 22:11:15 -06:00
|
|
|
} map Right.apply
|
|
|
|
}
|
2016-03-12 00:23:20 -07:00
|
|
|
|
2020-04-14 09:47:07 -06:00
|
|
|
def analysis(workId: String, slow: Boolean = false, stop: Boolean = false) =
|
|
|
|
ClientAction[JsonApi.Request.PostAnalysis] { data => client =>
|
2020-04-14 09:37:46 -06:00
|
|
|
import lila.fishnet.FishnetApi._
|
|
|
|
def onComplete =
|
|
|
|
if (stop) fuccess(Left(NoContent))
|
2020-04-14 09:47:07 -06:00
|
|
|
else api.acquire(client, slow) map Right.apply
|
2020-04-14 09:37:46 -06:00
|
|
|
api
|
|
|
|
.postAnalysis(Work.Id(workId), client, data)
|
|
|
|
.flatFold(
|
|
|
|
{
|
2021-09-12 18:12:47 -06:00
|
|
|
case WorkNotFound => onComplete
|
|
|
|
case GameNotFound => onComplete
|
|
|
|
case NotAcquired => onComplete
|
|
|
|
case e => fuccess(Left(InternalServerError(e.getMessage)))
|
2020-05-05 22:11:15 -06:00
|
|
|
},
|
|
|
|
{
|
2020-04-14 09:37:46 -06:00
|
|
|
case PostAnalysisResult.Complete(analysis) =>
|
|
|
|
env.round.proxyRepo.updateIfPresent(analysis.id)(_.setAnalysed)
|
|
|
|
onComplete
|
|
|
|
case _: PostAnalysisResult.Partial => fuccess(Left(NoContent))
|
|
|
|
case PostAnalysisResult.UnusedPartial => fuccess(Left(NoContent))
|
|
|
|
}
|
|
|
|
)
|
2020-04-14 09:47:07 -06:00
|
|
|
}
|
2016-03-12 00:23:20 -07:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def abort(workId: String) =
|
|
|
|
ClientAction[JsonApi.Request.Acquire] { _ => client =>
|
|
|
|
api.abort(Work.Id(workId), client) inject Right(none)
|
|
|
|
}
|
2016-03-14 22:53:39 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def keyExists(key: String) =
|
|
|
|
Action.async { _ =>
|
|
|
|
api keyExists lila.fishnet.Client.Key(key) map {
|
|
|
|
case true => Ok
|
|
|
|
case false => NotFound
|
|
|
|
}
|
2016-03-22 11:48:54 -06:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def status =
|
|
|
|
Action.async {
|
|
|
|
api.status map { Ok(_) }
|
|
|
|
}
|
2017-01-08 13:56:36 -07:00
|
|
|
|
2019-12-13 07:30:20 -07:00
|
|
|
private def ClientAction[A <: JsonApi.Request](
|
|
|
|
f: A => lila.fishnet.Client => Fu[Either[Result, Option[JsonApi.Work]]]
|
|
|
|
)(implicit reads: Reads[A]) =
|
2019-12-04 16:39:16 -07:00
|
|
|
Action.async(parse.tolerantJson) { req =>
|
2019-12-13 07:30:20 -07:00
|
|
|
req.body
|
|
|
|
.validate[A]
|
|
|
|
.fold(
|
|
|
|
err => {
|
|
|
|
logger.warn(s"Malformed request: $err\n${req.body}")
|
|
|
|
BadRequest(jsonError(JsError toJson err)).fuccess
|
|
|
|
},
|
|
|
|
data =>
|
2020-10-22 07:01:30 -06:00
|
|
|
api.authenticateClient(data, HTTPRequest ipAddress req) flatMap {
|
2019-12-13 07:30:20 -07:00
|
|
|
case Failure(msg) => Unauthorized(jsonError(msg.getMessage)).fuccess
|
|
|
|
case Success(client) =>
|
|
|
|
f(data)(client).map {
|
|
|
|
case Right(Some(work)) => Accepted(Json toJson work)
|
|
|
|
case Right(None) => NoContent
|
|
|
|
case Left(result) => result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2016-03-12 00:23:20 -07:00
|
|
|
}
|
2016-03-11 10:09:59 -07:00
|
|
|
}
|