diff --git a/migrations/0003_runs.up.sql b/migrations/0003_runs.up.sql index 1a48cec..394b171 100644 --- a/migrations/0003_runs.up.sql +++ b/migrations/0003_runs.up.sql @@ -1,7 +1,6 @@ CREATE TABLE IF NOT EXISTS runs ( id INTEGER NOT NULL PRIMARY KEY, feed INTEGER NOT NULL, - run 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 FOREIGN KEY (feed) REFERENCES feeds(id) diff --git a/src/lib.rs b/src/lib.rs index 69909b9..1be5968 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,12 +23,13 @@ const LAST_FETCHED: DateTime = DateTime::from_timestamp_nanos(0); 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 - (SELECT feed, MAX(id) _, run, fetched, posted FROM runs WHERE feed IN - (SELECT feed FROM (SELECT feed, MAX(id), active FROM status GROUP BY feed) WHERE active = TRUE) - GROUP BY feed) r - ON feeds.id = r.feed"#; + (SELECT feed, updated FROM (SELECT feed, MAX(id), updated, active FROM status GROUP BY feed) WHERE active = TRUE) s + ON feeds.id = s.feed + LEFT JOIN + (SELECT posted, MAX(fetched) fetched, feed FROM runs GROUP BY feed) r + ON r.feed = feeds.id"#; pub struct BlogdorTheAggregator { db: SqlitePool, @@ -113,6 +114,7 @@ pub struct ActiveFeed { id: i64, owner: i64, created: DateTime, + updated: DateTime, #[sqlx(flatten)] last_run: FeedRun, } @@ -120,8 +122,7 @@ pub struct ActiveFeed { #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, FromRow)] pub struct FeedRun { feed: i64, - run: DateTime, - fetched: DateTime, + fetched: Option>, posted: Option>, } @@ -222,7 +223,7 @@ impl BlogdorTheAggregator { let url = &feed.url; let user = feed.owner; let run = feed.last_run; - let last = run.fetched; + let last = run.fetched.unwrap_or(feed.updated); let dur = now - last; if dur.num_seconds() > STALE_FETCH_THRESHOLD.as_secs() as i64 { @@ -319,18 +320,6 @@ impl BlogdorTheAggregator { "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! txn.commit().await.map_err(|e| { tracing::error!("error committing add-feed transaction: {e}"); diff --git a/src/server.rs b/src/server.rs index 2ee1c6b..329d0fb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -169,7 +169,7 @@ struct ZulipMessage { _rest: Payload, } -fn parse_command<'i>(input: &mut &'i str) -> winnow::Result> { +fn parse_command(input: &mut &str) -> winnow::Result> { let s = take_until::<_, _, ()>(0.., "@**blogdor's manager**").parse_next(input); match s { Err(_) => {}