Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol committed Sep 26, 2024
1 parent d8ff878 commit addd0b8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion backend/project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//> using dep com.outr::scribe-cats::3.15.0
//> using file "../shared/protocol.scala"

//> using mainClass scalafmt_optimizer_cask.OptimizerServer
//> using mainClass scalafmt_optimizer_http4s.OptimizerServer

//> using resourceDirs "../frontend/dist"

9 changes: 3 additions & 6 deletions backend/server.cask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,14 @@ object OptimizerServer extends cask.MainRoutes:

@cask.get("/api/stats")
def stats() =
val result = Array.newBuilder[ujson.Value]
val result = List.newBuilder[JobSummary]

jobs.forEach: (id, job) =>
if job.instruction != TrainingInstruction.Halt then
result +=
ujson.Obj(
"id" -> ujson.Str(id.toString),
"heartbeat" -> ujson.Str(job.heartbeat.toString)
)
JobSummary(id.toString, job.heartbeat.toString)

ujson.Arr(result.result()*)
upickle.default.writeJs(Stats("cask", result.result()))
end stats

scribe.Logger.root.withMinimumLevel(scribe.Level.Info).replace()
Expand Down
21 changes: 9 additions & 12 deletions backend/server.http4s.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,15 @@ def routes(
case GET -> Root / "api" / "stats" =>
manager.getAll.flatMap: mp =>
val active = mp.filter(_._2.instruction != TrainingInstruction.Halt)
Ok(
ujson.write(
ujson.Arr(
active.map((id, j) =>
ujson.Obj(
"id" -> ujson.Str(id.toString),
"heartbeat" -> ujson.Str(j.heartbeat.toString)
)
)
)
)
).json
val stats = Stats(
"http4s",
active.toList
.sortBy(_._2.heartbeat)
.reverse
.map((id, j) => JobSummary(id.toString, j.heartbeat.toString))
)

Ok(upickle.default.write(stats)).json

case GET -> Root / "ws" / "connect" / UUIDVar(uuid) =>
manager.getAll
Expand Down
28 changes: 25 additions & 3 deletions frontend/src/main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ case class JobAttributes(
) derives ReadWriter

@main def hello =
val id = Var[Option[String]](None)
val result = Var[Option[JobProgress]](None)
val id = Var[Option[String]](None)
val result = Var[Option[JobProgress]](None)
val serverStats = Var[Option[Stats]](None)

val jobAttributes = Var(Option.empty[JobAttributes])

Expand Down Expand Up @@ -65,6 +66,13 @@ case class JobAttributes(
.map(Some(_))
.startWith(None)

val updateStats =
EventStream
.periodic(5000)
.flatMapSwitch: _ =>
FetchStream.get("/api/stats").map(upickle.default.read[Stats](_))
--> serverStats.someWriter

val cancelButton =
button(
"Cancel",
Expand Down Expand Up @@ -173,7 +181,8 @@ case class JobAttributes(
),
errorStatus,
trainingStatus
)
),
div(child.maybe <-- serverStats.signal.map(_.map(renderStats)))
),
onSubmit.preventDefault.flatMapTo(
jobAttributes.now() match
Expand Down Expand Up @@ -234,6 +243,7 @@ case class JobAttributes(
submitForm,
updateJob --> result,
resetError,
updateStats,
attrsSignal --> jobAttributes.someWriter,
text.signal --> SaveState.saveString(SaveState.TEXT_KEY),
generations.signal.map(_.toString) --> SaveState.saveString(
Expand Down Expand Up @@ -265,3 +275,15 @@ case class JobAttributes(
app
)
end hello

def renderStats(stats: Stats) = div(
p(strong("Server: "), stats.serverType),
p(
if stats.jobs.nonEmpty then strong("Active jobs") else em("No active jobs")
),
ul(
cls := "text-xs",
stats.jobs.map: job =>
li(cls := "ml-2", strong(job.id.toString), " : ", job.hearbeat)
)
)
5 changes: 5 additions & 0 deletions shared/protocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ enum JobProgress derives ReadWriter:
generations: Int
)

case class JobSummary(id: String, hearbeat: String) derives ReadWriter

case class Stats(
serverType: String,
jobs: List[JobSummary]
) derives ReadWriter

0 comments on commit addd0b8

Please sign in to comment.