fixing slowness when screen_pub queries the db #4

Merged
ben merged 7 commits from fix-db-read into main 2022-09-18 14:17:00 +00:00
6 changed files with 57 additions and 46 deletions

View File

@ -19,12 +19,13 @@ RUN apt-get update && \
chown -R -v ${USER_NAME}. /opt/${REPO_NAME}
USER ${USER_NAME}
WORKDIR /opt/${REPO_NAME}
WORKDIR /home/${USER_NAME}
ENV PATH="/home/${USER_NAME}/.local/bin:${PATH}"
FROM base as builder
ARG PIP_REPO_URL="https://git.sudo.is/api/packages/ben/pypi"
ARG PIP_REPO_NAME="gitea"
WORKDIR /opt/${REPO_NAME}
# --pre: enable installing pre-releases and dev-releases
RUN python3 -m pip install poetry --pre && \
@ -74,4 +75,5 @@ RUN ls -1 /opt/${REPO_NAME}/dist && \
HEALTHCHECK --start-period=5s --interval=15s --timeout=1s \
CMD ruok_${REPO_NAME}
ENTRYPOINT ['sudoisbot']
ENTRYPOINT ["sudoisbot"]
CMD []

24
poetry.lock generated
View File

@ -325,7 +325,7 @@ format-nongpl = ["fqdn", "idna", "isoduration", "jsonpointer (>1.13)", "rfc3339-
[[package]]
name = "keyring"
version = "23.9.1"
version = "23.9.3"
description = "Store and access your passwords safely."
category = "dev"
optional = false
@ -580,6 +580,18 @@ category = "dev"
optional = false
python-versions = "*"
[[package]]
name = "pymysql"
version = "1.0.2"
description = "Pure Python MySQL Driver"
category = "main"
optional = false
python-versions = ">=3.6"
[package.extras]
rsa = ["cryptography"]
ed25519 = ["PyNaCl (>=1.4.0)"]
[[package]]
name = "pyparsing"
version = "3.0.9"
@ -923,7 +935,7 @@ telegram = ["python-telegram-bot"]
[metadata]
lock-version = "1.1"
python-versions = "^3.9"
content-hash = "fc8b29b88ed8498ad730df20f70aaee6f5dd6835930a434b48f90b23a11b7c3b"
content-hash = "91854d75e5de47e9f5218c09b2037aef57cef4550c1dae087f3e77b0e3b6ae74"
[metadata.files]
apscheduler = [
@ -1142,8 +1154,8 @@ jsonschema = [
{file = "jsonschema-4.16.0.tar.gz", hash = "sha256:165059f076eff6971bae5b742fc029a7b4ef3f9bcf04c14e4776a7605de14b23"},
]
keyring = [
{file = "keyring-23.9.1-py3-none-any.whl", hash = "sha256:3565b9e4ea004c96e158d2d332a49f466733d565bb24157a60fd2e49f41a0fd1"},
{file = "keyring-23.9.1.tar.gz", hash = "sha256:39e4f6572238d2615a82fcaa485e608b84b503cf080dc924c43bbbacb11c1c18"},
{file = "keyring-23.9.3-py3-none-any.whl", hash = "sha256:69732a15cb1433bdfbc3b980a8a36a04878a6cfd7cb99f497b573f31618001c0"},
{file = "keyring-23.9.3.tar.gz", hash = "sha256:69b01dd83c42f590250fe7a1f503fc229b14de83857314b1933a3ddbf595c4a5"},
]
loguru = [
{file = "loguru-0.6.0-py3-none-any.whl", hash = "sha256:4e2414d534a2ab57573365b3e6d0234dfb1d84b68b7f3b948e6fb743860a77c3"},
@ -1282,6 +1294,10 @@ pylev = [
{file = "pylev-1.4.0-py2.py3-none-any.whl", hash = "sha256:7b2e2aa7b00e05bb3f7650eb506fc89f474f70493271a35c242d9a92188ad3dd"},
{file = "pylev-1.4.0.tar.gz", hash = "sha256:9e77e941042ad3a4cc305dcdf2b2dec1aec2fbe3dd9015d2698ad02b173006d1"},
]
pymysql = [
{file = "PyMySQL-1.0.2-py3-none-any.whl", hash = "sha256:41fc3a0c5013d5f039639442321185532e3e2c8924687abe6537de157d403641"},
{file = "PyMySQL-1.0.2.tar.gz", hash = "sha256:816927a350f38d56072aeca5dfb10221fe1dc653745853d30a216637f5d7ad36"},
]
pyparsing = [
{file = "pyparsing-3.0.9-py3-none-any.whl", hash = "sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc"},
{file = "pyparsing-3.0.9.tar.gz", hash = "sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb"},

View File

@ -14,10 +14,11 @@ loguru = "^0.6.0"
PyYAML = "^6.0"
pyzmq = "^23.2.1"
sudoistemper = "^0.1.0"
peewee = "^3.15.1"
peewee = "^3.15.2"
requests = "^2.28.1"
python-dateutil = "^2.8.2"
python-telegram-bot = { version = "^13.1", optional = true }
PyMySQL = "^1.0.2"
[tool.poetry.group.dev.dependencies]
autopep8 = "^1.7.0"

View File

@ -2,6 +2,6 @@
set -e
source ./docker/docker.env
source ./scripts/docker.env
docker run --rm -it ${repo_name}:${docker_tag} $*

View File

@ -2,13 +2,10 @@
# ansible for now
import argparse
from datetime import datetime, timezone, timedelta
from os import path
import sys
from datetime import datetime, timezone
import random
import time
from dataclasses import dataclass, field, asdict
from dataclasses import dataclass, field
from itertools import islice
from loguru import logger
@ -16,15 +13,18 @@ from loguru import logger
from sudoisbot.network.pub import Publisher
from sudoisbot.sink.models import Temperatures, People, Weather, dbconnect
def chunk(it, size=10):
it = iter(it)
return list(iter(lambda: list(islice(it, size)), []))
def bark():
numberofwoofs = random.randint(1,3)
numberofwoofs = random.randint(1, 3)
woofs = " " + ", ".join(["woof"] * numberofwoofs)
return woofs
@dataclass
class ScreenPublisher(Publisher):
addr: str
@ -37,7 +37,6 @@ class ScreenPublisher(Publisher):
statedir: str = "/dev/shm"
msgs: list = field(default_factory=list)
no_loop: bool = False
dry_run: bool = False
@ -83,35 +82,31 @@ class ScreenPublisher(Publisher):
return " ".join(homebodies)
except ValueError as e:
logger.error(e)
return "- - -"
def make_text(self):
return random.choice(self.msgs + [self.align_center(bark())])
def make_temps(self):
l = list()
temps = list()
for a in ['bedroom', 'study', 'livingroom', 'ls54', 'outdoor']:
# .replace does not mutate original string
shortname = a.replace('room', 'r')
try:
t0 = time.time()
result = Temperatures.get_last(a)
t1 = time.time()
print(f"query for: {t1-t0}s, name='{a}'")
tempstr = f"{result.temp:.1f}"
if result.temp < 10.0:
tempstr = " " + tempstr
l.append(f"{shortname}: {tempstr} C")
temps.append(f"{shortname}: {tempstr} C")
except KeyError:
logger.trace(f"no recent temp for '{a}'")
l.append(f"{shortname}: -- C")
temps.append(f"{shortname}: -- C")
fill = max([len(a) for a in l])
chunks = chunk([a.rjust(fill) for a in l], 2)
fill = max([len(a) for a in temps])
chunks = chunk([a.rjust(fill) for a in temps], 2)
temp_rows = list()
for row in chunks:
@ -122,17 +117,16 @@ class ScreenPublisher(Publisher):
return "\n".join(temp_rows)
def publish(self):
woof = " " + bark()
woof = " " + bark() # noqa
weth = self.make_weather()
temps = self.make_temps()
#folk = self.make_people()
# folk = self.make_people()
folk = " "
text = self.make_text()
rain = self.make_rain(weth)
text = f"{temps}\n{weth}\n{text}"
# add back logic to turn update intervals down pr stop when
# nodody is home
if len(folk) > 0:
@ -165,15 +159,14 @@ class ScreenPublisher(Publisher):
if self.no_loop:
raise StopIteration
def main(args, config):
db = dbconnect(**config['mysql'])
dbconnect(**config['mysql'])
addr = config['addr']
#people_home = config['people_home']
kwargs = {**config['screen'],
# people_home = config['people_home']
kwargs = {
**config['screen'],
**{
'rotation': args.rotation,
'people': config['people'],
@ -181,7 +174,6 @@ def main(args, config):
'dry_run': args.dry_run,
'no_loop': args.no_loop
}}
with ScreenPublisher(addr=addr, **kwargs) as p:
p.loop()

View File

@ -22,9 +22,9 @@ def dbconnect(**mysqlconf):
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.time.desc()).get()
cls.name == name).order_by(-cls.id).get()
@classmethod
def get_last_many(cls, names):