Skip to content

Commit

Permalink
scaffold matching
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Dec 8, 2024
1 parent c481e53 commit f8be7e1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
59 changes: 58 additions & 1 deletion src/analytics/offline_matching.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::BTreeMap;

use anyhow::Result;
use chrono::{DateTime, Utc};
use chrono::{DateTime, Duration, Utc};
use diem_types::account_address::AccountAddress;
use neo4rs::Graph;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -99,3 +101,58 @@ pub async fn get_min_funding(
}
Ok(min_funding)
}

#[derive(Clone, Default, Debug)]
pub struct Candidates {
maybe: Vec<AccountAddress>,
impossible: Vec<AccountAddress>,
}

#[derive(Clone, Default, Debug)]
pub struct Matching(pub BTreeMap<u32, Candidates>);

impl Matching {
pub fn new() -> Self {
Matching(BTreeMap::new())
}

pub fn match_deposit_to_funded(&mut self, deposits: Vec<Deposit>, funded: Vec<MinFunding>) {
for f in funded.iter() {
deposits.iter().for_each(|d| {
let candidates = self.0.entry(f.user_id).or_default();
// only addresses with minimum funded could be a Maybe
if d.deposited >= f.funded {
candidates.maybe.push(d.account);
} else {
candidates.impossible.push(d.account);
}
});
}
}
}

pub async fn rip_range(pool: &Graph, start: DateTime<Utc>, end: DateTime<Utc>) -> Result<Matching> {
let mut matches = Matching::new();

// loop each day.
for d in days_in_range(start, end) {
let deposits = get_date_range_deposits(pool, 100, start, d).await?;
let funded = get_min_funding(pool, 20, start, d).await?;

matches.match_deposit_to_funded(deposits, funded);
}

Ok(matches)
}

fn days_in_range(start: DateTime<Utc>, end: DateTime<Utc>) -> Vec<DateTime<Utc>> {
let mut days = Vec::new();
let mut current = start;

while current <= end {
days.push(current);
current += Duration::days(1); // Increment by one day
}

days
}
19 changes: 17 additions & 2 deletions tests/test_analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,26 @@ async fn test_offline_analytics() -> Result<()> {
let start_time = parse_date("2024-01-01");
let end_time = parse_date("2024-01-10");

let r = offline_matching::get_date_range_deposits(&pool, 20, start_time, end_time).await?;
let _r = offline_matching::get_date_range_deposits(&pool, 20, start_time, end_time).await?;
// dbg!(&r);

let r = offline_matching::get_min_funding(&pool, 20, start_time, end_time).await?;
let _r = offline_matching::get_min_funding(&pool, 20, start_time, end_time).await?;

Ok(())
}

#[tokio::test]
async fn test_offline_analytics_matching() -> Result<()> {
libra_forensic_db::log_setup();
let (uri, user, pass) = neo4j_init::get_credentials_from_env()?;
let pool = neo4j_init::get_neo4j_remote_pool(&uri, &user, &pass).await?;

let start_time = parse_date("2024-01-01");
let end_time = parse_date("2024-01-10");

let r = offline_matching::rip_range(&pool, start_time, end_time).await?;

dbg!(&r.0.len());

Ok(())
}

0 comments on commit f8be7e1

Please sign in to comment.