Skip to content

Commit

Permalink
Merge pull request #278 from ImMin5/master
Browse files Browse the repository at this point in the history
Fix get currency info
  • Loading branch information
ImMin5 authored Sep 9, 2024
2 parents 2bffff3 + a7c5cd8 commit 8113be1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
60 changes: 50 additions & 10 deletions src/spaceone/cost_analysis/connector/currency_connector.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"]]
8 changes: 4 additions & 4 deletions src/spaceone/cost_analysis/service/data_source_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8113be1

Please sign in to comment.