schemas_v1_db_queries_cost - OmniCloudOrg/OmniOrchestrator GitHub Wiki

cost (src/schemas/v1/db/queries)

Path: src/schemas/v1/db/queries/cost.rs

Table of Contents

Public Items

async fn list_resource_types

Definition

pub async fn list_resource_types(pool: &Pool<MySql>, page: i64, per_page: i64) -> anyhow::Result<Vec<ResourceType>> {
    println!("Attempting to fetch resource types from database...");

Documentation

Retrieves a paginated list of resource types from the database.

Arguments
  • pool - Database connection pool for executing the query
  • page - Zero-based page number (e.g., 0 for first page, 1 for second page)
  • per_page - Number of records to fetch per page
Returns
  • Ok(Vec<ResourceType>) - Successfully retrieved list of resource types
  • Err(anyhow::Error) - Failed to fetch resource types, with context

async fn count_resource_types

Definition

pub async fn count_resource_types(pool: &Pool<MySql>) -> anyhow::Result<i64> {
    let count = sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM resource_types")
        .fetch_one(pool)
        .await
        .context("Failed to count resource types")?;

Documentation

Counts the total number of resource types in the database.

async fn get_resource_type_by_id

Definition

pub async fn get_resource_type_by_id(pool: &Pool<MySql>, id: i32) -> anyhow::Result<ResourceType> {
    let resource_type = sqlx::query_as::<_, ResourceType>("SELECT * FROM resource_types WHERE id = ?")
        .bind(id)
        .fetch_one(pool)
        .await
        .context("Failed to fetch resource type")?;

Documentation

Retrieves a specific resource type by its unique identifier.

async fn create_resource_type

Definition

pub async fn create_resource_type(
    pool: &Pool<MySql>,
    name: &str,
    category: &str,
    unit_of_measurement: &str,
    description: Option<&str>
) -> anyhow::Result<ResourceType> {
    // ... function body
}

Documentation

Creates a new resource type in the database.

async fn update_resource_type

Definition

pub async fn update_resource_type(
    pool: &Pool<MySql>,
    id: i32,
    name: Option<&str>,
    category: Option<&str>,
    unit_of_measurement: Option<&str>,
    description: Option<&str>
) -> anyhow::Result<ResourceType> {
    // ... function body
}

Documentation

Updates an existing resource type in the database.

async fn delete_resource_type

Definition

pub async fn delete_resource_type(pool: &Pool<MySql>, id: i32) -> anyhow::Result<()> {
    let mut tx = pool.begin().await?;

Documentation

Deletes a resource type from the database.

async fn list_cost_metrics

Definition

pub async fn list_cost_metrics(
    pool: &Pool<MySql>, 
    page: i64, 
    per_page: i64,
    resource_type_id: Option<i32>,
    provider_id: Option<i64>,
    app_id: Option<i64>,
    start_date: Option<DateTime<Utc>>,
    end_date: Option<DateTime<Utc>>,
    billing_period: Option<&str>
) -> anyhow::Result<Vec<CostMetricWithType>> {
    // ... function body
}

Documentation

Retrieves a paginated list of cost metrics from the database, with optional filtering.

Arguments
  • pool - Database connection pool for executing the query
  • page - Zero-based page number (e.g., 0 for first page, 1 for second page)
  • per_page - Number of records to fetch per page
  • resource_type_id - Optional filter by resource type
  • provider_id - Optional filter by provider
  • app_id - Optional filter by application
  • start_date - Optional filter for metrics after this date
  • end_date - Optional filter for metrics before this date
  • billing_period - Optional filter by billing period (e.g., "2025-05")
Returns
  • Ok(Vec<CostMetricWithType>) - Successfully retrieved list of cost metrics with type information
  • Err(anyhow::Error) - Failed to fetch cost metrics, with context

async fn count_cost_metrics

Definition

pub async fn count_cost_metrics(
    pool: &Pool<MySql>,
    resource_type_id: Option<i32>,
    provider_id: Option<i64>,
    app_id: Option<i64>,
    start_date: Option<DateTime<Utc>>,
    end_date: Option<DateTime<Utc>>,
    billing_period: Option<&str>
) -> anyhow::Result<i64> {
    // ... function body
}

Documentation

Counts the total number of cost metrics in the database with optional filtering.

async fn create_cost_metric

Definition

pub async fn create_cost_metric(
    pool: &Pool<MySql>,
    resource_type_id: i32,
    provider_id: Option<i64>,
    region_id: Option<i64>,
    app_id: Option<i64>,
    worker_id: Option<i64>,
    org_id: Option<i64>,
    start_time: DateTime<Utc>,
    end_time: DateTime<Utc>,
    usage_quantity: f64,
    unit_cost: f64,
    currency: &str,
    total_cost: f64,
    discount_percentage: Option<f64>,
    discount_reason: Option<&str>,
    billing_period: Option<&str>
) -> anyhow::Result<CostMetric> {
    // ... function body
}

Documentation

Creates a new cost metric in the database.

async fn get_cost_metric_by_id

Definition

pub async fn get_cost_metric_by_id(pool: &Pool<MySql>, id: i64) -> anyhow::Result<CostMetricWithType> {
    let cost_metric = sqlx::query_as::<_, CostMetricWithType>(
        r#"
        SELECT 
            cm.id,
            cm.resource_type_id,
            cm.provider_id,
            cm.region_id,
            cm.app_id,
            cm.worker_id,
            cm.org_id,
            cm.start_time,
            cm.end_time,
            cm.usage_quantity,
            cm.unit_cost,
            cm.currency,
            cm.total_cost,
            cm.discount_percentage,
            cm.discount_reason,
            cm.billing_period,
            cm.created_at,
    // ... function definition continues
    // ... function body
}

Documentation

Retrieves a specific cost metric by its unique identifier, with resource type information.

async fn delete_cost_metric

Definition

pub async fn delete_cost_metric(pool: &Pool<MySql>, id: i64) -> anyhow::Result<()> {
    let mut tx = pool.begin().await?;

Documentation

Deletes a cost metric from the database.

async fn get_cost_metrics_by_dimension

Definition

pub async fn get_cost_metrics_by_dimension(
    pool: &Pool<MySql>,
    dimension: &str,
    start_date: DateTime<Utc>,
    end_date: DateTime<Utc>,
    limit: i64
) -> anyhow::Result<Vec<(String, f64)>> {
    // ... function body
}

Documentation

Get aggregate cost metrics grouped by a specific dimension.

Arguments
  • pool - Database connection pool for executing the query
  • dimension - Dimension to group by ('app', 'provider', 'resource_type', 'region', 'worker', 'org')
  • start_date - Filter for metrics after this date
  • end_date - Filter for metrics before this date
  • limit - Maximum number of results to return
Returns
  • Ok(Vec<(String, f64)>) - Successfully retrieved aggregated costs by dimension
  • Err(anyhow::Error) - Failed to fetch cost metrics, with context

async fn get_app_cost_over_time

Definition

pub async fn get_app_cost_over_time(
    pool: &Pool<MySql>,
    app_id: i64,
    interval: &str,
    start_date: DateTime<Utc>,
    end_date: DateTime<Utc>
) -> anyhow::Result<Vec<(DateTime<Utc>, f64)>> {
    // ... function body
}

Documentation

Retrieves cost metrics over time for a specific application.

Arguments
  • pool - Database connection pool for executing the query
  • app_id - ID of the application to analyze
  • interval - Time interval ('day', 'week', 'month')
  • start_date - Filter for metrics after this date
  • end_date - Filter for metrics before this date
Returns
  • Ok(Vec<(DateTime<Utc>, f64)>) - Successfully retrieved costs over time
  • Err(anyhow::Error) - Failed to fetch cost metrics, with context

async fn list_cost_budgets

Definition

pub async fn list_cost_budgets(pool: &Pool<MySql>, page: i64, per_page: i64) -> anyhow::Result<Vec<CostBudget>> {
    println!("Attempting to fetch cost budgets from database...");

Documentation

Retrieves a paginated list of cost budgets from the database.

async fn count_cost_budgets

Definition

pub async fn count_cost_budgets(pool: &Pool<MySql>) -> anyhow::Result<i64> {
    let count = sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM cost_budgets")
        .fetch_one(pool)
        .await
        .context("Failed to count cost budgets")?;

Documentation

Counts the total number of cost budgets in the database.

async fn create_cost_budget

Definition

pub async fn create_cost_budget(
    pool: &Pool<MySql>,
    org_id: i64,
    app_id: Option<i64>,
    budget_name: &str,
    budget_amount: f64,
    currency: &str,
    budget_period: &str,
    period_start: DateTime<Utc>,
    period_end: DateTime<Utc>,
    alert_threshold_percentage: f64,
    alert_contacts: &str,
    created_by: i64
) -> anyhow::Result<CostBudget> {
    // ... function body
}

Documentation

Creates a new cost budget in the database.

async fn get_cost_budget_by_id

Definition

pub async fn get_cost_budget_by_id(pool: &Pool<MySql>, id: i64) -> anyhow::Result<CostBudget> {
    let cost_budget = sqlx::query_as::<_, CostBudget>("SELECT * FROM cost_budgets WHERE id = ?")
        .bind(id)
        .fetch_one(pool)
        .await
        .context("Failed to fetch cost budget")?;

Documentation

Retrieves a specific cost budget by its unique identifier.

async fn update_cost_budget

Definition

pub async fn update_cost_budget(
    pool: &Pool<MySql>,
    id: i64,
    budget_name: Option<&str>,
    budget_amount: Option<f64>,
    alert_threshold_percentage: Option<f64>,
    alert_contacts: Option<&str>,
    is_active: Option<bool>
) -> anyhow::Result<CostBudget> {
    // ... function body
}

Documentation

Updates an existing cost budget in the database.

async fn delete_cost_budget

Definition

pub async fn delete_cost_budget(pool: &Pool<MySql>, id: i64) -> anyhow::Result<()> {
    let mut tx = pool.begin().await?;

Documentation

Deletes a cost budget from the database.

async fn list_cost_projections

Definition

pub async fn list_cost_projections(pool: &Pool<MySql>, page: i64, per_page: i64) -> anyhow::Result<Vec<CostProjection>> {
    println!("Attempting to fetch cost projections from database...");

Documentation

Retrieves a paginated list of cost projections from the database.

async fn create_cost_projection

Definition

pub async fn create_cost_projection(
    pool: &Pool<MySql>,
    org_id: i64,
    app_id: Option<i64>,
    projection_period: &str,
    start_date: DateTime<Utc>,
    end_date: DateTime<Utc>,
    projected_cost: f64,
    currency: &str,
    projection_model: &str,
    confidence_level: Option<f64>,
    metadata: Option<&str>
) -> anyhow::Result<CostProjection> {
    // ... function body
}

Documentation

Creates a new cost projection in the database.

async fn get_cost_projection_by_id

Definition

pub async fn get_cost_projection_by_id(pool: &Pool<MySql>, id: i64) -> anyhow::Result<CostProjection> {
    let cost_projection = sqlx::query_as::<_, CostProjection>("SELECT * FROM cost_projections WHERE id = ?")
        .bind(id)
        .fetch_one(pool)
        .await
        .context("Failed to fetch cost projection")?;

Documentation

Retrieves a specific cost projection by its unique identifier.

async fn delete_cost_projection

Definition

pub async fn delete_cost_projection(pool: &Pool<MySql>, id: i64) -> anyhow::Result<()> {
    let mut tx = pool.begin().await?;

Documentation

Deletes a cost projection from the database.

async fn list_resource_pricing

Definition

pub async fn list_resource_pricing(
    pool: &Pool<MySql>, 
    page: i64, 
    per_page: i64,
    resource_type_id: Option<i32>,
    provider_id: Option<i64>,
    region_id: Option<i64>,
    pricing_model: Option<&str>,
    tier_name: Option<&str>
) -> anyhow::Result<Vec<ResourcePricing>> {
    // ... function body
}

Documentation

Retrieves a paginated list of resource pricing entries from the database.

async fn create_resource_pricing

Definition

pub async fn create_resource_pricing(
    pool: &Pool<MySql>,
    resource_type_id: i32,
    provider_id: i64,
    region_id: Option<i64>,
    tier_name: &str,
    unit_price: f64,
    currency: &str,
    effective_from: DateTime<Utc>,
    effective_to: Option<DateTime<Utc>>,
    pricing_model: &str,
    commitment_period: Option<&str>,
    volume_discount_tiers: Option<&str>
) -> anyhow::Result<ResourcePricing> {
    // ... function body
}

Documentation

Creates a new resource pricing entry in the database.

async fn get_resource_pricing_by_id

Definition

pub async fn get_resource_pricing_by_id(pool: &Pool<MySql>, id: i64) -> anyhow::Result<ResourcePricing> {
    let resource_pricing = sqlx::query_as::<_, ResourcePricing>("SELECT * FROM resource_pricing WHERE id = ?")
        .bind(id)
        .fetch_one(pool)
        .await
        .context("Failed to fetch resource pricing")?;

Documentation

Retrieves a specific resource pricing entry by its unique identifier.

async fn update_resource_pricing

Definition

pub async fn update_resource_pricing(
    pool: &Pool<MySql>,
    id: i64,
    unit_price: Option<f64>,
    effective_to: Option<DateTime<Utc>>,
    volume_discount_tiers: Option<&str>
) -> anyhow::Result<ResourcePricing> {
    // ... function body
}

Documentation

Updates an existing resource pricing entry in the database.

async fn delete_resource_pricing

Definition

pub async fn delete_resource_pricing(pool: &Pool<MySql>, id: i64) -> anyhow::Result<()> {
    let mut tx = pool.begin().await?;

Documentation

Deletes a resource pricing entry from the database.

async fn get_cost_allocation_tags

Definition

pub async fn get_cost_allocation_tags(
    pool: &Pool<MySql>,
    resource_id: i64,
    resource_type: &str
) -> anyhow::Result<Vec<CostAllocationTag>> {
    // ... function body
}

Documentation

Retrieves a list of cost allocation tags for a specific resource.

async fn create_cost_allocation_tag

Definition

pub async fn create_cost_allocation_tag(
    pool: &Pool<MySql>,
    tag_key: &str,
    tag_value: &str,
    resource_id: i64,
    resource_type: &str
) -> anyhow::Result<CostAllocationTag> {
    // ... function body
}

Documentation

Creates a new cost allocation tag in the database.

async fn delete_cost_allocation_tag

Definition

pub async fn delete_cost_allocation_tag(pool: &Pool<MySql>, id: i64) -> anyhow::Result<()> {
    let mut tx = pool.begin().await?;

Documentation

Deletes a cost allocation tag from the database.

⚠️ **GitHub.com Fallback** ⚠️