fix the active feeds query to not needs runs to have a row

This commit is contained in:
Joe 2026-01-02 12:33:32 -08:00
parent 8cbb2ef0c5
commit 081cfa1a86
3 changed files with 10 additions and 22 deletions

View file

@ -1,7 +1,6 @@
CREATE TABLE IF NOT EXISTS runs ( CREATE TABLE IF NOT EXISTS runs (
id INTEGER NOT NULL PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
feed INTEGER NOT NULL, feed INTEGER NOT NULL,
run DATETIME NOT NULL DEFAULT current_timestamp,
fetched DATETIME NOT NULL DEFAULT current_timestamp, fetched DATETIME NOT NULL DEFAULT current_timestamp,
posted DATETIME, -- once this becomes non-null, the program will ensure it will never be null again posted DATETIME, -- once this becomes non-null, the program will ensure it will never be null again
FOREIGN KEY (feed) REFERENCES feeds(id) FOREIGN KEY (feed) REFERENCES feeds(id)

View file

@ -23,12 +23,13 @@ const LAST_FETCHED: DateTime<Utc> = DateTime::from_timestamp_nanos(0);
const STALE_FETCH_THRESHOLD: Duration = Duration::from_hours(24); const STALE_FETCH_THRESHOLD: Duration = Duration::from_hours(24);
const ACTIVE_FEEDS_QUERY: &str = r#"SELECT id, url, owner, created, fetched, posted, run, feed FROM feeds const ACTIVE_FEEDS_QUERY: &str = r#"SELECT id, url, owner, created, updated, fetched, posted, s.feed feed FROM feeds
INNER JOIN INNER JOIN
(SELECT feed, MAX(id) _, run, fetched, posted FROM runs WHERE feed IN (SELECT feed, updated FROM (SELECT feed, MAX(id), updated, active FROM status GROUP BY feed) WHERE active = TRUE) s
(SELECT feed FROM (SELECT feed, MAX(id), active FROM status GROUP BY feed) WHERE active = TRUE) ON feeds.id = s.feed
GROUP BY feed) r LEFT JOIN
ON feeds.id = r.feed"#; (SELECT posted, MAX(fetched) fetched, feed FROM runs GROUP BY feed) r
ON r.feed = feeds.id"#;
pub struct BlogdorTheAggregator { pub struct BlogdorTheAggregator {
db: SqlitePool, db: SqlitePool,
@ -113,6 +114,7 @@ pub struct ActiveFeed {
id: i64, id: i64,
owner: i64, owner: i64,
created: DateTime<Utc>, created: DateTime<Utc>,
updated: DateTime<Utc>,
#[sqlx(flatten)] #[sqlx(flatten)]
last_run: FeedRun, last_run: FeedRun,
} }
@ -120,8 +122,7 @@ pub struct ActiveFeed {
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, FromRow)] #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, FromRow)]
pub struct FeedRun { pub struct FeedRun {
feed: i64, feed: i64,
run: DateTime<Utc>, fetched: Option<DateTime<Utc>>,
fetched: DateTime<Utc>,
posted: Option<DateTime<Utc>>, posted: Option<DateTime<Utc>>,
} }
@ -222,7 +223,7 @@ impl BlogdorTheAggregator {
let url = &feed.url; let url = &feed.url;
let user = feed.owner; let user = feed.owner;
let run = feed.last_run; let run = feed.last_run;
let last = run.fetched; let last = run.fetched.unwrap_or(feed.updated);
let dur = now - last; let dur = now - last;
if dur.num_seconds() > STALE_FETCH_THRESHOLD.as_secs() as i64 { if dur.num_seconds() > STALE_FETCH_THRESHOLD.as_secs() as i64 {
@ -319,18 +320,6 @@ impl BlogdorTheAggregator {
"could not add feed".to_string() "could not add feed".to_string()
})?; })?;
// need one row in the runs table to allow the inner join in the active feeds
// query to work
//
// TODO: fix the active feed query so we don't need to do this.
sqlx::query!("insert into runs (feed) values (?)", id)
.execute(&self.db)
.await
.map_err(|e| {
tracing::error!("error inserting into runs: {e}");
"could not add feed".to_string()
})?;
// woo! // woo!
txn.commit().await.map_err(|e| { txn.commit().await.map_err(|e| {
tracing::error!("error committing add-feed transaction: {e}"); tracing::error!("error committing add-feed transaction: {e}");

View file

@ -169,7 +169,7 @@ struct ZulipMessage {
_rest: Payload, _rest: Payload,
} }
fn parse_command<'i>(input: &mut &'i str) -> winnow::Result<Option<FeedCommand>> { fn parse_command(input: &mut &str) -> winnow::Result<Option<FeedCommand>> {
let s = take_until::<_, _, ()>(0.., "@**blogdor's manager**").parse_next(input); let s = take_until::<_, _, ()>(0.., "@**blogdor's manager**").parse_next(input);
match s { match s {
Err(_) => {} Err(_) => {}