Skip to content

Conversation

amalcaraz
Copy link
Collaborator

@amalcaraz amalcaraz commented Jun 24, 2025

Summary

Implements historical pricing support for message cost recalculation, ensuring costs are calculated using the pricing models that were active when messages were originally created.

Key Changes

🔧 New Pricing Timeline System

  • Created pricing_utils.py with reusable pricing model utilities
  • Implemented get_pricing_timeline() to track pricing changes over time
  • Added proper cumulative merging of pricing aggregate elements

⚡ Enhanced Cost Recalculation

  • Updated /api/v0/price/recalculate endpoint to use historical pricing
  • Messages processed chronologically (oldest to newest) with correct pricing timeline
  • Maintains pricing model state efficiently during batch processing

🔄 Migration Refactoring

  • Refactored migration 0033_1c06d0ade60c to use reusable build_default_pricing_model()
  • Removed hardcoded pricing dictionary in favor of DEFAULT_PRICE_AGGREGATE constant
  • Improved maintainability and consistency

🧪 Comprehensive Testing

  • Added 60+ test cases covering timeline functionality, API endpoints, and migration compatibility
  • Tests ensure historical accuracy and proper error handling
  • Integration tests verify end-to-end pricing application

API Endpoints

  • POST /api/v0/price/recalculate - Recalculate all message costs with historical pricing
  • POST /api/v0/price/{item_hash}/recalculate - Recalculate specific message costs

Auth methods

The new endpoints have a basic token auth system with signed tokens that will expire after 5 minutes

  • Create a new token using the helper script:
    ./deployment/scripts/auth_token.py --create-token
  • Include the X-Auth-Token header with the base64 token generated by the script

Benefits

✅ Historical Accuracy: Costs calculated using pricing active when messages were created✅ Performance: Efficient timeline processing without repeated DB queries✅ Maintainability:
Reusable pricing utilities reduce code duplication✅ Robustness: Comprehensive error handling and extensive test coverage

@amalcaraz amalcaraz requested a review from nesitor June 24, 2025 14:59
@amalcaraz amalcaraz self-assigned this Jun 24, 2025
@amalcaraz amalcaraz added the enhancement New feature or request label Jun 24, 2025
),
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we shouldn't have to modify a migration already executed on the rest of the CCNs to prevent unconsistencies on the database.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are still using a constant (DEFAULT_PRICE_AGGREGATE) with the same hardcoded default pricing than before. The only change is a type conversion from Dict[str, dict] to Dict[ProductPriceType, ProductPricing] which is the type that the migration uses

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I think that we don't have to touch an already executed migration, just to be sure that doesn't crash and for keeping the same migration history on all the CCNs.

@amalcaraz amalcaraz requested a review from nesitor August 11, 2025 15:18
@nesitor nesitor self-requested a review August 12, 2025 14:39
Copy link
Member

@nesitor nesitor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good except the migration change, that I prefer to leave as is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants