move active_premium opt_in check

This commit is contained in:
Bryan Stitt 2023-07-21 13:58:09 -07:00
parent 044d19e3ec
commit dc4b6d501c
2 changed files with 20 additions and 22 deletions

@ -162,8 +162,8 @@ impl RpcQueryStats {
} }
/// stats for a single key /// stats for a single key
fn owned_timeseries_key(&self) -> Option<RpcQueryKey> { fn owned_timeseries_key(&self, active_premium: bool) -> Option<RpcQueryKey> {
if !self.paid_credits_used { if !active_premium {
return None; return None;
} }

@ -257,16 +257,18 @@ impl StatBuffer {
} else if user_balance.active_premium() { } else if user_balance.active_premium() {
active_premium = true; active_premium = true;
// paid credits were not used, but now we have active premium. invalidate the caches if user_balance.downgrade_tier_id.is_some() {
// TODO: this seems unliekly. should we warn if this happens so we can investigate? // paid credits were not used, but now we have active premium. invalidate the caches
if let Err(err) = self // TODO: this seems unliekly. should we warn if this happens so we can investigate?
.user_balance_cache if let Err(err) = self
.invalidate(&user_balance.user_id, &db_conn, &self.rpc_secret_key_cache) .user_balance_cache
.await .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? // was premium, but isn't anymore due to paying for this query. clear the cache
warn!(?err, "unable to clear caches"); // 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() { if self.influxdb_client.is_some() {
// TODO: round the timestamp at all? if let Some(opt_in_timeseries_key) = stat.owned_timeseries_key(active_premium) {
self.opt_in_timeseries_buffer
if active_premium { .entry(opt_in_timeseries_key)
if let Some(opt_in_timeseries_key) = stat.owned_timeseries_key() { .or_default()
self.opt_in_timeseries_buffer .add(stat.clone(), approximate_balance_remaining)
.entry(opt_in_timeseries_key) .await;
.or_default()
.add(stat.clone(), approximate_balance_remaining)
.await;
}
} }
let global_timeseries_key = stat.global_timeseries_key(); let global_timeseries_key = stat.global_timeseries_key();