From 86dc30b52c5f51db7a8649fb0eefd07c449692ba Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Mon, 12 Aug 2024 13:29:24 +1000 Subject: [PATCH] sheetsync: pull metric gathering into a seperate function --- sheetsync/sheetsync/main.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sheetsync/sheetsync/main.py b/sheetsync/sheetsync/main.py index a41c219..d239ff3 100644 --- a/sheetsync/sheetsync/main.py +++ b/sheetsync/sheetsync/main.py @@ -166,16 +166,21 @@ class SheetSync(object): ) result = query(self.conn, built_query) by_id = {} - counts = defaultdict(lambda: 0) for row in result.fetchall(): by_id[row.id] = row + self.observe_rows(by_id.values()) + return by_id + + def observe_rows(self, rows): + """Takes a list of DB rows and updates metrics""" + counts = defaultdict(lambda: 0) + for row in rows: counts[row.sheet_name, row.category, str(row.poster_moment), row.state, str(bool(row.error))] += 1 # Reach into metric internals and forget about all previous values, # or else any values we don't update will remain as a stale count. event_counts._metrics.clear() for labels, count in counts.items(): event_counts.labels(self.name, *labels).set(count) - return by_id def sync_row(self, sheet_row, db_row): """Take a row dict from the sheet (or None) and a row namedtuple from the database (or None)