Skip to content

Commit

Permalink
WIP try to add progress polling
Browse files Browse the repository at this point in the history
  • Loading branch information
adrw committed Jan 12, 2025
1 parent a96263e commit 9b28913
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// package app.cash.backfila.ui
//
// class ProgressUpdater {
// fun backfillProgress(backfillName: String, numerator: Number, denominator: Number): String {
// println("Backfill $backfillName is ${numerator.toDouble() / denominator.toDouble() * 100}% complete")
// }
// }

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class DashboardPageLayout @Inject constructor(
type = "module"
src = "/static/js/autocomplete_controller.js"
}
script {
type = "module"
src = "/static/js/auto_reload_controller.js"
}
script {
type = "module"
src = "/static/js/search_bar_controller.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app.cash.backfila.ui.components

import kotlin.math.round
import kotlinx.html.TagConsumer
import kotlinx.html.div
import kotlinx.html.style

fun TagConsumer<*>.ProgressBarReloader(numerator: Number, denominator: Number) {
div("w-full bg-gray-200 rounded-full dark:bg-gray-700") {
val percentComplete = if (numerator.toInt() == 0) 0 else round((numerator.toDouble() / denominator.toDouble()) * 100)
// Don't show blue sliver for 0%, just show the gray empty bar
val showPartialBarStyle = if (percentComplete.toInt() != 0) "bg-blue-600" else ""
div(
"$showPartialBarStyle text-xs font-medium text-blue-100 text-center p-0.5 leading-none rounded-full",
) {
style = "width: ${if (percentComplete.toInt() == 0) 100 else percentComplete}%"
+"""$percentComplete%"""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import app.cash.backfila.ui.components.PageTitle
import java.net.HttpURLConnection
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.html.role
import kotlinx.html.ul
import kotlinx.html.div
import misk.scope.ActionScoped
import misk.security.authz.Authenticated
import misk.tokens.TokenGenerator
import misk.web.Get
import misk.web.HttpCall
import misk.web.PathParam
import misk.web.QueryParam
import misk.web.Response
Expand All @@ -23,9 +25,15 @@ import okhttp3.Headers

@Singleton
class ServiceShowAction @Inject constructor(
private val getBackfillRunsAction: GetBackfillRunsAction,
private val clientHttpCall: ActionScoped<HttpCall>,
private val dashboardPageLayout: DashboardPageLayout,
private val getBackfillRunsAction: GetBackfillRunsAction,
private val tokenGenerator: TokenGenerator,
) : WebAction {
private val path by lazy {
clientHttpCall.get().url.encodedPath
}

@Get(PATH)
@ResponseContentType(MediaTypes.TEXT_HTML)
@Authenticated(capabilities = ["users"])
Expand All @@ -50,15 +58,18 @@ class ServiceShowAction @Inject constructor(
val htmlResponseBody = dashboardPageLayout.newBuilder()
.title("$label | Backfila")
.buildHtmlResponseBody {
PageTitle("Service", label)
div {
attributes["data-controller"] = "auto-reload"
attributes["data-auto-reload-target"] = "frame"

// TODO Add completed table
// TODO Add deleted support?
BackfillsTable(true, backfillRuns.running_backfills)
BackfillsTable(false, backfillRuns.paused_backfills)
PageTitle("Service", label)

ul("space-y-3") {
role = "list"
// TODO Add completed table
// TODO Add deleted support?
// turbo_frame(id = "backfill-tables", src = path.replace("services", "services/progress/${tokenGenerator.generate()}")) {
BackfillsTable(true, backfillRuns.running_backfills)
BackfillsTable(false, backfillRuns.paused_backfills)
// }
}
}

Expand Down
41 changes: 41 additions & 0 deletions service/src/main/resources/web/static/js/auto_reload_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Application, Controller } from "../cache/stimulus/3.1.0/stimulus.min.js";
window.Stimulus = Application.start();

// Hard reload
Stimulus.register("auto-reload", class extends Controller {
connect() {
console.log("Auto-reload connected...");
this.reloadInterval = setInterval(() => {
console.log("Reloading page...");
window.location.reload();
}, 5000); // 5000 milliseconds = 1 second
}

disconnect() {
console.log("Clearing auto-reload interval...");
clearInterval(this.reloadInterval);
}
});


// TODO fix soft-reload
//Stimulus.register("auto-reload", class extends Controller {
// static targets = ["frame"];
//
// connect() {
// console.log("Auto-reload connected...");
// this.reloadInterval = setInterval(() => {
// console.log("Reloading Turbo Frame...");
// this.reloadFrame();
// }, 5000); // 5000 milliseconds = 5 seconds
// }
//
// disconnect() {
// console.log("Clearing auto-reload interval...");
// clearInterval(this.reloadInterval);
// }
//
// reloadFrame() {
// this.frameTarget.src = this.frameTarget.src;
// }
//});

0 comments on commit 9b28913

Please sign in to comment.