iamjustkoi / StudyTimeStats

Customizable statistics add-on for Anki.
https://ankiweb.net/shared/info/1247171202
MIT License
21 stars 4 forks source link

Success Rate / Retention #41

Open AustinHasten opened 11 months ago

AustinHasten commented 11 months ago

Feature Description Add a macro for Success Rate / Retention. i.e. the percentage of reviews which you passed rather than failed.

I spent a minute looking through the code to see if I could figure out how to add this myself but it's pretty dense so I just added this function to overview.py:

def true_retention(days_back=0, deck='%'):
    try:
        today = datetime.combine(date.today(), datetime.min.time())
        start_date = today - timedelta(days=days_back)
        start_epoch = start_date.timestamp() * 1000  # nanoseconds to milliseconds or something
        revlog_cmd = f'''
            SELECT
                r.ease
            FROM
                revlog r
                JOIN cards c ON r.cid = c.id
                JOIN decks d ON d.id = c.did
            WHERE
                r.type = 1
                AND r.id > {start_epoch}
                AND d.name like '{deck}'
        '''
        full_revlog = mw.col.db.all(revlog_cmd)
        passed = len([_ for _ in full_revlog if _[0] > 1])
        flunked = len([_ for _ in full_revlog if _[0] == 1])
        if (passed + flunked) < 1:
            return 'No reviews'
        true_retention = passed / (passed + flunked) * 100
        return f'{int(true_retention)}%'
    except Exception as e:
        print(e)
        return 'Error'

And use %eval{true_retention(deck='Deck Name')} as my macro, which seems to be working the way I want it to, but it would be nice if this was integrated.

Great addon, by the way.

iamjustkoi commented 10 months ago

Ope, thanks! Can try to see about adding something that might make this a little easier in the future.

If you wanted to try taking a whack at forking and submitting a PR I can also review that and merge it a little sooner, but I can defs look into combining this with some other requests when I'm more available (hopefully soon, but no guarantees)!

Either way, thanks for suggestion and appreciate the code/write up too!