-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from yjinjo/master
Add exchange rate
- Loading branch information
Showing
5 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
spaceone-api | ||
pandas | ||
numpy | ||
jinja2 | ||
jinja2 | ||
finance-datareader | ||
plotly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import logging | ||
import FinanceDataReader as fdr | ||
|
||
from datetime import datetime, timedelta | ||
|
||
from spaceone.core.connector import BaseConnector | ||
|
||
__all__ = ["CurrencyConnector"] | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
EXCHANGE_CURRENCY_LIST = ["KRW", "USD", "JPY"] | ||
|
||
|
||
class CurrencyConnector(BaseConnector): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.today = datetime.utcnow() | ||
self.today_date = self.today.strftime("%Y-%m-%d") | ||
self.two_weeks_ago = (self.today - timedelta(days=14)).strftime("%Y-%m-%d") | ||
|
||
def add_exchange_rate(self, aggregated_cost_report: dict) -> dict: | ||
current_currency_dict = aggregated_cost_report.get("cost") | ||
cost = {} | ||
|
||
for current_currency, current_cost in current_currency_dict.items(): | ||
exchange_rate_cost = self._calculate_exchange_rate( | ||
current_currency, current_cost | ||
) | ||
cost.update({current_currency: exchange_rate_cost}) | ||
|
||
aggregated_cost_report.update({"cost": cost}) | ||
return aggregated_cost_report | ||
|
||
def _calculate_exchange_rate(self, from_currency: str, amount: float) -> float: | ||
exchange_rates = {} | ||
|
||
for to_currency in EXCHANGE_CURRENCY_LIST: | ||
if from_currency == to_currency: | ||
exchange_rate = 1.0 | ||
else: | ||
pair = f"{from_currency}/{to_currency}" | ||
exchange_rate_info = fdr.DataReader( | ||
pair, self.two_weeks_ago, self.today_date | ||
)["Close"].dropna() | ||
exchange_rate = exchange_rate_info.iloc[-1] | ||
|
||
exchange_rates[to_currency] = exchange_rate | ||
|
||
exchange_rate_cost = amount * exchange_rates[from_currency] | ||
return exchange_rate_cost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import logging | ||
|
||
from spaceone.core.manager import BaseManager | ||
from spaceone.cost_analysis.connector.currency_connector import CurrencyConnector | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class CurrencyManager(BaseManager): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.currency_connector: CurrencyConnector = CurrencyConnector() | ||
|
||
def convert_exchange_rate(self, aggregated_cost_report: dict) -> dict: | ||
return self.currency_connector.add_exchange_rate(aggregated_cost_report) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters