@@ -56,6 +56,7 @@ def __init__(self, *args, **kwargs):
5656 self .grouper = MQWriter (CFG .grouper_inventory_topic , partitioner = Partitioners .org_id_partitioner , loop = self .loop )
5757 self .repo_id_cache = {}
5858 self .group_set_cache = {}
59+ self .tag_set_cache = {}
5960
6061 async def init (self ):
6162 """Async constructor"""
@@ -282,11 +283,47 @@ async def _db_system_group_set_lookup(self, conn, groups: list[dict]) -> tuple[s
282283 result = await cur .fetchone ()
283284 return groups_checksum , result ["id" ]
284285
286+ async def _db_system_tag_set_lookup (self , conn , tags : list [dict ]) -> tuple [str , int ]:
287+ tags_str = json .dumps (tags )
288+ tags_checksum = hashlib .sha256 (tags_str .encode ("utf-8" )).hexdigest ()
289+ if tags_checksum in self .tag_set_cache :
290+ return tags_checksum , self .tag_set_cache [tags_checksum ]
291+
292+ async with conn .cursor (row_factory = dict_row ) as cur :
293+ await cur .execute (
294+ "SELECT id FROM system_tag_set WHERE tags_checksum = %s" ,
295+ (tags_checksum ,),
296+ )
297+ result = await cur .fetchone ()
298+ if result :
299+ self .tag_set_cache [tags_checksum ] = result ["id" ]
300+ return tags_checksum , result ["id" ]
301+
302+ await cur .execute (
303+ """
304+ INSERT INTO system_tag_set (tags, tags_checksum)
305+ VALUES (%s, %s) ON CONFLICT (tags_checksum) DO UPDATE SET
306+ tags = EXCLUDED.tags
307+ RETURNING id
308+ """ ,
309+ (
310+ tags_str ,
311+ tags_checksum ,
312+ ),
313+ )
314+ result = await cur .fetchone ()
315+ return tags_checksum , result ["id" ]
316+
285317 async def _commit_system_group_set_cache (self , groups_checksum : str , group_set_id : int ) -> None :
286318 """Insert group_set_id to the cache after succesful commit"""
287319 if groups_checksum not in self .group_set_cache :
288320 self .group_set_cache [groups_checksum ] = group_set_id
289321
322+ async def _commit_system_tag_set_cache (self , tags_checksum : str , tag_set_id : int ) -> None :
323+ """Insert tag_set_id to the cache after succesful commit"""
324+ if tags_checksum not in self .tag_set_cache :
325+ self .tag_set_cache [tags_checksum ] = tag_set_id
326+
290327 async def _db_import_system (self , conn , fields : dict , org_id : str , inventory_id : str ) -> (ImportStatus , int ):
291328 """Import system to system_platform table, update if exists"""
292329 rh_account_id , system_id = await self ._db_account_system_lookup (conn , org_id , inventory_id )
@@ -299,16 +336,16 @@ async def _db_import_system(self, conn, fields: dict, org_id: str, inventory_id:
299336 INSERT INTO system_platform
300337 (rh_account_id, inventory_id, s3_url, vmaas_json, json_checksum, display_name,
301338 stale_timestamp, stale_warning_timestamp, culled_timestamp,
302- host_type, last_upload, stale, operating_system_id, group_set_id)
339+ host_type, last_upload, stale, operating_system_id, group_set_id, tag_set_id )
303340 VALUES (%(rh_account_id)s, %(inventory_id)s, %(s3_url)s, %(vmaas_json)s, %(json_checksum)s, %(display_name)s,
304341 %(stale_timestamp)s, %(stale_warning_timestamp)s, %(culled_timestamp)s,
305- %(host_type)s, %(last_upload)s, 'F', %(operating_system_id)s, %(group_set_id)s)
342+ %(host_type)s, %(last_upload)s, 'F', %(operating_system_id)s, %(group_set_id)s, %(tag_set_id)s )
306343 ON CONFLICT (inventory_id) DO UPDATE SET
307344 rh_account_id = EXCLUDED.rh_account_id, inventory_id = EXCLUDED.inventory_id, s3_url = EXCLUDED.s3_url, vmaas_json = EXCLUDED.vmaas_json,
308345 json_checksum = EXCLUDED.json_checksum, display_name = EXCLUDED.display_name, stale_timestamp = EXCLUDED.stale_timestamp,
309346 stale_warning_timestamp = EXCLUDED.stale_warning_timestamp, culled_timestamp = EXCLUDED.culled_timestamp,
310347 host_type = EXCLUDED.host_type, last_upload = EXCLUDED.last_upload, stale = EXCLUDED.stale, operating_system_id = EXCLUDED.operating_system_id,
311- group_set_id = EXCLUDED.group_set_id
348+ group_set_id = EXCLUDED.group_set_id, tag_set_id = EXCLUDED.tag_set_id
312349 RETURNING (xmax = 0) AS inserted, unchanged_since, last_evaluation, id, when_deleted
313350 """
314351 else :
@@ -325,7 +362,8 @@ async def _db_import_system(self, conn, fields: dict, org_id: str, inventory_id:
325362 last_upload = %(last_upload)s,
326363 stale = FALSE,
327364 operating_system_id = %(operating_system_id)s,
328- group_set_id = %(group_set_id)s
365+ group_set_id = %(group_set_id)s,
366+ tag_set_id = %(tag_set_id)s
329367 WHERE id = %(system_id)s
330368 RETURNING (xmax = 0) AS inserted, unchanged_since, last_evaluation, id, when_deleted
331369 """
@@ -439,8 +477,10 @@ async def _process_upload(self, msg: InventoryMsg):
439477 operating_system = msg .msg ["host" ]["system_profile" ]["operating_system" ]
440478 operating_system_id = await self ._db_operating_system_lookup (conn , operating_system )
441479 groups_checksum , group_set_id = await self ._db_system_group_set_lookup (conn , msg .msg ["host" ]["groups" ])
480+ tags_checksum , tag_set_id = await self ._db_system_tag_set_lookup (conn , msg .msg ["host" ]["tags" ])
442481 insert_fields ["operating_system_id" ] = operating_system_id
443482 insert_fields ["group_set_id" ] = group_set_id
483+ insert_fields ["tag_set_id" ] = tag_set_id
444484 import_status , system_id = await self ._db_import_system (conn , insert_fields , org_id , inventory_id )
445485 if ImportStatus .FAILED in import_status :
446486 return
@@ -454,6 +494,7 @@ async def _process_upload(self, msg: InventoryMsg):
454494 # Insert new items to the cache only after succesfully commited insert/update
455495 await self ._commit_operating_system_cache (operating_system , operating_system_id )
456496 await self ._commit_system_group_set_cache (groups_checksum , group_set_id )
497+ await self ._commit_system_tag_set_cache (tags_checksum , tag_set_id )
457498 except Exception as exc :
458499 DATABASE_ERROR .inc ()
459500 LOGGER .exception ("Error importing system: %s" , exc )
0 commit comments