aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstuppie2026-10-07 18:55:44 -0600
committerstuppie2026-10-07 18:55:44 -0600
commitef85d7fd6f47c7e6f4a824e28ac7636340322823 (patch)
treea78c39a3d5b1ec6efacd8630a738bdaa2f358a8c
parentcce45f9d236eddaddb2fc184cba6850f07220118 (diff)
downloadgeneralresearch-ef85d7fd6f47c7e6f4a824e28ac7636340322823.tar.gz
generalresearch-ef85d7fd6f47c7e6f4a824e28ac7636340322823.zip
calculate balance_net_7d within prebuild_balance
-rw-r--r--generalresearch/models/thl/product.py11
1 files changed, 11 insertions, 0 deletions
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