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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "quantitative-data-analysis-rs"
version = "0.36.1"
version = "0.37.0"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
7,424 changes: 3,712 additions & 3,712 deletions assets/fetch_scoring_data.json

Large diffs are not rendered by default.

40,033 changes: 15,332 additions & 24,701 deletions assets/statements.tsv

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions examples/make_statements.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bytes::Bytes;
use csv::WriterBuilder;
use quantitative_data_analysis_rs::domain::models::statement::model;
use quantitative_data_analysis_rs::domain::repositories::company::repository::Company;
use quantitative_data_analysis_rs::domain::repositories::statement::queries::get_statement;
use quantitative_data_analysis_rs::domain::repositories::statement::queries::get_statements;
use quantitative_data_analysis_rs::domain::repositories::statement::repository::Statement;
use quantitative_data_analysis_rs::infrastructure::dsv::Dsv;
use quantitative_data_analysis_rs::infrastructure::jquants_api::JQuantsAPI;
Expand Down Expand Up @@ -43,15 +42,13 @@ async fn main() -> anyhow::Result<()> {

for (index, company) in companies.0.into_iter().enumerate() {
let code = company.code;
let query = get_statement::Query {
code: code.as_str(),
let query = get_statements::Query {
code: Some(code.as_str()),
};

match jquants_api.get_row_full_year_statements(&query).await {
Ok(mut rows) => {
rows.sort_by(|left, right| right.disclosed_date.cmp(&left.disclosed_date));
for row in rows {
let statement = model::Statement::try_from(row)?;
match jquants_api.get_statements(&query).await {
Ok(statements) => {
for statement in statements.0 {
writer.serialize(statement_tsv::Structure::from(statement))?;
}
success_count += 1;
Expand Down
10 changes: 5 additions & 5 deletions src/domain/models/company/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ impl Companies {
}
}

fn is_target_company(company: &Company) -> bool {
company.product_category.as_str() == DOMESTIC_STOCK_PRODUCT_CATEGORY
&& TARGET_MARKETS.contains(&company.market.as_str())
}

impl TryFrom<RowCompany> for Company {
type Error = anyhow::Error;

Expand All @@ -58,11 +63,6 @@ impl TryFrom<RowCompany> for Company {
}
}

fn is_target_company(company: &Company) -> bool {
company.product_category.as_str() == DOMESTIC_STOCK_PRODUCT_CATEGORY
&& TARGET_MARKETS.contains(&company.market.as_str())
}

#[cfg(test)]
mod tests {
use super::{Companies, Company};
Expand Down
35 changes: 20 additions & 15 deletions src/domain/models/statement/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
use chrono::NaiveDate;
use deref_derive::{Deref, DerefMut};
use serde::Deserialize;

#[derive(Deserialize, Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct RowStatement {
pub code: String,
pub disclosed_date: Option<NaiveDate>,
pub current_fiscal_year_end_date: Option<NaiveDate>,
pub bps: Option<f64>,
pub eps: Option<f64>,
pub annual_dividend_forecast: Option<f64>,
pub net_sales: Option<f64>,
pub opp: Option<f64>,
pub orp: Option<f64>,
pub profit: Option<f64>,
pub equity: Option<f64>,
pub total_assets: Option<f64>,
}

/// Represents a financial statement with various financial metrics.
///
Expand Down Expand Up @@ -55,21 +73,8 @@ pub struct Statement {
pub total_assets: f64,
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Default)]
pub struct RowStatement {
pub code: String,
pub disclosed_date: Option<NaiveDate>,
pub current_fiscal_year_end_date: Option<NaiveDate>,
pub bps: Option<f64>,
pub eps: Option<f64>,
pub annual_dividend_forecast: Option<f64>,
pub net_sales: Option<f64>,
pub opp: Option<f64>,
pub orp: Option<f64>,
pub profit: Option<f64>,
pub equity: Option<f64>,
pub total_assets: Option<f64>,
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Default, Deref, DerefMut)]
pub struct Statements(pub Vec<Statement>);

impl TryFrom<RowStatement> for Statement {
type Error = anyhow::Error;
Expand Down
2 changes: 1 addition & 1 deletion src/domain/repositories/statement/queries.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod get_statement;
pub mod get_statements;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, Default)]
pub struct Query<'a> {
pub code: &'a str,
pub code: Option<&'a str>,
}
32 changes: 14 additions & 18 deletions src/domain/repositories/statement/repository.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
use anyhow::Result;
use async_trait::async_trait;

use crate::domain::models::statement::model;
use crate::domain::repositories::statement::queries;
use anyhow::Result;
use async_trait::async_trait;

#[async_trait]
pub trait Statement {
async fn get_row_statement<'a>(
&self,
query: &queries::get_statement::Query<'a>,
) -> Result<model::RowStatement>;

async fn get_row_full_year_statements<'a>(
async fn get_statements<'a>(
&self,
query: &queries::get_statement::Query<'a>,
) -> Result<Vec<model::RowStatement>> {
Ok(vec![self.get_row_statement(query).await?])
query: &queries::get_statements::Query<'a>,
) -> Result<model::Statements> {
let row_statements = self.get_row_statements(query).await?;
let vec_statement = row_statements
.into_iter()
.filter_map(|row_statement| TryFrom::try_from(row_statement).ok())
.collect();
Ok(model::Statements(vec_statement))
}

async fn get_statement<'a>(
async fn get_row_statements<'a>(
&self,
query: &queries::get_statement::Query<'a>,
) -> Result<model::Statement> {
let row_statement = self.get_row_statement(query).await?;
TryFrom::try_from(row_statement)
}
query: &queries::get_statements::Query<'a>,
) -> Result<Vec<model::RowStatement>>;
}
78 changes: 29 additions & 49 deletions src/infrastructure/repositories/statement/dsv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,34 @@ use crate::domain::models::statement::model;
use crate::domain::repositories::statement::{queries, repository};
use crate::infrastructure::dsv::Dsv;
use crate::infrastructure::from_slice::FromSlice;
use anyhow::{Context, Result};
use std::backtrace::Backtrace;
use anyhow::Result;
use async_trait::async_trait;
use std::fmt::{Debug, Display};

#[async_trait::async_trait]
#[async_trait]
impl<T> repository::Statement for Dsv<T>
where
T: FromSlice<Item = model::RowStatement> + Send + Sync,
<T as FromSlice>::Deserialize: Send + Sync,
<T::Item as TryFrom<T::Deserialize>>::Error: Debug + Display + Send + Sync,
model::RowStatement: TryFrom<<T as FromSlice>::Deserialize>,
{
async fn get_row_statement<'a>(
async fn get_row_statements<'a>(
&self,
query: &queries::get_statement::Query<'a>,
) -> Result<model::RowStatement> {
let vec_row_statement = self.get_row_full_year_statements(query).await?;
vec_row_statement.first().cloned().with_context(|| {
format!(
"Not found statement: {:?}\n{}",
&query,
Backtrace::force_capture(),
)
})
}

async fn get_row_full_year_statements<'a>(
&self,
query: &queries::get_statement::Query<'a>,
query: &queries::get_statements::Query<'a>,
) -> Result<Vec<model::RowStatement>> {
let processed = T::process_tabular_data(self.buffer.as_ref(), self.has_headers)?;
let mut filtered: Vec<model::RowStatement> = processed
.into_iter()
.filter(|statement| statement.code.eq(query.code))
.collect();
filtered.sort_by(|a, b| {
let a_date = a.disclosed_date.unwrap_or_default();
let b_date = b.disclosed_date.unwrap_or_default();
b_date.cmp(&a_date)
});
Ok(filtered)

let vec_row_statement: Vec<model::RowStatement> = match query.code {
Some(code) => processed
.into_iter()
.filter(|statement| statement.code.eq(code))
.collect(),
// Noneの場合は、全ての行を返す
None => processed,
};

Ok(vec_row_statement)
}
}

Expand All @@ -53,33 +40,26 @@ mod tests {
use crate::infrastructure::dsv::Dsv;
use crate::infrastructure::repositories::statement::structures::internal::tsv;
use bytes::Bytes;
use chrono::NaiveDate;

const SAMPLE_CODE: &str = "72030";
const TSV: &[u8] = include_bytes!("../../../../assets/statements.tsv");

#[tokio::test]
async fn get_row_full_year_statements_returns_sorted_by_disclosed_date_desc()
-> anyhow::Result<()> {
let repository = Dsv::<tsv::Structure>::new(true, Bytes::from(TSV));
let query = queries::get_statement::Query { code: SAMPLE_CODE };
let actual = repository.get_row_full_year_statements(&query).await?;
assert!(!actual.is_empty());
for i in 0..actual.len() - 1 {
let current_date = actual[i].disclosed_date.unwrap_or_default();
let next_date = actual[i + 1].disclosed_date.unwrap_or_default();
assert!(current_date >= next_date);
}
Ok(())
}

#[tokio::test]
async fn get_row_statement_returns_latest() -> anyhow::Result<()> {
let repository = Dsv::<tsv::Structure>::new(true, Bytes::from(TSV));
let query = queries::get_statement::Query { code: SAMPLE_CODE };
let actual = repository.get_row_statement(&query).await?;
let all_statements = repository.get_row_full_year_statements(&query).await?;
assert_eq!(actual.code, all_statements[0].code);
assert_eq!(actual.disclosed_date, all_statements[0].disclosed_date);
let query = queries::get_statements::Query {
code: Some(SAMPLE_CODE),
};
let actual = repository.get_row_statements(&query).await?;
assert_eq!(actual[0].code, SAMPLE_CODE);
assert_eq!(
actual[0].disclosed_date,
NaiveDate::from_ymd_opt(2022, 5, 11)
);
let actual = actual.last().unwrap();
assert_eq!(actual.code, SAMPLE_CODE);
assert_eq!(actual.disclosed_date, NaiveDate::from_ymd_opt(2026, 5, 8));
Ok(())
}
}
Loading
Loading