From 41257067b579a54052e614db864ae59b3f78f1be Mon Sep 17 00:00:00 2001 From: paulobressan Date: Mon, 14 Oct 2024 18:39:36 -0300 Subject: [PATCH] feat: improved usage report adding resource name --- Cargo.lock | 2 +- src/domain/usage/mod.rs | 7 ++++++- src/driven/cache/usage.rs | 4 ++++ src/drivers/billing/mod.rs | 13 +++++++------ src/drivers/grpc/usage.rs | 1 + 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1fa8b4e..25fd939 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1094,7 +1094,7 @@ dependencies = [ [[package]] name = "dmtri" version = "0.1.0" -source = "git+https://github.com/demeter-run/specs.git#6f93355139c67522139af7c1f0923f9855a6ade5" +source = "git+https://github.com/demeter-run/specs.git#69e7b9189f6c5d22bd4915b3a481591c30e625d1" dependencies = [ "bytes 1.7.2", "pbjson", diff --git a/src/domain/usage/mod.rs b/src/domain/usage/mod.rs index 92165f6..8445ec3 100644 --- a/src/domain/usage/mod.rs +++ b/src/domain/usage/mod.rs @@ -53,6 +53,7 @@ pub struct UsageUnitMetric { pub struct UsageReport { pub resource_id: String, pub resource_kind: String, + pub resource_name: String, pub resource_spec: String, pub tier: String, pub units: i64, @@ -84,6 +85,7 @@ pub struct UsageReportAggregated { pub project_billing_provider_id: String, pub resource_id: String, pub resource_kind: String, + pub resource_name: String, pub tier: String, pub interval: u64, pub units: i64, @@ -95,6 +97,8 @@ pub struct UsageReportAggregated { mod tests { use uuid::Uuid; + use crate::domain::utils; + use super::*; impl Default for Usage { @@ -116,6 +120,7 @@ mod tests { Self { resource_id: Uuid::new_v4().to_string(), resource_kind: "CardanoNodePort".into(), + resource_name: format!("cardanonode-{}", utils::get_random_salt()), resource_spec: "{\"version\":\"stable\",\"network\":\"mainnet\",\"throughputTier\":\"1\"}" .into(), @@ -132,7 +137,7 @@ mod tests { project_id: Uuid::new_v4().to_string(), project_namespace: "xxx".into(), resource_id: Uuid::new_v4().to_string(), - resource_name: "cardanonode-xxx".into(), + resource_name: format!("cardanonode-{}", utils::get_random_salt()), resource_spec: "{\"version\":\"stable\",\"network\":\"mainnet\",\"throughputTier\":\"1\"}" .into(), diff --git a/src/driven/cache/usage.rs b/src/driven/cache/usage.rs index e0476fb..80e573a 100644 --- a/src/driven/cache/usage.rs +++ b/src/driven/cache/usage.rs @@ -32,6 +32,7 @@ impl UsageDrivenCache for SqliteUsageDrivenCache { SELECT r.id as resource_id, r.kind as resource_kind, + r.name as resource_name, r.spec as resource_spec, u.tier, SUM(u.interval) as interval, @@ -65,6 +66,7 @@ impl UsageDrivenCache for SqliteUsageDrivenCache { p.billing_provider_id as project_billing_provider_id, r.id as resource_id, r.kind as resource_kind, + r.name as resource_name, u.tier as tier, SUM(u.interval) as interval, SUM(u.units) as units, @@ -163,6 +165,7 @@ impl FromRow<'_, SqliteRow> for UsageReport { Ok(Self { resource_id: row.try_get("resource_id")?, resource_kind: row.try_get("resource_kind")?, + resource_name: row.try_get("resource_name")?, resource_spec: row.try_get("resource_spec")?, units: row.try_get("units")?, tier: row.try_get("tier")?, @@ -181,6 +184,7 @@ impl FromRow<'_, SqliteRow> for UsageReportAggregated { project_billing_provider_id: row.try_get("project_billing_provider_id")?, resource_id: row.try_get("resource_id")?, resource_kind: row.try_get("resource_kind")?, + resource_name: row.try_get("resource_name")?, tier: row.try_get("tier")?, interval: interval as u64, units: row.try_get("units")?, diff --git a/src/drivers/billing/mod.rs b/src/drivers/billing/mod.rs index 262186d..b9b10ef 100644 --- a/src/drivers/billing/mod.rs +++ b/src/drivers/billing/mod.rs @@ -52,8 +52,8 @@ fn csv(report: Vec, period: &str) { "", "project", "stripe_id", - "", - "port", + "kind", + "name", "tier", "time", "units", @@ -68,8 +68,8 @@ fn csv(report: Vec, period: &str) { &(i + 1).to_string(), &r.project_namespace, &r.project_billing_provider_id, - &r.resource_id, &r.resource_kind, + &r.resource_name, &r.tier, &format!("{:.1}h", ((r.interval as f64) / 60.) / 60.), &r.units.to_string(), @@ -99,6 +99,7 @@ fn json(report: Vec) { "stripe_id": r.project_billing_provider_id, "resource_id": r.resource_id, "resource_kind": r.resource_kind, + "resource_name": r.resource_name, "tier": r.tier, "interval": r.interval, "units": r.units, @@ -114,8 +115,8 @@ fn table(report: Vec) { "", "project", "stripe_id", - "", - "port", + "kind", + "name", "tier", "time", "units", @@ -126,8 +127,8 @@ fn table(report: Vec) { &(i + 1).to_string(), &r.project_namespace, &r.project_billing_provider_id, - &r.resource_id, &r.resource_kind, + &r.resource_name, &r.tier, &format!("{:.1}h", ((r.interval as f64) / 60.) / 60.), &r.units.to_string(), diff --git a/src/drivers/grpc/usage.rs b/src/drivers/grpc/usage.rs index d089bf3..77a7bab 100644 --- a/src/drivers/grpc/usage.rs +++ b/src/drivers/grpc/usage.rs @@ -55,6 +55,7 @@ impl From for proto::UsageReport { Self { resource_id: value.resource_id, resource_kind: value.resource_kind, + resource_name: value.resource_name, resource_spec: value.resource_spec, units: value.units, tier: value.tier,