Skip to content

Commit

Permalink
Merge pull request #127 from afadil/use-decimal
Browse files Browse the repository at this point in the history
Use decimal to support fraction
  • Loading branch information
afadil authored Sep 29, 2024
2 parents 121f74e + 9e892ae commit b0d84d1
Show file tree
Hide file tree
Showing 37 changed files with 994 additions and 688 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wealthfolio-app",
"private": true,
"version": "1.0.16",
"version": "1.0.17",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 2 additions & 0 deletions src-core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ rayon = "1.10.0"
r2d2 = "0.8.10"
dashmap = "6.1.0"
async-trait = "0.1.83"
bigdecimal = { version = "0.4.5", features = ["serde"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--- Down migration
ALTER TABLE portfolio_history DROP COLUMN calculated_at;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Your SQL goes here
-- Up migration
ALTER TABLE portfolio_history ADD COLUMN calculated_at TIMESTAMP NOT NULL DEFAULT '2024-09-28 12:00:00';

CREATE INDEX idx_activities_account_id ON activities(account_id);
22 changes: 22 additions & 0 deletions src-core/src/activity/activity_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
},
schema::{accounts, activities, assets},
};
use chrono::NaiveDate;
use diesel::prelude::*;
use uuid::Uuid;

Expand Down Expand Up @@ -212,4 +213,25 @@ impl ActivityRepository {
.order(activities::activity_date.asc())
.load::<Activity>(conn)
}

pub fn get_first_activity_date(
&self,
conn: &mut SqliteConnection,
account_ids: Option<&[String]>,
) -> Result<Option<NaiveDate>, diesel::result::Error> {
let mut query = activities::table
.inner_join(accounts::table.on(accounts::id.eq(activities::account_id)))
.filter(accounts::is_active.eq(true))
.into_boxed();

if let Some(ids) = account_ids {
query = query.filter(activities::account_id.eq_any(ids));
}

query
.select(diesel::dsl::min(diesel::dsl::date(
activities::activity_date,
)))
.first::<Option<NaiveDate>>(conn)
}
}
4 changes: 3 additions & 1 deletion src-core/src/market_data/market_data_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ impl MarketDataService {
.await
{
Ok(quotes) => all_quotes_to_insert.extend(quotes),
Err(e) => eprintln!("Error fetching history for {}: {}. Skipping.", symbol, e),
Err(e) => {
eprintln!("Error fetching history for {}: {}. Skipping.", symbol, e);
}
}
}

Expand Down
61 changes: 34 additions & 27 deletions src-core/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bigdecimal::BigDecimal;
use chrono::NaiveDateTime;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -283,7 +285,7 @@ pub struct ActivitySearchResponse {
pub meta: ActivitySearchResponseMeta,
}

#[derive(Serialize, Debug, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ActivityImport {
pub id: Option<String>,
Expand All @@ -304,18 +306,31 @@ pub struct ActivityImport {
pub line_number: Option<i32>,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Performance {
pub total_gain_percent: f64,
pub total_gain_amount: f64,
pub total_gain_amount_converted: f64,
pub day_gain_percent: Option<f64>,
pub day_gain_amount: Option<f64>,
pub day_gain_amount_converted: Option<f64>,
pub total_gain_percent: BigDecimal,
pub total_gain_amount: BigDecimal,
pub total_gain_amount_converted: BigDecimal,
pub day_gain_percent: Option<BigDecimal>,
pub day_gain_amount: Option<BigDecimal>,
pub day_gain_amount_converted: Option<BigDecimal>,
}

impl Default for Performance {
fn default() -> Self {
Performance {
total_gain_percent: BigDecimal::from(0),
total_gain_amount: BigDecimal::from(0),
total_gain_amount_converted: BigDecimal::from(0),
day_gain_percent: Some(BigDecimal::from(0)),
day_gain_amount: Some(BigDecimal::from(0)),
day_gain_amount_converted: Some(BigDecimal::from(0)),
}
}
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Sector {
pub name: String,
Expand All @@ -329,20 +344,21 @@ pub struct Holding {
pub symbol: String,
pub symbol_name: Option<String>,
pub holding_type: String,
pub quantity: f64,
pub quantity: BigDecimal,
pub currency: String,
pub base_currency: String,
pub market_price: Option<f64>,
pub average_cost: Option<f64>,
pub market_value: f64,
pub book_value: f64,
pub market_value_converted: f64,
pub book_value_converted: f64,
pub market_price: Option<BigDecimal>,
pub average_cost: Option<BigDecimal>,
pub market_value: BigDecimal,
pub book_value: BigDecimal,
pub market_value_converted: BigDecimal,
pub book_value_converted: BigDecimal,
pub performance: Performance,
pub account: Option<Account>,
pub asset_class: Option<String>,
pub asset_sub_class: Option<String>,
pub sectors: Option<Vec<Sector>>,
pub portfolio_percent: Option<BigDecimal>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -476,15 +492,6 @@ pub struct GoalsAllocation {
pub percent_allocation: i32,
}

#[derive(Debug, Serialize, QueryableByName)]
#[diesel(table_name = crate::schema::activities)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
#[serde(rename_all = "camelCase")]
pub struct MinDate {
pub activity_date: chrono::NaiveDateTime,
}


#[derive(Debug, Serialize, QueryableByName)]
#[serde(rename_all = "camelCase")]
#[diesel(table_name = crate::schema::activities)]
Expand All @@ -503,7 +510,6 @@ pub struct IncomeData {
pub amount: f64,
}


#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IncomeSummary {
Expand Down Expand Up @@ -572,7 +578,8 @@ pub struct PortfolioHistory {
pub day_gain_value: f64,
pub allocation_percentage: f64,
pub exchange_rate: f64,
pub holdings: Option<String>, // Holdings JSON
pub holdings: Option<String>,
pub calculated_at: NaiveDateTime,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
Loading

0 comments on commit b0d84d1

Please sign in to comment.