"""Logic for Infra.""" import uuid from typing import Dict, List, Optional from pdp.connectors.ows_account import OwsAccountClient from pdp.connectors.redis_client import NullSerializer, RedisConnector from pdp.constants.constants import TenantType from pdp.fastapi.schemas.infra import ( BludgeonCacheRequest, BludgeonCacheResponse, BustCacheRequest, BustCacheResponse, ListCacheRequest, ListCacheResponse, ) from pdp.fastapi.schemas.tenant import ( IdExchangeTenantHierarchy, IdToUuidExchangeTenant, UuidToIdExchangeTenant, ) from pdp.proxies.id_to_uuid_exchange_tenant_proxy import IdToUuidExchangeTenantProxy from pdp.proxies.uuid_to_id_exchange_tenant_proxy import UuidToIdExchangeTenantProxy WILDCARD = "*" async def cache_list( list_cache_request: ListCacheRequest, redis_connector: RedisConnector, ) -> ListCacheResponse: """List cache entries.""" keys = list_cache_request.keys cache_response = await redis_connector.mget( keys=keys, serializer=NullSerializer(), ) # mget returns a list of values ordered identically to `keys` cache_item_tuples = zip(keys, cache_response) cache_items = [ {"key": key, "value": value or {}} for key, value in cache_item_tuples ] return ListCacheResponse(items=cache_items) async def cache_bust( bust_cache_request: BustCacheRequest, redis_connector: RedisConnector, ) -> BustCacheResponse: """List and delete cache entries.""" keys = bust_cache_request.keys cache_response = await redis_connector.getdel( keys=keys, serializer=NullSerializer(), ) cache_items = [] # getdel returns a list of values ordered identically to `keys` for key, value in zip(keys, cache_response): cache_items.append( {"key": key, "value": value or {}, "deleted": value is not None} ) return BustCacheResponse(items=cache_items) async def cache_bludgeon( bludgeon_cache_request: BludgeonCacheRequest, redis_connector: RedisConnector, ) -> BludgeonCacheResponse: """Delete all cache entries for the cache_entry_type.""" total_rows_before_delete = await redis_connector.dbsize() list_pattern = f"{bludgeon_cache_request.cache_entry_type.value}{WILDCARD}" if bludgeon_cache_request.delete: # Delete entries. matching_keys = await redis_connector.delete_all_matching_pattern( pattern=list_pattern ) performed_delete = True else: # Dry-run mode: Return the number of keys that match the pattern. matching_keys = await redis_connector.list(pattern=list_pattern) performed_delete = False return BludgeonCacheResponse( total_rows_before_delete=total_rows_before_delete, total_rows_affected=len(matching_keys), performed_delete=performed_delete, ) async def gather_id_to_uuid_exchange( tenants: List[IdToUuidExchangeTenant], ows_account_client: OwsAccountClient, redis_connector: RedisConnector, ) -> Dict[TenantType, Dict[str, Optional[IdExchangeTenantHierarchy]]]: proxy = IdToUuidExchangeTenantProxy( tenants=tenants, redis_connector=redis_connector, ows_account_client=ows_account_client, ) return await proxy.gather_tenant_exchange() async def gather_uuid_to_id_exchange( tenants: List[UuidToIdExchangeTenant], ows_account_client: OwsAccountClient, redis_connector: RedisConnector, ) -> Dict[uuid.UUID, Optional[IdExchangeTenantHierarchy]]: """Gather tenants exchange from UuidToIdExchangeTenantProxy.""" proxy = UuidToIdExchangeTenantProxy( tenants=tenants, redis_connector=redis_connector, ows_account_client=ows_account_client, ) return await proxy.gather_tenant_exchange()