From 4985ad7a6dc3d4dfac560d9aa75df18a3be0fb96 Mon Sep 17 00:00:00 2001 From: Roni bhakta <77425964+ronibhakta1@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:06:17 +0530 Subject: [PATCH] fixed the deprecated keywords in the neo4j queries (#159) Closes #154 --------- Co-authored-by: Malte Tashiro --- iyp/__init__.py | 29 ++++++++++---------- iyp/crawlers/alice_lg/__init__.py | 4 +-- iyp/crawlers/cloudflare/dns_top_locations.py | 2 +- iyp/post/clean_links.py | 4 +-- iyp/post/country_information.py | 2 +- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/iyp/__init__.py b/iyp/__init__.py index c416da4..f025799 100644 --- a/iyp/__init__.py +++ b/iyp/__init__.py @@ -271,7 +271,8 @@ def batch_get_nodes_by_single_prop(self, label, prop_name, prop_set=set(), all=T if all: logging.info(f'Fetching all {label_str} nodes.') - existing_nodes = self.tx.run(f'MATCH (n:{label_str}) RETURN n.{prop_name} AS {prop_name}, ID(n) AS _id') + existing_nodes = self.tx.run( + f'MATCH (n:{label_str}) RETURN n.{prop_name} AS {prop_name}, elementId(n) AS _id') else: logging.info(f'Fetching up to {len(prop_set)} {label_str} nodes.') list_prop = list(prop_set) @@ -279,7 +280,7 @@ def batch_get_nodes_by_single_prop(self, label, prop_name, prop_set=set(), all=T WITH $list_prop AS list_prop MATCH (n:{label_str}) WHERE n.{prop_name} IN list_prop - RETURN n.{prop_name} AS {prop_name}, ID(n) AS _id""", list_prop=list_prop) + RETURN n.{prop_name} AS {prop_name}, elementId(n) AS _id""", list_prop=list_prop) ids = {node[prop_name]: node['_id'] for node in existing_nodes} existing_nodes_set = set(ids.keys()) @@ -294,7 +295,7 @@ def batch_get_nodes_by_single_prop(self, label, prop_name, prop_set=set(), all=T create_query = f"""WITH $batch AS batch UNWIND batch AS item CREATE (n:{label_str}) - SET n = item RETURN n.{prop_name} AS {prop_name}, ID(n) AS _id""" + SET n = item RETURN n.{prop_name} AS {prop_name}, elementId(n) AS _id""" new_nodes = self.tx.run(create_query, batch=batch) @@ -407,7 +408,7 @@ def batch_get_nodes(self, label, properties, id_properties=list(), create=True): query = f"""UNWIND $props AS prop {action} (a:{label_str} {where_clause_str}) {set_line} - RETURN {return_clause_str}, ID(a) AS _id""" + RETURN {return_clause_str}, elementId(a) AS _id""" ids = dict() for i in range(0, len(properties), BATCH_SIZE): @@ -463,11 +464,11 @@ def get_node(self, label, properties, id_properties=list(), create=True): result = self.tx.run( f"""MERGE (a:{label} {dict2str(id_property_dict)}) SET a += {dict2str(properties)} - RETURN ID(a)""" + RETURN elementId(a)""" ).single() else: # MATCH node - result = self.tx.run(f'MATCH (a:{label_str} {dict2str(properties)}) RETURN ID(a)').single() + result = self.tx.run(f'MATCH (a:{label_str} {dict2str(properties)}) RETURN elementId(a)').single() if result is not None: return result[0] @@ -491,7 +492,7 @@ def batch_add_node_label(self, node_ids, label): self.tx.run(f"""WITH $batch AS batch MATCH (n) - WHERE ID(n) IN batch + WHERE elementId(n) IN batch SET n:{label_str}""", batch=batch) self.commit() @@ -503,7 +504,7 @@ def batch_get_node_extid(self, id_type): Return None if the node does not exist. """ - result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(i:{id_type}) RETURN i.id AS extid, ID(a) AS nodeid') + result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(i:{id_type}) RETURN i.id AS extid, elementId(a) AS nodeid') ids = {} for node in result: @@ -518,7 +519,7 @@ def get_node_extid(self, id_type, id): Return None if the node does not exist. """ - result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(:{id_type} {{id:{id}}}) RETURN ID(a)').single() + result = self.tx.run(f'MATCH (a)-[:EXTERNAL_ID]->(:{id_type} {{id:{id}}}) RETURN elementId(a)').single() if result is not None: return result[0] @@ -549,7 +550,7 @@ def batch_add_links(self, type, links, action='create'): create_query = f"""WITH $batch AS batch UNWIND batch AS link MATCH (x), (y) - WHERE ID(x) = link.src_id AND ID(y) = link.dst_id + WHERE elementId(x) = link.src_id AND elementId(y) = link.dst_id CREATE (x)-[l:{type}]->(y) WITH l, link UNWIND link.props AS prop @@ -559,7 +560,7 @@ def batch_add_links(self, type, links, action='create'): create_query = f"""WITH $batch AS batch UNWIND batch AS link MATCH (x), (y) - WHERE ID(x) = link.src_id AND ID(y) = link.dst_id + WHERE elementId(x) = link.src_id AND elementId(y) = link.dst_id MERGE (x)-[l:{type}]-(y) WITH l, link UNWIND link.props AS prop @@ -587,7 +588,7 @@ def add_links(self, src_node, links): self.__create_range_index(relationship_type, 'reference_name', on_relationship=True) matches = ' MATCH (x)' - where = f' WHERE ID(x) = {src_node}' + where = f' WHERE elementId(x) = "{src_node}"' merges = '' for i, (type, dst_node, prop) in enumerate(links): @@ -600,7 +601,7 @@ def add_links(self, src_node, links): prop = format_properties(prop) matches += f', (x{i})' - where += f' AND ID(x{i}) = {dst_node}' + where += f' AND elementId(x{i}) = "{dst_node}"' merges += f' MERGE (x)-[:{type} {dict2str(prop)}]->(x{i}) ' self.tx.run(matches + where + merges).consume() @@ -621,7 +622,7 @@ def batch_add_properties(self, id_prop_list): add_query = """WITH $batch AS batch UNWIND batch AS item MATCH (n) - WHERE ID(n) = item.id + WHERE elementId(n) = item.id SET n += item.props""" res = self.tx.run(add_query, batch=batch) diff --git a/iyp/crawlers/alice_lg/__init__.py b/iyp/crawlers/alice_lg/__init__.py index 166f51c..eb5b153 100644 --- a/iyp/crawlers/alice_lg/__init__.py +++ b/iyp/crawlers/alice_lg/__init__.py @@ -52,7 +52,7 @@ # (:AS)-[:MEMBER_OF]->(:IXP) # Get IXP peering LANs: # MATCH (p:Prefix)-[:MANAGED_BY]->(i:IXP) -# RETURN p.prefix AS peering_lan, ID(i) AS ixp_qid +# RETURN p.prefix AS peering_lan, elementId(i) AS ixp_qid # neighbors -> list of neighbors # neighbor['address'] -> map to prefix # /routeservers/:id/neighbors/:neighborId/routes/received @@ -338,7 +338,7 @@ def __get_peering_lans(self) -> radix.Radix: """Get IXP peering LANs from IYP and return a radix tree containing the QID of the IXP node in the data['ixp_qid'] field of each tree node.""" query = """MATCH (p:Prefix)-[:MANAGED_BY]->(i:IXP) - RETURN p.prefix AS peering_lan, ID(i) AS ixp_qid""" + RETURN p.prefix AS peering_lan, elementId(i) AS ixp_qid""" peering_lans = radix.Radix() for res in self.iyp.tx.run(query): n = peering_lans.add(res['peering_lan']) diff --git a/iyp/crawlers/cloudflare/dns_top_locations.py b/iyp/crawlers/cloudflare/dns_top_locations.py index cfe599e..c76985f 100644 --- a/iyp/crawlers/cloudflare/dns_top_locations.py +++ b/iyp/crawlers/cloudflare/dns_top_locations.py @@ -53,7 +53,7 @@ def __init__(self, organization, url, name): existing_dn = self.iyp.tx.run( f"""MATCH (dn:DomainName)-[r:RANK]-(:Ranking) WHERE r.rank < {RANK_THRESHOLD} - RETURN ID(dn) AS _id, dn.name AS dname;""") + RETURN elementId(dn) AS _id, dn.name AS dname;""") self.domain_names_id = {node['dname']: node['_id'] for node in existing_dn} self.domain_names = list(self.domain_names_id.keys()) diff --git a/iyp/post/clean_links.py b/iyp/post/clean_links.py index 9c2c650..f4ed4e8 100644 --- a/iyp/post/clean_links.py +++ b/iyp/post/clean_links.py @@ -29,7 +29,7 @@ def get_links_of_type(self, link_type, prop_dict=None): query = f""" MATCH (src)-[r:{link_type}]->(dst) WHERE {prop_conditions} - RETURN ID(r) AS link_id, ID(src) AS src_id, ID(dst) AS dst_id, {prop_str} + RETURN elementId(r) AS link_id, elementId(src) AS src_id, elementId(dst) AS dst_id, {prop_str} """ result = self.iyp.tx.run(query) if result: @@ -49,7 +49,7 @@ def delete_links(self, link_ids): query = """ UNWIND $link_ids AS link_id MATCH ()-[r]->() - WHERE ID(r) = link_id + WHERE elementId(r) = link_id DELETE r """ self.iyp.tx.run(query, link_ids=link_ids) diff --git a/iyp/post/country_information.py b/iyp/post/country_information.py index 6a5fa3a..3377403 100644 --- a/iyp/post/country_information.py +++ b/iyp/post/country_information.py @@ -24,7 +24,7 @@ def run(self): 'alpha3': country_info.alpha3} self.iyp.tx.run(""" MATCH (n:Country) - WHERE ID(n) = $id + WHERE elementId(n) = $id SET n += $props """, id=country_id[country_code],