From 7f3a1236985546363f79a1fa5cefc0e34736134a Mon Sep 17 00:00:00 2001 From: Hunter Trujillo Date: Sun, 12 Nov 2023 14:00:09 -0800 Subject: [PATCH] Fixes. --- src/carbonado/metrics.rs | 122 ++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 48 deletions(-) diff --git a/src/carbonado/metrics.rs b/src/carbonado/metrics.rs index 21971d12..66247063 100644 --- a/src/carbonado/metrics.rs +++ b/src/carbonado/metrics.rs @@ -1,7 +1,7 @@ -use std::{collections::BTreeMap, path::Path, time::SystemTime}; +use std::{collections::BTreeMap, path::Path}; use anyhow::Result; -use chrono::{DateTime, Utc}; +use chrono::{DateTime, Duration, NaiveDate, Utc}; use serde::{Deserialize, Serialize}; use walkdir::WalkDir; @@ -23,38 +23,18 @@ pub fn metrics(dir: &Path) -> Result { response.wallets_by_network.insert("total".to_string(), 0); let mut total_wallets = 0; - let mut day_prior = None; for entry in WalkDir::new(dir) { let entry = entry?; let filename = entry.file_name().to_string_lossy().to_string(); let metadata = entry.metadata()?; - let day = metadata.created()?; - let day = round_system_time_to_day(day); + let day_created = metadata.modified()?; // TODO: .created(); + let day = round_datetime_to_day(day_created.into()); if metadata.is_file() { response.bytes += metadata.len(); - let bytes_total_day_prior = if let Some(dp) = &day_prior { - response.bytes_by_day.get(dp).unwrap_or(&0).to_owned() - } else { - 0 - }; - - let bitcoin_wallets_total_day_prior = if let Some(dp) = day_prior { - response - .bitcoin_wallets_by_day - .get(&dp) - .unwrap_or(&0) - .to_owned() - } else { - 0 - }; - - *response - .bytes_by_day - .entry(day.clone()) - .or_insert(bytes_total_day_prior) += metadata.len(); + *response.bytes_by_day.entry(day.clone()).or_insert(0) += metadata.len(); if filename == "bitcoin-6075e9716c984b37840f76ad2b50b3d1b98ed286884e5ceba5bcc8e6b74988d3.c15" @@ -66,7 +46,7 @@ pub fn metrics(dir: &Path) -> Result { *response .bitcoin_wallets_by_day .entry(day.clone()) - .or_insert(bitcoin_wallets_total_day_prior) += 1; + .or_insert(0) += 1; } if filename @@ -104,8 +84,6 @@ pub fn metrics(dir: &Path) -> Result { total_wallets += 1; } } - - day_prior = Some(day); } *response @@ -113,17 +91,60 @@ pub fn metrics(dir: &Path) -> Result { .get_mut("total") .unwrap_or(&mut 0) = total_wallets; + loop { + let start_day = DateTime::::from_naive_utc_and_offset( + NaiveDate::from_ymd_opt(2023, 7, 1) + .expect("correct date") + .and_hms_opt(0, 0, 0) + .expect("correct time"), + Utc, + ); + let day_prior = start_day - Duration::days(1); + let day = round_datetime_to_day(start_day); + let day_prior = round_datetime_to_day(day_prior); + + let mut bytes_day_prior = { + response + .bytes_by_day + .get(&day_prior) + .unwrap_or(&0) + .to_owned() + }; + + *response + .bytes_by_day + .get_mut(&day) + .unwrap_or(&mut bytes_day_prior) = bytes_day_prior; + + let mut bitcoin_wallets_day_prior = { + response + .bitcoin_wallets_by_day + .get(&day_prior) + .unwrap_or(&0) + .to_owned() + }; + + *response + .bitcoin_wallets_by_day + .get_mut(&day) + .unwrap_or(&mut bitcoin_wallets_day_prior) = bitcoin_wallets_day_prior; + + if day == "2023-07-01" { + break; + } + } + Ok(response) } pub fn metrics_csv(metrics: MetricsResponse) -> String { - let lines = vec![vec![ - "Wallet", - "Wallet Count", - "Bytes Total", - "Day", - "Bitcoin Wallets by Day", - "Bytes by Day", + let mut lines = vec![vec![ + "Wallet".to_owned(), + "Wallet Count".to_owned(), + "Bytes Total".to_owned(), + "Day".to_owned(), + "Bitcoin Wallets by Day".to_owned(), + "Bytes by Day".to_owned(), ]]; for (day, bitcoin_wallets) in metrics.bitcoin_wallets_by_day { @@ -135,7 +156,7 @@ pub fn metrics_csv(metrics: MetricsResponse) -> String { metrics .wallets_by_network .get("bitcoin") - .unwrap() + .expect("network is defined") .to_string(), ); line.push(metrics.bytes.to_string()); @@ -147,7 +168,7 @@ pub fn metrics_csv(metrics: MetricsResponse) -> String { metrics .wallets_by_network .get("testnet") - .unwrap() + .expect("network is defined") .to_string(), ); line.push("".to_owned()); @@ -159,7 +180,7 @@ pub fn metrics_csv(metrics: MetricsResponse) -> String { metrics .wallets_by_network .get("signet") - .unwrap() + .expect("network is defined") .to_string(), ); line.push("".to_owned()); @@ -171,7 +192,7 @@ pub fn metrics_csv(metrics: MetricsResponse) -> String { metrics .wallets_by_network .get("regtest") - .unwrap() + .expect("network is defined") .to_string(), ); line.push("".to_owned()); @@ -185,20 +206,25 @@ pub fn metrics_csv(metrics: MetricsResponse) -> String { line.push(day.clone()); line.push(bitcoin_wallets.to_string()); - line.push(metrics.bytes_by_day.get(&day).unwrap().to_string()) + line.push( + metrics + .bytes_by_day + .get(&day) + .expect("bytes values to be populated") + .to_string(), + ); + + lines.push(line); } let lines: Vec = lines.iter().map(|line| line.join(",")).collect(); lines.join("\n") } -fn round_system_time_to_day(system_time: SystemTime) -> String { - // Convert SystemTime to a Chrono DateTime - let datetime: DateTime = system_time.into(); - - // Round down to the nearest day (00:00:00) - let rounded = datetime.date_naive().and_hms_opt(0, 0, 0).unwrap(); - - // Format the date as a string in "YYYY-MM-DD" format +fn round_datetime_to_day(datetime: DateTime) -> String { + let rounded = datetime + .date_naive() + .and_hms_opt(0, 0, 0) + .expect("valid time"); rounded.format("%Y-%m-%d").to_string() }