Ë
    KµCj+  ã            
       ó
  — d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZmZ dd	lmZmZ dd
lmZ  e«       Z e«       Z ee«      Zej9                  ded¬«       ee
«      fdedede	defd„«       Zy)u¿  
POST /api/v1/analyze

The core endpoint. Deliberately does NOT run models inline â€” it checks
the cache, and on a miss, queues a Celery task and returns immediately
with a task_id. This is what lets the web tier stay fast and responsive
under load regardless of how long model inference + LLM calls take.

Accepts either a logged-in customer (JWT, via the SaaS dashboard) or a
service API key (machine-to-machine integration) â€” see
app/core/dual_auth.py. Results from a logged-in user are attributed to
their account (AnalysisRecord.user_id) so the dashboard can show their
history; API-key calls remain anonymous as before.

See GET /api/v1/tasks/{task_id} for how the client retrieves the result.
é    N)Ú	APIRouterÚDependsÚRequest)Úget_settings)ÚAuthContextÚget_auth_context)Ú
get_logger)ÚRedisRateLimiterÚget_client_identifier)ÚCropAnalysisRequestÚTaskSubmissionResponse)Úbuild_cache_keyÚcache_service)Úrun_crop_analysisz/analyzeéÊ   )Úresponse_modelÚstatus_codeÚpayloadÚrequestÚauthÚreturnc              ƒ   óF  K  — t        t        j                  «       «      }t        |«      ƒ d {  –—† }t        j
                  �At        t        j
                  t        j                  «      }|j                  |«      ƒ d {  –—†  t        | j                  | j                  | j                  t        j                  «      }t	        j                  |«      ƒ d {  –—† }|��t         j#                  d| j                  › d| j                  › d| j                  › �«       ||d<   d|d<   d|› �}t	        j$                  d|› �|d¬	«      ƒ d {  –—†  t'        |d
t        j(                  › d|› �¬«      S |j*                  r|j*                  j,                  nd }	t/        j0                  || j                  | j                  | j                  |j2                  r|j2                  d d nd |	|j4                  | j6                  ¬«      }
t         j#                  d|
j8                  › d|› d| j                  › d| j                  › d| j                  › d|j:                  › d|j4                  › �«       t'        |
j8                  dt        j(                  › d|
j8                  › �¬«      S 7 �Œg7 �Œ7 �ŒÅ7 �ŒX­w)NzCache hit for ú/Ú
request_idTÚcachedzcached-ztask_result:i,  )Úttl_secondsÚ	completedz/tasks/)Útask_idÚstatusÚpoll_urlé   )r   ÚdistrictÚcropÚmonthÚapi_key_prefixÚ	client_ipÚuser_idÚcrop_plan_idzQueued analysis task z for request_id=z crop=z
 district=z month=z auth_type=z	 user_id=Úqueued)ÚstrÚuuidÚuuid4r   r   Ú
raw_clientr
   ÚsettingsÚRATE_LIMIT_PER_MINUTEÚcheckr   Úlocationr#   r$   ÚMODEL_VERSIONÚgetÚloggerÚinfoÚsetr   ÚAPI_V1_PREFIXÚclientÚhostr   ÚdelayÚapi_keyr'   r(   ÚidÚ	auth_type)r   r   r   r   Ú
identifierÚlimiterÚ	cache_keyÚcached_resultÚsynthetic_task_idÚclient_hostÚtasks              úIC:\Crop_Prediction\Backend\crop-ai-system\app\api\v1\endpoints\analyze.pyÚanalyze_croprF   #   sQ  è ø€ ô ”T—Z‘Z“\Ó"€Jä,¨WÓ5×5€JÜ×ÑÐ+Ü"¤=×#;Ñ#;¼X×=[Ñ=[Ó\ˆØ�m‰m˜JÓ'×'Ð'ä × 0Ñ 0°'·,±,ÀÇÁÌx×OeÑOeÓf€IÜ'×+Ñ+¨IÓ6×6€MàÐ Ü�‰�n W§\¡\ N°!°G×4DÑ4DÐ3EÀQÀwÇ}Á}ÀoÐVÔWð '1ˆ�lÑ#Ø"&ˆ�hÑà% j \Ð2ÐÜ×Ñ ,Ð/@Ð.AÐ BÀMÐ_bÔc×cÐcÜ%Ø%ØÜ ×.Ñ.Ð/¨wÐ7HÐ6IÐJô
ð 	
ð *1¯ª�'—.‘.×%Ò%¸T€KÜ×"Ñ"ØØ×!Ñ!Ø�\‰\Ø�m‰mØ,0¯LªL�t—|‘| C RÑ(¸dØØ—‘Ø×)Ñ)ô	€Dô ‡K�KØ
 §¡˜yÐ(8¸¸ð EØ—‘ˆ~˜Z¨×(8Ñ(8Ð'9¸ÀÇÁÀð PØ—^‘^Ð$ I¨d¯l©l¨^ð	=ôô "Ø—‘ØÜ×*Ñ*Ð+¨7°4·7±7°)Ð<ôð ði 6ùð 	(ùð 7ùð* 	dûsI   ‚,J!®J¯AJ!Á?JÂ AJ!ÃJÃA/J!ÅJÅEJ!ÊJ!ÊJ!ÊJ!)Ú__doc__r+   Úfastapir   r   r   Úapp.core.configr   Úapp.core.dual_authr   r   Úapp.core.logging_configr	   Úapp.core.securityr
   r   Úapp.schemas.requestr   r   Úapp.services.cache_servicer   r   Úapp.workers.tasksr   Úrouterr.   Ú__name__r4   ÚpostrF   © ó    rE   ú<module>rU      s–   ðñó" ç /Ñ /å (ß <Ý .ß Eß Kß EÝ /á	‹€Ù‹>€Ù	�HÓ	€ð ‡�ˆZÐ(>ÈC€ÓPñ  Ð 0Ó1ñ?Ø ð?àð?ð ð?ð ò	?ó Qñ?rT   