From 965bba8d15b42c7bd2b166f968ec705ca273e88c Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 27 Jan 2022 11:16:28 +0000 Subject: [PATCH] Don't make unnecessary calls to update ad equations --- handler/agora.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 1fc2bd7..5d1f67a 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -355,10 +355,27 @@ class Agora(object): if asset == "BTC": new_margin = self.autoprice(public_ads, currency) new_formula = f"coingeckobtcusd*usd{currency.lower()}*{new_margin}" - rtrn = self.agora.ad_equation(ad_id, new_formula) - if not rtrn["success"]: - 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) + # 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) + if not rtrn["success"]: + 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) + else: + self.log.info("Not changed rate for {currency}, keeping old margin of {margin}", currency=currency, margin=our_margin) def autoprice(self, ads, currency): """