From ef85d7fd6f47c7e6f4a824e28ac7636340322823 Mon Sep 17 00:00:00 2001 From: stuppie Date: Wed, 7 Oct 2026 18:55:44 -0600 Subject: calculate balance_net_7d within prebuild_balance --- generalresearch/models/thl/product.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/generalresearch/models/thl/product.py b/generalresearch/models/thl/product.py index 84230fb..816339f 100644 --- a/generalresearch/models/thl/product.py +++ b/generalresearch/models/thl/product.py @@ -1205,6 +1205,7 @@ class Product(BaseModel, validate_assignment=True): LOG.debug(f"Product.prebuild_balance({self.uuid=})") self.balance = None + self.balance_net_7d = None if self.bp_account is None: self.prefetch_bp_account(thl_lm=thl_lm) assert self.bp_account is not None @@ -1219,6 +1220,16 @@ class Product(BaseModel, validate_assignment=True): balance_df = balance_df.drop( columns=["account_id", "product_id", "product_user_id"] ).set_index("time_idx") + balance_df.index = pd.to_datetime(balance_df.index, utc=True) + + cutoff = pd.Timestamp.now(tz="UTC") - timedelta(days=7) + balance_7d_df = balance_df.loc[balance_df.index >= cutoff] + self.balance_net_7d = ( + 0 + if balance_7d_df.empty + else ProductBalances.from_pandas(balance_7d_df).net + ) + balance = ProductBalances.from_pandas(balance_df) balance.product_id = self.uuid -- cgit v1.2.3