From dc4b6d501ccccee370b79b2e862785e5cd3def06 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Fri, 21 Jul 2023 13:58:09 -0700 Subject: [PATCH] move active_premium opt_in check --- web3_proxy/src/stats/mod.rs | 4 +-- web3_proxy/src/stats/stat_buffer.rs | 38 ++++++++++++++--------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/web3_proxy/src/stats/mod.rs b/web3_proxy/src/stats/mod.rs index 89e0203d..d4b571f9 100644 --- a/web3_proxy/src/stats/mod.rs +++ b/web3_proxy/src/stats/mod.rs @@ -162,8 +162,8 @@ impl RpcQueryStats { } /// stats for a single key - fn owned_timeseries_key(&self) -> Option { - if !self.paid_credits_used { + fn owned_timeseries_key(&self, active_premium: bool) -> Option { + if !active_premium { return None; } diff --git a/web3_proxy/src/stats/stat_buffer.rs b/web3_proxy/src/stats/stat_buffer.rs index da9531ba..e2f75b5b 100644 --- a/web3_proxy/src/stats/stat_buffer.rs +++ b/web3_proxy/src/stats/stat_buffer.rs @@ -257,16 +257,18 @@ impl StatBuffer { } else if user_balance.active_premium() { active_premium = true; - // paid credits were not used, but now we have active premium. invalidate the caches - // TODO: this seems unliekly. should we warn if this happens so we can investigate? - if let Err(err) = self - .user_balance_cache - .invalidate(&user_balance.user_id, &db_conn, &self.rpc_secret_key_cache) - .await - { - // was premium, but isn't anymore due to paying for this query. clear the cache - // TODO: stop at <$0.000001 instead of negative? - warn!(?err, "unable to clear caches"); + if user_balance.downgrade_tier_id.is_some() { + // paid credits were not used, but now we have active premium. invalidate the caches + // TODO: this seems unliekly. should we warn if this happens so we can investigate? + if let Err(err) = self + .user_balance_cache + .invalidate(&user_balance.user_id, &db_conn, &self.rpc_secret_key_cache) + .await + { + // was premium, but isn't anymore due to paying for this query. clear the cache + // TODO: stop at <$0.000001 instead of negative? + warn!(?err, "unable to clear caches"); + } } } @@ -284,16 +286,12 @@ impl StatBuffer { } if self.influxdb_client.is_some() { - // TODO: round the timestamp at all? - - if active_premium { - if let Some(opt_in_timeseries_key) = stat.owned_timeseries_key() { - self.opt_in_timeseries_buffer - .entry(opt_in_timeseries_key) - .or_default() - .add(stat.clone(), approximate_balance_remaining) - .await; - } + if let Some(opt_in_timeseries_key) = stat.owned_timeseries_key(active_premium) { + self.opt_in_timeseries_buffer + .entry(opt_in_timeseries_key) + .or_default() + .add(stat.clone(), approximate_balance_remaining) + .await; } let global_timeseries_key = stat.global_timeseries_key();