Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

133 linesCodeBlameRaw
1-- ?2 the weeks as a JSON array of { start, from, to, cut }: the first UTC
2-- day, the bounds (from inclusive, to exclusive) and when the week is read
3-- as ended for stalled pull requests (to, or now when that is earlier).
4--
5-- A person's verdict is an approval or request for changes by someone
6-- other than the owner, not g1t's review agent, and not a workspace agent
7-- (which keeps its verdict apart): the first one is the first review, as
8-- insights.sql counts it.
9,
10w AS (
11 SELECT json_extract(value, '$.start') AS start,
12 json_extract(value, '$.from') AS lo_at, json_extract(value, '$.to') AS hi_at,
13 julianday(json_extract(value, '$.from')) AS lo,
14 julianday(json_extract(value, '$.to')) AS hi,
15 julianday(json_extract(value, '$.cut')) AS cut
16 FROM json_each(?2)
17),
18-- The earliest and the latest cut.
19cuts AS (SELECT MIN(cut) AS lo, MAX(cut) AS hi FROM w),
20x AS MATERIALIZED (
21 SELECT pr.id, pr.repo_id, pr.number, pr.status, pr.agent, pr.draft_seconds, pr.head_commit,
22 julianday(pr.created_at) AS c, julianday(pr.merged_at) AS m, julianday(pr.updated_at) AS u,
23 julianday(pr.head_pushed_at) AS pushed,
24 julianday((SELECT MIN(cm.created_at) FROM comments cm
25 WHERE cm.repo_id = pr.repo_id AND cm.number = pr.number
26 AND cm.verdict IN ('approve', 'request_changes')
27 AND cm.author_id != pr.owner_id AND cm.author_id != 'usr_g1t_agent' AND cm.agent_id IS NULL)) AS r,
28 -- An agent's merged pull request a person asked for changes on before it merged.
29 (pr.agent AND pr.status = 'merged' AND EXISTS (SELECT 1 FROM comments cm
30 WHERE cm.repo_id = pr.repo_id AND cm.number = pr.number AND cm.verdict = 'request_changes'
31 AND cm.author_id != pr.owner_id AND cm.author_id != 'usr_g1t_agent' AND cm.agent_id IS NULL
32 AND julianday(cm.created_at) < julianday(pr.merged_at))) AS reworked
33 FROM pr
34),
35-- The pull requests that may be stalled at some cut: opened three days
36-- before the latest, and not finished before the earliest (a closed one's
37-- updated_at stands in for when it closed, which pulls do not keep).
38-- Only their activity is read, each by its own index.
39cand AS MATERIALIZED (
40 SELECT x.* FROM x, cuts
41 WHERE x.c <= cuts.hi - 3 AND NOT COALESCE(x.m < cuts.lo, 0)
42 AND NOT (x.status = 'closed' AND COALESCE(x.u < cuts.lo, 0))
43),
44-- What each of them saw, as spans [t0, t1] (a moment where t0 = t1).
45-- CROSS JOIN keeps each read starting from the few candidates.
46act(pull_id, t0, t1) AS MATERIALIZED (
47 -- Comments of every kind: reviews, and the timeline's events (labels,
48 -- assignees, review requests, milestones, ready, draft, closed,
49 -- reopened, base changes, the merge queue, auto-merge, branches deleted
50 -- and restored). Pushes are no timeline event.
51 SELECT k.id, julianday(cm.created_at), julianday(cm.created_at)
52 FROM cand k CROSS JOIN comments cm ON cm.repo_id = k.repo_id AND cm.number = k.number
53 -- Its latest push, the only one pulls keep.
54 UNION ALL SELECT k.id, k.pushed, k.pushed FROM cand k WHERE k.pushed IS NOT NULL
55 -- Entries recorded on it.
56 UNION ALL SELECT k.id, julianday(e.at), julianday(e.at) FROM cand k CROSS JOIN session_entries e ON e.pull_id = k.id
57 -- Its acceptance checks, from queued to finished.
58 UNION ALL SELECT k.id, julianday(cr.created_at), julianday(COALESCE(cr.finished_at, cr.created_at))
59 FROM cand k CROSS JOIN check_runs cr ON cr.pull_id = k.id
60 -- Statuses and check runs on its head commit, the one commit pulls keep
61 -- (a status keeps only when it last changed).
62 UNION ALL SELECT k.id, julianday(st.updated_at), julianday(st.updated_at)
63 FROM cand k CROSS JOIN commit_statuses st ON st.repo_id = k.repo_id AND st.sha = k.head_commit
64 UNION ALL SELECT k.id, julianday(ck.created_at), julianday(ck.updated_at)
65 FROM cand k CROSS JOIN commit_check_runs ck ON ck.repo_id = k.repo_id AND ck.head_sha = k.head_commit
66 -- The sessions tied to it, and their subagents, from start to last
67 -- heartbeat: a session that ran then counts, busy or not.
68 UNION ALL SELECT k.id, julianday(s.started_at), julianday(s.last_seen_at)
69 FROM cand k CROSS JOIN tied t ON t.pull_id = k.id CROSS JOIN sessions s ON s.id = t.session_id
70 UNION ALL SELECT k.id, julianday(s.started_at), julianday(s.last_seen_at)
71 FROM cand k CROSS JOIN tied t ON t.pull_id = k.id CROSS JOIN sessions s ON s.parent_id = t.session_id
72),
73-- Each week a pull request saw something in the three days before its cut.
74busy AS MATERIALIZED (
75 SELECT DISTINCT a.pull_id, w.start FROM act a JOIN w ON a.t0 < w.cut AND a.t1 >= w.cut - 3
76),
77pw AS (
78 SELECT x.repo_id, w.start,
79 COUNT(CASE WHEN x.status = 'merged' AND x.m >= w.lo AND x.m < w.hi THEN 1 END) AS merged,
80 COUNT(CASE WHEN x.status = 'merged' AND x.m >= w.lo AND x.m < w.hi AND x.agent THEN 1 END) AS agent_merged,
81 COUNT(CASE WHEN x.status = 'merged' AND x.m >= w.lo AND x.m < w.hi AND x.reworked THEN 1 END) AS agent_reworked,
82 -- Opened in the week by an agent whose session started first.
83 COUNT(CASE WHEN x.agent AND x.draft_seconds IS NOT NULL AND x.c >= w.lo AND x.c < w.hi THEN 1 END) AS agent_drafted,
84 TOTAL(CASE WHEN x.agent AND x.c >= w.lo AND x.c < w.hi THEN x.draft_seconds END) AS agent_draft_seconds,
85 COUNT(CASE WHEN x.r >= w.lo AND x.r < w.hi THEN 1 END) AS reviewed,
86 TOTAL(CASE WHEN x.r >= w.lo AND x.r < w.hi THEN MAX(0, (x.r - x.c) * 86400) END) AS review_seconds,
87 0 AS stalled, 0.0 AS session_cost_usd, 0 AS agent_commits
88 FROM x CROSS JOIN w
89 GROUP BY x.repo_id, w.start
90),
91-- Open at the cut (not merged, and not closed, before it), opened at
92-- least three days before, and nothing in the three days before it.
93sx AS (
94 SELECT k.repo_id, w.start, 0 AS merged, 0 AS agent_merged, 0 AS agent_reworked, 0 AS agent_drafted,
95 0.0 AS agent_draft_seconds, 0 AS reviewed, 0.0 AS review_seconds, COUNT(*) AS stalled, 0.0 AS session_cost_usd, 0 AS agent_commits
96 FROM cand k CROSS JOIN w LEFT JOIN busy b ON b.pull_id = k.id AND b.start = w.start
97 WHERE b.pull_id IS NULL AND k.c <= w.cut - 3
98 AND NOT COALESCE(k.m < w.cut, 0)
99 AND NOT (k.status = 'closed' AND COALESCE(k.u < w.cut, 0))
100 GROUP BY k.repo_id, w.start
101),
102-- What the sessions started in the week cost, subagents and all: each
103-- reports only its own transcript's.
104sw AS (
105 SELECT s.repo_id, w.start, 0 AS merged, 0 AS agent_merged, 0 AS agent_reworked, 0 AS agent_drafted,
106 0.0 AS agent_draft_seconds, 0 AS reviewed, 0.0 AS review_seconds, 0 AS stalled,
107 TOTAL(s.cost_usd) AS session_cost_usd, 0 AS agent_commits
108 FROM sessions s JOIN w ON s.started_at >= w.lo_at AND s.started_at < w.hi_at
109 WHERE s.repo_id IN (SELECT value FROM json_each(?1))
110 AND s.started_at >= (SELECT MIN(lo_at) FROM w) AND s.started_at < (SELECT MAX(hi_at) FROM w)
111 GROUP BY s.repo_id, w.start
112),
113-- The commits agent sessions pushed outside any pull request (to a
114-- branch no pull request is from, the default branch most of all): a
115-- session's own commits, by when it recorded them, each once.
116cw AS (
117 SELECT t.repo_id, w.start, 0 AS merged, 0 AS agent_merged, 0 AS agent_reworked, 0 AS agent_drafted,
118 0.0 AS agent_draft_seconds, 0 AS reviewed, 0.0 AS review_seconds, 0 AS stalled, 0.0 AS session_cost_usd,
119 COUNT(DISTINCT t.value) AS agent_commits
120 FROM session_touches t JOIN w ON t.at >= w.lo_at AND t.at < w.hi_at
121 WHERE t.repo_id IN (SELECT value FROM json_each(?1)) AND t.kind = 'commit'
122 AND t.session_id NOT IN (SELECT session_id FROM tied)
123 GROUP BY t.repo_id, w.start
124)
125SELECT repo_id, start, SUM(merged) AS merged, SUM(agent_merged) AS agent_merged,
126 SUM(agent_reworked) AS agent_reworked, SUM(agent_drafted) AS agent_drafted,
127 TOTAL(agent_draft_seconds) AS agent_draft_seconds, SUM(reviewed) AS reviewed,
128 TOTAL(review_seconds) AS review_seconds, SUM(stalled) AS stalled, TOTAL(session_cost_usd) AS session_cost_usd,
129 SUM(agent_commits) AS agent_commits
130FROM (SELECT * FROM pw UNION ALL SELECT * FROM sx UNION ALL SELECT * FROM sw UNION ALL SELECT * FROM cw)
131GROUP BY repo_id, start
132HAVING SUM(merged) + SUM(agent_drafted) + SUM(reviewed) + SUM(stalled) + SUM(agent_commits) > 0 OR TOTAL(session_cost_usd) > 0
133ORDER BY start, repo_id