-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(flags): add billing info to redis for new flags (#25191)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
210e96d
commit 6b23684
Showing
4 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use anyhow::Result; | ||
use std::sync::Arc; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
use crate::flag_request::FlagRequestType; | ||
use crate::redis::{Client as RedisClient, CustomRedisError}; | ||
|
||
const CACHE_BUCKET_SIZE: u64 = 60 * 2; // duration in seconds | ||
|
||
pub fn get_team_request_key(team_id: i32, request_type: FlagRequestType) -> String { | ||
match request_type { | ||
FlagRequestType::Decide => format!("posthog:decide_requests:{}", team_id), | ||
FlagRequestType::LocalEvaluation => { | ||
format!("posthog:local_evaluation_requests:{}", team_id) | ||
} | ||
} | ||
} | ||
|
||
pub async fn increment_request_count( | ||
redis_client: Arc<dyn RedisClient + Send + Sync>, | ||
team_id: i32, | ||
count: i32, | ||
request_type: FlagRequestType, | ||
) -> Result<(), CustomRedisError> { | ||
let time_bucket = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs() | ||
/ CACHE_BUCKET_SIZE; | ||
let key_name = get_team_request_key(team_id, request_type); | ||
redis_client | ||
.hincrby(key_name, time_bucket.to_string(), Some(count)) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::test_utils::setup_redis_client; | ||
|
||
#[tokio::test] | ||
async fn test_get_team_request_key() { | ||
assert_eq!( | ||
get_team_request_key(123, FlagRequestType::Decide), | ||
"posthog:decide_requests:123" | ||
); | ||
assert_eq!( | ||
get_team_request_key(456, FlagRequestType::LocalEvaluation), | ||
"posthog:local_evaluation_requests:456" | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_increment_request_count() { | ||
let redis_client = setup_redis_client(None); | ||
|
||
let team_id = 789; | ||
let count = 5; | ||
|
||
// Test for Decide request type | ||
increment_request_count( | ||
redis_client.clone(), | ||
team_id, | ||
count, | ||
FlagRequestType::Decide, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// Test for LocalEvaluation request type | ||
increment_request_count( | ||
redis_client.clone(), | ||
team_id, | ||
count, | ||
FlagRequestType::LocalEvaluation, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let decide_key = get_team_request_key(team_id, FlagRequestType::Decide); | ||
let local_eval_key = get_team_request_key(team_id, FlagRequestType::LocalEvaluation); | ||
|
||
// Get the current time bucket | ||
let time_bucket = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs() | ||
/ CACHE_BUCKET_SIZE; | ||
|
||
// Verify the counts in Redis | ||
let decide_count: i32 = redis_client | ||
.hget(decide_key.clone(), time_bucket.to_string()) | ||
.await | ||
.unwrap() | ||
.parse() | ||
.unwrap(); | ||
let local_eval_count: i32 = redis_client | ||
.hget(local_eval_key.clone(), time_bucket.to_string()) | ||
.await | ||
.unwrap() | ||
.parse() | ||
.unwrap(); | ||
|
||
assert_eq!(decide_count, count); | ||
assert_eq!(local_eval_count, count); | ||
|
||
// Clean up Redis after the test | ||
redis_client.del(decide_key).await.unwrap(); | ||
redis_client.del(local_eval_key).await.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters