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 ceaf4fb
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 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 @@ -247,29 +248,31 @@ def expire_data(
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)
)
count["expired: links"] = dbsession.execute(stmt).rowcount
logger.info(
"Marked inactive links",
count=count["expired: links"],
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)
)
count["expired: nodes"] = dbsession.execute(stmt).rowcount
logger.info(
"Marked inactive nodes",
count=count["expired: nodes"],
Expand Down

0 comments on commit ceaf4fb

Please sign in to comment.