quick fix timestamp
Browse files- app/sheets.py +28 -17
app/sheets.py
CHANGED
|
@@ -170,18 +170,18 @@ class SheetsClient:
|
|
| 170 |
values = data_result.get('values', [])
|
| 171 |
|
| 172 |
# Định danh của sự kiện đang xử lý
|
| 173 |
-
recipient_id = str(kwargs.get('recipient_id'))
|
| 174 |
-
page_id = str(kwargs.get('page_id'))
|
| 175 |
-
# Timestamp của sự kiện webhook này là duy nhất
|
| 176 |
ts_list = _flatten_and_unique_timestamps(kwargs.get('timestamp', []))
|
| 177 |
-
event_timestamp = str(ts_list[-1]) if ts_list else ''
|
| 178 |
|
|
|
|
|
|
|
| 179 |
# --- 3. Tìm kiếm bản ghi đã tồn tại ---
|
| 180 |
found_row_index = -1
|
| 181 |
found_row_data = {}
|
| 182 |
start_row = _get_start_row_from_range(SHEET_RANGE)
|
| 183 |
|
| 184 |
-
# Lấy vị trí các cột cần thiết từ header
|
| 185 |
try:
|
| 186 |
id_col_idx = header.index('conversation_id')
|
| 187 |
recipient_col_idx = header.index('recipient_id')
|
|
@@ -191,26 +191,38 @@ class SheetsClient:
|
|
| 191 |
logger.error(f"Thiếu cột bắt buộc trong header: {e}")
|
| 192 |
return None
|
| 193 |
|
| 194 |
-
|
| 195 |
-
target_conv_id = kwargs.get('conversation_id')
|
| 196 |
if target_conv_id:
|
|
|
|
| 197 |
for i, row in enumerate(values, start=start_row):
|
| 198 |
-
if len(row) > id_col_idx
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
logger.
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
# Nếu không tìm thấy bằng ID, tìm bằng bộ ba (user, page, timestamp)
|
| 205 |
if found_row_index == -1:
|
|
|
|
| 206 |
for i, row in enumerate(values, start=start_row):
|
| 207 |
if len(row) <= max(recipient_col_idx, page_col_idx, timestamp_col_idx):
|
| 208 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
if
|
| 211 |
try:
|
| 212 |
-
sheet_timestamps = [str(ts) for ts in _flatten_and_unique_timestamps(json.loads(row[timestamp_col_idx]))]
|
| 213 |
-
|
|
|
|
|
|
|
| 214 |
found_row_index = i
|
| 215 |
found_row_data = dict(zip(header, row))
|
| 216 |
logger.success(f"Tìm thấy bằng (user, page, timestamp) tại dòng {i}.")
|
|
@@ -225,7 +237,6 @@ class SheetsClient:
|
|
| 225 |
|
| 226 |
updated_data = found_row_data.copy()
|
| 227 |
for key, value in kwargs.items():
|
| 228 |
-
# Chỉ cập nhật nếu giá trị mới không rỗng hoặc là boolean (cho isdone)
|
| 229 |
if value is not None and value != '' or isinstance(value, bool):
|
| 230 |
updated_data[key] = value
|
| 231 |
|
|
|
|
| 170 |
values = data_result.get('values', [])
|
| 171 |
|
| 172 |
# Định danh của sự kiện đang xử lý
|
| 173 |
+
recipient_id = str(kwargs.get('recipient_id')).strip()
|
| 174 |
+
page_id = str(kwargs.get('page_id')).strip()
|
|
|
|
| 175 |
ts_list = _flatten_and_unique_timestamps(kwargs.get('timestamp', []))
|
| 176 |
+
event_timestamp = str(ts_list[-1]).strip() if ts_list else ''
|
| 177 |
|
| 178 |
+
logger.debug(f"UPSERT: Bắt đầu tìm kiếm với recipient_id='{recipient_id}', page_id='{page_id}', event_timestamp='{event_timestamp}'")
|
| 179 |
+
|
| 180 |
# --- 3. Tìm kiếm bản ghi đã tồn tại ---
|
| 181 |
found_row_index = -1
|
| 182 |
found_row_data = {}
|
| 183 |
start_row = _get_start_row_from_range(SHEET_RANGE)
|
| 184 |
|
|
|
|
| 185 |
try:
|
| 186 |
id_col_idx = header.index('conversation_id')
|
| 187 |
recipient_col_idx = header.index('recipient_id')
|
|
|
|
| 191 |
logger.error(f"Thiếu cột bắt buộc trong header: {e}")
|
| 192 |
return None
|
| 193 |
|
| 194 |
+
target_conv_id = str(kwargs.get('conversation_id') or '').strip()
|
|
|
|
| 195 |
if target_conv_id:
|
| 196 |
+
logger.debug(f"UPSERT: Ưu tiên tìm bằng conversation_id='{target_conv_id}'")
|
| 197 |
for i, row in enumerate(values, start=start_row):
|
| 198 |
+
if len(row) > id_col_idx:
|
| 199 |
+
sheet_conv_id = str(row[id_col_idx]).strip()
|
| 200 |
+
is_match = sheet_conv_id == target_conv_id
|
| 201 |
+
logger.trace(f"Dòng {i}: So sánh ID: '{sheet_conv_id}' == '{target_conv_id}' -> {is_match}")
|
| 202 |
+
if is_match:
|
| 203 |
+
found_row_index = i
|
| 204 |
+
found_row_data = dict(zip(header, row))
|
| 205 |
+
logger.success(f"Tìm thấy bằng conversation_id tại dòng {i}.")
|
| 206 |
+
break
|
| 207 |
|
|
|
|
| 208 |
if found_row_index == -1:
|
| 209 |
+
logger.debug(f"UPSERT: Không tìm thấy bằng ID, chuyển sang tìm bằng (user, page, timestamp).")
|
| 210 |
for i, row in enumerate(values, start=start_row):
|
| 211 |
if len(row) <= max(recipient_col_idx, page_col_idx, timestamp_col_idx):
|
| 212 |
continue
|
| 213 |
+
|
| 214 |
+
sheet_recipient_id = str(row[recipient_col_idx]).strip()
|
| 215 |
+
sheet_page_id = str(row[page_col_idx]).strip()
|
| 216 |
+
|
| 217 |
+
id_match = (sheet_recipient_id == recipient_id) and (sheet_page_id == page_id)
|
| 218 |
+
logger.trace(f"Dòng {i}: So sánh (user, page): ('{sheet_recipient_id}' == '{recipient_id}') AND ('{sheet_page_id}' == '{page_id}') -> {id_match}")
|
| 219 |
|
| 220 |
+
if id_match:
|
| 221 |
try:
|
| 222 |
+
sheet_timestamps = [str(ts).strip() for ts in _flatten_and_unique_timestamps(json.loads(row[timestamp_col_idx]))]
|
| 223 |
+
ts_match = event_timestamp and event_timestamp in sheet_timestamps
|
| 224 |
+
logger.trace(f"Dòng {i}: So sánh timestamp: '{event_timestamp}' in {sheet_timestamps} -> {ts_match}")
|
| 225 |
+
if ts_match:
|
| 226 |
found_row_index = i
|
| 227 |
found_row_data = dict(zip(header, row))
|
| 228 |
logger.success(f"Tìm thấy bằng (user, page, timestamp) tại dòng {i}.")
|
|
|
|
| 237 |
|
| 238 |
updated_data = found_row_data.copy()
|
| 239 |
for key, value in kwargs.items():
|
|
|
|
| 240 |
if value is not None and value != '' or isinstance(value, bool):
|
| 241 |
updated_data[key] = value
|
| 242 |
|