
    %7i                          d Z ddlmZmZ ddlmZmZmZmZmZm	Z	m
Z
mZ ddlmZ  G d de      Z G d de      Z G d	 d
e      Z G d de      Z G d de      Z G d de      Zy)zr
Hyper Forecast Learning System - Database Models
Models for logging predictions and tracking calibration history
    )datetimedate)ColumnIntegerStringDateTimeDateNumericTextJSON)Basec                   `   e Zd ZdZdZ eed      Z eede	j                        Z eedd      Z e edd	      d
      Z e edd	      d
      Z e edd	      d
      Z eedi       Z e edd	      d
      Z e ed      d      Z e ed      d      Z eed
      Zd Zy)ForecastLoga]  
    Logs every prediction made by the Hyper Forecast engine.
    This is the CRITICAL table for the learning system.
    
    Flow:
    1. Engine makes prediction -> saves entry with valor_previsto
    2. Daily job runs at 03:00 -> fills valor_real from actual orders
    3. Weekly job analyzes -> uses erro_percentual to calibrate multipliers
    forecast_logsTprimary_keyFnullabledefaultr   index
      r      2   heuristic_v1r      Nc           	      p    d| j                    d| j                   d| j                   d| j                   d	S )Nz<ForecastLog : z prev=z real=>)id	hora_alvovalor_previsto
valor_realselfs    J/var/www/hypershopcomercio.com.br/hyper-ai/app/models/forecast_learning.py__repr__zForecastLog.__repr__4   s=    twwir$..)9@S@S?TTZ[_[j[jZkklmm    N)__name__
__module____qualname____doc____tablename__r   r   r$   r   r   utcnowtimestamp_previsaor%   r
   r&   r'   erro_percentualr   fatores_usadosbaseline_usador   modelo_versao
calibratedcalibration_impactr+    r,   r*   r   r   
   s     $M	T	*B  5(//R x%t<I GBNU;N A6J WQ]T:O D5"=N GBNT:N6":~>M q	3/Jt4nr,   r   c                   J   e Zd ZdZdZ eed      Z eede	j                        Z e ed      dd      Z e ed	      d
      Z e edd      d
      Z e edd      d
      Z e edd      d
      Z eed
      Z e edd      d
      Z eed
      Zd Zy)CalibrationHistoryz
    Tracks all automatic calibration adjustments made to multipliers.
    Provides audit trail and allows reverting if needed.
    calibration_historyTr   Fr   r   r   d   r   r      r   c           	      p    d| j                    d| j                   d| j                   d| j                   d	S )Nz<CalibrationHistory .r"   z -> r#   )
tipo_fatorfator_chavevalor_anterior
valor_novor(   s    r*   r+   zCalibrationHistory.__repr__^   s@    %doo%6a8H8H7IDL_L_K``deietetduuvwwr,   N)r-   r.   r/   r0   r1   r   r   r$   r   r   r2   data_calibracaor   rB   rC   r
   rD   rE   
erro_medioamostrasajuste_percentualr   notasr+   r:   r,   r*   r<   r<   8   s     *M	T	*B XxOO r
U$?J u5K GAqME:N 16J 16J g.H wq!}t< 4$'Exr,   r<   c                   0   e Zd ZdZdZ eed      Z e ed      dd      Z	 e ed      d	      Z
 e ed
d      dd      Z eed      Z eeej                   ej                         Z e ed      d      Z e ed      d      ZddifZd Zy)MultiplierConfigz
    Stores current multiplier values that can be auto-updated.
    This replaces hardcoded constants with database-driven values.
    multiplier_configTr   r   Fr   r>   r   r   r?         ?r   r   r   onupdate   r   r   r    schemaNc                 V    d| j                    d| j                   d| j                   dS )Nz<MultiplierConfig rA   =r#   )tipochavevalorr(   s    r*   r+   zMultiplierConfig.__repr__   s)    #DII;a

|1TZZLJJr,   )r-   r.   r/   r0   r1   r   r   r$   r   rU   rV   r
   rW   	confiancar   r   r2   atualizado_em	calibradolocked__table_args__r+   r:   r,   r*   rL   rL   b   s     (M	T	*B &*uD9D 6#;/E 71a=5#>E w+I 8X__xWM vbz95I F1Is+F 
4N
Kr,   rL   c                      e Zd ZdZdZ eed      Z eeddd      Z	 eed      Z
 e ed	d
      d      Z e ed	d
      d      Z e edd
      d      Z e edd
      d      Z e edd
      d      Z eei       Z eed      Z eeg       Z e ed      d      Z e ed      d      Z eeej4                        Zd Zy)LearningSnapshotz
    Daily snapshot of learning metrics for historical analysis.
    Created at 23:55 each day to capture the day's performance.
    learning_snapshotsTr   F)r   uniquer   r   r      r   r   r      r>   c                 <    d| j                    d| j                   dS )Nz<LearningSnapshot z: acc=z%>)dataacuraciar(   s    r*   r+   zLearningSnapshot.__repr__   s    #DII;fT]]O2FFr,   N)r-   r.   r/   r0   r1   r   r   r$   r	   rd   total_previsoesr
   rG   erro_absoluto_mediore   receita_prevista_totalreceita_real_totalr   fatores_performanceajustes_realizadosdetalhes_ajustesr   melhor_fator
pior_fatorr   r   r2   
created_atr+   r:   r,   r*   r^   r^      s     )M	T	*B $t4@D Wa0O15J A>gamd3H $GBNA>A: !r2  3dB/ &+5Lsd3J(//:JGr,   r^   c                   v   e Zd ZdZdZ eed      Z e ed      d      Z	 ee
d      Z eedd      Z eed      Z e ed	d
      dd      Z e ed      d      Z e ed      d      Z e ed      d      Z eeej,                        Z eeej,                  ej,                        Zd Zy)ForecastEventu{   
    Special events that affect sales predictions.
    Examples: Black Friday, Natal, Dia das Mães, promoções, etc.
    forecast_eventsTr   r>   Fr   r      r   rN   r   r   manualr   r   r    YrO   c           	      p    d| j                    d| j                   d| j                   d| j                   d	S )Nz<ForecastEvent r"   z - z (zx)>)nomedata_iniciodata_fimmultiplicadorr(   s    r*   r+   zForecastEvent.__repr__   s<     2d.>.>-?s4==/QSTXTfTfSggjkkr,   N)r-   r.   r/   r0   r1   r   r   r$   r   rw   r   	descricaor	   rx   ry   r
   rz   rU   
recorrenteativor   r   r2   ro   
updated_atr+   r:   r,   r*   rq   rq      s     &M	T	*B &+.Dtd+I T:KdU+H 71a=5#FM &*h/D q	3/J 6!9c*E (//:J(//HOOTJlr,   rq   c                       e Zd ZdZdZ eed      Z e ed      dd      Z	 e ed      d      Z
 e ed	      d      Z e ed
      d      Z eeej                        Zd Zy)AllowedFactorzu
    Whitelist for allowed factor keys to prevent garbage data.
    E.g. factor_type='momentum', factor_key='up'
    allowed_factorsTr   r   Fr   r      r   ru   r   c                 <    d| j                    d| j                   dS )Nz<AllowedFactor rA   r#   )factor_type
factor_keyr(   s    r*   r+   zAllowedFactor.__repr__   s#     !1!1 2!DOO3DAFFr,   N)r-   r.   r/   r0   r1   r   r   r$   r   r   r   description	is_activer   r   r2   ro   r+   r:   r,   r*   r   r      sr     &M	T	*Be4@Kr
U3Jt4Kvay#.I(//:JGr,   r   N)r0   r   r   
sqlalchemyr   r   r   r   r	   r
   r   r   app.models.baser   r   r<   rL   r^   rq   r   r:   r,   r*   <module>r      st    $ S S S  +n$ +n\'x 'xT$Kt $KN%Gt %GP"lD "lJGD Gr,   