+
    zjb9                     	   R t ^ RIHt ^ RIHtHtHt ^ RIHtHt ^ RI	H
t
HtHt ^ RIHtHtHtHtHtHtHt ^ RIHtHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ]! R] 4      t!]! R4      t"]!PG                  RR.R7      ]]! R^<R7      R 4       4       4       t$ ]!PG                  RR.R7      R 4       t%]!PG                  RR.R7      R 4       t&]!PG                  RR.R7      R 4       t']!PG                  RR.R7      ]R 4       4       t(]!PG                  RR.R7      ]PR                  ]R 4       4       4       t*]!PG                  RR.R7      R 4       t+]!PG                  RR.R7      ]PR                  ]
! ]4      R  4       4       4       t,]!PG                  R!R.R7      ]PR                  ]
! ]4      R" 4       4       4       t-]!PG                  R#R.R7      ]PR                  ]
! ]4      R$ 4       4       4       t.]!PG                  R%R.R7      ]PR                  R& 4       4       t/]!PG                  R'R.R7      ]PR                  R( 4       4       t0]!PG                  R%R.R7      ]PR                  R) 4       4       t1]!PG                  R*R.R7      ]PR                  ]
! ]4      R+ 4       4       4       t2]!PG                  R,R.R7      ]PR                  R- 4       4       t3]!PG                  R.R.R7      ]PR                  R/ 4       4       t4]!PG                  R0R.R7      ]PR                  R1 4       4       t5]!PG                  R2R.R7      ]PR                  R3 4       4       t6]!PG                  R4R.R7      ]PR                  ]
! ]4      R5 4       4       4       t7]!PG                  R6R.R7      ]PR                  ]
! ]4      R7 4       4       4       t8]!PG                  R8R.R7      ]PR                  ]
! ]4      R9 4       4       4       t9]!PG                  R:R.R7      ]PR                  ]
! ]4      R; 4       4       4       t:]!PG                  R<R.R7      ]PR                  R= 4       4       t;]!PG                  R>R.R7      ]PR                  R? 4       4       t<]!PG                  R>R.R7      ]PR                  R@ 4       4       t=]!P}                  RA4      RB 4       t?]!P}                  RC4      RD 4       t@]!P}                  ]A4      RE 4       tBRF# )Gz
# File: pyapi/app/routes/api.py
# Description: API routes for the OTT Reports service, including caching, validation, and error handling
)datetime)	Blueprintjsonifyrequest)
get_loggerperformance_log)validate_jsonvalidate_queryvalidate_multiple)ProphetTrainRequestProphetForecastRequestProphetPlotRequestProphetValidateRequestRegressionTrainRequestCollectionRiskScoreRequestSalesTargetPredictRequest)CacheServicecache_response)auth_middleware)ProphetController)ReportController)RegressionControllerapiz/healthGET)methodshealth_check)timeoutc                     RRR\        \        P                  ! 4       4      R\        P                  ! 4       /p \        4       pVP                  4       p\        VR4      '       d   VP                  4       V R&   M\        VR4      '       d   VP                  V R&   Mc\        V\        4      '       dJ   \        V4      ^8  d:   V^ ,          p\        VR4      '       d   VP                  4       V R&   MRRR	R
/V R&   MW R&   \        V 4      p^Vn        V#   \         dk   p\         P#                  R\        T4       24       \        RRR	\        T4      R\        \        P                  ! 4       4      /4      pRTn        Tu Rp?# Rp?ii ; i)z+Health check endpoint with cache statisticsstatushealthy	timestampcacheget_json	databasesjsonunknownerrorz(Unable to parse database health responsezHealth check failed: 	unhealthy  N)strr   nowr   	get_statsr   get_database_healthhasattrr"   r$   
isinstancetuplelenr   status_code	Exceptionloggerr&   )health_datareport_controllerdb_health_responseresponse_dataresponsees         ;D:\Projects\python\client-ideal-oms_pyapi\app\routes\api.pyr   r   7   sn   
+ iX\\^,\++-
 -..BBD %z22'9'B'B'DK$'00'9'>'>K$*E22s;M7NRS7S.q1M}j11+8+A+A+CK( iG,K( (:$;'" ,SVH56{GSV[#hllnBUV
  #s   DD FAF<FFz/dataPOSTc                  T    \         P                  p  \        R RRV /4      p^Vn        V# )messagezData createddata)r   r$   r   r1   )r>   r8   s     r:   create_datar?   u   s2    <<D 	>64@AHHO    z/data/<int:data_id>c                2    \        RV RR/4      p^Vn        V# )z
Endpoint to retrieve data by ID
This is a placeholder for demonstration purposes.
In a real application, you would retrieve data from a database or service.
data_idr>   zSample datar   r1   rB   r8   s   & r:   get_datarE      s$     	7FMBCHHOr@   DELETEc                2    \        RRRV /4      p^Vn        V# )z
Endpoint to delete data by ID
This is a placeholder for demonstration purposes.
In a real application, you would delete data from a database or service.
r=   zData deletedrB   rC   rD   s   & r:   delete_datarH      s$     	>9gFGHHOr@   z/cache/statsc                 N    \         P                  ! 4       p \        RRRV R\        \        P
                  ! 4       4      /4      p^Vn        V#   \         dM   p\        P                  R\        T4       24       \        RRR\        T4      /4      pRTn        Tu R	p?# R	p?ii ; i)
zGet cache statisticsr   successr>   r    zError getting cache stats: r&   r=   r(   N)
r   r+   r   r)   r   r*   r1   r2   r3   r&   )statsr8   r9   s      r:   get_cache_statsrL      s    &&(y&%c(,,.>QR
  # 23q6(;<Hgy#a&AB"	s   A
A B$ABB$B$z/cache/clearc                 v    \         P                  ! 4       p \        RV '       d   RMRRV '       d   RMRR\        \        P
                  ! 4       4      /4      p^Vn        V#   \         dM   p\        P                  R\        T4       24       \        RR	R\        T4      /4      pR
Tn        Tu Rp?# Rp?ii ; i)z Clear all cache (admin endpoint)r   rJ   warningr=   zCache cleared successfullyzCache clear may have failedr    zError clearing cache: r&   r(   N)
r   clearr   r)   r   r*   r1   r2   r3   r&   )rJ   r8   r9   s      r:   clear_cacherP      s    
$$&w)I 82S0
  # -c!fX67Hgy#a&AB"	s#   .A! /A! !B8,AB3-B83B8z/reports/database/healthc                 6    \        4       p V P                  4       # )z"Database connectivity health check)r   r,   
controllers    r:   get_reports_database_healthrT      s     "#J))++r@   z/prophet/trainc                8    \        4       pVP                  V 4      # )z!Train a Prophet forecasting model)r   train_modelvalidated_jsonrS   s   & r:   train_prophet_modelrY      s    
 #$J!!.11r@   z/prophet/forecast/<model_id>c                8    \        4       pVP                  W4      # )z/Generate forecast using a trained Prophet model)r   generate_forecastmodel_idrX   rS   s   && r:   generate_prophet_forecastr^          
 #$J''AAr@   z/prophet/plot/<model_id>c                8    \        4       pVP                  W4      # )z2Generate forecast plot for a trained Prophet model)r   get_forecast_plotr\   s   && r:   generate_prophet_plotrb      r_   r@   z/prophet/models/<model_id>c                8    \        4       pVP                  V 4      # )z-Get information about a trained Prophet model)r   get_model_infor]   rS   s   & r:   get_prophet_model_inforf      s     #$J$$X..r@   z/prophet/modelsc                 6    \        4       p V P                  4       # )zList all trained Prophet models)r   list_modelsrR   s    r:   list_prophet_modelsri      s     #$J!!##r@   c                8    \        4       pVP                  V 4      # )zDelete a trained Prophet model)r   delete_modelre   s   & r:   delete_prophet_modelrl      s     #$J""8,,r@   z/prophet/validatec                8    \        4       pVP                  V 4      # )z,Validate time series data format for Prophet)r   validate_datarW   s   & r:   validate_prophet_dataro     s    
 #$J##N33r@   z/prophet/train/dbc                 v    \        4       p \        P                  ! 4       ;'       g    / pV P                  V4      # )z2Train a Prophet model using data from OMS database)r   r   r"   train_from_database)rS   r>   s     r:   train_prophet_from_databaserr     s3     #$J##D))$//r@   z/prophet/tablesc                 6    \        4       p V P                  4       # )z1Get available OMS tables for time series analysis)r   get_available_tablesrR   s    r:   get_prophet_tablesru     s     #$J**,,r@   z#/prophet/tables/<table_key>/previewc                t    \        4       p\        P                  P                  4       pVP	                  W4      # )z,Get preview of time series data from a table)r   r   argsto_dictget_data_preview)	table_keyrS   paramss   &  r:   get_prophet_data_previewr|   %  s/     #$J\\!!#F&&y99r@   z$/prophet/tables/<table_key>/validatec                8    \        4       pVP                  V 4      # )zValidate a table configuration)r   validate_table_config)rz   rS   s   & r:   validate_prophet_tabler   .  s     #$J++I66r@   z!/regression/train/collection-riskc                8    \        4       pVP                  V 4      # )z6Train XGBoost collection risk classifier for a company)r   train_collection_riskrW   s   & r:   train_collection_risk_modelr   :      
 &'J++N;;r@   z/regression/train/sales-targetc                8    \        4       pVP                  V 4      # )z=Train XGBoost sales target attainment regressor for a company)r   train_sales_targetrW   s   & r:   train_sales_target_modelr   C  s    
 &'J((88r@   z!/regression/score/collection-riskc                8    \        4       pVP                  V 4      # )z+Score clients for payment delay probability)r   score_collection_riskrW   s   & r:   r   r   L  r   r@   z /regression/predict/sales-targetc                8    \        4       pVP                  V 4      # )u<   Predict attainment % for district×product_head combinations)r   predict_sales_targetrW   s   & r:   r   r   U  s    
 &'J**>::r@   z/regression/modelsc                 6    \        4       p V P                  4       # )z1List all trained regression/classification models)r   rh   rR   s    r:   list_regression_modelsr   ^  s     &'J!!##r@   z/regression/models/<model_id>c                8    \        4       pVP                  V 4      # )z(Get info for a specific regression model)r   rd   re   s   & r:   get_regression_model_infor   f  s     &'J$$X..r@   c                8    \        4       pVP                  V 4      # )zDelete a regression model)r   rk   re   s   & r:   delete_regression_modelr   n  s     &'J""8,,r@     c                 z    \         P                  R \        P                   24       \	        RR/4      pRVn        V# )z404 error for path: r&   zEndpoint not foundr   )r3   rN   r   pathr   r1   r&   r8   s   & r:   	not_foundr   v  s7    
NN)',,89!567HHOr@     c                     \         P                  R \        P                   R\        P                   24       \        RR/4      pRVn        V# )z405 error for  r&   zMethod not allowedr   )r3   rN   r   methodr   r   r1   r   s   & r:   method_not_allowedr   ~  sA    
NN^GNN#31W\\NCD!567HHOr@   c                t    \         P                  R\        V 4       24       \        RRRR/4      pRVn        V# )zECatch any unhandled exceptions in API routes and ensure JSON responsezUnhandled API exception: r&   zInternal server errorr=   zAn unexpected error occurredr(   )r3   r&   r)   r   r1   r   s   & r:   handle_api_exceptionr     sF     LL,SZL9:,5	
H HOr@   N)C__doc__r   flaskr   r   r   app.utils.helpersr   r   app.decorators.validationr   r	   r
   app.schemas.request_schemasr   r   r   r   r   r   r   app.services.cache_servicer   r   app.middleware.auth_middlewarer   "app.controllers.prophet_controllerr   !app.controllers.report_controllerr   %app.controllers.regression_controllerr   __name__api_bpr3   router   r?   rE   rH   rL   require_api_keyrP   rT   rY   r^   rb   rf   ri   rl   ro   rr   ru   r|   r   r   r   r   r   r   r   r   errorhandlerr   r   r2   r    r@   r:   <module>r      s  
  - - 9 V V   D : @ > F	5(	#	E	: i%)+- ,  *-`	 gx( ) #eW5	 6	 #hZ8	 9	 nug.  /  nvh/    ! 0, (5':, ;, 1  "#2 $ ! 22 ,vh?  %&B ' ! @B (6(;  !"B # ! <B *UG<  / ! =/ %1  $ ! 2$ *XJ?  - ! @- !F84  %&4 ' ! 54 !F84  0 ! 50 %1  - ! 2- 3eWE  : ! F: 4ugF  7 ! G7 1F8D  %&< ' ! E< .A  %&9 ' ! B9 1F8D  )*< + ! E< 06(C  (); * ! D; "UG4  $ ! 5$ -w?  / ! @/ -zB  - ! C- S  S  Y
  
r@   