google-documents-read-caching #2

Merged
kacerr merged 4 commits from google-documents-read-caching into main 2026-03-11 10:13:18 +00:00
Showing only changes of commit 7170cd4d27 - Show all commits

46
app.py
View File

@@ -21,27 +21,15 @@ from attendance import get_members_with_fees, get_junior_members_with_fees, SHEE
from match_payments import reconcile, fetch_sheet_data, fetch_exceptions, normalize, DEFAULT_SPREADSHEET_ID as PAYMENTS_SHEET_ID from match_payments import reconcile, fetch_sheet_data, fetch_exceptions, normalize, DEFAULT_SPREADSHEET_ID as PAYMENTS_SHEET_ID
from cache_utils import get_sheet_modified_time, read_cache, write_cache, _LAST_CHECKED from cache_utils import get_sheet_modified_time, read_cache, write_cache, _LAST_CHECKED
def get_cached_data(cache_key, sheet_id, fetch_func, *args, **kwargs): def get_cached_data(cache_key, sheet_id, fetch_func, *args, serialize=None, deserialize=None, **kwargs):
mod_time = get_sheet_modified_time(cache_key) mod_time = get_sheet_modified_time(cache_key)
if mod_time: if mod_time:
cached = read_cache(cache_key, mod_time) cached = read_cache(cache_key, mod_time)
if cached is not None: if cached is not None:
return cached return deserialize(cached) if deserialize else cached
data = fetch_func(*args, **kwargs) data = fetch_func(*args, **kwargs)
if mod_time: if mod_time:
write_cache(cache_key, mod_time, data) write_cache(cache_key, mod_time, serialize(data) if serialize else data)
return data
def get_cached_exceptions(sheet_id, creds_path):
cache_key = "exceptions_dict"
mod_time = get_sheet_modified_time(cache_key)
if mod_time:
cached = read_cache(cache_key, mod_time)
if cached is not None:
return {tuple(k): v for k, v in cached}
data = fetch_exceptions(sheet_id, creds_path)
if mod_time:
write_cache(cache_key, mod_time, [[list(k), v] for k, v in data.items()])
return data return data
def get_month_labels(sorted_months, merged_months): def get_month_labels(sorted_months, merged_months):
@@ -123,7 +111,12 @@ def fees():
# Get exceptions for formatting # Get exceptions for formatting
credentials_path = ".secret/fuj-management-bot-credentials.json" credentials_path = ".secret/fuj-management-bot-credentials.json"
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path) exceptions = get_cached_data(
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
PAYMENTS_SHEET_ID, credentials_path,
serialize=lambda d: [[list(k), v] for k, v in d.items()],
deserialize=lambda c: {tuple(k): v for k, v in c},
)
record_step("fetch_exceptions") record_step("fetch_exceptions")
formatted_results = [] formatted_results = []
@@ -181,7 +174,12 @@ def fees_juniors():
# Get exceptions for formatting (reusing payments sheet) # Get exceptions for formatting (reusing payments sheet)
credentials_path = ".secret/fuj-management-bot-credentials.json" credentials_path = ".secret/fuj-management-bot-credentials.json"
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path) exceptions = get_cached_data(
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
PAYMENTS_SHEET_ID, credentials_path,
serialize=lambda d: [[list(k), v] for k, v in d.items()],
deserialize=lambda c: {tuple(k): v for k, v in c},
)
record_step("fetch_exceptions") record_step("fetch_exceptions")
formatted_results = [] formatted_results = []
@@ -253,7 +251,12 @@ def reconcile_view():
transactions = get_cached_data("payments_transactions", PAYMENTS_SHEET_ID, fetch_sheet_data, PAYMENTS_SHEET_ID, credentials_path) transactions = get_cached_data("payments_transactions", PAYMENTS_SHEET_ID, fetch_sheet_data, PAYMENTS_SHEET_ID, credentials_path)
record_step("fetch_payments") record_step("fetch_payments")
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path) exceptions = get_cached_data(
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
PAYMENTS_SHEET_ID, credentials_path,
serialize=lambda d: [[list(k), v] for k, v in d.items()],
deserialize=lambda c: {tuple(k): v for k, v in c},
)
record_step("fetch_exceptions") record_step("fetch_exceptions")
result = reconcile(members, sorted_months, transactions, exceptions) result = reconcile(members, sorted_months, transactions, exceptions)
record_step("reconcile") record_step("reconcile")
@@ -346,7 +349,12 @@ def reconcile_juniors_view():
transactions = get_cached_data("payments_transactions", PAYMENTS_SHEET_ID, fetch_sheet_data, PAYMENTS_SHEET_ID, credentials_path) transactions = get_cached_data("payments_transactions", PAYMENTS_SHEET_ID, fetch_sheet_data, PAYMENTS_SHEET_ID, credentials_path)
record_step("fetch_payments") record_step("fetch_payments")
exceptions = get_cached_exceptions(PAYMENTS_SHEET_ID, credentials_path) exceptions = get_cached_data(
"exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions,
PAYMENTS_SHEET_ID, credentials_path,
serialize=lambda d: [[list(k), v] for k, v in d.items()],
deserialize=lambda c: {tuple(k): v for k, v in c},
)
record_step("fetch_exceptions") record_step("fetch_exceptions")
# Adapt junior tuple format (name, tier, {month: (fee, total_count, adult_count, junior_count)}) # Adapt junior tuple format (name, tier, {month: (fee, total_count, adult_count, junior_count)})