Restructure db schema

The "commit_links" table is now called "commit_edges".

There is now a "metrics" table that run measurements have a foreign key
to. This provides canonical metric metadata and will speed up the
initial graph page (before any real data arrives). For now, it will be
overwritten with each new run, but more nuanced config options may be
added later.
This commit is contained in:
Joscha 2023-09-01 11:47:16 +02:00
parent 1bae83d116
commit a525e58211
18 changed files with 77 additions and 71 deletions

View file

@ -119,7 +119,7 @@ async fn insert_new_commits(
Ok(())
}
async fn insert_new_commit_links(
async fn insert_new_commit_edges(
conn: &mut SqliteConnection,
repo: &Repository,
new: &[ObjectId],
@ -132,7 +132,7 @@ async fn insert_new_commit_links(
// Commits *cough*linuxkernel*cough* may list the same parent
// multiple times, so we just ignore duplicates during insert.
sqlx::query!(
"INSERT OR IGNORE INTO commit_links (parent, child) VALUES (?, ?)",
"INSERT OR IGNORE INTO commit_edges (parent, child) VALUES (?, ?)",
parent,
child,
)
@ -142,7 +142,7 @@ async fn insert_new_commit_links(
// So the user has something to look at while importing big repos
if (i + 1) % 100000 == 0 {
info!("(2/2) Inserting links: {}/{}", i + 1, new.len());
info!("(2/2) Inserting edges: {}/{}", i + 1, new.len());
}
}
Ok(())
@ -214,7 +214,7 @@ async fn update_commit_tracked_status(conn: &mut SqliteConnection) -> somehow::R
tracked (hash) AS ( \
SELECT hash FROM refs WHERE tracked \
UNION \
SELECT parent FROM commit_links \
SELECT parent FROM commit_edges \
JOIN tracked ON hash = child \
), \
reachable (hash) AS ( \
@ -222,7 +222,7 @@ async fn update_commit_tracked_status(conn: &mut SqliteConnection) -> somehow::R
UNION \
SELECT hash FROM tracked \
UNION \
SELECT parent FROM commit_links \
SELECT parent FROM commit_edges \
JOIN reachable ON hash = child \
) \
UPDATE commits \
@ -273,7 +273,7 @@ pub async fn inner(db: &SqlitePool, repo: Repo) -> somehow::Result<()> {
// than if they were grouped by commit (insert commit and parents, then next
// commit and so on).
insert_new_commits(conn, &thread_local_repo, &new).await?;
insert_new_commit_links(conn, &thread_local_repo, &new).await?;
insert_new_commit_edges(conn, &thread_local_repo, &new).await?;
if repo_is_new {
mark_all_commits_as_old(conn).await?;
}

View file

@ -96,21 +96,21 @@ async fn save_work(
.await?;
}
for (idx, (source, text)) in run.output.into_iter().enumerate() {
// Hopefully we won't need more than 4294967296 output chunks per run :P
let idx = idx as u32;
for (line, (source, text)) in run.output.into_iter().enumerate() {
// Hopefully we won't need more than 4294967296 lines per run :P
let line = line as u32;
sqlx::query!(
"\
INSERT INTO run_output ( \
id, \
idx, \
line, \
source, \
text \
) \
VALUES (?, ?, ?, ?) \
",
run.id,
idx,
line,
source,
text,
)

View file

@ -50,7 +50,7 @@ pub async fn get_commit_by_hash(
let parents = sqlx::query!(
"\
SELECT hash, message, reachable FROM commits \
JOIN commit_links ON hash = parent \
JOIN commit_edges ON hash = parent \
WHERE child = ? \
ORDER BY reachable DESC, unixepoch(committer_date) ASC \
",
@ -64,7 +64,7 @@ pub async fn get_commit_by_hash(
let children = sqlx::query!(
"\
SELECT hash, message, reachable FROM commits \
JOIN commit_links ON hash = child \
JOIN commit_edges ON hash = child \
WHERE parent = ? \
ORDER BY reachable DESC, unixepoch(committer_date) ASC \
",

View file

@ -6,6 +6,7 @@ use futures::TryStreamExt;
use maud::html;
use serde::{Deserialize, Serialize};
use sqlx::{Acquire, SqlitePool};
use time::OffsetDateTime;
use crate::{
config::ServerConfig,
@ -55,10 +56,9 @@ pub async fn get_graph_metrics(
_path: PathGraphMetrics,
State(db): State<SqlitePool>,
) -> somehow::Result<impl IntoResponse> {
let metrics =
sqlx::query_scalar!("SELECT DISTINCT metric FROM run_measurements ORDER BY metric ASC")
.fetch_all(&db)
.await?;
let metrics = sqlx::query_scalar!("SELECT name FROM metrics ORDER BY name ASC")
.fetch_all(&db)
.await?;
Ok(Json(MetricsResponse {
data_id: 0, // TODO Implement
@ -96,8 +96,8 @@ pub async fn get_graph_commits(
SELECT \
hash, \
author, \
committer_date AS \"committer_date: time::OffsetDateTime\", \
message \
message, \
committer_date AS \"committer_date: OffsetDateTime\" \
FROM commits \
WHERE reachable = 2 \
ORDER BY hash ASC \
@ -124,7 +124,7 @@ pub async fn get_graph_commits(
let mut rows = sqlx::query!(
"\
SELECT child, parent \
FROM commit_links \
FROM commit_edges \
JOIN commits ON hash = child \
WHERE reachable = 2 \
ORDER BY hash ASC \

View file

@ -80,7 +80,7 @@ async fn from_finished_run(
"\
SELECT source, text FROM run_output \
WHERE id = ? \
ORDER BY idx ASC \
ORDER BY line ASC \
",
id,
)