query: init

This commit is contained in:
nym21
2025-03-02 11:08:35 +01:00
parent 6d7ff38cf2
commit 0ceae2852e
18 changed files with 497 additions and 210 deletions

View File

@@ -1,4 +1,13 @@
use clap::{Args, Parser, Subcommand};
use std::{path::Path, thread::sleep, time::Duration};
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 clap::{Parser, Subcommand};
use log::info;
#[derive(Parser)]
#[command(version, about)]
@@ -8,33 +17,87 @@ struct Cli {
command: Commands,
}
#[derive(Subcommand)]
#[derive(Subcommand, Debug)]
enum Commands {
Run(RunArgs),
Query(QueryArgs),
Query(QueryParams),
}
#[derive(Args)]
#[derive(Parser, Debug)]
struct RunArgs {
name: Option<String>,
}
#[derive(Args)]
struct QueryArgs {
name: Option<String>,
}
fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
brk_logger::init(Some(Path::new(".log")));
fn main() {
let cli = Cli::parse();
// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
let outputs_dir = Path::new("../../_outputs");
let mut indexer = Indexer::import(&outputs_dir.join("indexed"))?;
let mut computer = Computer::import(&outputs_dir.join("computed"))?;
match &cli.command {
Commands::Run(name) => {
println!("'myapp add' was used, name is: {:?}", name.name);
Commands::Run(args) => {
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(name) => {
println!("'myapp add' was used, name is: {:?}", name.name);
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(())
}
}
}