cli + query: improvements

This commit is contained in:
nym21
2025-03-02 20:15:01 +01:00
parent ceefc8ffc6
commit be2012f28d
15 changed files with 308 additions and 137 deletions

View File

@@ -1,13 +1,14 @@
use std::{path::Path, thread::sleep, time::Duration};
use std::path::Path;
use brk_computer::Computer;
use brk_exit::Exit;
use brk_indexer::Indexer;
use brk_parser::rpc::{self, RpcApi};
use brk_query::{Index, Params as QueryParams, Query};
use brk_server::tokio;
use brk_query::Params as QueryArgs;
use clap::{Parser, Subcommand};
use log::info;
use query::query;
use run::{RunArgs, run};
mod query;
mod run;
#[derive(Parser)]
#[command(version, about)]
@@ -19,13 +20,10 @@ struct Cli {
#[derive(Subcommand, Debug)]
enum Commands {
/// Run the indexer, computer and server
Run(RunArgs),
Query(QueryParams),
}
#[derive(Parser, Debug)]
struct RunArgs {
name: Option<String>,
/// Query generated datasets via the `run` command in a similar fashion as the server's API
Query(QueryArgs),
}
fn main() -> color_eyre::Result<()> {
@@ -37,67 +35,12 @@ fn main() -> color_eyre::Result<()> {
let outputs_dir = Path::new("../../_outputs");
let mut indexer = Indexer::import(&outputs_dir.join("indexed"))?;
let indexer = Indexer::import(&outputs_dir.join("indexed"))?;
let mut computer = Computer::import(&outputs_dir.join("computed"))?;
let computer = Computer::import(&outputs_dir.join("computed"))?;
match &cli.command {
Commands::Run(_) => {
let data_dir = Path::new("../../../bitcoin");
let rpc = Box::leak(Box::new(rpc::Client::new(
"http://localhost:8332",
rpc::Auth::CookieFile(Path::new(data_dir).join(".cookie")),
)?));
let exit = Exit::new();
let parser = brk_parser::Parser::new(data_dir, rpc);
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?
.block_on(async {
let served_indexer = indexer.clone();
let served_computer = computer.clone();
tokio::spawn(async move {
brk_server::main(served_indexer, served_computer).await.unwrap();
});
loop {
let block_count = rpc.get_block_count()?;
info!("{block_count} blocks found.");
let starting_indexes = indexer.index(&parser, rpc, &exit)?;
computer.compute(&mut indexer, starting_indexes, &exit)?;
info!("Waiting for new blocks...");
while block_count == rpc.get_block_count()? {
sleep(Duration::from_secs(1))
}
}
#[allow(unreachable_code)]
Ok(())
})
}
Commands::Query(args) => {
let query = Query::build(&indexer, &computer);
println!(
"{}",
query.search(
Index::try_from(args.index.as_str())?,
&args.values.iter().flat_map(|v| v.split(",")).collect::<Vec<_>>(),
args.from,
args.to,
args.format
)?
);
Ok(())
}
Commands::Run(_) => run(indexer, computer),
Commands::Query(args) => query(indexer, computer, args),
}
}