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
This commit is contained in:
nutcas3
2026-04-25 02:43:57 +03:00
parent 607040da6c
commit 69c54c4a35
8 changed files with 14 additions and 20 deletions
@@ -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<u64> {
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<u64> {
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<String> = 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()))?;
@@ -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<Option<RatingPlan>> {
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<bool> {
self.plans.deactivate(plan_id).await
}
#[allow(dead_code)]
pub async fn list_rating_plans(&self) -> ChargingResult<Vec<RatingPlan>> {
self.plans.list().await
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))
@@ -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<f64> {
// Get subscriber account to determine rating plan
@@ -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<Option<RatingPlan>> {
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<HashMap<String, RatingPlan>> {
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<bool> {
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"),
@@ -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<PerformanceMetrics> {
let mut conn = self.redis_client.get_multiplexed_async_connection().await
@@ -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);
@@ -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<String, u64>,
+5
View File
@@ -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)
}