From 23d21fb7957fa35839df2e3b6a51305cebe9c997 Mon Sep 17 00:00:00 2001 From: louisevelayo Date: Fri, 10 Nov 2023 14:23:02 +0100 Subject: [PATCH] refactor: extract logic for checking new node providers to separate file --- node_monitor/ic_api.py | 2 +- node_monitor/node_monitor.py | 14 ++++--------- .../get_new_node_providers.py | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 node_monitor/node_monitor_helpers/get_new_node_providers.py diff --git a/node_monitor/ic_api.py b/node_monitor/ic_api.py index 7bc96284..bd1a0f6e 100644 --- a/node_monitor/ic_api.py +++ b/node_monitor/ic_api.py @@ -39,8 +39,8 @@ class Nodes(BaseModel): nodes: List[Node] class NodeProvider(BaseModel): + principal_id: Principal display_name: str - principal_id: str class NodeProviders(BaseModel): node_providers: List[NodeProvider] diff --git a/node_monitor/node_monitor.py b/node_monitor/node_monitor.py index 7459ac96..c738865d 100644 --- a/node_monitor/node_monitor.py +++ b/node_monitor/node_monitor.py @@ -13,6 +13,8 @@ from node_monitor.node_provider_db import NodeProviderDB from node_monitor.node_monitor_helpers.get_compromised_nodes import \ get_compromised_nodes +from node_monitor.node_monitor_helpers.get_new_node_providers import \ + get_new_node_providers import node_monitor.node_monitor_helpers.messages as messages Seconds = int @@ -198,16 +200,8 @@ def update_node_provider_lookup_if_new( for node_provider in node_providers_api.node_providers } node_providers_db = self.node_provider_db.get_subscribers_as_dict() - - principals_api = set(node_providers_api_dict.keys()) - principals_db = set(node_providers_db.keys()) - - principals_new = principals_api - principals_db - node_providers_new = [ - NodeProvider( - display_name=node_providers_api_dict[principal_id], - principal_id=principal_id - ) for principal_id in principals_new] + node_providers_new = get_new_node_providers( + node_providers_api_dict, node_providers_db) query = """ INSERT INTO subscribers ( diff --git a/node_monitor/node_monitor_helpers/get_new_node_providers.py b/node_monitor/node_monitor_helpers/get_new_node_providers.py new file mode 100644 index 00000000..f4c8572b --- /dev/null +++ b/node_monitor/node_monitor_helpers/get_new_node_providers.py @@ -0,0 +1,21 @@ +from typing import Dict, List, Any +from node_monitor.ic_api import NodeProvider + +Principal = str + +def get_new_node_providers( + node_providers_api_dict: Dict[Principal, str], + node_providers_db: Dict[Principal, Dict[str, Any]], +) -> List[NodeProvider]: + principals_api = set(node_providers_api_dict.keys()) + principals_db = set(node_providers_db.keys()) + principals_new = principals_api - principals_db + + node_providers_new = [ + NodeProvider( + display_name=node_providers_api_dict[principal_id], + principal_id=principal_id + ) for principal_id in principals_new + ] + + return node_providers_new