From 803d427e8b9150f2bb6633e74d7ded87a785d38d Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 14 Jan 2021 00:05:47 -0800 Subject: [PATCH] setup some info logging --- app/helpers.py | 5 +++-- app/library/cache.py | 13 ++++++++++++- app/library/coingecko.py | 2 ++ app/library/digitalocean.py | 2 ++ app/library/monero.py | 2 ++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/helpers.py b/app/helpers.py index 2e52a10..3d7ca50 100644 --- a/app/helpers.py +++ b/app/helpers.py @@ -1,6 +1,7 @@ from app.models import Operation from app.library.digitalocean import do from app.factory import db +from flask import current_app def to_ausd(amount): @@ -21,7 +22,7 @@ def cancel_operation(codename): op.droplet_id = None op.volume_id = None db.session.commit() - print(f'Deleted droplet and associated volume for {codename}') + current_app.logger.info(f'Deleted droplet and associated volume for {codename}') if op.record_v4_id: r = do.delete_record(op.record_v4_id) if r: @@ -33,6 +34,6 @@ def cancel_operation(codename): if r: op.record_v6_id = None db.session.commit() - print(f'Deleted AAAA records for {codename}') + current_app.logger.info(f'Deleted AAAA records for {codename}') else: print('Not an op') diff --git a/app/library/cache.py b/app/library/cache.py index 5e5978b..e417649 100644 --- a/app/library/cache.py +++ b/app/library/cache.py @@ -3,6 +3,7 @@ from json import dumps as json_dumps from datetime import timedelta from redis import Redis from requests import get as r_get +from flask import current_app from app.library.coingecko import get_market_data from app.library.monero import wallet from app.library.digitalocean import do @@ -14,6 +15,7 @@ class Cache(object): self.redis = Redis(host=config.CACHE_HOST, port=config.CACHE_PORT) def store_data(self, item_name, expiration_minutes, data): + current_app.logger.info(f'MISS - {item_name} - {expiration_minutes} minutes') self.redis.setex( item_name, timedelta(minutes=expiration_minutes), @@ -24,6 +26,7 @@ class Cache(object): key_name = f'node_{codename}_info' data = self.redis.get(key_name) if data: + current_app.logger.info(f'HIT - {key_name}') return json_loads(data) else: try: @@ -40,6 +43,7 @@ class Cache(object): key_name = f'wallet_txes_{account_idx}' data = self.redis.get(key_name) if data: + current_app.logger.info(f'HIT - {key_name}') return json_loads(data) else: txes = wallet.get_transfers(account_idx) @@ -54,17 +58,21 @@ class Cache(object): key_name = f'wallet_balances_{account_idx}{extra}' data = self.redis.get(key_name) if data: + current_app.logger.info(f'HIT - {key_name}') return json_loads(data) else: balances = wallet.balances(account_idx, atomic) data = {'balance': balances[0], 'unlocked': balances[1]} - self.store_data(key_name, 1, json_dumps(data)) + self.store_data(key_name, 2, json_dumps(data)) return data def show_droplet(self, droplet_id): + if droplet_id is None: + return None key_name = f'droplet_{droplet_id}' data = self.redis.get(key_name) if data: + current_app.logger.info(f'HIT - {key_name}') return json_loads(data) else: droplet = do.show_droplet(droplet_id) @@ -75,6 +83,7 @@ class Cache(object): key_name = f'volume_{volume_id}' data = self.redis.get(key_name) if data: + current_app.logger.info(f'HIT - {key_name}') return json_loads(data) else: volume = do.show_volume(volume_id) @@ -85,6 +94,7 @@ class Cache(object): key_name = f'xmr_price_{cur}' data = self.redis.get(key_name) if data: + current_app.logger.info(f'HIT - {key_name}') return float(data.decode()) else: d = get_market_data() @@ -96,6 +106,7 @@ class Cache(object): key_name = f'xmr_wallet_{subaddress_index}_txes' data = self.redis.get(key_name) if data: + current_app.logger.info(f'HIT - {key_name}') return json_loads(data) else: wallet.get_transfers(subaddress_index) diff --git a/app/library/coingecko.py b/app/library/coingecko.py index ee928c5..f3bd15e 100644 --- a/app/library/coingecko.py +++ b/app/library/coingecko.py @@ -1,4 +1,5 @@ from requests import get as r_get +from flask import current_app def get_market_data(coin_name='monero'): @@ -12,5 +13,6 @@ def get_market_data(coin_name='monero'): } headers = {'accept': 'application/json'} url = f'https://api.coingecko.com/api/v3/coins/{coin_name}' + current_app.logger.info(f'Fetching {coin_name} price metrics from CoinGecko') r = r_get(url, headers=headers, data=data) return r.json() diff --git a/app/library/digitalocean.py b/app/library/digitalocean.py index a637c9c..e11a37d 100644 --- a/app/library/digitalocean.py +++ b/app/library/digitalocean.py @@ -1,4 +1,5 @@ import requests +from flask import current_app from app import config @@ -11,6 +12,7 @@ class DigitalOcean(object): def make_req(self, method, endpoint, data=None): url = self.base + endpoint + current_app.logger.info(f'{method.upper()} - {url}') if method == 'post': r = requests.post(url, headers=self.headers, json=data) elif method == 'get': diff --git a/app/library/monero.py b/app/library/monero.py index 4c70a05..945651b 100644 --- a/app/library/monero.py +++ b/app/library/monero.py @@ -2,6 +2,7 @@ import requests import six import json from decimal import Decimal +from flask import current_app from app import config @@ -18,6 +19,7 @@ class WalletRPC(object): data=json.dumps({'method': method, 'params': params}), auth=self.auth ) + current_app.logger.info(f'GET - {self.endpoint} - {method}') if 'error' in r.json(): return r.json()['error'] else: