Fix file rotation.

Now it's a method of Data classes
This commit is contained in:
Pierrick C 2018-09-10 09:57:01 +02:00
parent bb2a2f2665
commit af9d40ceb6
2 changed files with 41 additions and 35 deletions

View File

@ -60,6 +60,7 @@ class Data:
self._last_update = 0 self._last_update = 0
self._last_backup = 0 self._last_backup = 0
self._last_send = 0 self._last_send = 0
self._last_file_rotation = time.localtime()
# Check if data directories exists if we need to use them # Check if data directories exists if we need to use them
if self.write_interval >= 0: if self.write_interval >= 0:
@ -139,8 +140,11 @@ class Data:
self.write_interval)) self.write_interval))
if current - self._last_backup >= self.write_interval: if current - self._last_backup >= self.write_interval:
self._last_backup = current self._last_backup = current
#First check if a rotation is needed
self._file_rotation()
file_name = "{}.csv".format(self.name) file_name = "{}.csv".format(self.name)
file_path = "data/" file_path = "data"
try: try:
#Check if the file exists. If not, creates it with CSV header #Check if the file exists. If not, creates it with CSV header
if file_name not in os.listdir(file_path): if file_name not in os.listdir(file_path):
@ -179,6 +183,42 @@ class Data:
print(self.json) print(self.json)
#print("\n") #print("\n")
def _file_rotation(self):
"""Check if files need to rotate
=> every new hour
=> every new day
"""
current_time = time.localtime()
if self.debug:
print(self._last_file_rotation[3])
print(current_time[3])
# If the hour changed : copy current csv to hourly directory
if current_time[3] != self._last_file_rotation[3]:
print("Time to move hourly data !")
os.rename("data/{}.csv".format(self.name),
"data/hourly/{}_{:02}.csv".format(
self.name,
self._last_file_rotation[3]))
# If the day changed : copy content of hourly to daily directories
if current_time[2] != self._last_file_rotation[2]:
print("Time to move daily data !")
# Create new dir for the date of "yesterday"
newdir = "{}{:02}{:02}".format(*self._last_file_rotation[0:3])
if newdir not in os.listdir("data/daily/"):
os.mkdir("data/daily/" + newdir)
# Move each "hourly file" to the new directory
for file in os.listdir('data/hourly'):
# Move only file which name is beginning with self.name
if file.find(self.name) == 0:
print("Move {} to {}".format(file, newdir))
os.rename("data/hourly/{}".format(file), "{}/{}".format(newdir, file))
#Finally : remember last rotation check
self._last_file_rotation = current_time
return
class SysData(Data): class SysData(Data):
"""Subclass for Feather board data""" """Subclass for Feather board data"""
@ -375,36 +415,6 @@ class GPSData(Data):
# Functions # # Functions #
############# #############
def rotate_files(last_time, debug=False):
"""Check if files need to rotate
=> every new hour
=> every new day
"""
current_time = time.localtime()
if debug:
print(last_time[3])
print(current_time[3])
# If the hour changed : copy current data.csv to hourly directory
if current_time[3] != last_time[3]:
print("Time to move hourly data !")
os.rename("data/data.csv", "data/hourly/{:02}.csv".format(last_time[3]))
# If the day changed : copy content of hourly to daily directories
if current_time[2] != last_time[2]:
print("Time to move daily data !")
# Create new dir for the date of yesterday
newdir = "data/daily/{}{:02}{:02}".format(*last_time[0:3])
os.mkdir(newdir)
# Move each "hourly file" to the new directory
for file in os.listdir('data/hourly'):
print("Move {} to {}".format(file, newdir))
os.rename("data/hourly/{}".format(file), "{}/{}".format(newdir, file))
# Finally update last_time, each time
return current_time
def set_clock_from_localtime(clock, treshold=2.0): def set_clock_from_localtime(clock, treshold=2.0):
""" """
Compare internal RTC and date-time from time.localtime() and set Compare internal RTC and date-time from time.localtime() and set

View File

@ -97,7 +97,6 @@ data = [gps_data, sys_data, bme_data]
# Init timers # Init timers
last_update = last_sent_packet = last_written_data = time.monotonic() last_update = last_sent_packet = last_written_data = time.monotonic()
last_rotation = time.localtime()
############# #############
# Main loop # # Main loop #
@ -110,9 +109,6 @@ while True:
for src in data: for src in data:
src.update(current_time, verbose=PRINT_DATA) src.update(current_time, verbose=PRINT_DATA)
# First check if files need to rotate
last_rotation = cameteo.rotate_files(last_rotation, debug=True)
for src in data: for src in data:
src.write_on_flash(current_time) src.write_on_flash(current_time)
src.send_json(current_time, uart=rpi_uart, verbose=PRINT_DATA) src.send_json(current_time, uart=rpi_uart, verbose=PRINT_DATA)