Skip to content

Commit

Permalink
feat: improved usage report adding resource name (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan authored Oct 15, 2024
1 parent 3e23ec4 commit b1192a9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/domain/usage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -95,6 +97,8 @@ pub struct UsageReportAggregated {
mod tests {
use uuid::Uuid;

use crate::domain::utils;

use super::*;

impl Default for Usage {
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions src/driven/cache/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")?,
Expand All @@ -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")?,
Expand Down
13 changes: 7 additions & 6 deletions src/drivers/billing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ fn csv(report: Vec<UsageReportAggregated>, period: &str) {
"",
"project",
"stripe_id",
"",
"port",
"kind",
"name",
"tier",
"time",
"units",
Expand All @@ -68,8 +68,8 @@ fn csv(report: Vec<UsageReportAggregated>, 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(),
Expand Down Expand Up @@ -99,6 +99,7 @@ fn json(report: Vec<UsageReportAggregated>) {
"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,
Expand All @@ -114,8 +115,8 @@ fn table(report: Vec<UsageReportAggregated>) {
"",
"project",
"stripe_id",
"",
"port",
"kind",
"name",
"tier",
"time",
"units",
Expand All @@ -126,8 +127,8 @@ fn table(report: Vec<UsageReportAggregated>) {
&(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(),
Expand Down
1 change: 1 addition & 0 deletions src/drivers/grpc/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl From<UsageReport> 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,
Expand Down

0 comments on commit b1192a9

Please sign in to comment.