Skip to content

Commit

Permalink
Merge pull request #54 from TommyAmberson/feat/avg-revs
Browse files Browse the repository at this point in the history
Add macros for average daily reviews
  • Loading branch information
iamjustkoi authored Feb 22, 2024
2 parents b0eb372 + d56226a commit e658570
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ class Macro:

# Avg
CMD_CARD_AVERAGE_HOURS = '%card_avg_hrs'
CMD_CARD_AVERAGE_REVIEWS = '%card_avg_rev'
CMD_DAY_AVERAGE_HOURS = '%day_avg_hrs'
CMD_DAY_AVERAGE_REVIEWS = '%day_avg_rev'

# Previous Time
CMD_PREVIOUS_RANGE_HOURS = '%prev_range_hrs'
Expand Down Expand Up @@ -214,8 +216,12 @@ class Macro:
# Avg
CMD_CARD_AVERAGE_HOURS:
'''average study time per card for the current range''',
CMD_CARD_AVERAGE_REVIEWS:
'''average reviews per card for the current range''',
CMD_DAY_AVERAGE_HOURS:
'''average study time per day for the current range''',
CMD_DAY_AVERAGE_REVIEWS:
'''average reviews per day for the current range''',

# Previous Time
CMD_PREVIOUS_RANGE_HOURS:
Expand Down
39 changes: 35 additions & 4 deletions src/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ def time_macros():
def _avg_hrs_per_card(in_logs):
return (_total_hrs_in_revlog(in_logs) / len(in_logs)) if len(in_logs) > 0 else 0

def _avg_reviews_per_card(in_logs):
return (_reviews_in_revlog(in_logs) / len(in_logs)) if len(in_logs) > 0 else 0

cmd = Macro.CMD_ETA_HOURS
pattern = _time_pattern(cmd)
for match in re.findall(pattern, updated_string):
Expand Down Expand Up @@ -440,6 +443,20 @@ def _total_due_in_tree(tree: DeckTreeNode):
f'{cell_data[unit_key]}',
)

cmd = Macro.CMD_DAY_AVERAGE_REVIEWS
pattern = _time_pattern(cmd)
for match in re.findall(pattern, updated_string):
logs = _cached_log(
_cache_key(cmd, cell_data[Config.RANGE]),
addon_config[Config.EXCLUDED_DIDS],
_range_time_ms(),
)
from_date = datetime.fromtimestamp(_range_time_ms()[0] / 1000)
to_date = datetime.fromtimestamp(_range_time_ms()[1] / 1000)
days_in_logs = (to_date - from_date).days
avg_revs = _reviews_in_revlog(match, logs) / (days_in_logs if days_in_logs > 0 else 1)
_sub_text(match, str(round(avg_revs)))

cmd = Macro.CMD_CARD_AVERAGE_HOURS
pattern = _time_pattern(cmd)
for match in re.findall(pattern, updated_string):
Expand All @@ -453,6 +470,14 @@ def _total_due_in_tree(tree: DeckTreeNode):
f'{cell_data[unit_key]}',
)

cmd = Macro.CMD_CARD_AVERAGE_REVIEWS
pattern = _time_pattern(cmd)
for match in re.findall(pattern, updated_string):
logs = _cached_log(cmd, addon_config[Config.EXCLUDED_DIDS], _range_time_ms())
avg_revs = _avg_reviews_per_card(logs)

_sub_text(match, str(round(avg_revs)))

cmd = Macro.CMD_HIGHEST_DAY_HOURS
pattern = _time_pattern(cmd)
for match in re.findall(pattern, updated_string):
Expand Down Expand Up @@ -846,17 +871,23 @@ def _update_string_time(repl: str, revlog: list = None):
f'{_formatted_time(total_hrs, precision, addon_config[Config.USE_DECIMAL])} {cell_data[unit_key]}'
)

def _reviews_in_revlog(repl: str, revlog: list = None):
if revlog is None:
return 0

card_states = _states(repl)
filtered_logs = _logs_with_states(revlog, card_states) if card_states else revlog

return len(filtered_logs)

def _update_string_reviews(repl: str, revlog: list = None):
nonlocal updated_string

if revlog is None:
updated_string = re.sub(fr'(?<!%){repl}', f'ERR', updated_string)
return

card_states = _states(repl)
filtered_logs = _logs_with_states(revlog, card_states) if card_states else revlog

total_reviews = len(filtered_logs)
total_reviews = _reviews_in_revlog(repl, revlog)

_sub_text(repl, str(total_reviews))

Expand Down

0 comments on commit e658570

Please sign in to comment.