Skip to content

Commit

Permalink
save report file every time cryo is run
Browse files Browse the repository at this point in the history
  • Loading branch information
sslivkoff committed Aug 13, 2023
1 parent 700283a commit d6059df
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 33 deletions.
58 changes: 31 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "cryo_cli"
build = "build.rs"
description = "cryo is the easiest way to extract blockchain data to parquet, csv, or json"
version.workspace = true
edition.workspace = true
Expand Down Expand Up @@ -32,3 +33,5 @@ chrono = "0.4.26"
anstyle = "1.0.1"
eyre = "0.6.8"
governor = "0.5.1"
serde = { version = "1.0.183", features = ["derive"] }
serde_json = "1.0.104"
22 changes: 22 additions & 0 deletions crates/cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::process::Command;

fn main() {
// Run `git describe --tags --always`
let output =
Command::new("git").args(["describe", "--tags", "--always"]).output().unwrap_or_else(|e| {
panic!("Failed to execute git command: {}", e);
});

if output.status.success() {
let git_description = String::from_utf8(output.stdout)
.unwrap_or_else(|e| {
panic!("Failed to read git command output: {}", e);
})
.trim()
.to_string();

println!("cargo:rustc-env=GIT_DESCRIPTION={}", git_description);
} else {
println!("cargo:warning=Could not determine git description");
}
}
15 changes: 11 additions & 4 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use clap_cryo::Parser;
use color_print::cstr;
use serde::{Deserialize, Serialize};

/// Command line arguments
#[derive(Parser, Debug)]
#[derive(Parser, Debug, Serialize, Deserialize)]
#[command(name = "cryo", author, version, about = get_about_str(), long_about = None, styles=get_styles(), after_help=get_after_str(), allow_negative_numbers = true)]
pub struct Args {
/// datatype to collect
Expand Down Expand Up @@ -132,9 +133,15 @@ pub struct Args {
#[arg(long, help_heading="Output Options", value_name="NAME [#]", num_args(1..=2), default_value = "lz4")]
pub compression: Vec<String>,

// /// [transactions] track gas used by each transaction
// #[arg(long, help_heading = "Dataset-specific Options")]
// pub gas_used: bool,
/// Directory to save summary report
/// [default: {output_dir}/.cryo_reports]
#[arg(long, help_heading = "Output Options")]
pub report_dir: Option<String>,

/// Avoid saving a summary report
#[arg(long, help_heading = "Output Options")]
pub no_report: bool,

/// [logs] filter logs by contract address
#[arg(long, help_heading = "Dataset-specific Options")]
pub contract: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

mod args;
mod parse;
mod reports;
mod run;
mod summaries;

Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use clap_cryo::Parser;

mod args;
mod parse;
mod reports;
mod run;
mod summaries;

Expand Down
61 changes: 61 additions & 0 deletions crates/cli/src/reports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::args;
use chrono::{DateTime, Local};
use cryo_freeze::{FreezeError, FreezeSummary};
use std::{fs::File, io::Write, path::Path, time::SystemTime};

#[derive(serde::Serialize, Debug)]
struct FreezeReport<'a> {
cryo_version: String,
// node_client: String,
cli_command: Vec<String>,
args: &'a args::Args,
chunk_summary: Option<&'a FreezeSummary>,
}

pub(crate) fn get_report_path(
args: &args::Args,
t_start: SystemTime,
is_complete: bool,
) -> Result<String, FreezeError> {
let report_dir = match &args.report_dir {
Some(report_dir) => Path::new(&report_dir).into(),
None => Path::new(&args.output_dir).join(".cryo_reports"),
};
std::fs::create_dir_all(&report_dir)?;
let t_start: DateTime<Local> = t_start.into();
let timestamp: String = t_start.format("%Y-%m-%d_%H-%M-%S").to_string();
let filename = if is_complete {
timestamp + ".json"
} else {
format!("incomplete_{}", timestamp + ".json")
};
let path = report_dir.join(filename);
path.to_str()
.ok_or(FreezeError::GeneralError("non-String path".to_string()))
.map(|s| s.to_string())
}

pub(crate) fn write_report(
args: &args::Args,
freeze_summary: Option<&FreezeSummary>,
t_start: SystemTime,
) -> Result<String, FreezeError> {
// determine version
let cryo_version = format!("{}__{}", env!("CARGO_PKG_VERSION"), env!("GIT_DESCRIPTION"));
let report = FreezeReport {
cryo_version,
cli_command: std::env::args().collect(),
args,
chunk_summary: freeze_summary,
};
let serialized = serde_json::to_string(&report)?;

// create path
let path = get_report_path(args, t_start, freeze_summary.is_some())?;

// save to file
let mut file = File::create(&path)?;
file.write_all(serialized.as_bytes())?;

Ok(path)
}
Loading

0 comments on commit d6059df

Please sign in to comment.