
    Iyj&                         d dl Z d dlZd dlZd dlZd dlZd dlZd dl	Z
d dlZd dlmZmZ d dlmZ d dlmZ  e       Zej)                         Z G d d      Zy)    N)datetimedate)DatetimeTickFormatter)MonitoringAppTextsc                   D    e Zd Zd Zd Zd Zd Zd Zd ZddZ	d Z
d	 Zy
)MonitoringAppMassc                     t        j                  d       t        j                  d       dddd d d| _        dddd d d| _        | j	                          | j                          | j                          y )N
floatpanel	tabulatorgQX@g33333X@gZd;?)pdryinimean_psmean_pwqnegqsatg{Gz?)pn	extensionmeandelta	load_datacreate_widgetscreate_layoutselfs    5/home/carlos/GitHub/SMNAMonitoringApp/monitor_mass.py__init__zMonitoringAppMass.__init__)   sf    
\"
[! !'6eUYcgh	!%$5RV`de
    c                    t        j                  |      | _        |j                  d      d   | _        t
        j                  j                  t        j                         | j                        | _	        t        | j                  d      5 }|j                  | j                  j                         d d d        y # 1 sw Y   y xY w)N/wb)requestsgetrsplitfilenameospathjoingetcwdfullnameopenwritecontent)r   r'   fs      r   download_filezMonitoringAppMass.download_file6   s    d#

3+RYY[$--@$--& 	$!GGDFFNN#	$ 	$ 	$s   &B::Cc                 x   d}	 t        j                  |dd      }|j                  dk\  r]t        d| d|j                   d       t	        j
                         | _        t	        j
                         | _        d | _        d | _	        y t        d	|        	 	 | j                  |       t        j                  | j                        }t	        j                  d|dgd      j!                  dt"        j$                        | _        t	        j                  d|dgd      j!                  dt"        j$                        | _        | j                  j&                  s2| j                  j(                  j+                         j-                         nd | _        | j                  j&                  s8| j                  j(                  j/                         j-                         | _	        y d | _	        y # t         j                  $ r[}t        d
| d|        t	        j
                         | _        t	        j
                         | _        d | _        d | _	        Y d }~y d }~ww xY w# t0        $ rX}t        d|        t	        j
                         | _        t	        j
                         | _        d | _        d | _	        Y d }~y d }~ww xY w)Nzlhttps://dataserver.cptec.inpe.br/dataserver_dimnt/das/carlos.bastarz/SMNAMonitoringApp/mass/costFile_Oper.dbT   )allow_redirectstimeouti  u5   ❌ [MASS CONSTRAINS PLOTS] Arquivo não encontrado: z	 (status )u0   ✅ [MASS CONSTRAINS PLOTS] Arquivo acessível: u/   ⚠️ [MASS CONSTRAINS PLOTS] Erro ao acessar z: z$SELECT * FROM costCons ORDER BY dater   )parse_dates	index_colg*z$SELECT * FROM costFunc ORDER BY dateu!   ❌ Erro ao processar o arquivo: )r!   headstatus_codeprintpd	DataFramedfdcmin_datemax_dateRequestExceptionr/   sqlite3connectr%   read_sql_queryreplacenpnanemptyindexminr   max	Exception)r   url_dbresponseecons        r   r   zMonitoringAppMass.load_data=   s!   	}}VT1MH##s*MfXU^_g_s_s^ttuvw,,.,,. $ $HQR	!v&//$--0C''6#H 	
 geRVV$ G ''6#H 	
 geRVV$ G ?CggmmDGGMM--/446QUDM>BggmmDGGMM--/446DMQUDM9 (( 	CF82aSQRllnDGllnDG DM DM	<  	!5aS9:llnDGllnDG DM DMM	!s?   BG' G' EI I 'I:AII	J9!AJ44J9c                    | j                   j                  s| j                   j                  j                  j	                         j                         }d| j                   j                  v r+| j                   d   j	                         j                         ng }| j                   j                  dd  j                         }ng }g }g }| j                  j                  sLt        | j                  j                        dkD  r'| j                  j                  j                         dd  ng }ng }t        j                  j                  d|      | _        t        j                  j                  d|d      | _        t        j                  j                  d	|d      | _        t        j                  j!                  d
d      | _        t        j                  j                  d|d      | _        | j&                  r| j(                  rt        j                  j+                  d| j&                  | j(                  t-        | j&                  j.                  | j&                  j0                  | j&                  j2                  dd            | _        n&t        j                  j+                  dd      | _        | j                   j                  sot        j                  j7                  d| j                   j                  j9                         | j                   j                  j;                         fdd      | _        y t        j                  j7                  ddd      | _        y )Nouter      z
Hour Cycle)nameoptionsz
Outer Loop   )rT   rU   width	VariablesMean)rT   rW   zColumn to PlotzSelecione uma Data e Horar   )rT   startendvalueT)rT   disabledz
Date RangeF)rT   r\   enable_timerW   )rT   r]   rW   )r<   rG   rH   houruniquetolistcolumnsr=   lenr   widgetsRadioBoxGroupHourSelectOuterVarsCheckboxuse_meancolumn_namer>   r?   DatetimePickerr   yearmonthdaydate_time_pickerDatetimeRangePickerrI   rJ   date_range_slider)r   hoursouters	variablescolumn_optionss        r   r   z MonitoringAppMass.create_widgetsr   st   ww}}GGMM&&--/668E;Bdggoo;UTWWW%,,.557[]F+224IEFIww}}=@=QTU=UTWW__335ab9[]NNJJ,,,,N	ZZ&&L&PS&T
JJ%%;	QT%U	

++s+C::,,2BNbe,f==T]]$&JJ$=$=0mmMMt}}114==3F3FHYHY[\^_`	 %> %D! %'JJ$=$=C^im$=$nD!ww}}%'ZZ%C%C!ww}}((*DGGMM,=,=,?@!	 &D &D" &(ZZ%C%C! &D &D"r   c                    t        j                  t        j                  | j                  | j                  | j
                  | j                  | j                  j                  j                        d      | _
        t        j                  t        j                  | j                  t         j                  j                  dd            t        j                  | j                  t         j                  j                  dd            t        j                  | j
                  t         j                  j                  dd            t        j                  | j                  t         j                  j                  dd                  }t        j                  |      }t         j                  j                  d	d
      | _        | j                   j"                  j%                  |       y )Ni   )heightzChoose a date rangerZ   )r\   alignzChoose a variablezChoose the outerloopzWhether to use the meanzSMNA DashboardSMNAMonitoringApp)sitetitle)r   ColumnbindplotMassFigri   rh   rk   rs   paramr\   tab1Rowrd   TooltipIcontemplateFastListTemplateappsidebarappend)r   sidebar1cols      r   r   zMonitoringAppMass.create_layout   sL   IIGGD$$diiT]]DLbLbLhLhLnLno	
 99FF4))2::+A+AH]el+A+mnFF499bjj44;NV]4^_FF4::rzz55<RZa5bcFF4=="**"8"8?X`g"8"hi	
 ii! ;;//!% 0 
 	$r   c                    | j                   j                  s|| j                   j                  vr!t        j                  j                  dd      S g }dgt        t        j                  ddd            z   D ]  }d| j                   j                  v rD| j                   j                  d|       }||j                  j                  t        |      k(     }n| j                   j                         }|r'|\  }}	||j                  |k\  |j                  |	k  z     }|rG|j                  d	      j                         }
|
j!                  d
|| d      j#                  d      }n'|j!                  d
|| d      j#                  d      }|| j                  v rS| j                  |   Ct%        j&                  | j                  |         }t%        j&                  | j                  |   | j(                  |   z         }t%        j&                  | j                  |   | j(                  |   z
        }t%        j*                  | j                  |   | j(                  |   z   | j                  |   | j(                  |   z
        }|j-                  ddd       |j-                  ddd       |j-                  ddd       |j-                  d       |j/                  |||z  |z  z  |z         u|j/                  |        t%        j0                  |      j3                  d      }|j-                  d| d| ddi       |S )Nu*   ❌ Nenhum dado disponível para plotagem.X  rW   0      rQ   z	outer == MEr   z UTC)r}   x)	autorangereddashedg      ?)color	line_dash
line_widthblue	lightgray)r   rR   stretch_bothz: OuterLoop r}   15pt)sizing_moder}   fontsize)r<   rG   rb   r   paneMarkdownlistrE   arangequeryrH   r_   intcopyresampler   hvplotrU   hvHLiner   HSpanoptsr   Layoutcols)r   varrQ   rk   
date_rangeplotsr_   df_filtered
start_dateend_datedf_resampledpl1l2l3splayouts                    r   r   zMonitoringAppMass.plotMassFig   s   77==Ctww677##$PX[#\\ED1b!!455 	 D$''//)"ggmmiw,?@)+*;*;*@*@CI*MN"gglln'1$
H);+<+<
+J{O`O`dlOl*mn*33D9>>@ ''dV4='IQQ\_Q`&&vsTF$-&HPP[^P_ diiDIIcN$>XXdiin-XXdiintzz#>?XXdiintzz#>?XXdiintzz#>		#QUQ[Q[\_Q`@`aexCHfSIfSIk*R27R<0145Q=	 @ 5!&&q)\%6Q]dfl\mnr   c                 P   | j                   j                  s|| j                   j                  vr!t        j                  j                  dd      S | j                   | j                   j                  |k(     }|j                  r%t        j                  j                  d| dd      S y )Nu7   ❌ Nenhum dado disponível para plotagem desta coluna.r   r   u
   ❌ Data 'u   ' não encontrada no DataFrame.)r=   rG   rb   r   r   r   rH   )r   rl   	date_timenum_last_datesnum_last_daysdata_subsets         r   plot_columnzMonitoringAppMass.plot_column   s    77==Ktww>77##$]eh#iiggdggmmy8977##j;Z$[cf#gg r   c                     t        j                  t        j                  | j                  j                  d         dd      S )Nr   
ParametersF)r}   	collapsed)r   Cardr~   r   r   r   s    r   LayoutSidebarzMonitoringAppMass.LayoutSidebar   s.    wwryy!1!1!!45\UZ[[r   c                 z    t        j                  d      }t        j                  || j                  t        d      S )Nzg
        # Mass Constrains Plots

        Set the parameters on the left to update the curves.
        stretch_width)r   )r   r~   r   monitor_warning_bottom_main)r   	main_texts     r   
LayoutMainzMonitoringAppMass.LayoutMain   s2    II  	
 yyDII/JXghhr   N)      )__name__
__module____qualname__r   r/   r   r   r   r   r   r   r    r   r   r   r   (   s5    
$2!j,\%4'Th\ir   r   )r&   r!   rA   numpyrE   pandasr:   panelr   	holoviewsr   hvplot.pandasr   r   r   bokeh.models.formattersr   monitor_textsr   monitor_app_textswarningsr   r   r   r   r   <module>r      sN   0 
        # 9 ,&( /88: Mi Mir   