diff --git a/abr-testing/abr_testing/automation/google_sheets_tool.py b/abr-testing/abr_testing/automation/google_sheets_tool.py index 7e625b0f37a..0a2e2bdfeb5 100644 --- a/abr-testing/abr_testing/automation/google_sheets_tool.py +++ b/abr-testing/abr_testing/automation/google_sheets_tool.py @@ -87,6 +87,25 @@ def delete_row(self, row_index: int) -> None: """Delete Row from google sheet.""" self.worksheet.delete_rows(row_index) + def batch_delete_rows(self, row_indices: List[int]) -> None: + """Batch delete rows in list of indices.""" + delete_body = { + "requests": [ + { + "deleteDimension": { + "range": { + "sheetId": 0, + "dimension": "ROWS", + "startIndex": index, + "endIndex": index + 1, + } + } + } + for index in row_indices + ] + } + self.spread_sheet.batch_update(body=delete_body) + def update_cell( self, row: int, column: int, single_data: Any ) -> Tuple[int, int, Any]: @@ -94,7 +113,7 @@ def update_cell( self.worksheet.update_cell(row, column, single_data) return row, column, single_data - def get_all_data(self) -> Dict[str, Any]: + def get_all_data(self) -> List[Dict[str, Any]]: """Get all the Data recorded from worksheet.""" return self.worksheet.get_all_records() @@ -141,36 +160,45 @@ def get_row_index_with_value(self, some_string: str, col_num: int) -> Any: print("Row not found.") return None return row_index - - def create__line_chart(self, titles: List[str], series: List[Dict[str, Any]], spreadsheet_id: str): + + def create_line_chart( + self, + titles: List[str], + series: List[Dict[str, Any]], + domains: List[Dict[str, Any]], + ) -> None: """Create chart of data on google sheet.""" - chart_data = { + request_body = { + "requests": [ + { + "addChart": { "chart": { - "spec": { - "title": titles[0], - "basicChart": { - "chartType": "LINE", - "legendPosition": "BOTTOM_LEGEND", - "axis": [ - { - "position": "BOTTOM_AXIS", - "title": titles[1] + "spec": { + "title": titles[0], + "basicChart": { + "chartType": "LINE", + "legendPosition": "RIGHT_LEGEND", + "axis": [ + {"position": "BOTTOM_AXIS", "title": titles[1]}, + {"position": "LEFT_AXIS", "title": titles[2]}, + ], + "domains": domains, + "series": series, + "headerCount": 1, }, - { - "position": "LEFT_AXIS", - "title": titles[2] + }, + "position": { + "overlayPosition": { + "anchorCell": { + "sheetId": 0, + "rowIndex": 1, + "columnIndex": 1, + } } - ], - "ranges": [ - series - ], - "headerCount": 1 - } - }, - "position": { - "newSheet": True - } + }, } } - body = {"requests": [{"addChart": {"chart": chart_data}}]} - self.batchUpdate(spreadsheet_id, body = body).execute() + } + ] + } + self.spread_sheet.batch_update(body=request_body) diff --git a/abr-testing/abr_testing/data_collection/abr_lpc.py b/abr-testing/abr_testing/data_collection/abr_lpc.py index 3d1a90580a4..83e343a1d25 100644 --- a/abr-testing/abr_testing/data_collection/abr_lpc.py +++ b/abr-testing/abr_testing/data_collection/abr_lpc.py @@ -1,28 +1,36 @@ """Get Unique LPC Values from Run logs.""" import os import argparse -from typing import Any, Dict, List from abr_testing.automation import google_sheets_tool import sys -# TODO: Remove duplicate rows -def identify_duplicate_data(all_data): + +def remove_duplicate_data() -> None: """Determine unique sets of data.""" seen = set() new_values = [] - for row in all_data: - key = (row["Robot"], row["Errors"], row["Slot"], row["Module"], row["Adapter"], row["X"], row["Y"], row["Z"]) + row_indices = [] + sheet_data = google_sheet_lpc.get_all_data() + for i, row in enumerate(sheet_data): + key = ( + row["Robot"], + row["Software Version"], + row["Errors"], + row["Slot"], + row["Module"], + row["Adapter"], + row["X"], + row["Y"], + row["Z"], + ) + if key not in seen: seen.add(key) new_values.append(row) - return new_values - -def update_sheet_with_new_values(new_values): - """Update sheet with unique data sets only.""" - google_sheet_lpc.clear() - headers = list(new_values[0].keys()) - data = [headers] + [[row[col] for col in headers] for row in new_values] - google_sheet_lpc.update(data) + else: + row_indices.append(i) + if len(row_indices) > 0: + google_sheet_lpc.batch_delete_rows(row_indices) if __name__ == "__main__": @@ -42,24 +50,7 @@ def update_sheet_with_new_values(new_values): print(f"Add credentials.json file to: {storage_directory}.") sys.exit() google_sheet_lpc = google_sheets_tool.google_sheet(credentials_path, "ABR-LPC", 0) - sheet_data = google_sheet_lpc.get_all_data() - print(len(sheet_data)) - new_values = identify_duplicate_data(sheet_data) - print(len(new_values)) - update_sheet_with_new_values(new_values) - - num_of_rows = len(google_sheet_lpc.get_all_data()) - # Create graph - graph_title = "ABR LPC" - x_axis_title = "X Offset (mm)" - y_axis_title = "Y Offset (mm)" - titles = [graph_title, x_axis_title, y_axis_title] - series = [ - {"sheetId": 0, - "startRowIndex": 0, - "endRowIndex": num_of_rows, - "startColumnIndex": 29, - "endColumnIndex": 30} - ] - spreadsheet_id = "1m9c3Ql2Uez4MC_aLayeUX6YO7WMkNA-4B5xk_w8zJc4" - google_sheet_lpc.create_line_chart(titles, series, spreadsheet_id) + print(len(google_sheet_lpc.get_all_data())) + remove_duplicate_data() + num_of_rows = print(len(google_sheet_lpc.get_all_data())) + # TODO: automate data analysis