Skip to content

Commit

Permalink
Improved logging messages
Browse files Browse the repository at this point in the history
  • Loading branch information
felipet committed Feb 14, 2025
1 parent d3ff483 commit d5bff3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
15 changes: 6 additions & 9 deletions src/feeders/ibex_short_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use finance_ibex::IbexCompany;
use sqlx::{prelude::FromRow, types::Uuid, Executor, PgPool};
use std::error::Error;
use std::sync::Arc;
use tracing::{debug, error, info, instrument};
use tracing::{debug, error, info, warn, instrument};

/// Data provider for short positions against stocks that belong to the Ibex35.
///
Expand Down Expand Up @@ -184,6 +184,7 @@ impl<'a> IbexShortFeeder<'a> {
pub async fn add_today_data(&self) -> Result<(), Box<dyn Error>> {
// Let's get an updated listing of the Ibex35's companies.
let companies = self.stock_listing().await?;
debug!("{} companies listed from the IBEX35", companies.len());

// For each company, request to the CNMV's site if there's any open short position.
for company in companies.iter().filter(|x| x.extra_id().is_some()) {
Expand All @@ -195,14 +196,10 @@ impl<'a> IbexShortFeeder<'a> {

// If we got some short position
if !new_positions.positions.is_empty() {
debug!(
"The company {} has the following open short positions: {:?}",
company.ticker(),
new_positions.positions
);
// First, let's get a list of the active short positions for the company which are already registered
// in the DB.
let stored_position = self.active_positions(company.ticker()).await?;
debug!("Stored positions for {}: {:?}", company.ticker(), stored_position);

// Check whether any new position was already present in the DB.
for new_position in new_positions.positions {
Expand Down Expand Up @@ -258,7 +255,7 @@ impl<'a> IbexShortFeeder<'a> {
let stored_active_positions = self.active_positions(company.ticker()).await?;

if !stored_active_positions.is_empty() {
info!(
warn!(
"The company {} got free of significant short positions",
company.ticker()
);
Expand Down Expand Up @@ -287,8 +284,6 @@ impl<'a> IbexShortFeeder<'a> {
Err(e) => return Err(DbError::Unknown(format!("{e}"))),
};

debug!("The listing of companies of the Ibex35: {:?}", companies);

Ok(companies)
}

Expand Down Expand Up @@ -389,6 +384,8 @@ impl<'a> IbexShortFeeder<'a> {
.await
.map_err(|e| DbError::Unknown(e.to_string()))?;

info!("New position registered in the record ({uuid})");

Ok(uuid)
}

Expand Down
15 changes: 10 additions & 5 deletions src/web_scrappers/cnmv_scrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use finance_api::Company;
use finance_ibex::IbexCompany;
use reqwest;
use scraper::{Html, Selector};
use tracing::{error, instrument, trace};
use tracing::{error, instrument, trace, debug};

/// Handler to extract data from the CNMV web page.
///
Expand Down Expand Up @@ -93,7 +93,10 @@ impl CnmvProvider {
) -> Result<ShortResponse, CnmvError> {
// Select the endpoint that shall be used for the requested GET.
let endpoint = match endpoint {
EndpointSel::ShortEP => &self.short_ext[..],
EndpointSel::ShortEP => {
trace!("Collecting data for the short position endpoint");
&self.short_ext[..]
},
};

// Retrieve the companie's ISIN.
Expand All @@ -111,15 +114,15 @@ impl CnmvProvider {

if resp.status().as_u16() != 200 {
let error_string = resp.status().as_str().to_string();
error!("Error found during the request: {error_string}");
Err(CnmvError::ExternalError(error_string))
} else {
trace!("The endpoint returned valid data, proceeding to parse it");
let response = ShortResponse::parse(
resp.text()
.await
.map_err(|e| CnmvError::InternalError(e.to_string()))?,
)?;
trace!("Response: {:?}", response);

Ok(response)
}
}
Expand Down Expand Up @@ -195,13 +198,15 @@ impl CnmvProvider {
match Madrid.from_local_datetime(&date.and_hms_opt(15, 30, 0).unwrap()) {
LocalResult::Single(value) => value.to_utc(),
_ => {
error!("The given naive date does not convert to UTC.");
error!("The given naive date: ({date}) does not convert to UTC.");
return Err(CnmvError::InternalError(
"Failed to build a valid date.".to_owned(),
));
}
};

debug!("A valid short position was detected: {owner} - {weight} - {open_date} - {}", stock.ticker());

positions.push(ShortPosition {
owner,
weight,
Expand Down

0 comments on commit d5bff3c

Please sign in to comment.