"""Pass through models.""" import json import time from flask import g from ows_accounting.connectors import redis from ows_accounting.connectors import snowflake from ows_accounting.models.sql import passthrough from ows_accounting.utils import mysql def _get_key(cache_prefix, account_id, period_id): """Get generated key for cache. Args: cache_prefix (str): Cache prefix account_id (str): Account Id period_id (str): Accounting period id Returns: string: Key for cache record identifier """ return '{cache_prefix}:{account_id}:{period_id}'.format( cache_prefix=cache_prefix, account_id=account_id, period_id=period_id) def _get_created_time(): """Get cache record creation time in specified format. Returns: string: Formatted current time """ return time.strftime("%Y%m%d%H%M%S", time.localtime()) def get_from_cache(cache_prefix, account_id, period_id): """Get revenue information. Args: cache_prefix (str): Cache prefix account_id (str): Account Id period_id (str): Accounting period id Returns: dict: Account revenue data or error response """ result = redis.client.get(_get_key(cache_prefix, account_id, period_id)) if result: return json.loads(result.decode('utf8')) return {} def save_to_cache(cache_prefix, account_id, period_id, data): """Save account EQ data to cache. Args: cache_prefix (str): Cache prefix account_id (str): Account Id period_id (str): Accounting period id """ redis_response = redis.client.set( _get_key(cache_prefix, account_id, period_id), json.dumps(data)) if not redis_response: # log error g.log.error('Redis cache error. models.pasthrough') def get_currency_symbol_and_exchange_rate(account_id, period_id): """Query snowflake to get exchange rate and currency html symbol. Args: account_id (str): account id. period_id (str): accounting period id. Returns: dict: currency html symbol """ # cache_prefix = 'eq_exchange' # response = get_from_cache(cache_prefix, account_id, period_id) # if response: # return response query_params = { 'vendor_id': account_id, 'period_id': period_id} currency_html_symbol = None with snowflake.db_session() as session: result = session.execute( passthrough.SQL_GET_CURRENCY_ID_AND_EXCHANGE_RATE, query_params).fetchone() if not result or not result[0]: return {} currency_id = result[0] exchange_rate = float(result[1]) currency_result = session.execute( passthrough.SQL_GET_CURRENCY_SYMBOL, {'currency_id': currency_id}).fetchone() if currency_result and currency_result[0]: currency_html_symbol = currency_result[0] data = { 'currency_html_symbol': currency_html_symbol, 'exchange_rate': exchange_rate } # save_to_cache(cache_prefix, account_id, period_id, data) return data def get_account_eq_bonus(account_id, period_id): """Get EQ transactions information for an account. Args: account_id (str): account id. period_id (str): accounting period id. Returns: dict: Aggregated EQ transactions information. """ # cache_prefix = 'eq_bonus' # response = get_from_cache(cache_prefix, account_id, period_id) # if response: # return response query_params = { 'vendor_id': account_id, 'period_id': period_id} eq_bonus = None with snowflake.db_session() as session: result = session.execute( passthrough.SQL_GET_EQ_AGGREGATED, query_params).fetchone() if result and result[0]: eq_bonus = round(float(result[0]), 2) usd_eq_bonus = round(float(result[1]), 2) if eq_bonus: currency_result = get_currency_symbol_and_exchange_rate( account_id, period_id) data = { 'eq_bonus': eq_bonus, 'usd_eq_bonus': usd_eq_bonus, 'currency_html_symbol': currency_result.get( 'currency_html_symbol', None)} else: data = {} # save_to_cache(cache_prefix, account_id, period_id, data) return data def get_account_eq_payment_details(account_id, period_id): """Get EQ payment details for an account. Args: account_id (str): account id. period_id (str): accounting period id. Returns: Dict: EQ payment details. """ # cache_prefix = 'eq_payment' # response = get_from_cache(cache_prefix, account_id, period_id) # if response: # return response query_params = { 'vendor_id': account_id, 'period_id': period_id} with mysql.db_session() as session: result = session.execute( passthrough.SQL_GET_EQ_CHECKS_PAID, query_params) result = result.fetchall() if result: currency_data = get_currency_symbol_and_exchange_rate( account_id, period_id) data = [] for row in result: amount = float(row[0]) amount = amount * currency_data['exchange_rate'] check_no = row[1] cut_date = row[2] comments = row[3] item = { 'amount': round(amount, 2), 'currency_html_symbol': currency_data.get( 'currency_html_symbol'), 'check_no': check_no, 'date': cut_date.strftime('%m/%d/%Y'), 'comments': comments } data.append(item) else: data = [] # save_to_cache(cache_prefix, account_id, period_id, data) return data