From a7c5cd89a1da71117e813f6108636272e1a3cc56 Mon Sep 17 00:00:00 2001 From: ImMin5 Date: Mon, 9 Sep 2024 17:10:50 +0900 Subject: [PATCH] fix: fix get currency info Signed-off-by: ImMin5 --- .../connector/currency_connector.py | 60 +++++++++++++++---- .../service/data_source_service.py | 8 +-- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/spaceone/cost_analysis/connector/currency_connector.py b/src/spaceone/cost_analysis/connector/currency_connector.py index 44dc92c0..b3456481 100644 --- a/src/spaceone/cost_analysis/connector/currency_connector.py +++ b/src/spaceone/cost_analysis/connector/currency_connector.py @@ -1,9 +1,10 @@ import logging import FinanceDataReader as fdr -from datetime import datetime, timedelta +from datetime import datetime from typing import Tuple, Union +import requests from dateutil.relativedelta import relativedelta from spaceone.core.connector import BaseConnector @@ -51,20 +52,59 @@ def _initialize_currency_map(): return currency_map @staticmethod + def http_datareader(pair, currency_end_date, currency_start_date) -> dict: + pair = f"{pair.replace('/','')}=X" + start_date_time_stamp = int(currency_start_date.timestamp()) + end_date_time_stamp = int(currency_end_date.timestamp()) + + url = f"https://query2.finance.yahoo.com/v8/finance/chart/{pair}?period1={start_date_time_stamp}&period2={end_date_time_stamp}&interval=1d&events=history&includeAdjustedClose=true" + headers = { + "Content-Type": "application/json", + "User-Agent": "Mozilla/5.0", + } + response = requests.request(method="GET", url=url, headers=headers) + return response.json() + def _get_exchange_rate_info( + self, pair: str, currency_end_date: datetime, currency_start_date: Union[datetime, None] = None, ): if not currency_start_date: - currency_start_date = currency_end_date - relativedelta(days=14) + currency_start_date = currency_end_date - relativedelta(days=15) + currency_end_date = currency_end_date.utcnow() + try: + return ( + fdr.DataReader( + pair, + start=currency_start_date, + end=currency_end_date, + ) + .dropna() + .reset_index()[["Date", "Close"]] + ) + except Exception as e: + import pandas as pd + + _LOGGER.error(f"[get_exchange_rate_info] Error {e}") + response_json = self.http_datareader( + pair, currency_end_date, currency_start_date + ) + + quotes = response_json["chart"]["result"][0]["indicators"]["quote"][0] + timestamps = response_json["chart"]["result"][0]["timestamp"] - return ( - fdr.DataReader( - symbol=pair, - start=currency_start_date, - end=currency_end_date, + # convert bst to utc + converted_datetime = [ + datetime.utcfromtimestamp(ts + 3600) for ts in timestamps + ] + + df = pd.DataFrame( + { + "Date": converted_datetime, + "Close": quotes["close"], + } ) - .dropna() - .reset_index()[["Date", "Close"]] - ) + + return df.dropna().reset_index()[["Date", "Close"]] diff --git a/src/spaceone/cost_analysis/service/data_source_service.py b/src/spaceone/cost_analysis/service/data_source_service.py index 4e2b8230..a400d169 100644 --- a/src/spaceone/cost_analysis/service/data_source_service.py +++ b/src/spaceone/cost_analysis/service/data_source_service.py @@ -832,10 +832,10 @@ def create_data_source_account_with_data_source_vo( ) # Delete old data source accounts - for data_source_account_vo in exist_data_source_account_vo_map.values(): - self.data_source_account_mgr.delete_source_account_by_vo( - data_source_account_vo - ) + # for data_source_account_vo in exist_data_source_account_vo_map.values(): + # self.data_source_account_mgr.delete_source_account_by_vo( + # data_source_account_vo + # ) def _get_data_source_account_vo_map( self, data_source_id: str, domain_id: str