Main kanban board query 20x reduction (#273)

This commit is contained in:
Louis Knight-Webb
2025-07-19 18:29:52 +01:00
committed by GitHub
parent 123e24220b
commit 4a83e6b7fa
4 changed files with 132 additions and 181 deletions

View File

@@ -0,0 +1,86 @@
{
"db_name": "SQLite",
"query": "SELECT\n t.id AS \"id!: Uuid\",\n t.project_id AS \"project_id!: Uuid\",\n t.title,\n t.description,\n t.status AS \"status!: TaskStatus\",\n t.parent_task_attempt AS \"parent_task_attempt: Uuid\",\n t.created_at AS \"created_at!: DateTime<Utc>\",\n t.updated_at AS \"updated_at!: DateTime<Utc>\",\n\n CASE WHEN EXISTS (\n SELECT 1\n FROM task_attempts ta\n JOIN execution_processes ep\n ON ep.task_attempt_id = ta.id\n WHERE ta.task_id = t.id\n AND ep.status = 'running'\n AND ep.process_type IN ('setupscript','codingagent')\n LIMIT 1\n ) THEN 1 ELSE 0 END AS \"has_in_progress_attempt!: i64\",\n\n CASE WHEN EXISTS (\n SELECT 1\n FROM task_attempts ta\n WHERE ta.task_id = t.id\n AND ta.merge_commit IS NOT NULL\n LIMIT 1\n ) THEN 1 ELSE 0 END AS \"has_merged_attempt!: i64\",\n\n CASE WHEN (\n SELECT ep.status\n FROM task_attempts ta\n JOIN execution_processes ep\n ON ep.task_attempt_id = ta.id\n WHERE ta.task_id = t.id\n AND ep.process_type IN ('setupscript','codingagent')\n ORDER BY ep.created_at DESC\n LIMIT 1\n ) IN ('failed','killed') THEN 1 ELSE 0 END\n AS \"last_attempt_failed!: i64\",\n\n ( SELECT ta.executor\n FROM task_attempts ta\n WHERE ta.task_id = t.id\n ORDER BY ta.created_at DESC\n LIMIT 1\n ) AS \"latest_attempt_executor\"\n\nFROM tasks t\nWHERE t.project_id = $1\nORDER BY t.created_at DESC",
"describe": {
"columns": [
{
"name": "id!: Uuid",
"ordinal": 0,
"type_info": "Blob"
},
{
"name": "project_id!: Uuid",
"ordinal": 1,
"type_info": "Blob"
},
{
"name": "title",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "description",
"ordinal": 3,
"type_info": "Text"
},
{
"name": "status!: TaskStatus",
"ordinal": 4,
"type_info": "Text"
},
{
"name": "parent_task_attempt: Uuid",
"ordinal": 5,
"type_info": "Blob"
},
{
"name": "created_at!: DateTime<Utc>",
"ordinal": 6,
"type_info": "Text"
},
{
"name": "updated_at!: DateTime<Utc>",
"ordinal": 7,
"type_info": "Text"
},
{
"name": "has_in_progress_attempt!: i64",
"ordinal": 8,
"type_info": "Integer"
},
{
"name": "has_merged_attempt!: i64",
"ordinal": 9,
"type_info": "Integer"
},
{
"name": "last_attempt_failed!: i64",
"ordinal": 10,
"type_info": "Integer"
},
{
"name": "latest_attempt_executor",
"ordinal": 11,
"type_info": "Text"
}
],
"parameters": {
"Right": 1
},
"nullable": [
true,
false,
false,
true,
false,
true,
false,
false,
false,
false,
false,
true
]
},
"hash": "6333d1cf94f67854143c413f183ae2d5543729dce49f63208ca6681420531ce2"
}

View File

@@ -1,86 +0,0 @@
{
"db_name": "SQLite",
"query": "SELECT \n t.id AS \"id!: Uuid\",\n t.project_id AS \"project_id!: Uuid\",\n t.title,\n t.description,\n t.status AS \"status!: TaskStatus\",\n t.parent_task_attempt AS \"parent_task_attempt: Uuid\", \n t.created_at AS \"created_at!: DateTime<Utc>\",\n t.updated_at AS \"updated_at!: DateTime<Utc>\",\n CASE \n WHEN ip.task_id IS NOT NULL THEN true \n ELSE false \n END AS \"has_in_progress_attempt!: i64\",\n CASE \n WHEN ma.task_id IS NOT NULL THEN true \n ELSE false \n END AS \"has_merged_attempt!: i64\",\n CASE \n WHEN fa.task_id IS NOT NULL THEN true \n ELSE false \n END AS \"last_attempt_failed!: i64\",\n latest_executor_attempts.executor AS \"latest_attempt_executor\"\n FROM tasks t\n\n -- in-progress if any running setupscript/codingagent\n LEFT JOIN (\n SELECT DISTINCT ta.task_id\n FROM task_attempts ta\n JOIN execution_processes ep \n ON ta.id = ep.task_attempt_id\n WHERE ep.status = 'running'\n AND ep.process_type IN ('setupscript','codingagent')\n ) ip \n ON t.id = ip.task_id\n\n -- merged if merge_commit not null\n LEFT JOIN (\n SELECT DISTINCT task_id\n FROM task_attempts\n WHERE merge_commit IS NOT NULL\n ) ma \n ON t.id = ma.task_id\n\n -- failed if latest execution process has a failed setupscript/codingagent\n LEFT JOIN (\n SELECT sub.task_id\n FROM (\n SELECT\n ta.task_id,\n ep.status,\n ep.process_type,\n ROW_NUMBER() OVER (\n PARTITION BY ta.task_id \n ORDER BY ep.created_at DESC\n ) AS rn\n FROM task_attempts ta\n JOIN execution_processes ep \n ON ta.id = ep.task_attempt_id\n WHERE ep.process_type IN ('setupscript','codingagent')\n ) sub\n WHERE sub.rn = 1\n AND sub.status IN ('failed','killed')\n ) fa\n ON t.id = fa.task_id\n\n -- get the executor of the latest attempt\n LEFT JOIN (\n SELECT task_id, executor\n FROM (\n SELECT task_id, executor, created_at,\n ROW_NUMBER() OVER (PARTITION BY task_id ORDER BY created_at DESC) AS rn\n FROM task_attempts\n ) latest_attempts\n WHERE rn = 1\n ) latest_executor_attempts \n ON t.id = latest_executor_attempts.task_id\n\n WHERE t.project_id = $1\n ORDER BY t.created_at DESC;\n ",
"describe": {
"columns": [
{
"name": "id!: Uuid",
"ordinal": 0,
"type_info": "Blob"
},
{
"name": "project_id!: Uuid",
"ordinal": 1,
"type_info": "Blob"
},
{
"name": "title",
"ordinal": 2,
"type_info": "Text"
},
{
"name": "description",
"ordinal": 3,
"type_info": "Text"
},
{
"name": "status!: TaskStatus",
"ordinal": 4,
"type_info": "Text"
},
{
"name": "parent_task_attempt: Uuid",
"ordinal": 5,
"type_info": "Blob"
},
{
"name": "created_at!: DateTime<Utc>",
"ordinal": 6,
"type_info": "Text"
},
{
"name": "updated_at!: DateTime<Utc>",
"ordinal": 7,
"type_info": "Text"
},
{
"name": "has_in_progress_attempt!: i64",
"ordinal": 8,
"type_info": "Integer"
},
{
"name": "has_merged_attempt!: i64",
"ordinal": 9,
"type_info": "Integer"
},
{
"name": "last_attempt_failed!: i64",
"ordinal": 10,
"type_info": "Integer"
},
{
"name": "latest_attempt_executor",
"ordinal": 11,
"type_info": "Text"
}
],
"parameters": {
"Right": 1
},
"nullable": [
true,
false,
false,
true,
false,
true,
false,
false,
false,
false,
false,
true
]
},
"hash": "93a279fc7df10c38bdc1b0a3d7b48006147092bfb459e12424a55d42e6390d6a"
}

View File

@@ -1,20 +0,0 @@
{
"db_name": "SQLite",
"query": "SELECT ta.id as \"id!: Uuid\" FROM task_attempts ta \n JOIN tasks t ON ta.task_id = t.id \n WHERE ta.id = $1 AND t.id = $2 AND t.project_id = $3",
"describe": {
"columns": [
{
"name": "id!: Uuid",
"ordinal": 0,
"type_info": "Blob"
}
],
"parameters": {
"Right": 3
},
"nullable": [
true
]
},
"hash": "b922df6b55164f75eec60a5ddf17a81598862a5f794d42ca89c67b4da1a25be5"
}

View File

@@ -80,86 +80,57 @@ impl Task {
project_id: Uuid,
) -> Result<Vec<TaskWithAttemptStatus>, sqlx::Error> {
let records = sqlx::query!(
r#"SELECT
t.id AS "id!: Uuid",
t.project_id AS "project_id!: Uuid",
t.title,
t.description,
t.status AS "status!: TaskStatus",
t.parent_task_attempt AS "parent_task_attempt: Uuid",
t.created_at AS "created_at!: DateTime<Utc>",
t.updated_at AS "updated_at!: DateTime<Utc>",
CASE
WHEN ip.task_id IS NOT NULL THEN true
ELSE false
END AS "has_in_progress_attempt!: i64",
CASE
WHEN ma.task_id IS NOT NULL THEN true
ELSE false
END AS "has_merged_attempt!: i64",
CASE
WHEN fa.task_id IS NOT NULL THEN true
ELSE false
END AS "last_attempt_failed!: i64",
latest_executor_attempts.executor AS "latest_attempt_executor"
FROM tasks t
r#"SELECT
t.id AS "id!: Uuid",
t.project_id AS "project_id!: Uuid",
t.title,
t.description,
t.status AS "status!: TaskStatus",
t.parent_task_attempt AS "parent_task_attempt: Uuid",
t.created_at AS "created_at!: DateTime<Utc>",
t.updated_at AS "updated_at!: DateTime<Utc>",
-- in-progress if any running setupscript/codingagent
LEFT JOIN (
SELECT DISTINCT ta.task_id
FROM task_attempts ta
JOIN execution_processes ep
ON ta.id = ep.task_attempt_id
WHERE ep.status = 'running'
AND ep.process_type IN ('setupscript','codingagent')
) ip
ON t.id = ip.task_id
CASE WHEN EXISTS (
SELECT 1
FROM task_attempts ta
JOIN execution_processes ep
ON ep.task_attempt_id = ta.id
WHERE ta.task_id = t.id
AND ep.status = 'running'
AND ep.process_type IN ('setupscript','codingagent')
LIMIT 1
) THEN 1 ELSE 0 END AS "has_in_progress_attempt!: i64",
-- merged if merge_commit not null
LEFT JOIN (
SELECT DISTINCT task_id
FROM task_attempts
WHERE merge_commit IS NOT NULL
) ma
ON t.id = ma.task_id
CASE WHEN EXISTS (
SELECT 1
FROM task_attempts ta
WHERE ta.task_id = t.id
AND ta.merge_commit IS NOT NULL
LIMIT 1
) THEN 1 ELSE 0 END AS "has_merged_attempt!: i64",
-- failed if latest execution process has a failed setupscript/codingagent
LEFT JOIN (
SELECT sub.task_id
FROM (
SELECT
ta.task_id,
ep.status,
ep.process_type,
ROW_NUMBER() OVER (
PARTITION BY ta.task_id
ORDER BY ep.created_at DESC
) AS rn
FROM task_attempts ta
JOIN execution_processes ep
ON ta.id = ep.task_attempt_id
WHERE ep.process_type IN ('setupscript','codingagent')
) sub
WHERE sub.rn = 1
AND sub.status IN ('failed','killed')
) fa
ON t.id = fa.task_id
CASE WHEN (
SELECT ep.status
FROM task_attempts ta
JOIN execution_processes ep
ON ep.task_attempt_id = ta.id
WHERE ta.task_id = t.id
AND ep.process_type IN ('setupscript','codingagent')
ORDER BY ep.created_at DESC
LIMIT 1
) IN ('failed','killed') THEN 1 ELSE 0 END
AS "last_attempt_failed!: i64",
-- get the executor of the latest attempt
LEFT JOIN (
SELECT task_id, executor
FROM (
SELECT task_id, executor, created_at,
ROW_NUMBER() OVER (PARTITION BY task_id ORDER BY created_at DESC) AS rn
FROM task_attempts
) latest_attempts
WHERE rn = 1
) latest_executor_attempts
ON t.id = latest_executor_attempts.task_id
( SELECT ta.executor
FROM task_attempts ta
WHERE ta.task_id = t.id
ORDER BY ta.created_at DESC
LIMIT 1
) AS "latest_attempt_executor"
WHERE t.project_id = $1
ORDER BY t.created_at DESC;
"#,
FROM tasks t
WHERE t.project_id = $1
ORDER BY t.created_at DESC"#,
project_id
)
.fetch_all(pool)