fix the active feeds query to not needs runs to have a row
This commit is contained in:
parent
8cbb2ef0c5
commit
081cfa1a86
3 changed files with 10 additions and 22 deletions
|
|
@ -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)
|
||||
|
|
|
|||
29
src/lib.rs
29
src/lib.rs
|
|
@ -23,12 +23,13 @@ const LAST_FETCHED: DateTime<Utc> = 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<Utc>,
|
||||
updated: DateTime<Utc>,
|
||||
#[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<Utc>,
|
||||
fetched: DateTime<Utc>,
|
||||
fetched: Option<DateTime<Utc>>,
|
||||
posted: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
|
|
@ -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}");
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@ struct ZulipMessage {
|
|||
_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);
|
||||
match s {
|
||||
Err(_) => {}
|
||||
|
|
|
|||
Loading…
Reference in a new issue