From 77ed21e32b8f23b99d0a7f3731838985a25e74a4 Mon Sep 17 00:00:00 2001
From: zowoq <59103226+zowoq@users.noreply.github.com>
Date: Sat, 26 Oct 2024 18:24:59 +1000
Subject: [PATCH] nixpkgs-update: add queue

---
 docs/update-bot.md               |  1 +
 hosts/build02/nixpkgs-update.nix | 24 ++++++++++
 hosts/build02/update_queue.py    | 78 ++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+)
 create mode 100644 hosts/build02/update_queue.py

diff --git a/docs/update-bot.md b/docs/update-bot.md
index a691c03..2407570 100644
--- a/docs/update-bot.md
+++ b/docs/update-bot.md
@@ -3,4 +3,5 @@
 - Logs: [https://nixpkgs-update-logs.nix-community.org](https://nixpkgs-update-logs.nix-community.org)
 - Nixpkgs Pull Requests: [https://github.com/NixOS/nixpkgs/pulls?q=is%3Apr+author%3Ar-ryantm](https://github.com/NixOS/nixpkgs/pulls?q=is%3Apr+author%3Ar-ryantm)
 - Matrix: [https://matrix.to/#/#nixpkgs-update:nixos.org](https://matrix.to/#/#nixpkgs-update:nixos.org)
+- Queue: [https://nixpkgs-update-logs.nix-community.org/~supervisor/queue.html](https://nixpkgs-update-logs.nix-community.org/~supervisor/queue.html)
 - Source: [https://github.com/nix-community/nixpkgs-update](https://github.com/nix-community/nixpkgs-update)
diff --git a/hosts/build02/nixpkgs-update.nix b/hosts/build02/nixpkgs-update.nix
index 68e833a..87f8d2e 100644
--- a/hosts/build02/nixpkgs-update.nix
+++ b/hosts/build02/nixpkgs-update.nix
@@ -191,6 +191,30 @@ in
     '';
   };
 
+  systemd.services.nixpkgs-update-queue = {
+    after = [ config.systemd.services.nixpkgs-update-supervisor.name ];
+    wantedBy = [ config.systemd.targets.multi-user.name ];
+
+    serviceConfig = {
+      Type = "simple";
+      User = "r-ryantm";
+      Group = "r-ryantm";
+      Restart = "on-failure";
+      RestartSec = "5s";
+      LogsDirectory = "nixpkgs-update/";
+      LogsDirectoryMode = "755";
+      RuntimeDirectory = "nixpkgs-update-queue";
+      RuntimeDirectoryMode = "755";
+    };
+
+    path = [ pkgs.python3 ];
+
+    script = ''
+      cd "$LOGS_DIRECTORY/~supervisor"
+      python3 ${./update_queue.py}
+    '';
+  };
+
   systemd.services.nixpkgs-update-delete-old-logs = {
     startAt = "daily";
     # delete logs older than 18 months, delete worker logs older than 3 months, delete empty directories
diff --git a/hosts/build02/update_queue.py b/hosts/build02/update_queue.py
new file mode 100644
index 0000000..649054e
--- /dev/null
+++ b/hosts/build02/update_queue.py
@@ -0,0 +1,78 @@
+import sqlite3
+import time
+from datetime import datetime, timezone
+
+
+def get_db_connection(db_path):
+    conn = sqlite3.connect(db_path)
+    conn.row_factory = sqlite3.Row
+    return conn
+
+
+def fetch_queue_data(conn):
+    query = """
+    SELECT
+        ROW_NUMBER() OVER (ORDER BY last_started ASC) AS number,
+        attr_path,
+        payload
+    FROM
+        'queue'
+    ORDER BY
+        last_started ASC
+    """
+    return conn.execute(query).fetchall()
+
+
+def generate_html_table(rows):
+    table_rows = "".join(
+        f"""
+        <tr>
+            <td>{row['number']}</td>
+            <td>{row['attr_path']}</td>
+            <td>{row['payload']}</td>
+        </tr>
+        """
+        for row in rows
+    )
+    return table_rows
+
+
+def export_html(db_path):
+    with get_db_connection(db_path) as conn:
+        results = fetch_queue_data(conn)
+
+    generated = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
+
+    html = f"""
+    <html>
+    <head>
+        <title>nixpkgs-update queue</title>
+    </head>
+    <body>
+        <h1>nixpkgs-update queue</h1>
+        <h3>this page is updated every 15 minutes, last updated: {generated}</h3>
+        <table>
+            <thead>
+                <tr>
+                    <th>number</th>
+                    <th>attribute path</th>
+                    <th>payload</th>
+                </tr>
+            </thead>
+            <tbody>
+                {generate_html_table(results)}
+            </tbody>
+        </table>
+    </body>
+    </html>
+    """
+
+    with open("queue.html", "w") as f:
+        f.write(html)
+
+
+if __name__ == "__main__":
+    DB_PATH = "state.db"
+    while True:
+        export_html(DB_PATH)
+        time.sleep(15 * 60)