Skip to content

Commit

Permalink
feat: add IO
Browse files Browse the repository at this point in the history
  • Loading branch information
iajoiner committed Oct 29, 2024
1 parent 5c7b263 commit 551d01c
Showing 1 changed file with 43 additions and 11 deletions.
54 changes: 43 additions & 11 deletions scripts/count-ethereum-core/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use futures::StreamExt;
use indexmap::IndexMap;
use proof_of_sql::base::database::OwnedColumn;
use std::{env, fs::File, io::BufReader, sync::Arc};
use std::{env, fs::File, io::BufReader, path::Path, sync::Arc};
use sxt_proof_of_sql_sdk::SxTClient;

#[allow(dead_code)]
const ETHEREUM_CORE_COUNTS_FILE: &str = "ethereum-core-counts.json";

const ETHEREUM_CORE_TABLES: [&str; 21] = [
Expand All @@ -31,6 +30,7 @@ const ETHEREUM_CORE_TABLES: [&str; 21] = [
"ETHEREUM.ERC1155_OWNERS",
];

/// Count the number of rows in a table
async fn count_table(
client: &SxTClient,
table_ref: &str,
Expand All @@ -51,26 +51,55 @@ async fn count_table(
Ok(int_column[0])
}

/// Compare current and previous counts and warn if current is less than previous or if current is absent while previous is present
fn compare_counts(
current_counts: &IndexMap<String, i64>,
previous_counts: &IndexMap<String, i64>,
table_names: &[&str],
) {
for table_name in table_names {
let current_count = current_counts.get(*table_name);
let previous_count = previous_counts.get(*table_name);
match (current_count, previous_count) {
(Some(current), Some(previous)) => {
if current < previous {
log::warn!("count of {table_name} decreased from {previous} to {current}");
}
}
(None, Some(previous)) => {
log::warn!("count of {table_name} decreased from {previous} to 0");
}
(Some(current), None) => {
log::info!("count of {table_name} increased from 0 to {current}");
}
(None, None) => {
log::info!("count of {table_name} is 0");
}
}
}
}

/// Load the previous counts file
#[allow(dead_code)]
async fn load_from_file() -> IndexMap<String, i64> {
let file = File::open(ETHEREUM_CORE_COUNTS_FILE).expect("failed to open file");
fn load_from_file(file_path: &str) -> IndexMap<String, i64> {
// Check if the file exists
if !Path::new(file_path).exists() {
return IndexMap::new();
}
let file = File::open(file_path).expect("failed to open file");
let mut reader = BufReader::new(&file);
serde_json::from_reader(&mut reader).expect("failed to parse file")
}

/// Save the current counts to a file
#[allow(dead_code)]
async fn save_to_file(counts: IndexMap<String, i64>) {
let file = File::create(ETHEREUM_CORE_COUNTS_FILE).expect("failed to create file");
serde_json::to_writer(&file, &counts).expect("failed to write file");
fn save_to_file(counts: &IndexMap<String, i64>, file_path: &str) {
let file = File::create(file_path).expect("failed to create file");
serde_json::to_writer(&file, counts).expect("failed to write file");
}

#[tokio::main]
async fn main() {
env_logger::init();
dotenv::dotenv().unwrap();

let client = Arc::new(SxTClient::new(
env::var("PROVER_ROOT_URL").unwrap_or("https://api.spaceandtime.dev".to_string()),
env::var("AUTH_ROOT_URL").unwrap_or("https://proxy.api.spaceandtime.dev".to_string()),
Expand All @@ -94,5 +123,8 @@ async fn main() {
})
.collect()
.await;
println!("current_counts: {current_counts:?}");
save_to_file(&current_counts, ETHEREUM_CORE_COUNTS_FILE);
// Compare previous and current counts
let previous_counts = load_from_file(ETHEREUM_CORE_COUNTS_FILE);
compare_counts(&current_counts, &previous_counts, &ETHEREUM_CORE_TABLES);
}

0 comments on commit 551d01c

Please sign in to comment.