From b6adc918c61d6e0063a4b05d1221d42787312c1c Mon Sep 17 00:00:00 2001
From: clerie <git@clerie.de>
Date: Sun, 9 Feb 2025 21:56:06 +0100
Subject: [PATCH] Unify database structs

---
 src/storage.rs       | 84 ++++++++++++++++++++------------------------
 src/templates.rs     | 17 +++++----
 templates/flake.html |  2 +-
 3 files changed, 47 insertions(+), 56 deletions(-)

diff --git a/src/storage.rs b/src/storage.rs
index 297b52d..5d815f3 100644
--- a/src/storage.rs
+++ b/src/storage.rs
@@ -28,7 +28,7 @@ impl Storage {
         })
     }
 
-    pub async fn flakes(&self) -> Result<Vec<FlakeUri>> {
+    pub async fn flakes(&self) -> Result<Vec<FlakeRow>> {
         sqlx::query_as("
             SELECT
                 flake_uri
@@ -40,11 +40,15 @@ impl Storage {
             .context("Failed to fetch data from database")
     }
 
-    pub async fn revisions_from_flake(&self, uri: &str) -> Result<Vec<RevisionListModel>> {
+    pub async fn revisions_from_flake(&self, uri: &str) -> Result<Vec<RevisionRow>> {
         sqlx::query_as("
             SELECT
                 revision_uri,
-                last_modified
+                flake_uri,
+                nix_store_path,
+                nar_hash,
+                last_modified,
+                tracker_last_scanned
             FROM revisions
             WHERE flake_uri = ?
             ORDER BY last_modified DESC
@@ -55,7 +59,7 @@ impl Storage {
             .context("Failed to fetch data from database")
     }
 
-    pub async fn inputs_for_revision(&self, revision_uri: &str) -> Result<Vec<InputModel>> {
+    pub async fn inputs_for_revision(&self, revision_uri: &str) -> Result<Vec<InputRow>> {
         sqlx::query_as("
             SELECT
                 revision_uri,
@@ -74,7 +78,7 @@ impl Storage {
             .context("Failed to fetch data from database")
     }
 
-    pub async fn input_of_for_revision(&self, revision_uri: &str) -> Result<Vec<InputModel>> {
+    pub async fn input_of_for_revision(&self, revision_uri: &str) -> Result<Vec<InputRow>> {
         sqlx::query_as("
             SELECT
                 revision_uri,
@@ -93,14 +97,17 @@ impl Storage {
             .context("Failed to fetch data from database")
     }
 
-    pub async fn current_inputs_for_flake(&self, flake_uri: &str) -> Result<Vec<InputForFlakeRow>> {
+    pub async fn current_inputs_for_flake(&self, flake_uri: &str) -> Result<Vec<InputRow>> {
         sqlx::query_as("
 
             SELECT
                 revisions.revision_uri,
-                MAX(revisions.last_modified) AS last_modified,
                 inputs.input_name,
-                inputs.locked_flake_uri
+                inputs.locked_revision_uri,
+                inputs.locked_flake_uri,
+                inputs.locked_nar_hash,
+                inputs.last_modified,
+                MAX(revisions.last_modified)
             FROM
                 revisions
             LEFT JOIN
@@ -120,57 +127,30 @@ impl Storage {
 }
 
 #[derive(FromRow)]
-pub struct FlakeRevisionRow {
+pub struct RevisionRow {
     pub revision_uri: String,
     pub flake_uri: Option<String>,
     pub nix_store_path: Option<String>,
     pub nar_hash: Option<String>,
     pub last_modified: Option<i64>,
+    pub tracker_last_scanned: Option<i64>,
 }
 
-#[derive(FromRow)]
-pub struct InputForFlakeRow {
-    pub revision_uri: String,
-    pub last_modified: Option<i64>,
-    pub input_name: String,
-    pub locked_flake_uri: Option<String>,
-}
+impl RevisionRow {
+    pub fn revision_link(&self) -> String {
+        format!("/r/{}", urlencode(&self.revision_uri))
+    }
 
-impl InputForFlakeRow {
-    pub fn locked_flake_link(&self ) -> String {
-        match &self.locked_flake_uri {
-            Some(locked_flake_uri) => format!("/f/{}", urlencode(&locked_flake_uri)),
+    pub fn flake_link(&self) -> String {
+        match &self.flake_uri {
+            Some(flake_uri) => format!("/f/{}", urlencode(&flake_uri)),
             None => String::from("#"),
         }
     }
 }
 
-
 #[derive(FromRow)]
-pub struct RevisionListModel {
-    pub revision_uri: String,
-    pub last_modified: i64,
-}
-
-impl RevisionListModel {
-    pub fn revision_link(&self ) -> String {
-        format!("/r/{}", urlencode(&self.revision_uri))
-    }
-}
-
-#[derive(FromRow)]
-pub struct FlakeUri {
-    pub flake_uri: String,
-}
-
-impl FlakeUri {
-    pub fn flake_link(&self ) -> String {
-        format!("/f/{}", urlencode(&self.flake_uri))
-    }
-}
-
-#[derive(FromRow)]
-pub struct InputModel {
+pub struct InputRow {
     pub revision_uri: String,
     pub input_name: String,
     pub locked_revision_uri: Option<String>,
@@ -179,7 +159,7 @@ pub struct InputModel {
     pub last_modified: Option<i64>,
 }
 
-impl InputModel {
+impl InputRow {
     pub fn revision_link(&self) -> String {
         format!("/r/{}", urlencode(&self.revision_uri))
     }
@@ -198,3 +178,15 @@ impl InputModel {
         }
     }
 }
+
+#[derive(FromRow)]
+pub struct FlakeRow {
+    pub flake_uri: String,
+}
+
+impl FlakeRow {
+    pub fn flake_link(&self ) -> String {
+        format!("/f/{}", urlencode(&self.flake_uri))
+    }
+}
+
diff --git a/src/templates.rs b/src/templates.rs
index 5540d88..2f96517 100644
--- a/src/templates.rs
+++ b/src/templates.rs
@@ -3,10 +3,9 @@ use askama::{
 };
 use crate::{
     storage::{
-        FlakeUri,
-        InputModel,
-        InputForFlakeRow,
-        RevisionListModel,
+        FlakeRow,
+        InputRow,
+        RevisionRow,
     },
 };
 
@@ -18,21 +17,21 @@ pub struct IndexTemplate {
 #[derive(Template)]
 #[template(path = "flakes.html")]
 pub struct FlakesTemplate {
-    pub flakes: Vec<FlakeUri>,
+    pub flakes: Vec<FlakeRow>,
 }
 
 #[derive(Template)]
 #[template(path = "flake.html")]
 pub struct FlakeTemplate {
     pub uri: String,
-    pub revisions: Vec<RevisionListModel>,
-    pub current_inputs: Vec<InputForFlakeRow>,
+    pub revisions: Vec<RevisionRow>,
+    pub current_inputs: Vec<InputRow>,
 }
 
 #[derive(Template)]
 #[template(path = "revision.html")]
 pub struct RevisionTemplate {
     pub revision_uri: String,
-    pub inputs: Vec<InputModel>,
-    pub input_of: Vec<InputModel>,
+    pub inputs: Vec<InputRow>,
+    pub input_of: Vec<InputRow>,
 }
diff --git a/templates/flake.html b/templates/flake.html
index e97286b..93cf39f 100644
--- a/templates/flake.html
+++ b/templates/flake.html
@@ -11,7 +11,7 @@
 
 <ul>
 {% for revision in revisions %}
-	<li><a href="{{ revision.revision_link() }}">{{ revision.revision_uri }}</a> ({{ revision.last_modified }})</li>
+<li><a href="{{ revision.revision_link() }}">{{ revision.revision_uri }}</a> {% match revision.last_modified %}{% when Some with (last_modified) %}({{ last_modified }}){% when None %}{% endmatch %}</li>
 {% endfor %}
 </ul>