From 738dff9dff8c7f15d0e248203f79cc53d80c0a06 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 23:23:49 -0700 Subject: [PATCH] =?UTF-8?q?refactor(hermes=5Fcli):=20shared=5Fmetrics=20st?= =?UTF-8?q?ore=20=E2=80=94=20hoist=20counter=20upsert=20SQL,=20flatten=20a?= =?UTF-8?q?ctive-install=20guard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hermes_cli/observability/shared_metrics.py | 60 ++++++++++------------ 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/hermes_cli/observability/shared_metrics.py b/hermes_cli/observability/shared_metrics.py index 16e2d772cf..c86e97e0cf 100644 --- a/hermes_cli/observability/shared_metrics.py +++ b/hermes_cli/observability/shared_metrics.py @@ -103,6 +103,17 @@ _MIGRATE_V1_SQL = ( """, "DROP TABLE counter_aggregates_v1", ) +_INCREMENT_COUNTER_SQL = """ + INSERT INTO counter_aggregates( + period_start, metric_name, hermes_version, os_family, architecture, + install_method, dimensions_json, value, packaged_value + ) VALUES (?, ?, ?, ?, ?, ?, ?, 1, 0) + ON CONFLICT( + period_start, metric_name, hermes_version, os_family, architecture, + install_method, dimensions_json + ) + DO UPDATE SET value = value + 1 + """ # (column, declaration) added to package_outbox for transmission bookkeeping. _SEND_COLUMNS = ( # When the 202 was received. NULL = never acknowledged. @@ -156,9 +167,7 @@ class SharedMetricsStore: """Persist allowlisted counters and export immutable delta packages.""" def __init__( - self, - database_path: Path | None = None, - outbox_directory: Path | None = None, + self, database_path: Path | None = None, outbox_directory: Path | None = None ) -> None: root = get_hermes_home() / "telemetry" / "shared_metrics" self.database_path = database_path or root / "metrics.sqlite3" @@ -183,22 +192,19 @@ class SharedMetricsStore: (_ACTIVE_INSTALL_STATE_KEY,), ).fetchone() last_recorded = self._parse_state_timestamp(row["value"]) if row is not None else None - if last_recorded is not None: - if last_recorded > now: - # A wall-clock correction must not suppress activity until the - # stale future timestamp plus another full interval. - connection.execute( - "UPDATE telemetry_state SET value = ? WHERE key = ?", - (_isoformat(now), _ACTIVE_INSTALL_STATE_KEY), - ) - return False - if now < last_recorded + _ACTIVE_INSTALL_INTERVAL: - return False - + if last_recorded is not None and last_recorded > now: + # A wall-clock correction must not suppress activity until the stale future + # timestamp plus another full interval. + connection.execute( + "UPDATE telemetry_state SET value = ? WHERE key = ?", + (_isoformat(now), _ACTIVE_INSTALL_STATE_KEY), + ) + return False + if last_recorded is not None and now < last_recorded + _ACTIVE_INSTALL_INTERVAL: + return False self._install_id(connection) self._record_counter_in_transaction( - connection, CLIENT_ACTIVE_METRIC, dimensions, resource, - period_start=now.date().isoformat(), + connection, CLIENT_ACTIVE_METRIC, dimensions, resource, now.date().isoformat() ) connection.execute( "INSERT INTO telemetry_state(key, value) VALUES (?, ?)" @@ -214,8 +220,7 @@ class SharedMetricsStore: self._validate_counter(metric_name, dimensions, resource) with self._connection() as connection: self._record_counter_in_transaction( - connection, metric_name, dimensions, resource, - period_start=_utc_now().date().isoformat(), + connection, metric_name, dimensions, resource, _utc_now().date().isoformat() ) @staticmethod @@ -235,25 +240,12 @@ class SharedMetricsStore: metric_name: str, dimensions: dict[str, str], resource: dict[str, str], - *, period_start: str, ) -> None: connection.execute( - """ - INSERT INTO counter_aggregates( - period_start, metric_name, hermes_version, os_family, architecture, - install_method, dimensions_json, value, packaged_value - ) VALUES (?, ?, ?, ?, ?, ?, ?, 1, 0) - ON CONFLICT( - period_start, metric_name, hermes_version, os_family, architecture, - install_method, dimensions_json - ) - DO UPDATE SET value = value + 1 - """, + _INCREMENT_COUNTER_SQL, ( - period_start, - metric_name, - *(resource[column] for column in _RESOURCE_COLUMNS), + period_start, metric_name, *(resource[column] for column in _RESOURCE_COLUMNS), _compact_json(dimensions), ), )