Don't make unnecessary calls to update ad equations

This commit is contained in:
Mark Veidemanis 2022-01-27 11:16:28 +00:00
parent 35cf84998e
commit f871e8b93a
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 21 additions and 4 deletions

View File

@ -355,10 +355,27 @@ class Agora(object):
if asset == "BTC": if asset == "BTC":
new_margin = self.autoprice(public_ads, currency) new_margin = self.autoprice(public_ads, currency)
new_formula = f"coingeckobtcusd*usd{currency.lower()}*{new_margin}" new_formula = f"coingeckobtcusd*usd{currency.lower()}*{new_margin}"
# Get all of our ads
our_ads_list = [ad for ad in public_ads if ad[1] == settings.Agora.Username]
if not len(our_ads_list) == 1:
self.log.error("More ads than expected: {ads}", ads=our_ads_list)
# Get one from the list, they're probably the same, if not we will deal with the other
# ones in a later pass
# Get one
our_ad = our_ads_list.pop()
# Take the 4th argument, the margin
our_margin = our_ad[3]
# Don't waste API rate limits on setting the same margin as before
if new_margin != our_margin:
rtrn = self.agora.ad_equation(ad_id, new_formula) rtrn = self.agora.ad_equation(ad_id, new_formula)
if not rtrn["success"]: if not rtrn["success"]:
self.log.error("Error updating ad {ad_id}: {response}", ad_id=ad_id, response=rtrn["response"]) self.log.error("Error updating ad {ad_id}: {response}", ad_id=ad_id, response=rtrn["response"])
self.log.info("Rate for {currency}: {margin}", currency=currency, margin=new_margin) self.log.info("Rate for {currency}: {margin}", currency=currency, margin=new_margin)
else:
self.log.info("Not changed rate for {currency}, keeping old margin of {margin}", currency=currency, margin=our_margin)
def autoprice(self, ads, currency): def autoprice(self, ads, currency):
""" """