Skip to content

Commit

Permalink
update_cards & run_on_main & subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
L-M-Sherlock committed Aug 9, 2024
1 parent 2ca8229 commit d34ffe4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
57 changes: 27 additions & 30 deletions schedule/flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def flatten_background(did, desired_flatten_limit):

cards_exceed_future = mw.col.db.all(
f"""
WITH ranked_cards AS (
SELECT rc.id, rc.true_due, rc.stability
FROM (
SELECT id,
true_due,
stability,
Expand All @@ -73,38 +74,30 @@ def flatten_background(did, desired_flatten_limit):
ORDER BY stability
) AS rank
FROM (
SELECT id,
{true_due} AS true_due,
json_extract(data, '$.s') AS stability
FROM cards
WHERE true_due >= { today }
AND data != ''
AND json_extract(data, '$.s') IS NOT NULL
AND queue = {QUEUE_TYPE_REV}
{"AND did IN %s" % did_list if did is not None else ""}
)
),
overdue AS (
SELECT id,
{true_due} AS true_due,
json_extract(data, '$.s') AS stability
FROM cards
WHERE true_due >= {today}
AND data != ''
AND json_extract(data, '$.s') IS NOT NULL
AND queue = {QUEUE_TYPE_REV}
{"AND did IN %s" % did_list if did is not None else ""}
) AS subquery
) AS rc
JOIN (
SELECT {true_due} AS true_due
FROM cards
WHERE true_due >= { today }
WHERE true_due >= {today}
AND queue = {QUEUE_TYPE_REV}
AND data != ''
AND json_extract(data, '$.s') IS NOT NULL
{"AND did IN %s" % did_list if did is not None else ""}
GROUP BY true_due
HAVING COUNT(*) > { desired_flatten_limit }
)
SELECT id
, true_due
, stability
FROM ranked_cards
WHERE true_due IN (
SELECT true_due
FROM overdue
)
AND rank > { desired_flatten_limit }
ORDER BY true_due
HAVING COUNT(*) > {desired_flatten_limit}
) AS overdue ON rc.true_due = overdue.true_due
WHERE rc.rank > {desired_flatten_limit}
ORDER BY rc.true_due
"""
)

Expand All @@ -124,6 +117,7 @@ def flatten_background(did, desired_flatten_limit):
)

cards_to_flatten = cards_backlog + cards_exceed_future
total_cnt = len(cards_to_flatten)

due_cnt_per_day = defaultdict(
int,
Expand All @@ -141,10 +135,12 @@ def flatten_background(did, desired_flatten_limit):
)

undo_entry = mw.col.add_custom_undo_entry("flatten")
mw.progress.start()
mw.taskman.run_on_main(
lambda: mw.progress.start(label="Flattening", max=total_cnt, immediate=True)
)
cnt = 0
cancelled = False
total_cnt = len(cards_to_flatten)
flattened_cards = []

for new_due in range(today, today + 36500):
if cancelled:
Expand All @@ -167,8 +163,7 @@ def flatten_background(did, desired_flatten_limit):
new_ivl = new_due - last_review
card = update_card_due_ivl(card, new_ivl)
write_custom_data(card, "v", "flatten")
mw.col.update_card(card)
mw.col.merge_undo_entries(undo_entry)
flattened_cards.append(card)
cnt += 1
if cnt % 500 == 0:
mw.taskman.run_on_main(
Expand All @@ -181,5 +176,7 @@ def flatten_background(did, desired_flatten_limit):
if mw.progress.want_cancel():
cancelled = True

mw.col.update_cards(flattened_cards)
mw.col.merge_undo_entries(undo_entry)
finish_text = f"{cnt} cards flattened"
return finish_text
6 changes: 4 additions & 2 deletions schedule/reschedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def reschedule_background(
)
cnt = 0
cancelled = False
rescheduled_cards = []
for cid, _, _, desired_retention, maximum_interval in cards:
if cancelled:
break
Expand All @@ -294,8 +295,7 @@ def reschedule_background(
card = reschedule_card(cid, fsrs, filter_flag)
if card is None:
continue
mw.col.update_card(card)
mw.col.merge_undo_entries(undo_entry)
rescheduled_cards.append(card)
cnt += 1
if cnt % 500 == 0:
mw.taskman.run_on_main(
Expand All @@ -308,6 +308,8 @@ def reschedule_background(
if mw.progress.want_cancel():
cancelled = True

mw.col.update_cards(rescheduled_cards)
mw.col.merge_undo_entries(undo_entry)
finish_text = f"{cnt} cards rescheduled"

if config.auto_disperse_after_reschedule:
Expand Down

0 comments on commit d34ffe4

Please sign in to comment.