[PR #376] [MERGED] Fix billing-period key drift and fully reset provider counters (including audio) #679

Closed
opened 2026-03-13 21:03:27 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/AJaySi/ALwrity/pull/376
Author: @AJaySi
Created: 3/4/2026
Status: Merged
Merged: 3/5/2026
Merged by: @AJaySi

Base: mainHead: codex/update-reset_current_billing_period-logic


📝 Commits (1)

  • cf4c08f Align usage period keys with subscription window and reset audio counters

📊 Changes

1 file changed (+54 additions, -10 deletions)

View changed files

📝 backend/services/subscription/usage_tracking_service.py (+54 -10)

📄 Description

Motivation

  • Prevent boundary-month drift where reads/writes use different billing keys by deriving authoritative keys from UserSubscription.current_period_start/current_period_end instead of relying on a calendar month string alone.
  • Ensure reset_current_billing_period clears every tracked provider counter/cost so renewed plans start from a true zero state.
  • Reduce inconsistencies between usage logs, summaries, and alerts by centralizing billing-period resolution.

Description

  • Added internal helper def _get_authoritative_billing_period_keys(user_id, billing_period=None) to resolve canonical billing_period, lookup_periods, and subscription boundary metadata, with explicit-period and fallback behavior.
  • Replaced ad-hoc datetime.now().strftime("%Y-%m") / get_current_billing_period usage with the helper in track_api_usage, _update_usage_summary, _check_usage_alerts, and get_user_usage_stats so reads and writes use lookup_periods and the canonical period consistently.
  • Updated reset_current_billing_period to resolve the reset target from the subscription window (using the helper and billing_period.in_(lookup_periods)) and expanded reset coverage to include all tracked counters/costs such as audio_calls, audio_cost, and exa_calls.
  • Minor flow adjustment in get_user_usage_stats to preserve an explicitly requested period (requested_billing_period) while still using the authoritative lookup keys for DB queries and initialization.

Testing

  • Ran python -m py_compile backend/services/subscription/usage_tracking_service.py and the module compiled successfully.

Codex Task


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/AJaySi/ALwrity/pull/376 **Author:** [@AJaySi](https://github.com/AJaySi) **Created:** 3/4/2026 **Status:** ✅ Merged **Merged:** 3/5/2026 **Merged by:** [@AJaySi](https://github.com/AJaySi) **Base:** `main` ← **Head:** `codex/update-reset_current_billing_period-logic` --- ### 📝 Commits (1) - [`cf4c08f`](https://github.com/AJaySi/ALwrity/commit/cf4c08ff7cee1f17e00da49e05e0606b85bb7b37) Align usage period keys with subscription window and reset audio counters ### 📊 Changes **1 file changed** (+54 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `backend/services/subscription/usage_tracking_service.py` (+54 -10) </details> ### 📄 Description ### Motivation - Prevent boundary-month drift where reads/writes use different billing keys by deriving authoritative keys from `UserSubscription.current_period_start/current_period_end` instead of relying on a calendar month string alone. - Ensure `reset_current_billing_period` clears every tracked provider counter/cost so renewed plans start from a true zero state. - Reduce inconsistencies between usage logs, summaries, and alerts by centralizing billing-period resolution. ### Description - Added internal helper `def _get_authoritative_billing_period_keys(user_id, billing_period=None)` to resolve canonical `billing_period`, `lookup_periods`, and subscription boundary metadata, with explicit-period and fallback behavior. - Replaced ad-hoc `datetime.now().strftime("%Y-%m")` / `get_current_billing_period` usage with the helper in `track_api_usage`, `_update_usage_summary`, `_check_usage_alerts`, and `get_user_usage_stats` so reads and writes use `lookup_periods` and the canonical period consistently. - Updated `reset_current_billing_period` to resolve the reset target from the subscription window (using the helper and `billing_period.in_(lookup_periods)`) and expanded reset coverage to include all tracked counters/costs such as `audio_calls`, `audio_cost`, and `exa_calls`. - Minor flow adjustment in `get_user_usage_stats` to preserve an explicitly requested period (`requested_billing_period`) while still using the authoritative lookup keys for DB queries and initialization. ### Testing - Ran `python -m py_compile backend/services/subscription/usage_tracking_service.py` and the module compiled successfully. ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69a84690399c8328b493a9e834f89499) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-13 21:03:27 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#679
No description provided.