crates: snapshot

This commit is contained in:
nym21
2025-12-30 11:48:09 +01:00
parent d9f28e85af
commit bd376f86ea
36 changed files with 1372 additions and 1355 deletions
+51 -45
View File
@@ -6,7 +6,7 @@ use brk_grouper::{
AGE_RANGE_NAMES, AMOUNT_RANGE_NAMES, EPOCH_NAMES, GE_AMOUNT_NAMES, LT_AMOUNT_NAMES,
MAX_AGE_NAMES, MIN_AGE_NAMES, SPENDABLE_TYPE_NAMES, TERM_NAMES, YEAR_NAMES,
};
use brk_types::{pools, Index, TreeNode};
use brk_types::{Index, PoolSlug, TreeNode, pools};
use serde_json::Value;
use crate::{
@@ -29,7 +29,6 @@ pub fn generate_javascript_client(
writeln!(output, "// Auto-generated BRK JavaScript client").unwrap();
writeln!(output, "// Do not edit manually\n").unwrap();
generate_constants(&mut output);
generate_type_definitions(&mut output, schemas);
generate_base_client(&mut output);
generate_index_accessors(&mut output, &metadata.index_set_patterns);
@@ -68,60 +67,63 @@ fn update_package_json_version(package_json_path: &Path) -> io::Result<()> {
Ok(())
}
fn generate_constants(output: &mut String) {
writeln!(output, "// Constants\n").unwrap();
fn generate_static_constants(output: &mut String) {
use serde::Serialize;
fn static_const<T: Serialize>(output: &mut String, name: &str, value: &T) {
let json = serde_json::to_string_pretty(value).unwrap();
// Indent the JSON for proper formatting inside the class
let indented = json
.lines()
.enumerate()
.map(|(i, line)| {
if i == 0 {
line.to_string()
} else {
format!(" {}", line)
}
})
.collect::<Vec<_>>()
.join("\n");
writeln!(
output,
" static {} = /** @type {{const}} */ ({});\n",
name, indented
)
.unwrap();
}
fn static_const_raw(output: &mut String, name: &str, value: &str) {
writeln!(output, " static {} = {};\n", name, value).unwrap();
}
// VERSION
writeln!(output, "export const VERSION = \"v{VERSION}\";\n").unwrap();
static_const_raw(output, "VERSION", &format!("\"v{}\"", VERSION));
// INDEXES
let indexes = Index::all();
writeln!(output, "export const INDEXES = /** @type {{const}} */ ([").unwrap();
for index in &indexes {
writeln!(output, " \"{}\",", index.serialize_long()).unwrap();
}
writeln!(output, "]);\n").unwrap();
let indexes_json: Vec<&'static str> = indexes.iter().map(|i| i.serialize_long()).collect();
static_const(output, "INDEXES", &indexes_json);
// POOL_ID_TO_POOL_NAME
let pools = pools();
let mut sorted_pools: Vec<_> = pools.iter().collect();
sorted_pools.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
let pool_map: std::collections::BTreeMap<PoolSlug, &'static str> =
sorted_pools.iter().map(|p| (p.slug(), p.name)).collect();
static_const(output, "POOL_ID_TO_POOL_NAME", &pool_map);
writeln!(output, "export const POOL_ID_TO_POOL_NAME = /** @type {{const}} */ ({{").unwrap();
for pool in &sorted_pools {
writeln!(output, " {}: \"{}\",", pool.slug(), pool.name).unwrap();
}
writeln!(output, "}});\n").unwrap();
// Cohort names - serialize from brk_grouper using serde_json
generate_cohort_names(output);
}
fn generate_cohort_names(output: &mut String) {
use serde::Serialize;
fn export_const<T: Serialize>(output: &mut String, name: &str, value: &T) {
let json = serde_json::to_string_pretty(value).unwrap();
writeln!(
output,
"export const {} = /** @type {{const}} */ ({});\n",
name, json
)
.unwrap();
}
writeln!(output, "// Cohort names\n").unwrap();
export_const(output, "TERM_NAMES", &TERM_NAMES);
export_const(output, "EPOCH_NAMES", &EPOCH_NAMES);
export_const(output, "YEAR_NAMES", &YEAR_NAMES);
export_const(output, "SPENDABLE_TYPE_NAMES", &SPENDABLE_TYPE_NAMES);
export_const(output, "AGE_RANGE_NAMES", &AGE_RANGE_NAMES);
export_const(output, "MAX_AGE_NAMES", &MAX_AGE_NAMES);
export_const(output, "MIN_AGE_NAMES", &MIN_AGE_NAMES);
export_const(output, "AMOUNT_RANGE_NAMES", &AMOUNT_RANGE_NAMES);
export_const(output, "GE_AMOUNT_NAMES", &GE_AMOUNT_NAMES);
export_const(output, "LT_AMOUNT_NAMES", &LT_AMOUNT_NAMES);
// Cohort names
static_const(output, "TERM_NAMES", &TERM_NAMES);
static_const(output, "EPOCH_NAMES", &EPOCH_NAMES);
static_const(output, "YEAR_NAMES", &YEAR_NAMES);
static_const(output, "SPENDABLE_TYPE_NAMES", &SPENDABLE_TYPE_NAMES);
static_const(output, "AGE_RANGE_NAMES", &AGE_RANGE_NAMES);
static_const(output, "MAX_AGE_NAMES", &MAX_AGE_NAMES);
static_const(output, "MIN_AGE_NAMES", &MIN_AGE_NAMES);
static_const(output, "AMOUNT_RANGE_NAMES", &AMOUNT_RANGE_NAMES);
static_const(output, "GE_AMOUNT_NAMES", &GE_AMOUNT_NAMES);
static_const(output, "LT_AMOUNT_NAMES", &LT_AMOUNT_NAMES);
}
fn generate_type_definitions(output: &mut String, schemas: &TypeSchemas) {
@@ -794,6 +796,10 @@ fn generate_main_client(
writeln!(output, " * @extends BrkClientBase").unwrap();
writeln!(output, " */").unwrap();
writeln!(output, "class BrkClient extends BrkClientBase {{").unwrap();
// Generate static properties for constants
generate_static_constants(output);
writeln!(output, " /**").unwrap();
writeln!(output, " * @param {{BrkClientOptions|string}} options").unwrap();
writeln!(output, " */").unwrap();
+42 -38
View File
@@ -4,7 +4,7 @@ use brk_grouper::{
AGE_RANGE_NAMES, AMOUNT_RANGE_NAMES, EPOCH_NAMES, GE_AMOUNT_NAMES, LT_AMOUNT_NAMES,
MAX_AGE_NAMES, MIN_AGE_NAMES, SPENDABLE_TYPE_NAMES, TERM_NAMES, YEAR_NAMES,
};
use brk_types::{pools, Index, TreeNode};
use brk_types::{Index, TreeNode, pools};
use serde::Serialize;
use serde_json::Value;
@@ -36,7 +36,6 @@ pub fn generate_python_client(
writeln!(output, "import httpx\n").unwrap();
writeln!(output, "T = TypeVar('T')\n").unwrap();
generate_constants(&mut output);
generate_type_definitions(&mut output, schemas);
generate_base_client(&mut output);
generate_metric_node(&mut output);
@@ -50,53 +49,54 @@ pub fn generate_python_client(
Ok(())
}
fn generate_constants(output: &mut String) {
writeln!(output, "# Constants\n").unwrap();
fn generate_class_constants(output: &mut String) {
fn class_const<T: Serialize>(output: &mut String, name: &str, value: &T) {
let json = serde_json::to_string_pretty(value).unwrap();
// Indent all lines for class body
let indented = json
.lines()
.enumerate()
.map(|(i, line)| {
if i == 0 {
format!(" {} = {}", name, line)
} else {
format!(" {}", line)
}
})
.collect::<Vec<_>>()
.join("\n");
writeln!(output, "{}\n", indented).unwrap();
}
// VERSION
writeln!(output, "VERSION: Final[str] = \"v{VERSION}\"\n").unwrap();
writeln!(output, " VERSION = \"v{}\"\n", VERSION).unwrap();
// INDEXES
let indexes = Index::all();
writeln!(output, "INDEXES: Final[tuple[str, ...]] = (").unwrap();
for index in &indexes {
writeln!(output, " \"{}\",", index.serialize_long()).unwrap();
}
writeln!(output, ")\n").unwrap();
let indexes_list: Vec<&str> = indexes.iter().map(|i| i.serialize_long()).collect();
class_const(output, "INDEXES", &indexes_list);
// POOL_ID_TO_POOL_NAME
let pools = pools();
let mut sorted_pools: Vec<_> = pools.iter().collect();
sorted_pools.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
writeln!(output, "POOL_ID_TO_POOL_NAME: Final[dict[str, str]] = {{").unwrap();
for pool in &sorted_pools {
writeln!(output, " \"{}\": \"{}\",", pool.slug(), pool.name).unwrap();
}
writeln!(output, "}}\n").unwrap();
let pool_map: std::collections::BTreeMap<String, &str> = sorted_pools
.iter()
.map(|p| (p.slug().to_string(), p.name))
.collect();
class_const(output, "POOL_ID_TO_POOL_NAME", &pool_map);
// Cohort names
generate_cohort_names(output);
}
fn generate_cohort_names(output: &mut String) {
fn export_const<T: Serialize>(output: &mut String, name: &str, value: &T) {
let json = serde_json::to_string_pretty(value).unwrap();
writeln!(output, "{}: Final = {}\n", name, json).unwrap();
}
writeln!(output, "# Cohort names\n").unwrap();
export_const(output, "TERM_NAMES", &TERM_NAMES);
export_const(output, "EPOCH_NAMES", &EPOCH_NAMES);
export_const(output, "YEAR_NAMES", &YEAR_NAMES);
export_const(output, "SPENDABLE_TYPE_NAMES", &SPENDABLE_TYPE_NAMES);
export_const(output, "AGE_RANGE_NAMES", &AGE_RANGE_NAMES);
export_const(output, "MAX_AGE_NAMES", &MAX_AGE_NAMES);
export_const(output, "MIN_AGE_NAMES", &MIN_AGE_NAMES);
export_const(output, "AMOUNT_RANGE_NAMES", &AMOUNT_RANGE_NAMES);
export_const(output, "GE_AMOUNT_NAMES", &GE_AMOUNT_NAMES);
export_const(output, "LT_AMOUNT_NAMES", &LT_AMOUNT_NAMES);
class_const(output, "TERM_NAMES", &TERM_NAMES);
class_const(output, "EPOCH_NAMES", &EPOCH_NAMES);
class_const(output, "YEAR_NAMES", &YEAR_NAMES);
class_const(output, "SPENDABLE_TYPE_NAMES", &SPENDABLE_TYPE_NAMES);
class_const(output, "AGE_RANGE_NAMES", &AGE_RANGE_NAMES);
class_const(output, "MAX_AGE_NAMES", &MAX_AGE_NAMES);
class_const(output, "MIN_AGE_NAMES", &MIN_AGE_NAMES);
class_const(output, "AMOUNT_RANGE_NAMES", &AMOUNT_RANGE_NAMES);
class_const(output, "GE_AMOUNT_NAMES", &GE_AMOUNT_NAMES);
class_const(output, "LT_AMOUNT_NAMES", &LT_AMOUNT_NAMES);
}
fn generate_type_definitions(output: &mut String, schemas: &TypeSchemas) {
@@ -846,7 +846,11 @@ fn generate_main_client(output: &mut String, endpoints: &[Endpoint]) {
" \"\"\"Main BRK client with catalog tree and API methods.\"\"\""
)
.unwrap();
writeln!(output, " ").unwrap();
writeln!(output).unwrap();
// Generate class-level constants
generate_class_constants(output);
writeln!(
output,
" def __init__(self, base_url: str = 'http://localhost:3000', timeout: float = 30.0):"
+17 -13
View File
@@ -337,13 +337,13 @@ fn generate_parameterized_rust_field(
let metric_expr = if let Some(pos) = pattern.get_field_position(&field.name) {
match pos {
FieldNamePosition::Append(suffix) => format!("format!(\"/{{acc}}{}\")", suffix),
FieldNamePosition::Prepend(prefix) => format!("format!(\"/{}{{acc}}\")", prefix),
FieldNamePosition::Identity => "format!(\"/{acc}\")".to_string(),
FieldNamePosition::SetBase(base) => format!("\"/{}\".to_string()", base),
FieldNamePosition::Append(suffix) => format!("format!(\"{{acc}}{}\")", suffix),
FieldNamePosition::Prepend(prefix) => format!("format!(\"{}{{acc}}\")", prefix),
FieldNamePosition::Identity => "acc.to_string()".to_string(),
FieldNamePosition::SetBase(base) => format!("\"{}\".to_string()", base),
}
} else {
format!("format!(\"/{{acc}}_{}\")", field.name)
format!("format!(\"{{acc}}_{}\")", field.name)
};
if metadata.field_uses_accessor(field) {
@@ -374,7 +374,7 @@ fn generate_tree_path_rust_field(
if metadata.is_pattern_type(&field.rust_type) {
writeln!(
output,
" {}: {}::new(client.clone(), &format!(\"{{base_path}}/{}\")),",
" {}: {}::new(client.clone(), &format!(\"{{base_path}}_{}\")),",
field_name, field.rust_type, field.name
)
.unwrap();
@@ -382,14 +382,14 @@ fn generate_tree_path_rust_field(
let accessor = metadata.find_index_set_pattern(&field.indexes).unwrap();
writeln!(
output,
" {}: {}::new(client.clone(), &format!(\"{{base_path}}/{}\")),",
" {}: {}::new(client.clone(), &format!(\"{{base_path}}_{}\")),",
field_name, accessor.name, field.name
)
.unwrap();
} else {
writeln!(
output,
" {}: MetricNode::new(client.clone(), format!(\"{{base_path}}/{}\")),",
" {}: MetricNode::new(client.clone(), format!(\"{{base_path}}_{}\")),",
field_name, field.name
)
.unwrap();
@@ -523,7 +523,7 @@ fn generate_tree_node(
} else {
writeln!(
output,
" {}: {}::new(client.clone(), &format!(\"{{base_path}}/{}\")),",
" {}: {}::new(client.clone(), &format!(\"{{base_path}}_{}\")),",
field_name, field.rust_type, field.name
)
.unwrap();
@@ -532,7 +532,7 @@ fn generate_tree_node(
let accessor = metadata.find_index_set_pattern(&field.indexes).unwrap();
writeln!(
output,
" {}: {}::new(client.clone(), &format!(\"{{base_path}}/{}\")),",
" {}: {}::new(client.clone(), &format!(\"{{base_path}}_{}\")),",
field_name, accessor.name, field.name
)
.unwrap();
@@ -540,7 +540,7 @@ fn generate_tree_node(
// Non-pattern branch - instantiate the nested struct
writeln!(
output,
" {}: {}::new(client.clone(), &format!(\"{{base_path}}/{}\")),",
" {}: {}::new(client.clone(), &format!(\"{{base_path}}_{}\")),",
field_name, field.rust_type, field.name
)
.unwrap();
@@ -548,7 +548,7 @@ fn generate_tree_node(
// Leaf - use MetricNode with base_path
writeln!(
output,
" {}: MetricNode::new(client.clone(), format!(\"{{base_path}}/{}\")),",
" {}: MetricNode::new(client.clone(), format!(\"{{base_path}}_{}\")),",
field_name, field.name
)
.unwrap();
@@ -587,6 +587,9 @@ pub struct BrkClient {{
}}
impl BrkClient {{
/// Client version.
pub const VERSION: &'static str = "v{VERSION}";
/// Create a new client with the given base URL.
pub fn new(base_url: impl Into<String>) -> Self {{
let base = Arc::new(BrkClientBase::new(base_url));
@@ -605,7 +608,8 @@ impl BrkClient {{
pub fn tree(&self) -> &CatalogTree {{
&self.tree
}}
"#
"#,
VERSION = crate::VERSION
)
.unwrap();
+1243 -1240
View File
File diff suppressed because it is too large Load Diff
@@ -6,7 +6,7 @@ use rustc_hash::FxHashMap;
use crate::stateful::{
address::{AddressTypeToTypeIndexMap, AddressesDataVecs, AnyAddressIndexesVecs},
compute::VecsReaders,
states::Transacted,
state::Transacted,
};
use super::{
@@ -4,7 +4,7 @@ use brk_types::{Sats, TxIndex, TypeIndex};
use crate::stateful::{
address::{AddressTypeToTypeIndexMap, AddressesDataVecs, AnyAddressIndexesVecs},
compute::{TxOutData, VecsReaders},
states::Transacted,
state::Transacted,
};
use super::{
@@ -14,7 +14,7 @@ use crate::{
Indexes,
grouped::{ComputedVecsFromHeight, Source, VecBuilderOptions},
indexes, price,
stateful::states::AddressCohortState,
stateful::state::AddressCohortState,
};
use super::{
@@ -9,7 +9,7 @@ use vecdb::{AnyStoredVec, Database, Exit, IterableVec};
use crate::{
Indexes, indexes, price,
stateful::{CohortVecs, DynCohortVecs, states::UTXOCohortState},
stateful::{CohortVecs, DynCohortVecs, state::UTXOCohortState},
};
use super::super::metrics::{CohortMetrics, ImportConfig, SupplyMetrics};
@@ -1,6 +1,6 @@
use brk_types::{Dollars, Height, Timestamp};
use crate::stateful::states::Transacted;
use crate::stateful::state::Transacted;
use super::UTXOCohorts;
@@ -3,7 +3,7 @@ use rustc_hash::FxHashMap;
use vecdb::VecIndex;
use crate::{
stateful::states::{BlockState, Transacted},
stateful::state::{BlockState, Transacted},
utils::OptionExt,
};
@@ -1,7 +1,7 @@
use brk_grouper::AGE_BOUNDARIES;
use brk_types::{ONE_DAY_IN_SEC, Timestamp};
use crate::stateful::states::BlockState;
use crate::stateful::state::BlockState;
use super::UTXOCohorts;
@@ -13,11 +13,11 @@ use crate::{
stateful::{
address::AddressTypeToAddressCount,
compute::write::{process_address_updates, write},
process::{
block::{
AddressCache, InputsResult, process_inputs, process_outputs, process_received,
process_sent,
},
states::{BlockState, Transacted},
state::{BlockState, Transacted},
},
txins,
utils::OptionExt,
@@ -8,11 +8,11 @@ use vecdb::{AnyStoredVec, GenericStoredVec, Stamp};
use crate::stateful::{
Vecs,
process::{
block::{
EmptyAddressDataWithSource, LoadedAddressDataWithSource, process_empty_addresses,
process_loaded_addresses,
},
states::BlockState,
state::BlockState,
};
use super::super::address::{AddressTypeToTypeIndexMap, AddressesDataVecs, AnyAddressIndexesVecs};
@@ -21,7 +21,7 @@ use brk_types::{Bitcoin, DateIndex, Dollars, Height, Version};
use rayon::prelude::*;
use vecdb::{AnyStoredVec, Exit, IterableVec};
use crate::{Indexes, indexes, price, stateful::states::CohortState};
use crate::{Indexes, indexes, price, stateful::state::CohortState};
/// All metrics for a cohort, organized by category.
#[derive(Clone, Traversable)]
@@ -9,7 +9,7 @@ use vecdb::{
use crate::{
Indexes,
grouped::{ComputedVecsFromHeight, PricePercentiles, Source, VecBuilderOptions},
stateful::states::CohortState,
stateful::state::CohortState,
};
use super::ImportConfig;
@@ -15,7 +15,7 @@ use crate::{
VecBuilderOptions,
},
indexes, price,
stateful::states::RealizedState,
stateful::state::RealizedState,
utils::OptionExt,
};
@@ -13,7 +13,7 @@ use crate::{
ComputedHeightValueVecs, ComputedValueVecsFromDateIndex, ComputedVecsFromDateIndex,
DollarsMinus, DollarsPlus, LazyVecsFromDateIndex, Source, VecBuilderOptions,
},
stateful::states::UnrealizedState,
stateful::state::UnrealizedState,
};
use super::ImportConfig;
+2 -2
View File
@@ -1,10 +1,10 @@
pub mod address;
mod block;
pub mod cohorts;
pub mod compute;
pub mod metrics;
mod process;
mod range_map;
mod states;
mod state;
mod vecs;
pub use range_map::RangeMap;
@@ -4,7 +4,7 @@ use brk_error::Result;
use brk_types::{Dollars, Height, LoadedAddressData, Sats, SupplyState};
use vecdb::unlikely;
use crate::stateful::states::RealizedState;
use crate::stateful::state::RealizedState;
use super::CohortState;
+1 -1
View File
@@ -22,7 +22,7 @@ use crate::{
indexes, price,
stateful::{
compute::{StartMode, determine_start_mode, process_blocks, recover_state, reset_state},
states::BlockState,
state::BlockState,
},
txins,
utils::OptionExt,