Make ES optional and properly calculate open trade volume

This commit is contained in:
Mark Veidemanis 2022-02-22 19:21:29 +00:00
parent 05993ac119
commit 18011741c5
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 36 additions and 15 deletions

View File

@ -31,13 +31,14 @@ class Transactions(object):
Set the logger.
"""
self.log = Logger("transactions")
self.es = Elasticsearch(
f"https://{settings.ES.Host}:9200",
verify_certs=False,
basic_auth=(settings.ES.Username, settings.ES.Pass),
# ssl_assert_fingerprint=("6b264fd2fd107d45652d8add1750a8a78f424542e13b056d0548173006260710"),
ca_certs="certs/ca.crt",
)
if settings.ES.Enabled == "1":
self.es = Elasticsearch(
f"https://{settings.ES.Host}:9200",
verify_certs=False,
basic_auth=(settings.ES.Username, settings.ES.Pass),
# ssl_assert_fingerprint=("6b264fd2fd107d45652d8add1750a8a78f424542e13b056d0548173006260710"),
ca_certs="certs/ca.crt",
)
def run_checks_in_thread(self):
"""
@ -55,9 +56,10 @@ class Transactions(object):
"""
Set up the LoopingCalls to get the balance so we have data in ES.
"""
self.lc_es_checks = LoopingCall(self.run_checks_in_thread)
delay = int(settings.ES.RefreshSec)
self.lc_es_checks.start(delay)
if settings.ES.Enabled == "1":
self.lc_es_checks = LoopingCall(self.run_checks_in_thread)
delay = int(settings.ES.RefreshSec)
self.lc_es_checks.start(delay)
# TODO: write tests then refactor, this is terribly complicated!
def transaction(self, data):
@ -536,10 +538,11 @@ class Transactions(object):
return cast
def write_to_es(self, msgtype, cast):
cast["type"] = msgtype
cast["ts"] = str(datetime.now().isoformat())
cast["xtype"] = "tx"
self.es.index(index=settings.ES.Index, document=cast)
if settings.ES.Enabled == "1":
cast["type"] = msgtype
cast["ts"] = str(datetime.now().isoformat())
cast["xtype"] = "tx"
self.es.index(index=settings.ES.Index, document=cast)
def get_remaining(self):
"""
@ -599,7 +602,25 @@ class Transactions(object):
rates = self.agora.get_rates_all()
cumul_usd = 0
for contact_id, contact in dash.items():
amount = contact["data"]["amount"]
# We need created at in order to look up the historical prices
created_at = contact["data"]["created_at"]
# Reformat the date how CoinGecko likes
date_parsed = datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%S.%fZ")
date_formatted = date_parsed.strftime("%d-%m-%Y")
# Get the historical rates for the right asset, extract the price
asset = contact["data"]["advertisement"]["asset"]
if asset == "XMR":
amount_crypto = contact["data"]["amount_xmr"]
history = self.agora.cg.get_coin_history_by_id(id="monero", date=date_formatted)
crypto_usd = float(history["market_data"]["current_price"]["usd"])
elif asset == "BTC":
amount_crypto = contact["data"]["amount_btc"]
history = self.agora.cg.get_coin_history_by_id(id="bitcoin", date=date_formatted)
crypto_usd = float(history["market_data"]["current_price"]["usd"])
# Convert crypto to fiat
amount = float(amount_crypto) * crypto_usd
currency = contact["data"]["currency"]
if not contact["data"]["is_selling"]:
continue