From 8ab45c982a5d1025764fcb42156e564f1d8ca4a0 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 8 Feb 2025 15:53:25 +0100 Subject: [PATCH] Display current inputs for flakes --- src/bin/web.rs | 1 + src/storage.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/templates.rs | 2 ++ templates/flake.html | 18 ++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/bin/web.rs b/src/bin/web.rs index ee47dd5..36cc7a0 100644 --- a/src/bin/web.rs +++ b/src/bin/web.rs @@ -125,6 +125,7 @@ async fn route_flake( Ok(render_template(&FlakeTemplate { uri: uri.clone(), revisions: state.storage.revisions_from_flake(&uri).await?, + current_inputs: state.storage.current_inputs_for_flake(&uri).await?, })?) } diff --git a/src/storage.rs b/src/storage.rs index f72b7ea..be3a63d 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -105,6 +105,31 @@ impl Storage { .await .context("Failed to fetch data from database") } + + pub async fn current_inputs_for_flake(&self, flake_uri: &str) -> Result> { + sqlx::query_as(" + + SELECT + flake_revisions.revision_uri, + MAX(flake_revisions.last_modified) AS last_modified, + flake_revisions_inputs.input_name, + flake_revisions_inputs.uri + FROM + flake_revisions + LEFT JOIN + flake_revisions_inputs + ON + flake_revisions.revision_uri = flake_revisions_inputs.flake_revision_uri + WHERE + flake_revisions.uri = ? + GROUP BY + flake_revisions_inputs.input_name + ") + .bind(&flake_uri) + .fetch_all(&self.db) + .await + .context("Failed to fetch data from database") + } } #[derive(FromRow)] @@ -117,6 +142,24 @@ pub struct FlakeRevisionRow { pub last_modified: Option, } +#[derive(FromRow)] +pub struct InputForFlakeRow { + pub revision_uri: String, + pub last_modified: Option, + pub input_name: String, + pub uri: Option, +} + +impl InputForFlakeRow { + pub fn flake_link(&self ) -> String { + match &self.uri { + Some(uri) => format!("/f/{}", urlencode(&uri)), + None => String::from("#"), + } + } +} + + #[derive(FromRow)] pub struct RevisionListModel { pub revision_uri: String, diff --git a/src/templates.rs b/src/templates.rs index d09e236..5540d88 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -5,6 +5,7 @@ use crate::{ storage::{ FlakeUri, InputModel, + InputForFlakeRow, RevisionListModel, }, }; @@ -25,6 +26,7 @@ pub struct FlakesTemplate { pub struct FlakeTemplate { pub uri: String, pub revisions: Vec, + pub current_inputs: Vec, } #[derive(Template)] diff --git a/templates/flake.html b/templates/flake.html index 5a9fb5d..1b318a9 100644 --- a/templates/flake.html +++ b/templates/flake.html @@ -14,5 +14,23 @@
  • {{ revision.revision_uri }} ({{ revision.last_modified }})
  • {% endfor %} + +

    Current Inputs

    + +
      +{% for input in current_inputs %} +
    • + {{ input.input_name }} +
        + {% match input.uri %} + {% when Some with (uri) %} +
      • Flake: {{ uri }}
      • + {% when None %} + {% endmatch %} +
      +
    • +{% endfor %} +
    + {% endblock %}