Skip to content

Commit addd0b8

Browse files
committed
WIP
1 parent d8ff878 commit addd0b8

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

backend/project.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//> using dep com.outr::scribe-cats::3.15.0
1414
//> using file "../shared/protocol.scala"
1515

16-
//> using mainClass scalafmt_optimizer_cask.OptimizerServer
16+
//> using mainClass scalafmt_optimizer_http4s.OptimizerServer
1717

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

backend/server.cask.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,14 @@ object OptimizerServer extends cask.MainRoutes:
106106

107107
@cask.get("/api/stats")
108108
def stats() =
109-
val result = Array.newBuilder[ujson.Value]
109+
val result = List.newBuilder[JobSummary]
110110

111111
jobs.forEach: (id, job) =>
112112
if job.instruction != TrainingInstruction.Halt then
113113
result +=
114-
ujson.Obj(
115-
"id" -> ujson.Str(id.toString),
116-
"heartbeat" -> ujson.Str(job.heartbeat.toString)
117-
)
114+
JobSummary(id.toString, job.heartbeat.toString)
118115

119-
ujson.Arr(result.result()*)
116+
upickle.default.writeJs(Stats("cask", result.result()))
120117
end stats
121118

122119
scribe.Logger.root.withMinimumLevel(scribe.Level.Info).replace()

backend/server.http4s.scala

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,15 @@ def routes(
115115
case GET -> Root / "api" / "stats" =>
116116
manager.getAll.flatMap: mp =>
117117
val active = mp.filter(_._2.instruction != TrainingInstruction.Halt)
118-
Ok(
119-
ujson.write(
120-
ujson.Arr(
121-
active.map((id, j) =>
122-
ujson.Obj(
123-
"id" -> ujson.Str(id.toString),
124-
"heartbeat" -> ujson.Str(j.heartbeat.toString)
125-
)
126-
)
127-
)
128-
)
129-
).json
118+
val stats = Stats(
119+
"http4s",
120+
active.toList
121+
.sortBy(_._2.heartbeat)
122+
.reverse
123+
.map((id, j) => JobSummary(id.toString, j.heartbeat.toString))
124+
)
125+
126+
Ok(upickle.default.write(stats)).json
130127

131128
case GET -> Root / "ws" / "connect" / UUIDVar(uuid) =>
132129
manager.getAll

frontend/src/main.scala

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ case class JobAttributes(
2323
) derives ReadWriter
2424

2525
@main def hello =
26-
val id = Var[Option[String]](None)
27-
val result = Var[Option[JobProgress]](None)
26+
val id = Var[Option[String]](None)
27+
val result = Var[Option[JobProgress]](None)
28+
val serverStats = Var[Option[Stats]](None)
2829

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

@@ -65,6 +66,13 @@ case class JobAttributes(
6566
.map(Some(_))
6667
.startWith(None)
6768

69+
val updateStats =
70+
EventStream
71+
.periodic(5000)
72+
.flatMapSwitch: _ =>
73+
FetchStream.get("/api/stats").map(upickle.default.read[Stats](_))
74+
--> serverStats.someWriter
75+
6876
val cancelButton =
6977
button(
7078
"Cancel",
@@ -173,7 +181,8 @@ case class JobAttributes(
173181
),
174182
errorStatus,
175183
trainingStatus
176-
)
184+
),
185+
div(child.maybe <-- serverStats.signal.map(_.map(renderStats)))
177186
),
178187
onSubmit.preventDefault.flatMapTo(
179188
jobAttributes.now() match
@@ -234,6 +243,7 @@ case class JobAttributes(
234243
submitForm,
235244
updateJob --> result,
236245
resetError,
246+
updateStats,
237247
attrsSignal --> jobAttributes.someWriter,
238248
text.signal --> SaveState.saveString(SaveState.TEXT_KEY),
239249
generations.signal.map(_.toString) --> SaveState.saveString(
@@ -265,3 +275,15 @@ case class JobAttributes(
265275
app
266276
)
267277
end hello
278+
279+
def renderStats(stats: Stats) = div(
280+
p(strong("Server: "), stats.serverType),
281+
p(
282+
if stats.jobs.nonEmpty then strong("Active jobs") else em("No active jobs")
283+
),
284+
ul(
285+
cls := "text-xs",
286+
stats.jobs.map: job =>
287+
li(cls := "ml-2", strong(job.id.toString), " : ", job.hearbeat)
288+
)
289+
)

shared/protocol.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ enum JobProgress derives ReadWriter:
1818
generations: Int
1919
)
2020

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

23+
case class Stats(
24+
serverType: String,
25+
jobs: List[JobSummary]
26+
) derives ReadWriter

0 commit comments

Comments
 (0)