Skip to content

Commit 73340e2

Browse files
feat(abr_testing): remove temp data > 60 days (#17022)
# Overview Before running temperature sensors, all data older than 60 days in the abr ambient temperature sheet is removed
1 parent 873e375 commit 73340e2

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed

abr-testing/abr_testing/tools/abr_setup.py

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import configparser
55
import traceback
66
import sys
7+
from datetime import datetime, timedelta
8+
from typing import Any
79
from hardware_testing.scripts import ABRAsairScript # type: ignore
10+
from abr_testing.automation import google_sheets_tool
811
from abr_testing.data_collection import (
912
get_run_logs,
1013
abr_google_drive,
@@ -13,15 +16,60 @@
1316
from abr_testing.tools import sync_abr_sheet
1417

1518

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+
1662
def run_sync_abr_sheet(
1763
storage_directory: str, abr_data_sheet: str, room_conditions_sheet: str
1864
) -> None:
1965
"""Sync ABR sheet with temp and lifetime percents."""
2066
sync_abr_sheet.run(storage_directory, abr_data_sheet, room_conditions_sheet)
2167

2268

23-
def run_temp_sensor() -> None:
69+
def run_temp_sensor(ambient_conditions_sheet: str, credentials: str) -> None:
2470
"""Run temperature sensors on all robots."""
71+
# Remove entries > 60 days
72+
clean_sheet(ambient_conditions_sheet, credentials)
2573
processes = ABRAsairScript.run()
2674
for process in processes:
2775
process.start()
@@ -71,34 +119,27 @@ def main(configurations: configparser.ConfigParser) -> None:
71119
ambient_conditions_sheet = None
72120
sheet_url = None
73121

74-
has_defaults = False
75122
# If default is not specified get all values
76123
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))
88130

89131
# 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"]
92134
print("Starting temp sensors...")
93-
run_temp_sensor()
135+
run_temp_sensor(ambient_conditions_sheet_name, credentials)
94136
print("Temp Sensors Started")
95137
# 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"]
102143
print(sheet_name)
103144
if storage_directory and drive_folder and sheet_name and email:
104145
print("Retrieving robot run logs...")
@@ -113,11 +154,10 @@ def main(configurations: configparser.ConfigParser) -> None:
113154
if storage_directory and sheet_url and ambient_conditions_sheet:
114155
run_sync_abr_sheet(storage_directory, sheet_url, ambient_conditions_sheet)
115156
# 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"]
121161
if storage_directory and drive_folder and sheet_name and email:
122162
print("Retrieving and recording robot calibration data...")
123163
get_calibration_data(storage_directory, drive_folder, sheet_name, email)

0 commit comments

Comments
 (0)