66import os
77import sys
88
9+ def append_to_memory_csv (filename , used , total , current_date , output_dir ):
10+ """Append memory data to a CSV file."""
11+ data = {'Date' : current_date , 'Used (B)' : used , 'Total (B)' : total , 'Usage (%)' : (used / total )* 100 }
12+ df = pd .DataFrame ([data ])
13+
14+ csv_path = os .path .join (output_dir , filename )
15+
16+ if os .path .exists (csv_path ):
17+ print (f"CSV exists at { csv_path } , appending data" )
18+ # Read existing data
19+ existing_df = pd .read_csv (csv_path )
20+ print (f"Existing data:\n { existing_df } " )
21+
22+ # Combine existing and new data
23+ combined_df = pd .concat ([existing_df , df ], ignore_index = True )
24+ print (f"Combined data:\n { combined_df } " )
25+
26+ # Write back the complete dataset
27+ combined_df .to_csv (csv_path , index = False )
28+ else :
29+ print (f"Creating new CSV at { csv_path } " )
30+ df .to_csv (csv_path , index = False )
31+
932def append_to_csv (ram_used , ram_total , flash_used , flash_total , output_dir ):
1033 """Append memory usage data to CSV files with timestamps."""
1134 current_date = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )
1235
1336 # Ensure output directory exists
1437 os .makedirs (output_dir , exist_ok = True )
1538
16- # RAM CSV
17- ram_csv = os .path .join (output_dir , "ram_history.csv" )
18- ram_data = {'Date' : current_date , 'Used (B)' : ram_used , 'Total (B)' : ram_total , 'Usage (%)' : (ram_used / ram_total )* 100 }
19- ram_df = pd .DataFrame ([ram_data ])
20-
21- if os .path .exists (ram_csv ):
22- ram_df .to_csv (ram_csv , mode = 'a' , header = False , index = False )
23- else :
24- ram_df .to_csv (ram_csv , mode = 'w' , header = True , index = False )
25-
26- # Flash CSV
27- flash_csv = os .path .join (output_dir , "flash_history.csv" )
28- flash_data = {'Date' : current_date , 'Used (B)' : flash_used , 'Total (B)' : flash_total , 'Usage (%)' : (flash_used / flash_total )* 100 }
29- flash_df = pd .DataFrame ([flash_data ])
30-
31- if os .path .exists (flash_csv ):
32- flash_df .to_csv (flash_csv , mode = 'a' , header = False , index = False )
33- else :
34- flash_df .to_csv (flash_csv , mode = 'w' , header = True , index = False )
39+ # Append to RAM and Flash CSVs
40+ append_to_memory_csv ("ram_history.csv" , ram_used , ram_total , current_date , output_dir )
41+ append_to_memory_csv ("flash_history.csv" , flash_used , flash_total , current_date , output_dir )
3542
3643def generate_memory_plots (output_dir ):
3744 """Generate HTML plots from the CSV data."""
@@ -41,7 +48,9 @@ def generate_memory_plots(output_dir):
4148 # RAM Plot
4249 ram_csv = os .path .join (output_dir , "ram_history.csv" )
4350 if os .path .exists (ram_csv ):
51+ print (f"Reading RAM CSV for plotting from: { ram_csv } " )
4452 df_ram = pd .read_csv (ram_csv )
53+ print (f"RAM data for plotting:\n { df_ram } " )
4554 df_ram ['Date' ] = pd .to_datetime (df_ram ['Date' ])
4655
4756 # Calculate y-axis range for RAM
0 commit comments