Use libdrcr async API
This commit is contained in:
parent
af47021e4f
commit
4ff0ea46db
5
src-tauri/Cargo.lock
generated
5
src-tauri/Cargo.lock
generated
@ -211,9 +211,9 @@ checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.83"
|
version = "0.1.88"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
|
checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -2179,6 +2179,7 @@ checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
|
|||||||
name = "libdrcr"
|
name = "libdrcr"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
"chrono",
|
"chrono",
|
||||||
"downcast-rs 2.0.1",
|
"downcast-rs 2.0.1",
|
||||||
"dyn-clone",
|
"dyn-clone",
|
||||||
|
@ -28,7 +28,6 @@ use libdrcr::reporting::types::{
|
|||||||
};
|
};
|
||||||
use tauri::State;
|
use tauri::State;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tokio::task::spawn_blocking;
|
|
||||||
|
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
|
|
||||||
@ -40,55 +39,51 @@ pub(crate) async fn get_balance_sheet(
|
|||||||
let state = state.lock().await;
|
let state = state.lock().await;
|
||||||
let db_filename = state.db_filename.clone().unwrap();
|
let db_filename = state.db_filename.clone().unwrap();
|
||||||
|
|
||||||
spawn_blocking(move || {
|
// Connect to database
|
||||||
// Connect to database
|
let db_connection =
|
||||||
let db_connection =
|
DbConnection::new(format!("sqlite:{}", db_filename.as_str()).as_str()).await;
|
||||||
DbConnection::connect(format!("sqlite:{}", db_filename.as_str()).as_str());
|
|
||||||
|
|
||||||
// Initialise ReportingContext
|
// Initialise ReportingContext
|
||||||
let eofy_date = db_connection.metadata().eofy_date;
|
let eofy_date = db_connection.metadata().eofy_date;
|
||||||
let mut context = ReportingContext::new(db_connection, eofy_date, "$".to_string());
|
let mut context = ReportingContext::new(db_connection, eofy_date, "$".to_string());
|
||||||
register_lookup_fns(&mut context);
|
register_lookup_fns(&mut context);
|
||||||
register_dynamic_builders(&mut context);
|
register_dynamic_builders(&mut context);
|
||||||
|
|
||||||
// Get balance sheet
|
// Get balance sheet
|
||||||
let mut date_args = Vec::new();
|
let mut date_args = Vec::new();
|
||||||
for date in dates.iter() {
|
for date in dates.iter() {
|
||||||
date_args.push(DateArgs {
|
date_args.push(DateArgs {
|
||||||
date: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(),
|
date: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let targets = vec![
|
let targets = vec![
|
||||||
ReportingProductId {
|
ReportingProductId {
|
||||||
name: "CalculateIncomeTax",
|
name: "CalculateIncomeTax",
|
||||||
kind: ReportingProductKind::Transactions,
|
kind: ReportingProductKind::Transactions,
|
||||||
args: Box::new(VoidArgs {}),
|
args: Box::new(VoidArgs {}),
|
||||||
},
|
},
|
||||||
ReportingProductId {
|
ReportingProductId {
|
||||||
name: "BalanceSheet",
|
name: "BalanceSheet",
|
||||||
kind: ReportingProductKind::Generic,
|
kind: ReportingProductKind::Generic,
|
||||||
args: Box::new(MultipleDateArgs {
|
args: Box::new(MultipleDateArgs {
|
||||||
dates: date_args.clone(),
|
dates: date_args.clone(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// Run report
|
// Run report
|
||||||
let products = generate_report(targets, &context).unwrap();
|
let products = generate_report(targets, &context).await.unwrap();
|
||||||
let result = products
|
let result = products
|
||||||
.get_or_err(&ReportingProductId {
|
.get_or_err(&ReportingProductId {
|
||||||
name: "BalanceSheet",
|
name: "BalanceSheet",
|
||||||
kind: ReportingProductKind::Generic,
|
kind: ReportingProductKind::Generic,
|
||||||
args: Box::new(MultipleDateArgs { dates: date_args }),
|
args: Box::new(MultipleDateArgs { dates: date_args }),
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let balance_sheet = result.downcast_ref::<DynamicReport>().unwrap().to_json();
|
let balance_sheet = result.downcast_ref::<DynamicReport>().unwrap().to_json();
|
||||||
|
|
||||||
Ok(balance_sheet)
|
Ok(balance_sheet)
|
||||||
})
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@ -99,54 +94,50 @@ pub(crate) async fn get_income_statement(
|
|||||||
let state = state.lock().await;
|
let state = state.lock().await;
|
||||||
let db_filename = state.db_filename.clone().unwrap();
|
let db_filename = state.db_filename.clone().unwrap();
|
||||||
|
|
||||||
spawn_blocking(move || {
|
// Connect to database
|
||||||
// Connect to database
|
let db_connection =
|
||||||
let db_connection =
|
DbConnection::new(format!("sqlite:{}", db_filename.as_str()).as_str()).await;
|
||||||
DbConnection::connect(format!("sqlite:{}", db_filename.as_str()).as_str());
|
|
||||||
|
|
||||||
// Initialise ReportingContext
|
// Initialise ReportingContext
|
||||||
let eofy_date = db_connection.metadata().eofy_date;
|
let eofy_date = db_connection.metadata().eofy_date;
|
||||||
let mut context = ReportingContext::new(db_connection, eofy_date, "$".to_string());
|
let mut context = ReportingContext::new(db_connection, eofy_date, "$".to_string());
|
||||||
register_lookup_fns(&mut context);
|
register_lookup_fns(&mut context);
|
||||||
register_dynamic_builders(&mut context);
|
register_dynamic_builders(&mut context);
|
||||||
|
|
||||||
// Get income statement
|
// Get income statement
|
||||||
let mut date_args = Vec::new();
|
let mut date_args = Vec::new();
|
||||||
for (date_start, date_end) in dates.iter() {
|
for (date_start, date_end) in dates.iter() {
|
||||||
date_args.push(DateStartDateEndArgs {
|
date_args.push(DateStartDateEndArgs {
|
||||||
date_start: NaiveDate::parse_from_str(date_start, "%Y-%m-%d").unwrap(),
|
date_start: NaiveDate::parse_from_str(date_start, "%Y-%m-%d").unwrap(),
|
||||||
date_end: NaiveDate::parse_from_str(date_end, "%Y-%m-%d").unwrap(),
|
date_end: NaiveDate::parse_from_str(date_end, "%Y-%m-%d").unwrap(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
let targets = vec![
|
let targets = vec![
|
||||||
ReportingProductId {
|
ReportingProductId {
|
||||||
name: "CalculateIncomeTax",
|
name: "CalculateIncomeTax",
|
||||||
kind: ReportingProductKind::Transactions,
|
kind: ReportingProductKind::Transactions,
|
||||||
args: Box::new(VoidArgs {}),
|
args: Box::new(VoidArgs {}),
|
||||||
},
|
},
|
||||||
ReportingProductId {
|
ReportingProductId {
|
||||||
name: "IncomeStatement",
|
name: "IncomeStatement",
|
||||||
kind: ReportingProductKind::Generic,
|
kind: ReportingProductKind::Generic,
|
||||||
args: Box::new(MultipleDateStartDateEndArgs {
|
args: Box::new(MultipleDateStartDateEndArgs {
|
||||||
dates: date_args.clone(),
|
dates: date_args.clone(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// Run report
|
// Run report
|
||||||
let products = generate_report(targets, &context).unwrap();
|
let products = generate_report(targets, &context).await.unwrap();
|
||||||
let result = products
|
let result = products
|
||||||
.get_or_err(&ReportingProductId {
|
.get_or_err(&ReportingProductId {
|
||||||
name: "IncomeStatement",
|
name: "IncomeStatement",
|
||||||
kind: ReportingProductKind::Generic,
|
kind: ReportingProductKind::Generic,
|
||||||
args: Box::new(MultipleDateStartDateEndArgs { dates: date_args }),
|
args: Box::new(MultipleDateStartDateEndArgs { dates: date_args }),
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let income_statement = result.downcast_ref::<DynamicReport>().unwrap().to_json();
|
let income_statement = result.downcast_ref::<DynamicReport>().unwrap().to_json();
|
||||||
|
|
||||||
Ok(income_statement)
|
Ok(income_statement)
|
||||||
})
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user