| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use super::Pool; |
| 7 | | use sqlx::FromRow; |
| 8 | |
|
| 9 | |
|
| 10 | | #[derive(Clone, Debug, FromRow)] |
| 11 | | pub struct Activity { |
| 12 | | pub owner: String, |
| 13 | | pub name: String, |
| 14 | | pub at_ms: i64, |
| 15 | | pub actor: String, |
| 16 | | |
| 17 | | pub kind: String, |
| 18 | | |
| 19 | | pub event: String, |
| 20 | | |
| 21 | | pub title: String, |
| 22 | | |
| 23 | | pub number: i64, |
| 24 | | } |
| 25 | |
|
| 26 | | impl Activity { |
| 27 | | |
| 28 | 8× | pub fn url(&self) -> String { |
| 29 | 8× | let base = format!("/{}/{}", self.owner, self.name); |
| 30 | 8× | match self.kind.as_str() { |
| 31 | 8× | "proposal" => format!("{base}/proposal/{}", self.number), |
| 32 | 1× | "issue" => format!("{base}/issue/{}", self.number), |
| 33 | 1× | "release" => format!("{base}/releases/tag/{}", self.title), |
| 34 | 0× | _ => format!("{base}/pushes"), |
| 35 | | } |
| 36 | 8× | } |
| 37 | | } |
| 38 | |
|
| 39 | 231× | fn ids_json(ids: &[i64]) -> String { |
| 40 | 231× | serde_json::to_string(ids).unwrap_or_else(|_| "[]".into()) |
| 41 | 231× | } |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 253× | fn each(sql: &'static str) -> std::borrow::Cow<'static, str> { |
| 46 | 253× | if cfg!(feature = "postgres") { |
| 47 | 0× | let mut out = sql.to_string(); |
| 48 | 0× | for n in 1..=4 { |
| 49 | 0× | out = out.replace( |
| 50 | 0× | &format!("SELECT value FROM json_each(${n})"), |
| 51 | 0× | &format!("SELECT CAST(value AS BIGINT) FROM json_array_elements_text(CAST(${n} AS json)) AS t(value)"), |
| 52 | 0× | ); |
| 53 | 0× | } |
| 54 | 0× | out.into() |
| 55 | | } else { |
| 56 | 253× | sql.into() |
| 57 | | } |
| 58 | 253× | } |
| 59 | |
|
| 60 | |
|
| 61 | 10× | pub async fn feed(pool: &Pool, repo_ids: &[i64], limit: i64) -> sqlx::Result<Vec<Activity>> { |
| 62 | 10× | feed_by(pool, repo_ids, None, limit).await |
| 63 | 10× | } |
| 64 | |
|
| 65 | |
|
| 66 | 21× | pub async fn feed_by(pool: &Pool, repo_ids: &[i64], actor: Option<&str>, limit: i64) -> sqlx::Result<Vec<Activity>> { |
| 67 | 21× | sqlx::query_as( |
| 68 | 21× | &each("WITH seen(id) AS (SELECT value FROM json_each($1)), ev AS ( \ |
| 69 | 21× | SELECT repo_id, at_ms, pusher AS actor, 'push' AS kind, reference AS event, '' AS title, 0 AS number FROM pushes WHERE ok = TRUE \ |
| 70 | 21× | UNION ALL SELECT repo_id, created_ms, author, 'release', 'published', tag, 0 FROM releases \ |
| 71 | 21× | UNION ALL SELECT p.repo_id, e.at_ms, e.actor, 'proposal', e.event, p.title, p.number FROM proposal_events e JOIN proposals p ON p.id = e.proposal_id \ |
| 72 | 21× | UNION ALL SELECT i.repo_id, e.at_ms, e.actor, 'issue', e.event, i.title, i.number FROM issue_events e JOIN issues i ON i.id = e.issue_id) \ |
| 73 | 21× | SELECT r.owner, r.name, ev.at_ms, ev.actor, ev.kind, ev.event, ev.title, ev.number \ |
| 74 | 21× | FROM ev JOIN repositories r ON r.id = ev.repo_id WHERE ev.repo_id IN (SELECT id FROM seen) \ |
| 75 | 21× | AND ($2 IS NULL OR lower(ev.actor) = lower($3)) \ |
| 76 | 21× | ORDER BY ev.at_ms DESC LIMIT $4"), |
| 77 | 21× | ) |
| 78 | 21× | .bind(ids_json(repo_ids)) |
| 79 | 21× | .bind(actor) |
| 80 | 21× | .bind(actor) |
| 81 | 21× | .bind(limit) |
| 82 | 21× | .fetch_all(pool) |
| 83 | 21× | .await |
| 84 | 21× | } |
| 85 | |
|
| 86 | |
|
| 87 | | #[derive(Clone, Debug, FromRow)] |
| 88 | | pub struct ProposalLine { |
| 89 | | pub owner: String, |
| 90 | | pub name: String, |
| 91 | | pub number: i64, |
| 92 | | pub title: String, |
| 93 | | pub author: String, |
| 94 | | pub state: String, |
| 95 | | pub conflicted: bool, |
| 96 | | pub updated_ms: i64, |
| 97 | | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | 13× | pub async fn awaiting_review(pool: &Pool, account_id: i64, repo_ids: &[i64]) -> sqlx::Result<Vec<ProposalLine>> { |
| 102 | 13× | sqlx::query_as(&each( |
| 103 | 13× | "SELECT r.owner, r.name, p.number, p.title, p.author, p.state, p.conflicted, p.updated_ms \ |
| 104 | 13× | FROM proposals p JOIN repositories r ON r.id = p.repo_id \ |
| 105 | 13× | WHERE p.repo_id IN (SELECT value FROM json_each($1)) AND p.state = 'open' \ |
| 106 | 13× | AND (p.author_id IS NULL OR p.author_id != $2) \ |
| 107 | 13× | AND NOT EXISTS (SELECT 1 FROM proposal_reviews v WHERE v.proposal_id = p.id AND v.author_id = $3) \ |
| 108 | 13× | ORDER BY p.updated_ms DESC", |
| 109 | 13× | )) |
| 110 | 13× | .bind(ids_json(repo_ids)) |
| 111 | 13× | .bind(account_id) |
| 112 | 13× | .bind(account_id) |
| 113 | 13× | .fetch_all(pool) |
| 114 | 13× | .await |
| 115 | 13× | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | 13× | pub async fn authored(pool: &Pool, account_id: i64, repo_ids: &[i64]) -> sqlx::Result<Vec<ProposalLine>> { |
| 120 | 13× | sqlx::query_as(&each( |
| 121 | 13× | "SELECT r.owner, r.name, p.number, p.title, p.author, p.state, p.conflicted, p.updated_ms \ |
| 122 | 13× | FROM proposals p JOIN repositories r ON r.id = p.repo_id \ |
| 123 | 13× | WHERE p.author_id = $1 AND p.state IN ('open', 'draft') AND p.repo_id IN (SELECT value FROM json_each($2)) \ |
| 124 | 13× | ORDER BY p.updated_ms DESC", |
| 125 | 13× | )) |
| 126 | 13× | .bind(account_id) |
| 127 | 13× | .bind(ids_json(repo_ids)) |
| 128 | 13× | .fetch_all(pool) |
| 129 | 13× | .await |
| 130 | 13× | } |
| 131 | |
|
| 132 | |
|
| 133 | | #[derive(Clone, Debug, FromRow)] |
| 134 | | pub struct IssueLine { |
| 135 | | pub owner: String, |
| 136 | | pub name: String, |
| 137 | | pub number: i64, |
| 138 | | pub title: String, |
| 139 | | pub updated_ms: i64, |
| 140 | | } |
| 141 | |
|
| 142 | |
|
| 143 | 8× | pub async fn assigned(pool: &Pool, account_id: i64, repo_ids: &[i64]) -> sqlx::Result<Vec<IssueLine>> { |
| 144 | 8× | sqlx::query_as(&each( |
| 145 | 8× | "SELECT r.owner, r.name, i.number, i.title, i.updated_ms \ |
| 146 | 8× | FROM issues i JOIN issue_assignees a ON a.issue_id = i.id JOIN repositories r ON r.id = i.repo_id \ |
| 147 | 8× | WHERE a.account_id = $1 AND i.state = 'open' AND i.repo_id IN (SELECT value FROM json_each($2)) \ |
| 148 | 8× | ORDER BY i.updated_ms DESC", |
| 149 | 8× | )) |
| 150 | 8× | .bind(account_id) |
| 151 | 8× | .bind(ids_json(repo_ids)) |
| 152 | 8× | .fetch_all(pool) |
| 153 | 8× | .await |
| 154 | 8× | } |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | | #[derive(Clone, Debug, FromRow)] |
| 160 | | pub struct SkyEvent { |
| 161 | | pub repo_id: i64, |
| 162 | | pub at_ms: i64, |
| 163 | | pub kind: i64, |
| 164 | | pub n: i64, |
| 165 | | } |
| 166 | |
|
| 167 | |
|
| 168 | 176× | pub async fn sky(pool: &Pool, repo_ids: &[i64], since_ms: i64, limit: i64) -> sqlx::Result<Vec<SkyEvent>> { |
| 169 | 176× | sqlx::query_as( |
| 170 | 176× | &each("WITH seen(id) AS (SELECT value FROM json_each($1)), ev AS ( \ |
| 171 | 176× | SELECT repo_id, at_ms, CAST(0 AS BIGINT) AS kind, CASE WHEN changes > 1 THEN changes ELSE 1 END AS n FROM pushes WHERE ok = TRUE \ |
| 172 | 176× | UNION ALL SELECT p.repo_id, e.at_ms, 1, 1 FROM proposal_events e JOIN proposals p ON p.id = e.proposal_id \ |
| 173 | 176× | UNION ALL SELECT i.repo_id, e.at_ms, 2, 1 FROM issue_events e JOIN issues i ON i.id = e.issue_id \ |
| 174 | 176× | UNION ALL SELECT repo_id, created_ms, 3, 1 FROM releases \ |
| 175 | 176× | UNION ALL SELECT repo_id, created_ms, 4, 1 FROM flow_runs) \ |
| 176 | 176× | SELECT repo_id, at_ms, kind, n FROM ev WHERE repo_id IN (SELECT id FROM seen) AND at_ms >= $2 \ |
| 177 | 176× | ORDER BY at_ms DESC LIMIT $3"), |
| 178 | 176× | ) |
| 179 | 176× | .bind(ids_json(repo_ids)) |
| 180 | 176× | .bind(since_ms) |
| 181 | 176× | .bind(limit) |
| 182 | 176× | .fetch_all(pool) |
| 183 | 176× | .await |
| 184 | 176× | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | 11× | pub async fn pulse(pool: &Pool, account_id: i64, repo_ids: &[i64], since_ms: i64) -> sqlx::Result<Vec<(i64, i64, i64, String)>> { |
| 191 | 11× | let ids = serde_json::to_string(repo_ids).unwrap_or_else(|_| "[]".into()); |
| 192 | 11× | sqlx::query_as(&each( |
| 193 | 11× | "SELECT repo_id, at_ms, changes, via FROM pushes \ |
| 194 | 11× | WHERE account_id = $1 AND ok = TRUE AND changes > 0 AND at_ms >= $2 AND reason NOT LIKE 'landed%' \ |
| 195 | 11× | AND repo_id IN (SELECT value FROM json_each($3)) ORDER BY at_ms", |
| 196 | 11× | )) |
| 197 | 11× | .bind(account_id) |
| 198 | 11× | .bind(since_ms) |
| 199 | 11× | .bind(ids) |
| 200 | 11× | .fetch_all(pool) |
| 201 | 11× | .await |
| 202 | 11× | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | 11× | pub async fn releases_by(pool: &Pool, account_id: i64, repo_ids: &[i64], since_ms: i64) -> sqlx::Result<Vec<(i64, String, i64)>> { |
| 207 | 11× | let ids = serde_json::to_string(repo_ids).unwrap_or_else(|_| "[]".into()); |
| 208 | 11× | sqlx::query_as(&each( |
| 209 | 11× | "SELECT repo_id, tag, created_ms FROM releases WHERE author_id = $1 AND created_ms >= $2 \ |
| 210 | 11× | AND repo_id IN (SELECT value FROM json_each($3)) ORDER BY created_ms", |
| 211 | 11× | )) |
| 212 | 11× | .bind(account_id) |
| 213 | 11× | .bind(since_ms) |
| 214 | 11× | .bind(ids) |
| 215 | 11× | .fetch_all(pool) |
| 216 | 11× | .await |
| 217 | 11× | } |