OXYGENHUB
# df68c58de947 oxygenhub / crates / hub / src / web / flow.rs
18.1 KiB · Rust · last changed Run pipeline and Re-run: runs that move no bookmark 1 month ago source blame history raw
1
//! The Flow tab (hub spec §13.2): runs of a repository, and a run's job
2
//! graph with live status and logs.
3
4
use super::settings::{csrf, require};
5
use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell};
6
use crate::auth::{Access, Role, Viewer};
7
use crate::db::{self, flow as fdb};
8
use crate::Shared;
9
use askama::Template;
10
use axum::extract::{Form, Path, Query, State};
11
use axum::response::{IntoResponse, Redirect, Response};
12
use axum::Json;
13
use serde::Deserialize;
14
15
pub struct RunRow {
16
    pub r: fdb::Run,
17
    pub duration: String,
18
    pub jobs: Vec<(String, String)>,
19
}
20
21
#[derive(Template)]
22
#[template(path = "flow_runs.html")]
23
pub struct RunsPage {
24
    shell: Shell,
25
    repo: RepoShell,
26
    runs: Vec<RunRow>,
27
    enabled: bool,
28
    older: Option<i64>,
29
    /// Writers may start runs: the bookmarks to offer, default first.
30
    can_run: bool,
31
    bookmarks: Vec<String>,
32
    error: Option<String>,
33
}
34
35
#[derive(Deserialize)]
36
pub struct RunsQuery {
37
    before: Option<i64>,
38
    /// Why a "Run pipeline" or "Re-run" did not start.
39
    error: Option<String>,
40
}
41
42
const PER_PAGE: i64 = 30;
43
44
pub async fn runs(
45
    State(app): State<Shared>,
46
    viewer: Viewer,
47
    Path((owner, name)): Path<(String, String)>,
48
    Query(q): Query<RunsQuery>,
49
) -> Result<Page<RunsPage>, Error> {
50
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow"), &viewer);
51
    let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
52
    let list = fdb::runs(&app.db, row.id, PER_PAGE + 1, q.before).await.map_err(|e| Error::from_db(shell.clone(), e))?;
53
    let older = (list.len() as i64 > PER_PAGE).then(|| list[PER_PAGE as usize - 1].id);
54
    let mut runs = Vec::new();
55
    for r in list.into_iter().take(PER_PAGE as usize) {
56
        let jobs = fdb::jobs(&app.db, r.id).await.unwrap_or_default();
57
        let duration = match r.finished_ms {
58
            Some(f) => crate::flow::human_ms(f - r.created_ms),
59
            None if r.live() => crate::flow::human_ms(db::now_ms() - r.created_ms),
60
            None => "–".into(),
61
        };
62
        runs.push(RunRow { jobs: jobs.into_iter().map(|j| (j.name, j.status)).collect(), duration, r });
63
    }
64
    let enabled = crate::flow::enabled(&app, &row);
65
    let can_run = enabled && viewer.account().is_some() && role >= Role::Write;
66
    let bookmarks = if can_run { bookmark_names(&app, &row).await } else { vec![] };
67
    Ok(Page(RunsPage {
68
        shell,
69
        repo: RepoShell::new(&app, &row, "flow").with_role(role),
70
        runs,
71
        enabled,
72
        older,
73
        can_run,
74
        bookmarks,
75
        error: q.error,
76
    }))
77
}
78
79
/// The repository's bookmarks, its default first.
80
async fn bookmark_names(app: &Shared, row: &db::repos::RepoRow) -> Vec<String> {
81
    let Some(repo) = crate::repos::RepoName::new(&row.owner, &row.name) else { return vec![] };
82
    let mut names: Vec<String> = app.repos.read(&repo, |r, _| Ok(r.view()?.bookmarks.keys().cloned().collect())).await.unwrap_or_default();
83
    names.sort_by_key(|n| (n != &row.default_bookmark, n.clone()));
84
    names
85
}
86
87
#[derive(Deserialize)]
88
pub struct StartForm {
89
    #[serde(rename = "_csrf")]
90
    csrf_token: String,
91
    bookmark: String,
92
}
93
94
/// `POST /{owner}/{name}/flow/start`: "Run pipeline" on a bookmark.
95
pub async fn start(
96
    State(app): State<Shared>,
97
    viewer: Viewer,
98
    Path((owner, name)): Path<(String, String)>,
99
    Form(f): Form<StartForm>,
100
) -> Result<Response, Error> {
101
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow"), &viewer);
102
    let me = require(&viewer, &shell)?;
103
    let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?;
104
    csrf(&viewer, &f.csrf_token, &shell)?;
105
    Ok(match crate::flow::run_bookmark(&app, &row, f.bookmark.trim(), &me.account.username).await {
106
        Ok(n) => {
107
            db::accounts::audit(&app.db, Some(&me.account), "flow.run", &format!("{owner}/{name}"), &format!("#{n} {}", f.bookmark.trim()))
108
                .await
109
                .ok();
110
            Redirect::to(&format!("/{owner}/{name}/flow/{n}")).into_response()
111
        }
112
        Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(),
113
    })
114
}
115
116
#[derive(Deserialize)]
117
pub struct RerunForm {
118
    #[serde(rename = "_csrf")]
119
    csrf_token: String,
120
}
121
122
/// `POST /{owner}/{name}/flow/{number}/rerun`.
123
pub async fn rerun(
124
    State(app): State<Shared>,
125
    viewer: Viewer,
126
    Path((owner, name, number)): Path<(String, String, i64)>,
127
    Form(f): Form<RerunForm>,
128
) -> Result<Response, Error> {
129
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{number}"), &viewer);
130
    let me = require(&viewer, &shell)?;
131
    let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?;
132
    csrf(&viewer, &f.csrf_token, &shell)?;
133
    let run = fdb::run(&app.db, row.id, number)
134
        .await
135
        .map_err(|e| Error::from_db(shell.clone(), e))?
136
        .ok_or_else(|| Error::not_found(shell.clone()))?;
137
    Ok(match crate::flow::rerun(&app, &row, &run, &me.account.username).await {
138
        Ok(n) => {
139
            db::accounts::audit(&app.db, Some(&me.account), "flow.rerun", &format!("{owner}/{name}"), &format!("#{n} of #{number}"))
140
                .await
141
                .ok();
142
            Redirect::to(&format!("/{owner}/{name}/flow/{n}")).into_response()
143
        }
144
        Err(msg) => Redirect::to(&format!("/{owner}/{name}/flow?error={}", urlencoding(&msg))).into_response(),
145
    })
146
}
147
148
/// Percent-encode a message for a query string.
149
fn urlencoding(s: &str) -> String {
150
    s.bytes()
151
        .map(|b| match b {
152
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => (b as char).to_string(),
153
            _ => format!("%{b:02X}"),
154
        })
155
        .collect()
156
}
157
158
/// A job placed in the graph.
159
pub struct Node {
160
    pub j: fdb::Job,
161
    pub x: i64,
162
    pub y: i64,
163
    pub duration: String,
164
}
165
166
pub struct Edge {
167
    pub d: String,
168
    pub status: String,
169
}
170
171
pub struct Graph {
172
    pub nodes: Vec<Node>,
173
    pub edges: Vec<Edge>,
174
    pub width: i64,
175
    pub height: i64,
176
}
177
178
pub const NODE_W: i64 = 200;
179
pub const NODE_H: i64 = 48;
180
const COL_GAP: i64 = 72;
181
const ROW_GAP: i64 = 18;
182
const PAD: i64 = 12;
183
184
/// Columns by dependency depth, jobs in each column in name order.
185
pub fn layout(jobs: Vec<fdb::Job>) -> Graph {
186
    let mut by_depth: std::collections::BTreeMap<i64, Vec<fdb::Job>> = Default::default();
187
    for j in jobs {
188
        by_depth.entry(j.depth).or_default().push(j);
189
    }
190
    let rows = by_depth.values().map(Vec::len).max().unwrap_or(0) as i64;
191
    let height = PAD * 2 + rows * NODE_H + (rows - 1).max(0) * ROW_GAP;
192
    let mut nodes = Vec::new();
193
    for (col, (_, mut js)) in by_depth.into_iter().enumerate() {
194
        js.sort_by(|a, b| a.name.cmp(&b.name));
195
        let n = js.len() as i64;
196
        let column_h = n * NODE_H + (n - 1).max(0) * ROW_GAP;
197
        let top = (height - column_h) / 2;
198
        for (i, j) in js.into_iter().enumerate() {
199
            let duration = crate::flow::duration(&j);
200
            nodes.push(Node { x: PAD + col as i64 * (NODE_W + COL_GAP), y: top + i as i64 * (NODE_H + ROW_GAP), duration, j });
201
        }
202
    }
203
    let mut edges = Vec::new();
204
    for n in &nodes {
205
        for d in n.j.depends_on() {
206
            if let Some(from) = nodes.iter().find(|m| m.j.name == d) {
207
                let (x1, y1) = (from.x + NODE_W, from.y + NODE_H / 2);
208
                let (x2, y2) = (n.x, n.y + NODE_H / 2);
209
                let mid = (x1 + x2) / 2;
210
                edges.push(Edge { d: format!("M{x1},{y1} C{mid},{y1} {mid},{y2} {x2},{y2}"), status: from.j.status.clone() });
211
            }
212
        }
213
    }
214
    let cols = nodes.iter().map(|n| n.x).max().map(|x| x + NODE_W + PAD).unwrap_or(PAD * 2);
215
    Graph { nodes, edges, width: cols, height: height.max(NODE_H + PAD * 2) }
216
}
217
218
pub fn glyph(status: &str) -> &'static str {
219
    match status {
220
        "passed" => "✓",
221
        "failed" => "✕",
222
        "running" => "●",
223
        "skipped" => "⤼",
224
        "error" => "!",
225
        _ => "○",
226
    }
227
}
228
229
pub struct JobLog {
230
    pub j: fdb::Job,
231
    pub lines: Vec<fdb::LogLine>,
232
    pub duration: String,
233
    /// The last line shown; the live view asks for what follows.
234
    pub last_seq: i64,
235
}
236
237
#[derive(Template)]
238
#[template(path = "flow_run.html")]
239
pub struct RunPage {
240
    shell: Shell,
241
    repo: RepoShell,
242
    run: fdb::Run,
243
    graph: Graph,
244
    logs: Vec<JobLog>,
245
    duration: String,
246
    /// Whether the viewer may re-run it.
247
    can_rerun: bool,
248
}
249
250
/// Lines of a job's log a page shows before "the rest is loaded as it comes".
251
const PAGE_LINES: i64 = 5000;
252
253
pub async fn run(
254
    State(app): State<Shared>,
255
    viewer: Viewer,
256
    Path((owner, name, id)): Path<(String, String, i64)>,
257
) -> Result<Page<RunPage>, Error> {
258
    let shell = Shell::new(&app, &format!("/{owner}/{name}/flow/{id}"), &viewer);
259
    let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
260
    let run = fdb::run(&app.db, row.id, id)
261
        .await
262
        .map_err(|e| Error::from_db(shell.clone(), e))?
263
        .ok_or_else(|| Error::not_found(shell.clone()))?;
264
    let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
265
    let mut logs = Vec::new();
266
    for j in &jobs {
267
        let lines = fdb::log(&app.db, j.id, 0, PAGE_LINES).await.unwrap_or_default();
268
        let last_seq = lines.last().map(|l| l.seq).unwrap_or(0);
269
        logs.push(JobLog { duration: crate::flow::duration(j), j: j.clone(), lines, last_seq });
270
    }
271
    let duration = match run.finished_ms {
272
        Some(f) => crate::flow::human_ms(f - run.created_ms),
273
        None => crate::flow::human_ms(db::now_ms() - run.created_ms),
274
    };
275
    let graph = layout(jobs);
276
    let can_rerun = !run.live() && viewer.account().is_some() && role >= Role::Write && crate::flow::enabled(&app, &row);
277
    Ok(Page(RunPage { shell, repo: RepoShell::new(&app, &row, "flow").with_role(role), run, graph, logs, duration, can_rerun }))
278
}
279
280
#[derive(Deserialize)]
281
pub struct LiveQuery {
282
    /// `job:seq,job:seq`: the last log line each open job already shows.
283
    after: Option<String>,
284
}
285
286
/// `GET /{owner}/{name}/flow/{id}/live`: statuses and new log lines, for the
287
/// run page while it is live.
288
pub async fn live(
289
    State(app): State<Shared>,
290
    viewer: Viewer,
291
    Path((owner, name, id)): Path<(String, String, i64)>,
292
    Query(q): Query<LiveQuery>,
293
) -> Result<Response, Error> {
294
    let shell = Shell::new(&app, "", &viewer);
295
    let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
296
    let run = fdb::run(&app.db, row.id, id)
297
        .await
298
        .map_err(|e| Error::from_db(shell.clone(), e))?
299
        .ok_or_else(|| Error::not_found(shell.clone()))?;
300
    let jobs = fdb::jobs(&app.db, run.id).await.map_err(|e| Error::from_db(shell.clone(), e))?;
301
    let after: std::collections::HashMap<i64, i64> = q
302
        .after
303
        .unwrap_or_default()
304
        .split(',')
305
        .filter_map(|p| p.split_once(':'))
306
        .filter_map(|(a, b)| Some((a.parse().ok()?, b.parse().ok()?)))
307
        .collect();
308
    let mut out = Vec::new();
309
    for j in &jobs {
310
        let lines = match after.get(&j.id) {
311
            Some(seq) => fdb::log(&app.db, j.id, *seq, 1000).await.unwrap_or_default(),
312
            None => vec![],
313
        };
314
        out.push(serde_json::json!({
315
            "id": j.id,
316
            "name": j.name,
317
            "status": j.status,
318
            "glyph": glyph(&j.status),
319
            "duration": crate::flow::duration(j),
320
            "exit_code": j.exit_code,
321
            "lines": lines.iter().map(|l| serde_json::json!({ "seq": l.seq, "t": l.t, "stream": l.stream, "text": l.text })).collect::<Vec<_>>(),
322
        }));
323
    }
324
    Ok(Json(serde_json::json!({ "status": run.status, "live": run.live(), "jobs": out })).into_response())
325
}
326
327
// ------------------------------------------------------------ state stamps
328
329
/// In rendered Markdown, the image path that is the viewed state's stamp.
330
pub const STAMP_IMAGE: &str = ".flow/stamp";
331
/// In rendered Markdown, the link path to the viewed state's run.
332
pub const RUN_LINK: &str = ".flow/run";
333
334
/// Resolve a ref to its state address, and whether the ref can move.
335
async fn state_of(app: &Shared, owner: &str, name: &str, r: &str) -> Result<(String, bool), crate::repos::RepoError> {
336
    let repo = crate::repos::RepoName::new(owner, name).ok_or(crate::repos::RepoError::NotFound)?;
337
    let spec = r.to_string();
338
    app.repos
339
        .read(&repo, move |repo, cache| {
340
            let t = crate::repos::view::resolve(repo, cache, &spec)?;
341
            let moves = matches!(t.kind, crate::repos::view::RefKind::Bookmark | crate::repos::view::RefKind::Tag);
342
            Ok((t.permalink().unwrap_or_default(), moves))
343
        })
344
        .await
345
}
346
347
/// `GET /{owner}/{name}/flow/stamp/{ref}` (hub spec §13.2).
348
pub async fn stamp(
349
    State(app): State<Shared>,
350
    viewer: Viewer,
351
    Path((owner, name, r)): Path<(String, String, String)>,
352
) -> Result<Response, Error> {
353
    let shell = Shell::new(&app, "", &viewer);
354
    let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
355
    let (heads, moves) = state_of(&app, &owner, &name, &r).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
356
    if moves && !heads.is_empty() {
357
        let mut res = axum::response::Redirect::to(&format!("/{owner}/{name}/flow/stamp/{heads}")).into_response();
358
        res.headers_mut().insert(axum::http::header::CACHE_CONTROL, axum::http::HeaderValue::from_static("no-cache"));
359
        return Ok(res);
360
    }
361
    let run = fdb::latest_for_heads(&app.db, row.id, &heads).await.map_err(|e| Error::from_db(shell.clone(), e))?;
362
    let jobs = match &run {
363
        Some(r) => fdb::jobs(&app.db, r.id).await.map_err(|e| Error::from_db(shell.clone(), e))?,
364
        None => vec![],
365
    };
366
    let svg = stamp_svg(&heads, run.as_ref(), &jobs);
367
    let cache = if run.as_ref().is_some_and(|r| !r.live()) { "public, max-age=3600" } else { "public, max-age=60" };
368
    Ok((
369
        [
370
            (axum::http::header::CONTENT_TYPE, "image/svg+xml; charset=utf-8"),
371
            (axum::http::header::CACHE_CONTROL, cache),
372
            (axum::http::header::CONTENT_SECURITY_POLICY, "default-src 'none'; style-src 'unsafe-inline'"),
373
        ],
374
        svg,
375
    )
376
        .into_response())
377
}
378
379
/// `GET /{owner}/{name}/flow/at/{ref}`: the run of the ref's state, or the Flow tab.
380
pub async fn at(
381
    State(app): State<Shared>,
382
    viewer: Viewer,
383
    Path((owner, name, r)): Path<(String, String, String)>,
384
) -> Result<Response, Error> {
385
    let shell = Shell::new(&app, "", &viewer);
386
    let (_repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
387
    let (heads, _) = state_of(&app, &owner, &name, &r).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
388
    let run = fdb::latest_for_heads(&app.db, row.id, &heads).await.map_err(|e| Error::from_db(shell.clone(), e))?;
389
    let to = match run {
390
        Some(r) => format!("/{owner}/{name}/flow/{}", r.number),
391
        None => format!("/{owner}/{name}/flow"),
392
    };
393
    Ok(axum::response::Redirect::to(&to).into_response())
394
}
395
396
fn xml(s: &str) -> String {
397
    s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;")
398
}
399
400
/// The stamp: the mark, one glyph per job, the status, the state and the
401
/// date; the same facts machine-readable in `<metadata>`. Fixed colours,
402
/// because an image does not follow the page's theme.
403
pub fn stamp_svg(heads: &str, run: Option<&fdb::Run>, jobs: &[fdb::Job]) -> String {
404
    const CH: f64 = 6.6; // IBM Plex Mono at 11px
405
    let colour = |s: &str| match s {
406
        "passed" => "#5fe3a1",
407
        "failed" | "error" => "#ff5e7a",
408
        "running" => "#00e0f0",
409
        _ => "#8592ad",
410
    };
411
    let short: String = heads.chars().take(12).collect();
412
    let (status, date) = match run {
413
        Some(r) => (r.status.as_str(), crate::render::time::absolute(r.created_ms as u64).chars().take(10).collect::<String>()),
414
        None => ("no run", String::new()),
415
    };
416
    let mut ordered: Vec<&fdb::Job> = jobs.iter().collect();
417
    ordered.sort_by(|a, b| (a.depth, &a.name).cmp(&(b.depth, &b.name)));
418
    let label = "OXYGENFLOW";
419
    let label_w = 14.0 + label.len() as f64 * 6.2 + 8.0;
420
    let glyphs_w = if ordered.is_empty() { 0.0 } else { ordered.len() as f64 * 10.0 + 6.0 };
421
    let tail = if date.is_empty() { format!("{status} · {short}") } else { format!("{status} · {short} · {date}") };
422
    let right_w = 8.0 + glyphs_w + tail.chars().count() as f64 * CH + 8.0;
423
    let w = (label_w + right_w).ceil();
424
    let mut glyphs = String::new();
425
    for (i, j) in ordered.iter().enumerate() {
426
        let x = label_w + 8.0 + i as f64 * 10.0 + 4.0;
427
        glyphs.push_str(&format!(
428
            r#"<text x="{x:.1}" y="15" fill="{}" text-anchor="middle">{}</text>"#,
429
            colour(&j.status),
430
            glyph(&j.status)
431
        ));
432
    }
433
    let meta = serde_json::json!({
434
        "heads": heads.split('+').filter(|h| !h.is_empty()).collect::<Vec<_>>(),
435
        "run": run.map(|r| r.number),
436
        "status": status,
437
        "jobs": ordered.iter().map(|j| serde_json::json!({ "name": j.name, "status": j.status, "receipt": j.receipt_hash })).collect::<Vec<_>>(),
438
    });
439
    let title = match run {
440
        Some(r) => format!("OxygenFlow run #{} {status} on state {short}", r.number),
441
        None => format!("OxygenFlow: no run for state {short}"),
442
    };
443
    format!(
444
        r##"<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="22" viewBox="0 0 {w} 22" role="img" aria-label="{t}"><title>{t}</title><metadata>{m}</metadata><rect width="{w}" height="22" rx="4" fill="#0e1526"/><rect width="{lw:.1}" height="22" rx="4" fill="#0b3f9e"/><rect x="{lx:.1}" width="4" height="22" fill="#0b3f9e"/><circle cx="9" cy="11" r="3.2" fill="none" stroke="#00f0ff" stroke-width="1.6"/><text x="16" y="15" fill="#ffffff" font-family="Barlow, Verdana, sans-serif" font-size="10.5" font-weight="700" letter-spacing=".6">{label}</text><g font-family="IBM Plex Mono, ui-monospace, monospace" font-size="11">{glyphs}<text x="{tx:.1}" y="15" fill="{sc}">{tail}</text></g><rect x=".5" y=".5" width="{wi}" height="21" rx="3.5" fill="none" stroke="#1f2b47"/></svg>"##,
445
        t = xml(&title),
446
        m = xml(&meta.to_string()),
447
        lw = label_w,
448
        lx = label_w - 4.0,
449
        tx = label_w + 8.0 + glyphs_w,
450
        sc = colour(status),
451
        tail = xml(&tail),
452
        wi = w - 1.0,
453
    )
454
}