use super::{
SubnetCatalogInfoReport, SubnetCatalogInfoRequest, load_catalog_for_report,
rate::{FORMULA_VERSION, catalog_cycles_per_billion, charge_applicability},
};
use crate::{QueryProgress, progress::IgnoreQueryProgress};
use crate::{
nns::LiveNnsSource,
subnet_catalog::{
SUBNET_CATALOG_INFO_REPORT_SCHEMA_VERSION, SubnetCatalogHostError, SubnetCatalogSource,
},
};
pub async fn build_subnet_catalog_info_report(
request: &SubnetCatalogInfoRequest,
) -> Result<SubnetCatalogInfoReport, SubnetCatalogHostError> {
build_subnet_catalog_info_report_with_source(request, &LiveNnsSource::default()).await
}
pub async fn build_subnet_catalog_info_report_with_progress(
request: &SubnetCatalogInfoRequest,
progress: &mut (dyn QueryProgress + Send),
) -> Result<SubnetCatalogInfoReport, SubnetCatalogHostError> {
build_subnet_catalog_info_report_with_source_and_progress(
request,
&LiveNnsSource::default(),
progress,
)
.await
}
pub async fn build_subnet_catalog_info_report_with_source(
request: &SubnetCatalogInfoRequest,
source: &dyn SubnetCatalogSource,
) -> Result<SubnetCatalogInfoReport, SubnetCatalogHostError> {
build_subnet_catalog_info_report_with_source_and_progress(
request,
source,
&mut IgnoreQueryProgress,
)
.await
}
pub async fn build_subnet_catalog_info_report_with_source_and_progress(
request: &SubnetCatalogInfoRequest,
source: &dyn SubnetCatalogSource,
progress: &mut (dyn QueryProgress + Send),
) -> Result<SubnetCatalogInfoReport, SubnetCatalogHostError> {
let (cached, stale) = load_catalog_for_report(
&request.cache,
&request.read_policy,
request.now_unix_secs,
request.stale_after_seconds,
source,
progress,
)
.await?;
let resolved = cached
.catalog
.raw()
.resolve_principal_or_prefix(&request.input, request.forced)?;
let (charges_apply_to_subject, charge_applicability_reason) = charge_applicability(
resolved.resolved_as,
resolved.subnet.classification.subnet_kind,
);
let cycles_per_billion_instructions = catalog_cycles_per_billion(&resolved.subnet);
let rate_source = cycles_per_billion_instructions
.is_some()
.then(|| "nns-registry-cache".to_string());
let formula_version = cycles_per_billion_instructions
.is_some()
.then(|| FORMULA_VERSION.to_string());
Ok(SubnetCatalogInfoReport {
classification: resolved.subnet.classification,
metadata: super::SubnetCatalogReportMetadata::from_catalog(&cached.catalog),
cache_disposition: cached.disposition,
catalog_schema_version: cached.catalog.raw().catalog_schema_version,
catalog_path: cached.path.display().to_string(),
catalog_stale: stale.catalog_stale,
stale_reason: stale.stale_reason,
schema_version: SUBNET_CATALOG_INFO_REPORT_SCHEMA_VERSION,
input_principal: resolved.input_principal,
resolved_as: resolved.resolved_as.as_str().to_string(),
resolved_from: resolved.resolved_from,
subnet_principal: resolved.subnet.subnet_principal,
node_count: resolved.subnet.node_count,
charges_apply_to_subject,
charge_applicability_reason,
matched_canister_principal: resolved.matched_canister_principal,
matched_routing_range: resolved.matched_routing_range,
cycles_per_billion_instructions,
rate_source,
formula_version,
})
}