Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions patch.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- tests/integration_test.rs
+++ tests/integration_test.rs
@@ -155,7 +155,9 @@
let _micro_var = helpers::prompt_microstructure_variable(&mut connection)
.unwrap_or('s');

+ tokio::time::sleep(Duration::from_secs(5)).await;
+
let divide_s = helpers::prompt_cutoff_value().unwrap_or(180000.0);

+ tokio::time::sleep(Duration::from_secs(5)).await;
+
// Detach the default 100000 partitions to keep the data safe before dropping target partitions
18 changes: 12 additions & 6 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use ::ai_micro::*;
use ai_infra::establish_connection;

Expand All @@ -11,8 +10,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
return Ok(());
};

let avg_value_population =
calculate_whole_population_trades_ec_average(connection, micro_var);
let avg_value_population = calculate_whole_population_trades_ec_average(connection, micro_var);

let divide_s = if let Some(cutoff) = helpers::prompt_cutoff_value() {
cutoff
Expand All @@ -22,9 +20,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let _divide = divide::Divide::Float(divide_s);

// Something else
calculate_population_1_trades_ec_average(connection, divide::Divide::Float(divide_s), micro_var);

calculate_population_2_trades_ec_average(connection, divide::Divide::Float(divide_s), micro_var);
calculate_population_1_trades_ec_average(
connection,
divide::Divide::Float(divide_s),
micro_var,
);

calculate_population_2_trades_ec_average(
connection,
divide::Divide::Float(divide_s),
micro_var,
);

let (p1, p2, n1, n2) = calculate_proportions_partioned_table(connection, avg_value_population)?;

Expand Down
23 changes: 16 additions & 7 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io::{self, Write};
use diesel::prelude::*;
use diesel::dsl::{min, max};
use ai_infra::schema::objects_s::dsl::*;
use diesel::PgConnection;
use diesel::dsl::{max, min};
use diesel::prelude::*;
use std::io::{self, Write};

pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<char> {
print!("Choose a microstructure variable amongst the following choices: 1) Trade size: ");
Expand All @@ -11,7 +11,9 @@ pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<c
let mut input = String::new();

// Read user input
io::stdin().read_line(&mut input).expect("Failed to read input");
io::stdin()
.read_line(&mut input)
.expect("Failed to read input");

// Check if input is "1)" or "1"
let trimmed = input.trim();
Expand All @@ -25,7 +27,10 @@ pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<c

match result {
Ok(Some((Some(min_val), Some(max_val)))) => {
println!("Range of values for trade size: min = {}, max = {}", min_val, max_val);
println!(
"Range of values for trade size: min = {}, max = {}",
min_val, max_val
);
}
_ => {
println!("Could not find min/max values for trade size.");
Expand All @@ -39,13 +44,17 @@ pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<c
}

pub fn prompt_cutoff_value() -> Option<f32> {
print!("Enter a cutoff value for the microstructure variable you selected, it should be within the range: ");
print!(
"Enter a cutoff value for the microstructure variable you selected, it should be within the range: "
);
io::stdout().flush().unwrap();

let mut input = String::new();

// Read user input
io::stdin().read_line(&mut input).expect("Failed to read input");
io::stdin()
.read_line(&mut input)
.expect("Failed to read input");

// The user can only enter a number for now, anything else will exit the function.
if let Ok(divide) = input.trim().parse::<f32>() {
Expand Down
99 changes: 48 additions & 51 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pub mod helpers;
pub mod divide;
pub mod helpers;

use ai_infra::models::ObjectS;
use ai_infra::schema::objects_s::dsl::objects_s;
use ai_infra::schema::{objects::dsl as obj_dsl, objects_s::dsl as obj_s_dsl};
use ai_infra::schema::objects_s::dsl::*;
use ai_infra::schema::{objects::dsl as obj_dsl, objects_s::dsl as obj_s_dsl};
use chrono::NaiveDateTime;
use diesel::ExpressionMethods;
use diesel::dsl::{avg, max};
use diesel::prelude::*;
use diesel::{PgConnection, QueryDsl, RunQueryDsl};
use divide::Divide;
use chrono::NaiveDateTime;

pub fn backwards(
connection: &mut PgConnection,
Expand Down Expand Up @@ -153,40 +153,40 @@ pub fn calculate_population_2_trades_ec_average(
if micro_var == 's' {
let result_2: Result<Option<f64>, diesel::result::Error> = match divide {
Divide::Float(val) => {
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s.select(max(obj_s_dsl::s)).first(connection);
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s
.select(max(obj_s_dsl::s))
.first(connection);
match max_value {
Ok(Some(max_val)) => {
obj_s_dsl::objects_s
.filter(obj_s_dsl::s.ge(val).and(obj_s_dsl::s.le(max_val)))
.select(avg(obj_s_dsl::c))
.first(connection)
},
_ => Ok(None)
Ok(Some(max_val)) => obj_s_dsl::objects_s
.filter(obj_s_dsl::s.ge(val).and(obj_s_dsl::s.le(max_val)))
.select(avg(obj_s_dsl::c))
.first(connection),
_ => Ok(None),
}
}
Divide::Timestamp(ts) => {
let max_value: Result<Option<NaiveDateTime>, _> = obj_s_dsl::objects_s.select(max(obj_s_dsl::d)).first(connection);
let max_value: Result<Option<NaiveDateTime>, _> = obj_s_dsl::objects_s
.select(max(obj_s_dsl::d))
.first(connection);
match max_value {
Ok(Some(max_val)) => {
obj_s_dsl::objects_s
.filter(obj_s_dsl::d.ge(ts).and(obj_s_dsl::d.le(max_val)))
.select(avg(obj_s_dsl::c))
.first(connection)
},
_ => Ok(None)
Ok(Some(max_val)) => obj_s_dsl::objects_s
.filter(obj_s_dsl::d.ge(ts).and(obj_s_dsl::d.le(max_val)))
.select(avg(obj_s_dsl::c))
.first(connection),
_ => Ok(None),
}
}
Divide::None => {
let divide_s = 195000.0_f32;
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s.select(max(obj_s_dsl::s)).first(connection);
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s
.select(max(obj_s_dsl::s))
.first(connection);
match max_value {
Ok(Some(max_val)) => {
obj_s_dsl::objects_s
.filter(obj_s_dsl::s.ge(divide_s).and(obj_s_dsl::s.le(max_val)))
.select(avg(obj_s_dsl::c))
.first(connection)
},
_ => Ok(None)
Ok(Some(max_val)) => obj_s_dsl::objects_s
.filter(obj_s_dsl::s.ge(divide_s).and(obj_s_dsl::s.le(max_val)))
.select(avg(obj_s_dsl::c))
.first(connection),
_ => Ok(None),
}
}
};
Expand All @@ -199,40 +199,37 @@ pub fn calculate_population_2_trades_ec_average(
} else {
let result_2: Result<Option<f64>, diesel::result::Error> = match divide {
Divide::Float(val) => {
let max_value: Result<Option<f32>, _> = obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
let max_value: Result<Option<f32>, _> =
obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
match max_value {
Ok(Some(max_val)) => {
obj_dsl::objects
.filter(obj_dsl::s.ge(val).and(obj_dsl::s.le(max_val)))
.select(avg(obj_dsl::c))
.first(connection)
},
_ => Ok(None)
Ok(Some(max_val)) => obj_dsl::objects
.filter(obj_dsl::s.ge(val).and(obj_dsl::s.le(max_val)))
.select(avg(obj_dsl::c))
.first(connection),
_ => Ok(None),
}
}
Divide::Timestamp(ts) => {
let max_value: Result<Option<NaiveDateTime>, _> = obj_dsl::objects.select(max(obj_dsl::d)).first(connection);
let max_value: Result<Option<NaiveDateTime>, _> =
obj_dsl::objects.select(max(obj_dsl::d)).first(connection);
match max_value {
Ok(Some(max_val)) => {
obj_dsl::objects
.filter(obj_dsl::d.ge(ts).and(obj_dsl::d.le(max_val)))
.select(avg(obj_dsl::c))
.first(connection)
},
_ => Ok(None)
Ok(Some(max_val)) => obj_dsl::objects
.filter(obj_dsl::d.ge(ts).and(obj_dsl::d.le(max_val)))
.select(avg(obj_dsl::c))
.first(connection),
_ => Ok(None),
}
}
Divide::None => {
let divide_s = 195000.0_f32;
let max_value: Result<Option<f32>, _> = obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
let max_value: Result<Option<f32>, _> =
obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
match max_value {
Ok(Some(max_val)) => {
obj_dsl::objects
.filter(obj_dsl::s.ge(divide_s).and(obj_dsl::s.le(max_val)))
.select(avg(obj_dsl::c))
.first(connection)
},
_ => Ok(None)
Ok(Some(max_val)) => obj_dsl::objects
.filter(obj_dsl::s.ge(divide_s).and(obj_dsl::s.le(max_val)))
.select(avg(obj_dsl::c))
.first(connection),
_ => Ok(None),
}
}
};
Expand Down
31 changes: 23 additions & 8 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ai_infra::divider;
use ai_infra::schema::objects_s::dsl::objects_s;
use ai_infra::schema::objects_s::*;
use ai_infra::divider;
use ai_micro::{calculate_c, calculate_mp, find_all_with_t, find_nearest, helpers};
use chrono::{Duration as ChronoDuration, NaiveDate, NaiveTime};
use diesel::ExpressionMethods;
Expand Down Expand Up @@ -143,20 +143,29 @@ async fn test_end_to_end_sequence() {
#[cfg(unix)]
{
let temp_file_path = "/tmp/mock_stdin.txt";
let mut mock_file = std::fs::File::create(temp_file_path).expect("Failed to create mock file");
mock_file.write_all(b"1\n180000.0\n").expect("Failed to write mock input");
let mut mock_file =
std::fs::File::create(temp_file_path).expect("Failed to create mock file");
mock_file
.write_all(b"1\n180000.0\n")
.expect("Failed to write mock input");

let mock_file_read = std::fs::File::open(temp_file_path).expect("Failed to open mock file");
unsafe {
libc::dup2(std::os::fd::AsRawFd::as_raw_fd(&mock_file_read), libc::STDIN_FILENO);
libc::dup2(
std::os::fd::AsRawFd::as_raw_fd(&mock_file_read),
libc::STDIN_FILENO,
);
}
}

let _micro_var = helpers::prompt_microstructure_variable(&mut connection)
.unwrap_or('s');
let _micro_var = helpers::prompt_microstructure_variable(&mut connection).unwrap_or('s');

tokio::time::sleep(Duration::from_secs(5)).await;

let divide_s = helpers::prompt_cutoff_value().unwrap_or(180000.0);

tokio::time::sleep(Duration::from_secs(5)).await;

// Detach the default 100000 partitions to keep the data safe before dropping target partitions
sql_query("ALTER TABLE objects_s DETACH PARTITION objects_s_100000_below;")
.execute(&mut connection)
Expand All @@ -165,8 +174,14 @@ async fn test_end_to_end_sequence() {
.execute(&mut connection)
.unwrap();

let drop_below = format!("DROP TABLE IF EXISTS objects_s_below_{} CASCADE;", divide_s as i64);
let drop_above = format!("DROP TABLE IF EXISTS objects_s_above_{} CASCADE;", divide_s as i64);
let drop_below = format!(
"DROP TABLE IF EXISTS objects_s_below_{} CASCADE;",
divide_s as i64
);
let drop_above = format!(
"DROP TABLE IF EXISTS objects_s_above_{} CASCADE;",
divide_s as i64
);

sql_query(drop_below).execute(&mut connection).unwrap();
sql_query(drop_above).execute(&mut connection).unwrap();
Expand Down
Loading