Ë
    À7j%  ã                  ó–   — d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ  ed«      Zdd	„Zdd
„Z	 d	 	 	 	 	 	 	 	 	 	 	 dd„Zy)a”  Discovery upload via the session flow (Phase 2).

Drives open-session -> per-category items -> complete/failed against the
Central API. Upload is best-effort: any transport failure is logged and the
local last.json snapshot remains the fallback. A category whose upload fails is
reported in failed_categories so the backend records the session as partial
(never silently dropped, never reconciled away).
é    )Úannotations)ÚAny)Ú	ApiClientÚApiError)ÚConfig)Úload_agent_token)Ú
get_loggerzdiscovery.uploadc                ó¤   — t        | t        «      r$| D �cg c]  }t        |t        «      r|nd|i‘Œ c}S t        | t        «      r| gS d| igS c c}w )zENormalize a category value into a list of dict items for raw storage.Úvalue)Ú
isinstanceÚlistÚdict)r   Úitems     úJ/var/www/downloads.wolfpanel.net/public_html/agent/src/discovery/upload.pyÚ_coerce_itemsr      sT   € ä�%œÔØPUÖVÈœ
 4¬Ô.‘°W¸d°OÑCÒVÐVÜ�%œÔØˆwˆØ�eÐÐÐùò Ws   •Ac                óh   — | j                  «       D ��ci c]  \  }}|t        |«      “Œ c}}S c c}}w )zAMap a full-discovery inventory dict to category -> list-of-items.)Úitemsr   )Ú	inventoryÚcategoryr   s      r   Úcategories_from_inventoryr      s+   € àBKÇ/Á/ÓBS×T©¨x¸ˆH”m EÓ*Ñ*ÓTÐTùÓTs   ”.Nc                ó  — |xs t        | «      }t        |j                  «       «      }	 |j                  ||dœ|«      }|d   }g }	g }
|j                  «       D ]-  \  }}	 |j                  |||dœ|«       |	j                  |«       Œ/ 	 |	s|j                  |ddœ|«      }n|j                  ||	|
d	œ|«      }t        j                  d
|||j                  d«      «       |S # t        t
        t        f$ r }t        j                  d|«       Y d}~yd}~ww xY w# t        $ r2}t        j                  d||«       |
j                  |«       Y d}~Œõd}~ww xY w# t        $ r }t        j                  d|«       Y d}~yd}~ww xY w)z”Run the full session flow for one scan. Returns the finalize response.

    Never raises: returns None if the session could not even be opened.
    )Ú	scan_typeÚexpected_categoriesÚ
session_idz!discovery session open failed: %sN)r   r   r   z(discovery items upload failed for %s: %szall category uploads failed)r   Úerror)r   Úsucceeded_categoriesÚfailed_categoriesz1discovery %s scan finalized: session=%s status=%sÚstatuszdiscovery finalize failed: %s)r   ÚsortedÚkeysÚopen_discovery_sessionr   ÚKeyErrorÚ	TypeErrorÚlogÚwarningr   Úupload_discovery_itemsÚappendÚfail_discoveryÚcomplete_discoveryÚinfoÚget)ÚconfigÚ
api_clientr   Ú
categoriesÚtokenÚexpectedÚopenedr   ÚexcÚ	succeededÚfailedr   r   Úresults                 r   Úupload_inventoryr6   $   s›  € ð Ò-Ô% fÓ-€EÜ�j—o‘oÓ'Ó(€HðØ×2Ñ2Ø#¸HÑEÀuó
ˆð ˜LÑ)ˆ
ð
 €IØ€FØ%×+Ñ+Ó-ò 	$‰ˆ�%ð	$Ø×-Ñ-Ø)°xÈ%ÑPØôð ×Ñ˜XÕ&ð	$ðÙà×.Ñ.Ø)Ð4QÑRØó‰Fð
  ×2Ñ2à",Ø,5Ø)/ñð
 óˆFô 	�‰Ø?ØØØ�J‰J�xÓ ô		
ð ˆøôO ”h¤	Ð*ò Ü�‰Ð7¸Ô=Üûðûô ò 	$Ü�K‰KÐBÀHÈcÔRØ�M‰M˜(×#Ñ#ûð	$ûô6 ò Ü�‰Ð3°SÔ9ÜûðúsH   ªC  Á'DÂ	AE Ã DÃ4DÄDÄ	EÄ (EÅEÅ	E>ÅE9Å9E>)r   r   Úreturnzlist[dict[str, Any]])r   zdict[str, Any]r7   údict[str, list[dict[str, Any]]])N)r,   r   r-   r   r   Ústrr.   r8   r/   z
str | Noner7   zdict[str, Any] | None)Ú__doc__Ú
__future__r   Útypingr   r-   r   r   r,   r   Úidentityr   Úloggerr	   r$   r   r   r6   © ó    r   ú<module>rA      sv   ðñõ #å ç *Ý Ý %Ý áÐ#Ó$€óóUð ð=Øð=àð=ð ð=ð 0ð	=ð
 ð=ð ô=r@   