quick fix timestamp
Browse files- app/sheets.py +26 -13
app/sheets.py
CHANGED
|
@@ -38,6 +38,22 @@ def _flatten_and_unique_timestamps(items: Any) -> List[Any]:
|
|
| 38 |
flat_list.append(item)
|
| 39 |
return list(dict.fromkeys(flat_list))
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
class SheetsClient:
|
| 43 |
def __init__(self, credentials_file: str, token_file: str, sheet_id: str):
|
|
@@ -89,10 +105,8 @@ class SheetsClient:
|
|
| 89 |
|
| 90 |
logger.debug(f"[get_conversation_history] Đã lấy được {len(values)} dòng từ sheet. Bắt đầu xử lý...")
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# start=2 giả định rằng SHEET_RANGE bắt đầu từ dòng 2
|
| 95 |
-
for i, row in enumerate(values, start=2):
|
| 96 |
if len(row) < 14:
|
| 97 |
row.extend([""] * (14 - len(row)))
|
| 98 |
|
|
@@ -144,9 +158,6 @@ class SheetsClient:
|
|
| 144 |
if not self.service:
|
| 145 |
self.authenticate()
|
| 146 |
|
| 147 |
-
# --- THAY ĐỔI LOGIC ĐỂ HOẠT ĐỘNG KHI SHEET_RANGE KHÔNG CÓ HEADER ---
|
| 148 |
-
# 1. Lấy header từ dòng 1 một cách tường minh
|
| 149 |
-
# Tự động xác định tên sheet từ SHEET_RANGE
|
| 150 |
sheet_name_match = re.match(r"([^!]+)!", SHEET_RANGE)
|
| 151 |
sheet_name = sheet_name_match.group(1) if sheet_name_match else "Sheet1"
|
| 152 |
header_range = f"{sheet_name}!A1:Z1"
|
|
@@ -157,23 +168,25 @@ class SheetsClient:
|
|
| 157 |
logger.error(f"Không thể lấy được header từ range '{header_range}'.")
|
| 158 |
return None
|
| 159 |
|
| 160 |
-
# 2. Lấy dữ liệu từ SHEET_RANGE (không có header)
|
| 161 |
data_result = self.service.spreadsheets().values().get(spreadsheetId=self.sheet_id, range=SHEET_RANGE).execute()
|
| 162 |
values = data_result.get('values', [])
|
| 163 |
|
| 164 |
conversation_id = kwargs.get('conversation_id')
|
| 165 |
row_to_update_index = -1
|
| 166 |
|
| 167 |
-
# Nếu có conversation_id, tìm dòng để cập nhật
|
| 168 |
if conversation_id:
|
|
|
|
| 169 |
try:
|
| 170 |
id_col_index = header.index('conversation_id')
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
for i, row in enumerate(values, start=
|
| 174 |
if len(row) > id_col_index and row[id_col_index] == conversation_id:
|
| 175 |
row_to_update_index = i
|
|
|
|
| 176 |
break
|
|
|
|
|
|
|
| 177 |
except ValueError:
|
| 178 |
logger.error("Không tìm thấy cột 'conversation_id' trong header của sheet.")
|
| 179 |
return None
|
|
@@ -192,7 +205,7 @@ class SheetsClient:
|
|
| 192 |
|
| 193 |
if row_to_update_index != -1:
|
| 194 |
logger.info(f"Đang cập nhật conversation_id {conversation_id} tại dòng {row_to_update_index}")
|
| 195 |
-
range_to_update = f"A{row_to_update_index}"
|
| 196 |
body = {'values': [row_data]}
|
| 197 |
self.service.spreadsheets().values().update(
|
| 198 |
spreadsheetId=self.sheet_id,
|
|
|
|
| 38 |
flat_list.append(item)
|
| 39 |
return list(dict.fromkeys(flat_list))
|
| 40 |
|
| 41 |
+
def _get_start_row_from_range(range_string: str) -> int:
|
| 42 |
+
"""
|
| 43 |
+
Phân tích một chuỗi range (ví dụ: 'Sheet1!A2:Z') để lấy ra số của dòng bắt đầu.
|
| 44 |
+
Hàm này giúp code hoạt động chính xác ngay cả khi range không bắt đầu từ dòng 2.
|
| 45 |
+
"""
|
| 46 |
+
# Tìm số đầu tiên xuất hiện sau một chữ cái trong chuỗi range
|
| 47 |
+
match = re.search(r"[A-Z]+([0-9]+)", range_string)
|
| 48 |
+
if match:
|
| 49 |
+
try:
|
| 50 |
+
return int(match.group(1))
|
| 51 |
+
except (ValueError, IndexError):
|
| 52 |
+
pass # Bỏ qua nếu không chuyển đổi được và dùng giá trị mặc định
|
| 53 |
+
|
| 54 |
+
logger.warning(f"Không thể xác định dòng bắt đầu từ range '{range_string}'. Mặc định là 2.")
|
| 55 |
+
return 2 # Mặc định là 2 nếu không phân tích được
|
| 56 |
+
|
| 57 |
|
| 58 |
class SheetsClient:
|
| 59 |
def __init__(self, credentials_file: str, token_file: str, sheet_id: str):
|
|
|
|
| 105 |
|
| 106 |
logger.debug(f"[get_conversation_history] Đã lấy được {len(values)} dòng từ sheet. Bắt đầu xử lý...")
|
| 107 |
|
| 108 |
+
start_row = _get_start_row_from_range(range_name)
|
| 109 |
+
for i, row in enumerate(values, start=start_row):
|
|
|
|
|
|
|
| 110 |
if len(row) < 14:
|
| 111 |
row.extend([""] * (14 - len(row)))
|
| 112 |
|
|
|
|
| 158 |
if not self.service:
|
| 159 |
self.authenticate()
|
| 160 |
|
|
|
|
|
|
|
|
|
|
| 161 |
sheet_name_match = re.match(r"([^!]+)!", SHEET_RANGE)
|
| 162 |
sheet_name = sheet_name_match.group(1) if sheet_name_match else "Sheet1"
|
| 163 |
header_range = f"{sheet_name}!A1:Z1"
|
|
|
|
| 168 |
logger.error(f"Không thể lấy được header từ range '{header_range}'.")
|
| 169 |
return None
|
| 170 |
|
|
|
|
| 171 |
data_result = self.service.spreadsheets().values().get(spreadsheetId=self.sheet_id, range=SHEET_RANGE).execute()
|
| 172 |
values = data_result.get('values', [])
|
| 173 |
|
| 174 |
conversation_id = kwargs.get('conversation_id')
|
| 175 |
row_to_update_index = -1
|
| 176 |
|
|
|
|
| 177 |
if conversation_id:
|
| 178 |
+
logger.debug(f"Đang tìm dòng để cập nhật cho conversation_id: {conversation_id}")
|
| 179 |
try:
|
| 180 |
id_col_index = header.index('conversation_id')
|
| 181 |
+
start_row = _get_start_row_from_range(SHEET_RANGE)
|
| 182 |
+
|
| 183 |
+
for i, row in enumerate(values, start=start_row):
|
| 184 |
if len(row) > id_col_index and row[id_col_index] == conversation_id:
|
| 185 |
row_to_update_index = i
|
| 186 |
+
logger.success(f"Đã tìm thấy conversation_id tại dòng {row_to_update_index} để cập nhật.")
|
| 187 |
break
|
| 188 |
+
if row_to_update_index == -1:
|
| 189 |
+
logger.warning(f"Không tìm thấy dòng nào khớp với conversation_id: {conversation_id}. Sẽ tiến hành thêm dòng mới.")
|
| 190 |
except ValueError:
|
| 191 |
logger.error("Không tìm thấy cột 'conversation_id' trong header của sheet.")
|
| 192 |
return None
|
|
|
|
| 205 |
|
| 206 |
if row_to_update_index != -1:
|
| 207 |
logger.info(f"Đang cập nhật conversation_id {conversation_id} tại dòng {row_to_update_index}")
|
| 208 |
+
range_to_update = f"{sheet_name}!A{row_to_update_index}"
|
| 209 |
body = {'values': [row_data]}
|
| 210 |
self.service.spreadsheets().values().update(
|
| 211 |
spreadsheetId=self.sheet_id,
|