
    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.WdO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HmE**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)zRun 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joo'(H22#HEu
 L)

 IF%++- 	$%	$--)x%P X&	$..)4QRF
  22",,5)/
 F 	?JJx 		
 O h	* 7=  	$KKBHcRMM(##	$6  3S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@   