-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add more fields to CardReviewData
#798
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a9692cc
Add fields to `CardReviewData`
RisingOrange c001024
Get field values from db and add them to review data
RisingOrange 003fc6a
Begin updating tests
RisingOrange 4c573cc
Change log message
RisingOrange d624fdf
fixup get_first_and_last
RisingOrange c0ca158
Update tests
RisingOrange f66df74
Merge branch 'main' into feat/add-more-review-data-fields
RisingOrange 9e1e6ea
Add comment
RisingOrange 9635301
Merge branch 'feat/add-more-review-data-fields' of github.com:ankipal…
RisingOrange ba7cc3c
Remove newline
RisingOrange File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import uuid | ||
from datetime import datetime, timedelta | ||
from typing import Optional | ||
from typing import Optional, Tuple | ||
|
||
import aqt | ||
|
||
|
@@ -10,34 +10,40 @@ | |
from ..db import attached_ankihub_db | ||
from ..settings import config | ||
|
||
# The server needs the review counts for the last 30 days | ||
REVIEW_PERIOD_DAYS = timedelta(days=30) | ||
|
||
|
||
def send_review_data() -> None: | ||
"""Send data about card reviews for each installed AnkiHub deck to the server. | ||
Data about decks that have not been reviewed yet will not be included.""" | ||
since = datetime.now() - REVIEW_PERIOD_DAYS | ||
now = datetime.now() | ||
|
||
with attached_ankihub_db(): | ||
card_review_data = [] | ||
for ah_did in config.deck_ids(): | ||
last_card_review_at = _get_last_review_datetime_for_ah_deck(ah_did) | ||
if last_card_review_at is None: | ||
first_and_last_review_times = ( | ||
_get_first_and_last_review_datetime_for_ah_deck(ah_did) | ||
) | ||
if first_and_last_review_times is None: | ||
# This deck has no reviews yet | ||
continue | ||
|
||
first_review_at, last_review_at = first_and_last_review_times | ||
total_card_reviews_last_7_days = _get_review_count_for_ah_deck_since( | ||
ah_did, now - timedelta(days=7) | ||
) | ||
total_card_reviews_last_30_days = _get_review_count_for_ah_deck_since( | ||
ah_did, since | ||
ah_did, now - timedelta(days=30) | ||
) | ||
card_review_data.append( | ||
CardReviewData( | ||
ah_did=ah_did, | ||
total_card_reviews_last_7_days=total_card_reviews_last_7_days, | ||
total_card_reviews_last_30_days=total_card_reviews_last_30_days, | ||
last_card_review_at=last_card_review_at, | ||
first_card_review_at=first_review_at, | ||
last_card_review_at=last_review_at, | ||
) | ||
) | ||
|
||
LOGGER.info(f"Review counts: {card_review_data}") | ||
LOGGER.info(f"Review data: {card_review_data}") | ||
|
||
client = AnkiHubClient() | ||
client.send_card_review_data(card_review_data) | ||
|
@@ -61,22 +67,29 @@ def _get_review_count_for_ah_deck_since(ah_did: uuid.UUID, since: datetime) -> i | |
return result | ||
|
||
|
||
def _get_last_review_datetime_for_ah_deck(ah_did: uuid.UUID) -> Optional[datetime]: | ||
"""Get the date time of the last review (recorded in Anki's review log table) for an ankihub deck. | ||
def _get_first_and_last_review_datetime_for_ah_deck( | ||
ah_did: uuid.UUID, | ||
) -> Optional[Tuple[datetime, datetime]]: | ||
"""Get the date time of the first and last review (recorded in Anki's review log table) for an ankihub deck. | ||
Requires the ankihub db to be attached to the Anki db.""" | ||
timestamp_str = aqt.mw.col.db.scalar( | ||
row = aqt.mw.col.db.first( | ||
""" | ||
SELECT MAX(r.id) | ||
SELECT MIN(r.id), MAX(r.id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like how this was built in a way that the change to get the new data was really simple! 💯 |
||
FROM revlog as r | ||
JOIN cards as c ON r.cid = c.id | ||
JOIN ankihub_db.notes as ah_n ON c.nid = ah_n.anki_note_id | ||
WHERE ah_n.ankihub_deck_id = ? | ||
""", | ||
str(ah_did), | ||
) | ||
if timestamp_str is None: | ||
if row[0] is None: | ||
return None | ||
|
||
timestamp_ms = int(timestamp_str) | ||
result = datetime.fromtimestamp(timestamp_ms / 1000) | ||
return result | ||
first_timestamp_str, last_timestamp_str = row | ||
first_review_datetime = _ms_timestamp_to_datetime(int(first_timestamp_str)) | ||
last_review_datetime = _ms_timestamp_to_datetime(int(last_timestamp_str)) | ||
return first_review_datetime, last_review_datetime | ||
|
||
|
||
def _ms_timestamp_to_datetime(timestamp: int) -> datetime: | ||
return datetime.fromtimestamp(timestamp / 1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, really nice that you could reuse this method! 💯