Skip to content

Commit

Permalink
Merge pull request #100 from croque-scp/reopt-post-download
Browse files Browse the repository at this point in the history
Track last seen post timestamp per wiki
  • Loading branch information
rossjrw authored May 4, 2024
2 parents 7fe5885 + b347980 commit a4d48f5
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 7 deletions.
6 changes: 6 additions & 0 deletions notifier/database/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def store_supported_wikis(self, wikis: List[SupportedWikiConfig]) -> None:
"""Stores a set of supported wikis in the database, overwriting any
that are already present."""

@abstractmethod
def store_latest_post_timestamp(
self, wiki_id: str, timestamp: int
) -> None:
"""Stores the latest seen post timestamp for the given wiki."""

@abstractmethod
def store_post(self, post: NotifiablePost) -> None:
"""Store a post."""
Expand Down
8 changes: 8 additions & 0 deletions notifier/database/drivers/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@ def store_supported_wikis(self, wikis: List[SupportedWikiConfig]) -> None:
)
# Wikis that were removed from the service since the last run are still available as context

def store_latest_post_timestamp(
self, wiki_id: str, timestamp: int
) -> None:
self.execute_named(
"store_latest_post_timestamp",
{"wiki_id": wiki_id, "timestamp": timestamp},
)

def store_post(self, post: NotifiablePost) -> None:
self.execute_named(
"store_post",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE
context_wiki
DROP COLUMN
new_posts_checked_timestamp;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ALTER TABLE
context_wiki
ADD COLUMN
new_posts_checked_timestamp INT UNSIGNED NOT NULL;

UPDATE
context_wiki
SET
new_posts_checked_timestamp = 0;
4 changes: 2 additions & 2 deletions notifier/database/queries/delete_non_notifiable_posts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ WHERE
notifiable_post.posted_timestamp < ((
SELECT latest_timestamp FROM (
SELECT
MAX(posted_timestamp) AS latest_timestamp
FROM notifiable_post
MAX(new_posts_checked_timestamp) AS latest_timestamp
FROM context_wiki
) AS t
) - (60 * 60 * 24 * 365))

Expand Down
6 changes: 3 additions & 3 deletions notifier/database/queries/get_latest_post_timestamp.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SELECT
MAX(posted_timestamp) as posted_timestamp
new_posts_checked_timestamp AS posted_timestamp
FROM
notifiable_post
context_wiki
WHERE
notifiable_post.context_wiki_id = %(wiki_id)s
context_wiki.wiki_id = %(wiki_id)s
6 changes: 4 additions & 2 deletions notifier/database/queries/store_context_wiki.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ INSERT INTO
wiki_id,
wiki_name,
wiki_service_configured,
wiki_uses_https
wiki_uses_https,
new_posts_checked_timestamp
)
VALUES
(
%(wiki_id)s,
%(wiki_name)s,
%(wiki_service_configured)s,
%(wiki_uses_https)s
%(wiki_uses_https)s,
0
)
ON DUPLICATE KEY UPDATE
wiki_name = %(wiki_name)s,
Expand Down
6 changes: 6 additions & 0 deletions notifier/database/queries/store_latest_post_timestamp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UPDATE
context_wiki
SET
new_posts_checked_timestamp = %(timestamp)s
WHERE
context_wiki.wiki_id = %(wiki_id)s
6 changes: 6 additions & 0 deletions notifier/newposts.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def fetch_posts_with_context(
},
)

# If there's at least one post, store the new highest timestamp for this wiki
if len(new_posts) > 0:
database.store_latest_post_timestamp(
wiki_id, max(post["posted_timestamp"] for post in new_posts)
)

# Cache the latest downloaded thread to prevent multiple identical downloads when multiple posts share a thread
thread_meta: RawThreadMeta = None # type:ignore
thread_page_posts: List[RawPost] = []
Expand Down

0 comments on commit a4d48f5

Please sign in to comment.