4
4
import configparser
5
5
import traceback
6
6
import sys
7
+ from datetime import datetime , timedelta
8
+ from typing import Any
7
9
from hardware_testing .scripts import ABRAsairScript # type: ignore
10
+ from abr_testing .automation import google_sheets_tool
8
11
from abr_testing .data_collection import (
9
12
get_run_logs ,
10
13
abr_google_drive ,
13
16
from abr_testing .tools import sync_abr_sheet
14
17
15
18
19
+ def clean_sheet (sheet_name : str , credentials : str ) -> Any :
20
+ """Remove data older than 60 days from sheet."""
21
+ sheet = google_sheets_tool .google_sheet (
22
+ credentials = credentials , file_name = sheet_name , tab_number = 0
23
+ )
24
+ date_columns = sheet .get_column (3 )
25
+ curr_date = datetime .now ()
26
+ cutoff_days = 60 # Cutoff period in days
27
+ cutoff_date = curr_date - timedelta (days = cutoff_days )
28
+
29
+ rem_rows = []
30
+ for row_id , date in enumerate (date_columns ):
31
+ # Convert to datetime if needed
32
+ formatted_date = None
33
+ if isinstance (date , str ): # Assuming dates might be strings
34
+ try :
35
+ formatted_date = datetime .strptime (date , "%m/%d/%Y" )
36
+ except ValueError :
37
+ try :
38
+ formatted_date = datetime .strptime (date , "%Y-%m-%d" )
39
+ except ValueError :
40
+ continue
41
+
42
+ # Check if the date is older than the cutoff
43
+ if formatted_date < cutoff_date :
44
+ rem_rows .append (row_id )
45
+ if len (rem_rows ) > 2000 :
46
+ break
47
+ if len (rem_rows ) == 0 :
48
+ # No more rows to remove
49
+ print ("Nothing to remove" )
50
+ return
51
+ print (f"Rows to be removed: { rem_rows } " )
52
+ try :
53
+ sheet .batch_delete_rows (rem_rows )
54
+ print ("deleted rows" )
55
+ except Exception :
56
+ print ("could not delete rows" )
57
+ traceback .print_exc ()
58
+ sys .exit (1 )
59
+ clean_sheet (sheet_name , credentials )
60
+
61
+
16
62
def run_sync_abr_sheet (
17
63
storage_directory : str , abr_data_sheet : str , room_conditions_sheet : str
18
64
) -> None :
19
65
"""Sync ABR sheet with temp and lifetime percents."""
20
66
sync_abr_sheet .run (storage_directory , abr_data_sheet , room_conditions_sheet )
21
67
22
68
23
- def run_temp_sensor () -> None :
69
+ def run_temp_sensor (ambient_conditions_sheet : str , credentials : str ) -> None :
24
70
"""Run temperature sensors on all robots."""
71
+ # Remove entries > 60 days
72
+ clean_sheet (ambient_conditions_sheet , credentials )
25
73
processes = ABRAsairScript .run ()
26
74
for process in processes :
27
75
process .start ()
@@ -71,34 +119,27 @@ def main(configurations: configparser.ConfigParser) -> None:
71
119
ambient_conditions_sheet = None
72
120
sheet_url = None
73
121
74
- has_defaults = False
75
122
# If default is not specified get all values
76
123
default = configurations ["DEFAULT" ]
77
- if len (default ) > 0 :
78
- has_defaults = True
79
- try :
80
- if has_defaults :
81
- storage_directory = default ["Storage" ]
82
- email = default ["Email" ]
83
- drive_folder = default ["Drive_Folder" ]
84
- sheet_name = default ["Sheet_Name" ]
85
- sheet_url = default ["Sheet_Url" ]
86
- except KeyError as e :
87
- print ("Cannot read config file\n " + str (e ))
124
+ credentials = ""
125
+ if default :
126
+ try :
127
+ credentials = default ["Credentials" ]
128
+ except KeyError as e :
129
+ print ("Cannot read config file\n " + str (e ))
88
130
89
131
# Run Temperature Sensors
90
- if not has_defaults :
91
- ambient_conditions_sheet = configurations ["TEMP-SENSOR" ]["Sheet_Url " ]
132
+ ambient_conditions_sheet = configurations [ "TEMP-SENSOR" ][ "Sheet_Url" ]
133
+ ambient_conditions_sheet_name = configurations ["TEMP-SENSOR" ]["Sheet_Name " ]
92
134
print ("Starting temp sensors..." )
93
- run_temp_sensor ()
135
+ run_temp_sensor (ambient_conditions_sheet_name , credentials )
94
136
print ("Temp Sensors Started" )
95
137
# Get Run Logs and Record
96
- if not has_defaults :
97
- storage_directory = configurations ["RUN-LOG" ]["Storage" ]
98
- email = configurations ["RUN-LOG" ]["Email" ]
99
- drive_folder = configurations ["RUN-LOG" ]["Drive_Folder" ]
100
- sheet_name = configurations ["RUN-LOG" ]["Sheet_Name" ]
101
- sheet_url = configurations ["RUN-LOG" ]["Sheet_Url" ]
138
+ storage_directory = configurations ["RUN-LOG" ]["Storage" ]
139
+ email = configurations ["RUN-LOG" ]["Email" ]
140
+ drive_folder = configurations ["RUN-LOG" ]["Drive_Folder" ]
141
+ sheet_name = configurations ["RUN-LOG" ]["Sheet_Name" ]
142
+ sheet_url = configurations ["RUN-LOG" ]["Sheet_Url" ]
102
143
print (sheet_name )
103
144
if storage_directory and drive_folder and sheet_name and email :
104
145
print ("Retrieving robot run logs..." )
@@ -113,11 +154,10 @@ def main(configurations: configparser.ConfigParser) -> None:
113
154
if storage_directory and sheet_url and ambient_conditions_sheet :
114
155
run_sync_abr_sheet (storage_directory , sheet_url , ambient_conditions_sheet )
115
156
# Collect calibration data
116
- if not has_defaults :
117
- storage_directory = configurations ["CALIBRATION" ]["Storage" ]
118
- email = configurations ["CALIBRATION" ]["Email" ]
119
- drive_folder = configurations ["CALIBRATION" ]["Drive_Folder" ]
120
- sheet_name = configurations ["CALIBRATION" ]["Sheet_Name" ]
157
+ storage_directory = configurations ["CALIBRATION" ]["Storage" ]
158
+ email = configurations ["CALIBRATION" ]["Email" ]
159
+ drive_folder = configurations ["CALIBRATION" ]["Drive_Folder" ]
160
+ sheet_name = configurations ["CALIBRATION" ]["Sheet_Name" ]
121
161
if storage_directory and drive_folder and sheet_name and email :
122
162
print ("Retrieving and recording robot calibration data..." )
123
163
get_calibration_data (storage_directory , drive_folder , sheet_name , email )
0 commit comments