
    E(i&                         d dl Z d dlZd dlmZ d dlmZ d dlmZmZmZ da	de
fdZddee
   fd	Zddee
   fd
Zddee
   fdZddeeef   dee
   dee
   fdZddee
   dee
   fdZddee
   fdZddedee
   fdZddee
   dee
   fdZy)    N)datetime)Path)DictAnyOptionalz/var/www/html/btcusdtnew_dirc                     | a y)z"Set the data directory dynamicallyN)DATA_DIR)r   s    */var/www/html/crypto/logger_daily_multi.pyset_data_dirr      s	     H    symbolc                     t        j                         j                  d      }| r| j                          d| dS d| dS )z"Generate filename for today's data%Y-%m-%d_.jsonbtcusdt_)r   utcnowstrftimelower)r   todays     r   get_daily_filenamer      sE    OO&&z2E,,.!5'//%&&r   c                 `    t        |       }t        j                  j                  t        |      S )z#Get full path for today's data file)r   ospathjoinr
   )r   filenames     r   get_daily_filepathr      s!    !&)H77<<(++r   	directoryc                     | xs t         }t        |      j                  dd       	 t        j                  |d       y# t
        $ r}t        d|        Y d}~yd}~ww xY w)z)Create data directory if it doesn't existT)parentsexist_oki  u-   ⚠️  Could not set directory permissions: N)r
   r   mkdirr   chmod	Exceptionprint)r   dir_pathes      r   ensure_data_directoryr)   #   s\    $HHN5C
5! C=aSABBCs   ? 	A AA recordc           	         |xs t         }t        |       t        j                         j	                  d      }|r|j                          d| d}nd| d}t        j                  j                  ||      }	 t        j                  j                  |      rL	 t        |dd      5 }t        j                  |      }ddd       t        t              st        d	       |g}ng }t        d|        |j+                  |        |dz   }t        |dd      5 }t        j,                  ||dd       ddd       t        j.                  ||       	 t        j0                  |d       t3        |      }|dz  dk(  r9t        j                  j5                  |      dz  }t        d| d| d|dd       yy# 1 sw Y   xY w# t        j                   $ r}	t        d
|	        t        d       |dt#        t        j                         j%                                z   }
	 t        j&                  ||
       t        d|
        n$# t(        $ r}t        d|        Y d}~nd}~ww xY wg }Y d}	~	{d}	~	ww xY w# 1 sw Y   GxY w# t(        $ r Y )w xY w# t(        $ r}	t        d|	        	 |dt#        t        j                         j%                                dz   }t        |dd      5 }t        j,                  | g|dd       ddd       n# 1 sw Y   nxY wt        d |        n)# t(        $ r}t        d!|        Y d}~nd}~ww xY wY d}	~	yY d}	~	yd}	~	ww xY w)"a*  
    Append a data record to today's JSON file
    
    IMPORTANT: This function APPENDS to existing file, never overwrites!
    
    Args:
        record (dict): Data record to append
        symbol (str): Optional symbol name for filename
        directory (str): Optional directory override
    r   r   r   r   rutf-8encodingNu;   ⚠️  Warning: Existing file is not a list, converting...u!   ❌ Error reading existing file: u-   ⚠️  Creating backup and starting fresh...z.backup.u"   💾 Backed up corrupted file to: u   ❌ Could not backup: u   ✅ Creating new daily log: z.tmpw   F)indentensure_asciii  d   r      u   📁 z: z records | z.2f MBu"   ❌ Error appending to daily log: z.emergency.u    💾 Saved to emergency backup: u"   ❌ Emergency backup also failed: )r
   r)   r   r   r   r   r   r   r   existsopenjsonload
isinstancelistr&   JSONDecodeErrorint	timestamprenamer%   appenddumpreplacer$   lengetsize)r*   r   r   r'   r   r   filepathfdatar(   backup_pathbetemp_filepathrecord_count	file_sizeemergency_filepathe2s                    r   append_daily_logrP   /   s8    $HH (# OO&&z2Elln%QugU3eWE*ww||Hh/HB=77>>(#(C': (a99Q<D( "$-WY 6D$ D0
;< 	F !6)-w7 	=1IIdAae<	= 	

=(+	HHXu%
 4y#"1[AIE(2l^;yoSQR #_( ( '' 9!=>EG '8C8I8S8S8U4V3W)XX9IIh4>{mLM  922$7889 2	= 	=  		  
=21#67	=!)k#hoo>O>Y>Y>[:\9]]b,c!c(#@ EA		6(AaeDE E E45G4HIJ 	=6rd;<<	=< K
=s  9J G  'F4=&G  #5J I72J J 'AJ 4F=9G   I4A
I/$II/	I$II/I$$I/)J /I44J 7J<J 	JJ JJ 	MM,A L+,L	L+L	L+*M+	M4MMMMMc                    |xs t         }t        j                         j                  d      }| r| j	                          d| d}nd| d}t
        j                  j                  ||      }t
        j                  j                  |      sddd|dS 	 t        |dd	
      5 }t        j                  |      }ddd       t        t              rt        |      nd}t
        j                  j                  |      dz  }	d|t!        |	d      ||dS # 1 sw Y   ZxY w# t"        $ r}
dt%        |
      |dcY d}
~
S d}
~
ww xY w)z%Get statistics about today's log filer   r   r   r   Fr   )r7   recordssize_mbr   r,   r-   r.   N   r5   Tr1   )r7   rR   rS   r   rF   )r7   errorr   )r
   r   r   r   r   r   r   r   r7   r8   r9   r:   r;   r<   rD   rE   roundr%   str)r   r   r'   r   r   rF   rG   rH   rL   rM   r(   s              r   get_log_statsrX      s5    $HHOO&&z2Elln%QugU3eWE*ww||Hh/H77>>(# 	
 	

(C'2 	 a99Q<D	  %/tT$:s4yGGOOH-=	 #Y*  
 	
	  	   
V 
 	

s7   D "D8AD DD 	D?&D:4D?:D?c                    | xs t         }t        |       g }	 t        j                  j	                  |      s|S t        j
                  |      }t        |d      D ]  }|j                  d      s|j                  d      r't        j                  j                  ||      }t        j                  j                  |      dz  }	 t        |dd      5 }t        j                  |      }d	d	d	       t        t              rt        |      nd
}	|j!                  |t#        |d      |	d        	 |S # 1 sw Y   JxY w#  d}	Y 6xY w# t$        $ r}
t'        d|
        Y d	}
~
|S d	}
~
ww xY w)zList all available log filesT)reverser   z	.tmp.jsonr5   r,   r-   r.   NrT   unknownr1   )r   rS   rR   u   ❌ Error listing logs: )r
   r)   r   r   r7   listdirsortedendswithr   rE   r8   r9   r:   r;   r<   rD   rA   rV   r%   r&   )r   r'   logsfilesr   rF   rM   rG   rH   rL   r(   s              r   list_all_logsra      sF    $HH(#D.ww~~h'K

8$ud3 	H  )(2C2CK2P77<<(;GGOOH5E	-hg> ,!#yy|,0:40F3t9AL  ($Y2+ 	* K, ,-#,L  .(,--K.sY    E 6E 1E AE D?D3*%D?!E 3D<	8D??EE 	E*E%%E*	keep_daysc                 V   ddl }|xs t        }t        |       t        j                  j                  |      sg S |j                         }|| dz  dz  dz  z
  }g }	 t        j                  |      }|D ]  }|j                  d      st        j                  j                  ||      }	t        j                  j                  |	      }
|
|k  sZt        j                  |	       |j                  |       t        d|         	 |S # t        $ r}t        d|        Y d}~|S d}~ww xY w)z
    Delete log files older than keep_days
    
    Args:
        keep_days (int): Number of days to keep (default: 30)
        directory (str): Optional directory override
    r   N   <   r   u   🗑️  Deleted old log: u   ❌ Error cleaning up logs: )timer
   r)   r   r   r7   r\   r^   r   getmtimeremoverA   r&   r%   )rb   r   rf   r'   current_timecutoff_timedeletedr`   r   rF   
file_mtimer(   s               r   cleanup_old_logsrm      s    $HH(#77>>(#	99;L)b.2"5":;KG2

8$ 	CH  )77<<(;WW--h7
+IIh'NN8,6xjAB	C N  2,QC011N2s%   +D AD 6D 	D(D##D(c                 J   t        | |      }|d   rt        d       t        d       | rt        d|         t        d       t        d|d           t        d|d	   d
       t        d|d    d       |d	   dkD  rddl}t        j                         }|j                  dddd      }||z
  j                         }t        |dz        }|dkD  r|d	   |z  dz  nd}t        d|d
d       t        d|dd       t        d       y| xs d}	t        d|d    d       y)z(Print summary of today's data collectionr7   z=
============================================================u$   📊 Today's Data Collection SummaryzSymbol: z<============================================================zFile: r   z	Records: rR   ,zSize: rS   r6   r   N)hourminutesecondmicrosecondr1   r4   zExpected: ~z recordszCompletion: z.1f%z=============================================================
BTCUSDTu#   
📋 No data collected today yet: 
)rX   r&   rf   r   r   rC   total_secondsr>   )
r   r   statsrf   nowstart_of_dayseconds_elapsedexpected_records
completionsymbol_names
             r   print_daily_summaryr   	  sL    &),EXo46HVH%&huZ()*+	%	*1-./uY'(,-a//#C;;Aaq;QL"\1@@BO"?Q#67HX[\H\%	*-==CbcJK 038<=LC 023o)	4U:5F4GrJKr   )N)NN)   N)r9   r   r   pathlibr   typingr   r   r   r
   rW   r   r   r   r)   rP   rX   ra   r>   rm   r    r   r   <module>r      s     	   & & ## 'x} ',x} ,	CXc] 	Ca=T#s(^ a=Xc] a=V^_bVc a=H(
(3- (
8C= (
V"Xc] "J# #Xc] #LL L# Lr   