
    KCj+              
       
   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     TZZ\"J,W55J+"=#;#;X=[=[\mmJ''' 0 0',,xOeOefI'++I66M nW\\N!G4D4D3EQw}}oVW '1l#"&h%j\2,/@.A BM_bccc%% ../w7H6IJ
 	
 *1'..%%TK""!!\\mm,0LLt||CR(d))	D KK
y(8 E~Z(8(8'9 P^^$Idll^	= "**+7477)< 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   