From eb7f6360bbd39daeeb3ebb9e9a07361810e75a89 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:19:43 +0100 Subject: [PATCH 01/18] fix context.destroy in proxy, get recent more efficiently --- sudoisbot/network/proxy.py | 6 +++--- sudoisbot/sink/models.py | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/sudoisbot/network/proxy.py b/sudoisbot/network/proxy.py index f4e9897..fc1d04a 100644 --- a/sudoisbot/network/proxy.py +++ b/sudoisbot/network/proxy.py @@ -28,7 +28,7 @@ def dealer(dealer_addr, router_addr): dealer.close() router.close() - context.close() + context.destroy() def proxy_buffering(frontend_addr, backend_addr, capture_addr=None, @@ -200,7 +200,7 @@ def proxy_buffering(frontend_addr, backend_addr, capture_addr=None, # we never used to get here frontend.close() backend.close() - context.close() + context.destroy() def proxy_forwarder(frontend_addr, backend_addr, capture_addr): @@ -236,7 +236,7 @@ def proxy_forwarder(frontend_addr, backend_addr, capture_addr): backend.close() if capture: capture.close() - context.close() + context.destroy() def capture(capture_addr): diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index 0fc7642..3a827b1 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -41,8 +41,10 @@ class BaseModel(peewee.Model): @classmethod def get_last(cls, name): # http://docs.peewee-orm.com/en/latest/peewee/querying.html - return cls.select().where( - cls.name == name).order_by(-cls.id).get() + return cls.select() + .where(cls.name == name) + .order_by(-cls.id) + .get() @classmethod def get_last_many(cls, names): @@ -50,9 +52,10 @@ class BaseModel(peewee.Model): @classmethod def get_recent(cls, name, secs): - return cls.select().where( - cls.time > seconds(secs) and cls.name == name).order_by( - cls.time.desc()).get() + return cls.select().where(cls.name == name) + .where(cls.time > seconds(secs)) + .order_by(-cls.id) + .get() @classmethod def retry_create(cls, *args, **kwargs): -- 2.40.1 From dddaf18ccd7a5061f12f1b938c297126b8e128ea Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:25:31 +0100 Subject: [PATCH 02/18] syntax --- sudoisbot/sink/models.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index 3a827b1..ff114d6 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -41,10 +41,7 @@ class BaseModel(peewee.Model): @classmethod def get_last(cls, name): # http://docs.peewee-orm.com/en/latest/peewee/querying.html - return cls.select() - .where(cls.name == name) - .order_by(-cls.id) - .get() + return cls.select().where(cls.name == name).order_by(-cls.id).get() @classmethod def get_last_many(cls, names): @@ -52,10 +49,7 @@ class BaseModel(peewee.Model): @classmethod def get_recent(cls, name, secs): - return cls.select().where(cls.name == name) - .where(cls.time > seconds(secs)) - .order_by(-cls.id) - .get() + return cls.select().where(cls.name == name).where(cls.time > seconds(secs)).order_by(-cls.id).get() @classmethod def retry_create(cls, *args, **kwargs): -- 2.40.1 From 53e21b044f7302fa163bdfc27af3c3bd8ca1646f Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:26:06 +0100 Subject: [PATCH 03/18] remove double where --- sudoisbot/sink/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index ff114d6..b871d67 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -49,7 +49,7 @@ class BaseModel(peewee.Model): @classmethod def get_recent(cls, name, secs): - return cls.select().where(cls.name == name).where(cls.time > seconds(secs)).order_by(-cls.id).get() + return cls.select().where(cls.name == name and cls.time > seconds(secs)).order_by(-cls.id).get() @classmethod def retry_create(cls, *args, **kwargs): -- 2.40.1 From a46fc3b9618846d77811fb93e9c57d2c22137695 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:37:24 +0100 Subject: [PATCH 04/18] handle no recent result --- sudoisbot/screen/screen_pub.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index 8fe49e1..a136c28 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -70,8 +70,11 @@ class ScreenPublisher(Publisher): return padding + msg def make_weather(self): - current = Weather.get_recent(self.weather, 30*60) - return self.align_center(current.desc) + try: + current = Weather.get_recent(self.weather, 30*60) + return self.align_center(current.desc) + except Weather.DoesNotExist: + return "no weather" def make_rain(self, weather): return "~?~" @@ -97,13 +100,18 @@ class ScreenPublisher(Publisher): try: t0 = time.time() - result = Temperatures.get_recent(a, secs=30*60) + try: + result = Temperatures.get_recent(a, 3*60) + result_str = result.temp.round(1).rjust(4) + except Temperatures.DoesNotExist: + logger.warning("No recent temp found for '{a}'") + result_str = "NONE" + t1 = time.time() - t0 - logger.debug(f"query for: {t1:.3f}s, name='{a}'") - tempstr = f"{result.temp:.1f}" - if result.temp < 10.0: - tempstr = " " + tempstr - temps.append(f"{shortname}: {tempstr} C") + t_total = round(t1, 3) + logger.debug(f"query for: {t_total}s, name='{a}'") + + temps.append(f"{shortname}: {result_str} C") except KeyError: logger.trace(f"no recent temp for '{a}'") temps.append(f"{shortname}: -- C") -- 2.40.1 From 0f558348f04bb6b052a89c6485b7a81f4706675d Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:38:46 +0100 Subject: [PATCH 05/18] round a decimal --- sudoisbot/screen/screen_pub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index a136c28..5198e50 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -102,7 +102,7 @@ class ScreenPublisher(Publisher): t0 = time.time() try: result = Temperatures.get_recent(a, 3*60) - result_str = result.temp.round(1).rjust(4) + result_str = round(result.temp, 1).rjust(4) except Temperatures.DoesNotExist: logger.warning("No recent temp found for '{a}'") result_str = "NONE" -- 2.40.1 From 58dab39f8e54bb947ec980402c5740f6015a0d78 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:40:14 +0100 Subject: [PATCH 06/18] rjust a str --- sudoisbot/screen/screen_pub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index 5198e50..38eed2f 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -102,7 +102,7 @@ class ScreenPublisher(Publisher): t0 = time.time() try: result = Temperatures.get_recent(a, 3*60) - result_str = round(result.temp, 1).rjust(4) + result_str = str(round(result.temp, 1)).rjust(4) except Temperatures.DoesNotExist: logger.warning("No recent temp found for '{a}'") result_str = "NONE" -- 2.40.1 From 2804a20d26cae99f470be620fc37e1af070d7883 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:41:09 +0100 Subject: [PATCH 07/18] debug message --- sudoisbot/screen/screen_pub.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index 38eed2f..3466725 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -74,6 +74,7 @@ class ScreenPublisher(Publisher): current = Weather.get_recent(self.weather, 30*60) return self.align_center(current.desc) except Weather.DoesNotExist: + logger.debug("no recent weather found") return "no weather" def make_rain(self, weather): -- 2.40.1 From 37bfb884528c0d97bffefa82dbba9c286145c82b Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:47:26 +0100 Subject: [PATCH 08/18] use get_last --- sudoisbot/screen/screen_pub.py | 2 +- sudoisbot/sink/models.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index 3466725..76fa916 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -102,7 +102,7 @@ class ScreenPublisher(Publisher): try: t0 = time.time() try: - result = Temperatures.get_recent(a, 3*60) + result = Temperatures.get_recent(a, 1) result_str = str(round(result.temp, 1)).rjust(4) except Temperatures.DoesNotExist: logger.warning("No recent temp found for '{a}'") diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index b871d67..832befc 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -49,7 +49,11 @@ class BaseModel(peewee.Model): @classmethod def get_recent(cls, name, secs): - return cls.select().where(cls.name == name and cls.time > seconds(secs)).order_by(-cls.id).get() + last = cls.get_last(name) + if last.time > seconds(secs): + return last + else: + raise cls.DoesNotExist @classmethod def retry_create(cls, *args, **kwargs): -- 2.40.1 From f521724477311e4aada3094eac827e2454c35fc9 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:49:14 +0100 Subject: [PATCH 09/18] not timezone-aware --- sudoisbot/screen/screen_pub.py | 2 +- sudoisbot/sink/models.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index 76fa916..6346ebf 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -105,7 +105,7 @@ class ScreenPublisher(Publisher): result = Temperatures.get_recent(a, 1) result_str = str(round(result.temp, 1)).rjust(4) except Temperatures.DoesNotExist: - logger.warning("No recent temp found for '{a}'") + logger.warning(f"No recent temp found for '{a}'") result_str = "NONE" t1 = time.time() - t0 diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index 832befc..433bb13 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 import json -from datetime import datetime, timezone, timedelta +from datetime import datetime, timedelta import peewee from peewee import DateTimeField, TextField, DecimalField, CharField, BooleanField @@ -34,7 +34,7 @@ def dbconnect(**mysqlconf): def seconds(secs): - return datetime.now(timezone.utc)-timedelta(seconds=secs) + return datetime.now()-timedelta(seconds=secs) class BaseModel(peewee.Model): -- 2.40.1 From 508a7a1f9dcf2183a7e6d515bfa6b4e2b0ca3c40 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:49:57 +0100 Subject: [PATCH 10/18] 15 mins --- sudoisbot/screen/screen_pub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index 6346ebf..d7c90d7 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -102,7 +102,7 @@ class ScreenPublisher(Publisher): try: t0 = time.time() try: - result = Temperatures.get_recent(a, 1) + result = Temperatures.get_recent(a, 15*60) result_str = str(round(result.temp, 1)).rjust(4) except Temperatures.DoesNotExist: logger.warning(f"No recent temp found for '{a}'") -- 2.40.1 From 4a8fda76fe13b900367a5a64b6995cdb64f42c47 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:52:35 +0100 Subject: [PATCH 11/18] log age --- sudoisbot/sink/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index 433bb13..ff00ffd 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -50,6 +50,8 @@ class BaseModel(peewee.Model): @classmethod def get_recent(cls, name, secs): last = cls.get_last(name) + last_age = datetime.now() - last.time + logger.info(f"last value age: {age}") if last.time > seconds(secs): return last else: -- 2.40.1 From 4ffd874113265e5dd85b95030ff705f923ff42b2 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:53:02 +0100 Subject: [PATCH 12/18] foo --- sudoisbot/sink/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index ff00ffd..106441a 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -51,7 +51,7 @@ class BaseModel(peewee.Model): def get_recent(cls, name, secs): last = cls.get_last(name) last_age = datetime.now() - last.time - logger.info(f"last value age: {age}") + logger.info(f"last value age: {last_age}") if last.time > seconds(secs): return last else: -- 2.40.1 From e5c220dd5c9864746e1ed5865928156aa07505b1 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 01:55:14 +0100 Subject: [PATCH 13/18] log id --- sudoisbot/sink/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index 106441a..0bc764d 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -51,7 +51,7 @@ class BaseModel(peewee.Model): def get_recent(cls, name, secs): last = cls.get_last(name) last_age = datetime.now() - last.time - logger.info(f"last value age: {last_age}") + logger.info(f"last value age: {last_age}, id={last.id}") if last.time > seconds(secs): return last else: -- 2.40.1 From d81813d9d52ff6134a6ed4f11881d24a5d2b0d8f Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 02:00:40 +0100 Subject: [PATCH 14/18] utcnow --- sudoisbot/sink/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index 0bc764d..cd569c6 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -50,11 +50,12 @@ class BaseModel(peewee.Model): @classmethod def get_recent(cls, name, secs): last = cls.get_last(name) - last_age = datetime.now() - last.time - logger.info(f"last value age: {last_age}, id={last.id}") + last_age = datetime.utcnow() - last.time + logger.info(f"Last value in {cls.__name__} is too old, age: {last_age}") if last.time > seconds(secs): return last else: + logger.info(f"Last value in {cls.__name__} is too old, age: {last_age}") raise cls.DoesNotExist @classmethod -- 2.40.1 From de5e3cd8a821d7f5ede7547ec53fc8b9bdcf132a Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 02:02:03 +0100 Subject: [PATCH 15/18] seconds --- sudoisbot/sink/models.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index cd569c6..4fc4066 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -51,11 +51,10 @@ class BaseModel(peewee.Model): def get_recent(cls, name, secs): last = cls.get_last(name) last_age = datetime.utcnow() - last.time - logger.info(f"Last value in {cls.__name__} is too old, age: {last_age}") - if last.time > seconds(secs): + if last_age.total_seconds() > float(secs): return last else: - logger.info(f"Last value in {cls.__name__} is too old, age: {last_age}") + logger.info(f"Last value in {cls.__name__} is older than {secs}s, age: {last_age}") raise cls.DoesNotExist @classmethod -- 2.40.1 From 60c945c071cf076b57d8b05046754fefca33cb01 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 02:02:40 +0100 Subject: [PATCH 16/18] fucking logic --- sudoisbot/sink/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoisbot/sink/models.py b/sudoisbot/sink/models.py index 4fc4066..25420fc 100644 --- a/sudoisbot/sink/models.py +++ b/sudoisbot/sink/models.py @@ -51,7 +51,7 @@ class BaseModel(peewee.Model): def get_recent(cls, name, secs): last = cls.get_last(name) last_age = datetime.utcnow() - last.time - if last_age.total_seconds() > float(secs): + if last_age.total_seconds() <= float(secs): return last else: logger.info(f"Last value in {cls.__name__} is older than {secs}s, age: {last_age}") -- 2.40.1 From 01524b298007e615ec71c38e2eed153cb3a9fb1c Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 02:04:11 +0100 Subject: [PATCH 17/18] remove keyerror --- sudoisbot/screen/screen_pub.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index d7c90d7..55932b2 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -99,23 +99,18 @@ class ScreenPublisher(Publisher): # .replace does not mutate original string shortname = a.replace('room', 'r') + t0 = time.time() try: - t0 = time.time() - try: - result = Temperatures.get_recent(a, 15*60) - result_str = str(round(result.temp, 1)).rjust(4) - except Temperatures.DoesNotExist: - logger.warning(f"No recent temp found for '{a}'") - result_str = "NONE" + result = Temperatures.get_recent(a, 15*60) + result_str = str(round(result.temp, 1)).rjust(4) + except Temperatures.DoesNotExist: + logger.warning(f"No recent temp found for '{a}'") + result_str = "NONE" - t1 = time.time() - t0 - t_total = round(t1, 3) - logger.debug(f"query for: {t_total}s, name='{a}'") - - temps.append(f"{shortname}: {result_str} C") - except KeyError: - logger.trace(f"no recent temp for '{a}'") - temps.append(f"{shortname}: -- C") + t1 = time.time() - t0 + t_total = round(t1, 3) + logger.debug(f"query for: {t_total}s, name='{a}'") + temps.append(f"{shortname}: {result_str} C") fill = max([len(a) for a in temps]) chunks = chunk([a.rjust(fill) for a in temps], 2) -- 2.40.1 From 6d472d0997d724b6014a551b13ee0fedd30b3a25 Mon Sep 17 00:00:00 2001 From: Ben Kristinsson Date: Sat, 30 Dec 2023 02:04:37 +0100 Subject: [PATCH 18/18] remove debug log --- sudoisbot/screen/screen_pub.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sudoisbot/screen/screen_pub.py b/sudoisbot/screen/screen_pub.py index 55932b2..2780cb9 100644 --- a/sudoisbot/screen/screen_pub.py +++ b/sudoisbot/screen/screen_pub.py @@ -74,7 +74,6 @@ class ScreenPublisher(Publisher): current = Weather.get_recent(self.weather, 30*60) return self.align_center(current.desc) except Weather.DoesNotExist: - logger.debug("no recent weather found") return "no weather" def make_rain(self, weather): -- 2.40.1