storm dashboard API - closes #8203

pull/8210/head
Thibault Duplessis 2021-02-18 12:06:06 +01:00
parent d0c46e57d1
commit 75b540370f
4 changed files with 38 additions and 1 deletions

View File

@ -62,4 +62,16 @@ final class Storm(env: Env)(implicit mat: akka.stream.Materializer) extends Lila
Ok(views.html.storm.dashboard(user, history, high))
}
}
def apiDashboardOf(username: String, days: Int) =
Open { implicit ctx =>
val userId = lila.user.User normalize username
if (days < 0 || days > 365) notFoundJson("Invalid days parameter")
else
((days > 0) ?? env.storm.dayApi.apiHistory(userId, days)) zip env.storm.highApi.get(userId) map {
case (history, high) =>
Ok(env.storm.json.apiDashboard(high, history))
}
}
}

View File

@ -113,6 +113,7 @@ GET /storm controllers.Storm.home
POST /storm controllers.Storm.record
GET /storm/dashboard controllers.Storm.dashboard(page: Int ?= 1)
GET /storm/dashboard/:username controllers.Storm.dashboardOf(username: String, page: Int ?= 1)
GET /api/storm/dashboard/:username controllers.Storm.apiDashboardOf(username: String, days: Int ?= 30)
# User Analysis
GET /analysis/help controllers.UserAnalysis.help

View File

@ -12,6 +12,7 @@ import lila.db.dsl._
import lila.db.paginator.Adapter
import lila.user.User
import lila.user.UserRepo
import reactivemongo.api.ReadPreference
// stores data of the best run of the day
// plus the number of runs
@ -114,5 +115,12 @@ final class StormDayApi(coll: Coll, highApi: StormHighApi, userRepo: UserRepo, s
def eraseAllFor(user: User) =
coll.delete.one(idRegexFor(user.id)).void
def apiHistory(userId: User.ID, days: Int): Fu[List[StormDay]] =
coll
.find(idRegexFor(userId))
.sort($sort desc "_id")
.cursor[StormDay](ReadPreference.secondaryPreferred)
.list(days)
private def idRegexFor(userId: User.ID) = $doc("_id" $startsWith s"${userId}:")
}

View File

@ -4,10 +4,11 @@ import play.api.libs.json._
import lila.common.Json._
import lila.user.User
import org.joda.time.format.DateTimeFormat
final class StormJson(sign: StormSign) {
import StormJson.puzzleWrites
import StormJson._
def apply(puzzles: List[StormPuzzle], user: Option[User]): JsObject = Json
.obj(
@ -35,12 +36,27 @@ final class StormJson(sign: StormSign) {
"prev" -> nh.previous
)
})
def apiDashboard(high: StormHigh, days: List[StormDay]) = Json.obj(
"high" -> high,
"days" -> days
)
}
object StormJson {
import lila.puzzle.JsonView.puzzleIdWrites
implicit val highWrites: OWrites[StormHigh] = Json.writes[StormHigh]
private val dateFormat = DateTimeFormat forPattern "Y/M/d"
implicit val dayIdWrites: Writes[StormDay.Id] = Writes { id =>
JsString(dateFormat print id.day.toDate)
}
implicit val dayWrites: OWrites[StormDay] = Json.writes[StormDay]
implicit val puzzleWrites: OWrites[StormPuzzle] = OWrites { p =>
Json.obj(
"id" -> p.id,