adding import count to posts

This commit is contained in:
Jakub Doka 2024-11-23 00:06:25 +01:00
parent 0aa355695a
commit b12579ff65
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
5 changed files with 36 additions and 12 deletions

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" height="18px" viewBox="0 -960 960 960" width="24px" fill="#e8eaed"><path d="M480-320 280-520l56-58 104 104v-326h80v326l104-104 56 58-200 200ZM240-160q-33 0-56.5-23.5T160-240v-120h80v120h480v-120h80v120q0 33-23.5 56.5T720-160H240Z"/></svg>

After

Width:  |  Height:  |  Size: 279 B

View file

@ -44,10 +44,11 @@ div.preview {
}
}
div.stats {
div.stat {
display: flex;
gap: var(--small-gap);
}
margin: var(--small-gap) 0px;
}
form {

View file

@ -253,6 +253,7 @@ impl Post {
name: r.get(1)?,
timestamp: r.get(2)?,
code: r.get(3)?,
imports: r.get(4)?,
..Default::default()
})
}
@ -322,13 +323,12 @@ impl fmt::Display for Post {
name
</span>
<span apply="timestamp">timestamp</span>
</div>
<div class="stats">
for (name, count) in "inps runs deps".split(' ')
for (name, count) in [include_str!("icons/download.svg"), "runs", "deps"]
.iter()
.zip([imports, runs, dependencies])
.filter(|(_, &c)| c != 0)
{
name ": "<span>count</span>
<div class="stat">!name count</div>
}
</div>
<pre apply="fmt">code</pre>
@ -759,7 +759,18 @@ mod db {
get_session: "SELECT username, expiration FROM session WHERE id = ?",
get_user_posts: "SELECT author, name, timestamp, code FROM post WHERE author = ?
ORDER BY timestamp DESC",
get_pots_before: "SELECT author, name, timestamp, code FROM post WHERE timestamp < ?",
// TODO: we might want to cache the recursive queries
get_pots_before: "SELECT author, name, timestamp, code, (
WITH RECURSIVE roots(name, author, code) AS (
SELECT name, author, code FROM post WHERE name = outher.name AND author = outher.author
UNION
SELECT post.name, post.author, post.code FROM
post JOIN import ON post.name = import.from_name
AND post.author = import.from_author
JOIN roots ON import.to_name = roots.name
AND import.to_author = roots.author
) SELECT (count(*) - 1) FROM roots
) AS imports FROM post AS outher WHERE timestamp < ?",
create_post: "INSERT INTO post (name, author, timestamp, code) VALUES(?, ?, ?, ?)",
fetch_deps: "
WITH RECURSIVE roots(name, author, code) AS (
@ -800,8 +811,22 @@ mod db {
}
pub fn init() {
const SCHEMA_VERSION: usize = 0;
const MIGRATIONS: &[&str] = &[include_str!("migrations/1.sql")];
let db = rusqlite::Connection::open("db.sqlite").unwrap();
db.execute_batch(include_str!("schema.sql")).unwrap();
let schema_version =
db.pragma_query_value(None, "user_version", |v| v.get::<_, usize>(0)).unwrap();
if schema_version != SCHEMA_VERSION {
for &mig in &MIGRATIONS[schema_version..] {
db.execute_batch(mig).expect(mig);
}
db.pragma_update(None, "user_version", SCHEMA_VERSION).unwrap();
}
Queries::new(&db);
}
}

View file

@ -0,0 +1 @@

View file

@ -9,11 +9,7 @@ use {
PLoc, Sig, Types,
},
alloc::{borrow::ToOwned, vec::Vec},
core::{
mem,
ops::{Range, RangeBounds},
usize,
},
core::{mem, ops::Range},
hbbytecode::{self as instrs},
};