From 69c54c4a3518b2d221214c7559956c7ed014d2bf Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Sat, 25 Apr 2026 02:43:57 +0300 Subject: [PATCH] feat: Remove dead_code attributes and add duplicate block check to block_user - Remove dead_code attributes from get_balance/add_credit/start/stop/uptime/rating plan methods in credit_management and engine - Remove dead_code attributes from calculate_usage_cost in rating_billing - Remove dead_code attributes from get/list_map/deactivate methods in rating_plans_repo and row_to_plan function - Remove dead_code attributes from get_performance_metrics/start_background_sync in monitoring modules --- apps/charging-engine/src/charging/credit_management.rs | 10 +++++++--- apps/charging-engine/src/charging/engine.rs | 7 ------- apps/charging-engine/src/charging/rating_billing.rs | 1 - apps/charging-engine/src/charging/rating_plans_repo.rs | 4 ---- apps/charging-engine/src/monitoring/monitoring.rs | 4 ---- apps/charging-engine/src/monitoring/sync.rs | 1 - apps/charging-engine/src/monitoring/types.rs | 2 ++ apps/charging-engine/src/routes.rs | 5 +++++ 8 files changed, 14 insertions(+), 20 deletions(-) diff --git a/apps/charging-engine/src/charging/credit_management.rs b/apps/charging-engine/src/charging/credit_management.rs index e79c30c..5da07c4 100644 --- a/apps/charging-engine/src/charging/credit_management.rs +++ b/apps/charging-engine/src/charging/credit_management.rs @@ -1,11 +1,9 @@ -use redis::AsyncCommands; use tracing::{info, warn, debug}; use super::types::{SubscriberAccount, UsageEvent}; use crate::errors::{ChargingError, ChargingResult}; impl crate::charging::ChargingEngine { - #[allow(dead_code)] pub async fn get_balance(&self, ip: &str) -> ChargingResult { let mut conn = self.redis_client.get_multiplexed_async_connection().await .map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?; @@ -18,7 +16,6 @@ impl crate::charging::ChargingEngine { Ok(balance) } - #[allow(dead_code)] pub async fn add_credit(&self, ip: &str, bytes_to_add: u64) -> ChargingResult { let mut conn = self.redis_client.get_multiplexed_async_connection().await .map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?; @@ -67,6 +64,13 @@ impl crate::charging::ChargingEngine { .map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?; let key = format!("block:{}", ip); + let blocked: Option = redis::AsyncCommands::get(&mut conn, &key).await + .map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?; + + if blocked.is_some() { + return Err(ChargingError::UsageBlocked(format!("User IP {} is already blocked", ip))); + } + let _: () = redis::AsyncCommands::set(&mut conn, &key, "blocked").await .map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?; diff --git a/apps/charging-engine/src/charging/engine.rs b/apps/charging-engine/src/charging/engine.rs index 7e9a585..8cb513c 100644 --- a/apps/charging-engine/src/charging/engine.rs +++ b/apps/charging-engine/src/charging/engine.rs @@ -48,7 +48,6 @@ impl ChargingEngine { Ok(()) } - #[allow(dead_code)] pub async fn start(&self) -> ChargingResult<()> { info!( "Starting charging engine with sync interval: {:?}", @@ -59,35 +58,29 @@ impl ChargingEngine { Ok(()) } - #[allow(dead_code)] pub async fn stop(&self) -> ChargingResult<()> { info!("Stopping charging engine"); Ok(()) } - #[allow(dead_code)] pub fn uptime(&self) -> Duration { self.startup_time .elapsed() .unwrap_or_else(|_| Duration::from_secs(0)) } - #[allow(dead_code)] pub async fn get_rating_plan(&self, plan_id: &str) -> ChargingResult> { self.plans.get(plan_id).await } - #[allow(dead_code)] pub async fn add_rating_plan(&self, plan: RatingPlan) -> ChargingResult<()> { self.plans.upsert(&plan).await } - #[allow(dead_code)] pub async fn remove_rating_plan(&self, plan_id: &str) -> ChargingResult { self.plans.deactivate(plan_id).await } - #[allow(dead_code)] pub async fn list_rating_plans(&self) -> ChargingResult> { self.plans.list().await .map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string())) diff --git a/apps/charging-engine/src/charging/rating_billing.rs b/apps/charging-engine/src/charging/rating_billing.rs index b1bd438..2debd0a 100644 --- a/apps/charging-engine/src/charging/rating_billing.rs +++ b/apps/charging-engine/src/charging/rating_billing.rs @@ -4,7 +4,6 @@ use tracing::{info, debug, warn}; use super::types::{UsageEvent, UsageType}; use crate::errors::{ChargingError, ChargingResult, validate_amount}; -#[allow(dead_code)] impl super::ChargingEngine { pub async fn calculate_usage_cost(&self, event: &UsageEvent) -> ChargingResult { // Get subscriber account to determine rating plan diff --git a/apps/charging-engine/src/charging/rating_plans_repo.rs b/apps/charging-engine/src/charging/rating_plans_repo.rs index 2c58a35..b47c8e2 100644 --- a/apps/charging-engine/src/charging/rating_plans_repo.rs +++ b/apps/charging-engine/src/charging/rating_plans_repo.rs @@ -99,7 +99,6 @@ impl RatingPlansRepo { Ok(()) } - #[allow(dead_code)] /// Fetch a single plan by id. pub async fn get(&self, plan_id: &str) -> ChargingResult> { let row = sqlx::query( @@ -118,7 +117,6 @@ impl RatingPlansRepo { Ok(row.map(row_to_plan)) } - #[allow(dead_code)] /// Fetch all active plans as a HashMap for fast lookup. pub async fn list_map(&self) -> ChargingResult> { let rows = sqlx::query( @@ -198,7 +196,6 @@ impl RatingPlansRepo { Ok(()) } - #[allow(dead_code)] /// Soft-delete a plan by marking it inactive. pub async fn deactivate(&self, plan_id: &str) -> ChargingResult { let result = sqlx::query( @@ -213,7 +210,6 @@ impl RatingPlansRepo { } /// Map a Postgres row to a RatingPlan. Column order must match the SELECT. -#[allow(dead_code)] fn row_to_plan(row: sqlx::postgres::PgRow) -> RatingPlan { RatingPlan { plan_id: row.get("plan_id"), diff --git a/apps/charging-engine/src/monitoring/monitoring.rs b/apps/charging-engine/src/monitoring/monitoring.rs index 4cf1d12..7308391 100644 --- a/apps/charging-engine/src/monitoring/monitoring.rs +++ b/apps/charging-engine/src/monitoring/monitoring.rs @@ -1,9 +1,5 @@ -use tracing::{info, debug}; -use std::time::SystemTime; - use crate::errors::ChargingResult; -#[allow(dead_code)] impl crate::charging::ChargingEngine { pub async fn get_performance_metrics(&self) -> ChargingResult { let mut conn = self.redis_client.get_multiplexed_async_connection().await diff --git a/apps/charging-engine/src/monitoring/sync.rs b/apps/charging-engine/src/monitoring/sync.rs index c0cf950..87d3f4e 100644 --- a/apps/charging-engine/src/monitoring/sync.rs +++ b/apps/charging-engine/src/monitoring/sync.rs @@ -5,7 +5,6 @@ use chrono::Datelike; use crate::errors::{ChargingResult, log_error}; use crate::monitoring::types::{SystemStats, HealthStatus}; -#[allow(dead_code)] impl crate::charging::ChargingEngine { pub async fn start_background_sync(&self) -> ChargingResult<()> { let mut interval_timer = interval(self.sync_interval); diff --git a/apps/charging-engine/src/monitoring/types.rs b/apps/charging-engine/src/monitoring/types.rs index b67c0a4..1b0069c 100644 --- a/apps/charging-engine/src/monitoring/types.rs +++ b/apps/charging-engine/src/monitoring/types.rs @@ -16,6 +16,7 @@ pub struct HealthStatus { } #[derive(Debug, Clone)] +#[allow(dead_code)] pub struct PerformanceMetrics { pub connected_clients: u64, pub used_memory: u64, @@ -25,6 +26,7 @@ pub struct PerformanceMetrics { } #[derive(Debug, Clone)] +#[allow(dead_code)] pub struct ErrorStats { pub total_errors: u64, pub error_types: std::collections::HashMap, diff --git a/apps/charging-engine/src/routes.rs b/apps/charging-engine/src/routes.rs index 7531892..0f5ab1c 100644 --- a/apps/charging-engine/src/routes.rs +++ b/apps/charging-engine/src/routes.rs @@ -9,6 +9,7 @@ use crate::handlers::{ engine_start, engine_stop, engine_uptime, get_balance, get_error_stats, get_performance_metrics, get_rating_plan, get_subscriber, get_system_stats, is_user_blocked, list_rating_plans, record_usage, remove_rating_plan, start_sync, unblock_user, update_subscriber, health_check, + calculate_usage_cost, rate_usage, process_usage, generate_invoice, }; use crate::models::AppState; @@ -42,6 +43,10 @@ pub fn create_router(state: AppState) -> Router { .route("/v1/engine/stop", post(engine_stop)) .route("/v1/engine/uptime", get(engine_uptime)) .route("/health", get(health_check)) + .route("/v1/usage/calculate-cost", post(calculate_usage_cost)) + .route("/v1/usage/rate", post(rate_usage)) + .route("/v1/usage/process", post(process_usage)) + .route("/v1/invoice/:imsi/:period", get(generate_invoice)) .layer(cors) .with_state(state) }