Skip to content

Commit

Permalink
WIP: Update collector for SQLAlchemy 2.0
Browse files Browse the repository at this point in the history
TODO:
* Use bulk insert/update methods (optimize lookup query?)
* TEST!
  • Loading branch information
smsearcy committed Jul 15, 2024
1 parent e9c01ee commit b176587
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions meshinfo/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pendulum
import structlog
from sqlalchemy import sql
from sqlalchemy.orm import Session
from structlog.contextvars import bound_contextvars

Expand Down Expand Up @@ -230,7 +231,7 @@ def expire_data(
*,
nodes_expire: int,
links_expire: int,
count: defaultdict[str, int] | None = None,
count: defaultdict[str, int],
):
"""Update the status of nodes/links that have not been seen recently.
Expand All @@ -244,37 +245,40 @@ def expire_data(

timestamp = pendulum.now()

if count is None:
count = defaultdict(int)
inactive_cutoff = timestamp.subtract(days=links_expire)
count["expired: links"] = (
dbsession.query(Link)
.filter(
stmt = (
sql.update(Link)
.where(
Link.status == LinkStatus.RECENT,
Link.last_seen < inactive_cutoff,
)
.update({Link.status: LinkStatus.INACTIVE})
.values(status=LinkStatus.INACTIVE)
)
link_count = dbsession.execute(stmt).rowcount
logger.info(
"Marked inactive links",
count=count["expired: links"],
count=link_count,
cutoff=inactive_cutoff,
)

inactive_cutoff = timestamp.subtract(days=nodes_expire)
count["expired: nodes"] = (
dbsession.query(Node)
.filter(
stmt = (
sql.update(Node)
.where(
Node.status == NodeStatus.ACTIVE,
Node.last_seen < inactive_cutoff,
)
.update({Node.status: NodeStatus.INACTIVE})
.values(status=NodeStatus.INACTIVE)
)
node_count = dbsession.execute(stmt).rowcount
logger.info(
"Marked inactive nodes",
count=count["expired: nodes"],
count=node_count,
cutoff=inactive_cutoff,
)

count["expired: links"] = link_count
count["expired: nodes"] = node_count
return


Expand Down

0 comments on commit b176587

Please sign in to comment.