🎨 — Improve code structure following review of MR !25

This commit is contained in:
Luc Didry 2024-03-18 09:01:39 +01:00
parent 8875c704ee
commit cf609eae6b
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C
4 changed files with 32 additions and 28 deletions

View file

@ -24,6 +24,8 @@ djlint: venv ## Format the templates
venv/bin/djlint --ignore=H030,H031,H006 --profile jinja --lint argos/server/templates/*html venv/bin/djlint --ignore=H030,H031,H006 --profile jinja --lint argos/server/templates/*html
pylint: venv ## Runs pylint on the code pylint: venv ## Runs pylint on the code
venv/bin/pylint argos venv/bin/pylint argos
pylint-alembic: venv ## Runs pylint on alembic migration files
venv/bin/pylint --disable invalid-name,no-member alembic/versions/*.py
lint: djlint pylint lint: djlint pylint
help: help:
@python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) @python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

View file

@ -7,8 +7,8 @@ Create Date: 2024-03-13 15:28:09.185377
""" """
from typing import Sequence, Union from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
@ -19,17 +19,13 @@ depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None: def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('config_cache', op.create_table('config_cache',
sa.Column('name', sa.String(), nullable=False), sa.Column('name', sa.String(), nullable=False),
sa.Column('val', sa.String(), nullable=False), sa.Column('val', sa.String(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False), sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('name') sa.PrimaryKeyConstraint('name')
) )
# ### end Alembic commands ###
def downgrade() -> None: def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('config_cache') op.drop_table('config_cache')
# ### end Alembic commands ###

View file

@ -117,7 +117,13 @@ class Result(Base):
class ConfigCache(Base): class ConfigCache(Base):
"""Contains some informations on the previous config state """Contains some informations on the previous config state
Used to quickly determine if we need to update the tasks Used to quickly determine if we need to update the tasks.
There is currently two cached settings:
- general_frequency: the content of general.frequency setting, in minutes
ex: 5
- websites_hash: the sha256sum of websites setting, to allow a quick
comparison without looping through all websites
ex: 8b886e7db7b553fe99f6d5437f31745987e243c77b2109b84cf9a7f8bf7d75b1
""" """
__tablename__ = "config_cache" __tablename__ = "config_cache"
name: Mapped[str] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column(primary_key=True)

View file

@ -62,7 +62,7 @@ async def count_results(db: Session):
return db.query(Result).count() return db.query(Result).count()
async def is_config_unchanged(db: Session, config: schemas.Config) -> bool: async def has_config_changed(db: Session, config: schemas.Config) -> bool:
"""Check if websites config has changed by using a hashsum and a config cache""" """Check if websites config has changed by using a hashsum and a config cache"""
websites_hash = sha256(str(config.websites).encode()).hexdigest() websites_hash = sha256(str(config.websites).encode()).hexdigest()
conf_caches = ( conf_caches = (
@ -72,24 +72,24 @@ async def is_config_unchanged(db: Session, config: schemas.Config) -> bool:
same_config = True same_config = True
if conf_caches: if conf_caches:
for conf in conf_caches: for conf in conf_caches:
if not same_config: match (conf.name):
break case 'websites_hash':
if conf.val != websites_hash:
same_config = False
conf.val = websites_hash
conf.updated_at = datetime.now()
case 'general_frequency':
if conf.val != str(config.general.frequency):
same_config = False
conf.val = config.general.frequency
conf.updated_at = datetime.now()
if conf.name == 'websites_hash': db.commit()
same_config = conf.val == websites_hash
elif conf.name == 'general_frequency':
same_config = conf.val == str(config.general.frequency)
if same_config: if same_config:
return True return False
for conf in conf_caches: else: # no config cache found
if conf.name == 'websites_hash':
conf.val = websites_hash
elif conf.name == 'general_frequency':
conf.val = config.general.frequency
conf.updated_at = datetime.now()
else:
web_hash = ConfigCache( web_hash = ConfigCache(
name='websites_hash', name='websites_hash',
val=websites_hash, val=websites_hash,
@ -104,13 +104,13 @@ async def is_config_unchanged(db: Session, config: schemas.Config) -> bool:
db.add(gen_freq) db.add(gen_freq)
db.commit() db.commit()
return False return True
async def update_from_config(db: Session, config: schemas.Config): async def update_from_config(db: Session, config: schemas.Config):
"""Update tasks from config file""" """Update tasks from config file"""
config_unchanged = await is_config_unchanged(db, config) config_changed = await has_config_changed(db, config)
if config_unchanged: if not config_changed:
return {'added': 0, 'vanished': 0} return {'added': 0, 'vanished': 0}
max_task_id = ( max_task_id = (