Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoquick committed Nov 12, 2023
1 parent e75d5cd commit 7f3a123
Showing 1 changed file with 74 additions and 48 deletions.
122 changes: 74 additions & 48 deletions src/carbonado/metrics.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -23,38 +23,18 @@ pub fn metrics(dir: &Path) -> Result<MetricsResponse> {
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"
Expand All @@ -66,7 +46,7 @@ pub fn metrics(dir: &Path) -> Result<MetricsResponse> {
*response
.bitcoin_wallets_by_day
.entry(day.clone())
.or_insert(bitcoin_wallets_total_day_prior) += 1;
.or_insert(0) += 1;
}

if filename
Expand Down Expand Up @@ -104,26 +84,67 @@ pub fn metrics(dir: &Path) -> Result<MetricsResponse> {
total_wallets += 1;
}
}

day_prior = Some(day);
}

*response
.wallets_by_network
.get_mut("total")
.unwrap_or(&mut 0) = total_wallets;

loop {
let start_day = DateTime::<Utc>::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 {
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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<String> = 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<Utc> = 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<Utc>) -> String {
let rounded = datetime
.date_naive()
.and_hms_opt(0, 0, 0)
.expect("valid time");
rounded.format("%Y-%m-%d").to_string()
}

0 comments on commit 7f3a123

Please sign in to comment.