cloud_scanner_cli/
estimated_inventory_exporter.rs

1use anyhow::Result;
2
3use crate::impact_provider::ImpactsSummary;
4use crate::model::EstimatedInventory;
5use crate::usage_location::UsageLocation;
6
7/// Build a summary (aggregated data) from an estimated inventory
8pub async fn build_impact_summary(
9    estimated_inventory: &EstimatedInventory,
10    aws_region: &str,
11    use_duration_hours: &f32,
12) -> Result<ImpactsSummary> {
13    let usage_location: UsageLocation = UsageLocation::try_from(aws_region)?;
14    let summary: ImpactsSummary = ImpactsSummary::new(
15        String::from(aws_region),
16        usage_location.iso_country_code,
17        estimated_inventory,
18        (*use_duration_hours).into(),
19    );
20    debug!("Summary: {:#?}", summary);
21    Ok(summary)
22}
23
24/// Convert an estimated inventory (inventory with impacts) into json
25pub async fn get_estimated_inventory_as_json(
26    estimated_inventory: &EstimatedInventory,
27    aws_region: &str,
28    use_duration_hours: &f32,
29    summary_only: bool,
30) -> Result<String> {
31    if summary_only {
32        let summary: ImpactsSummary =
33            build_impact_summary(estimated_inventory, aws_region, use_duration_hours).await?;
34        return Ok(serde_json::to_string(&summary)?);
35    }
36    Ok(serde_json::to_string(&estimated_inventory)?)
37}