VietCat commited on
Commit
35c4977
·
1 Parent(s): 55400cc

quick fix timestamp

Browse files
Files changed (1) hide show
  1. app/sheets.py +26 -9
app/sheets.py CHANGED
@@ -70,6 +70,8 @@ class SheetsClient:
70
  @timing_decorator_sync
71
  def get_conversation_history(self, user_id: str, page_id: str) -> List[Dict[str, Any]]:
72
  """Lấy lịch sử hội thoại từ sheet, đảm bảo so sánh kiểu dữ liệu an toàn."""
 
 
73
  try:
74
  if not self.service:
75
  self.authenticate()
@@ -81,20 +83,27 @@ class SheetsClient:
81
  values = result.get('values', [])
82
  history = []
83
 
84
- # Nếu không có dữ liệu hoặc chỉ có header, trả về rỗng
85
  if not values or len(values) < 2:
 
86
  return []
87
 
 
88
  header = values[0]
89
- for row in values[1:]:
 
90
  row_data = dict(zip(header, row + [""] * (len(header) - len(row))))
91
 
92
- # ==================================================================
93
- # >>>>> SỬA LỖI SO SÁNH KIỂU DỮ LIỆU TẠI ĐÂY <<<<<
94
- # Ép kiểu cả hai vế về string để đảm bảo so sánh chính xác
95
- if str(row_data.get('recipient_id')) == str(user_id) and \
96
- str(row_data.get('page_id')) == str(page_id):
97
- # ==================================================================
 
 
 
 
 
98
  try:
99
  timestamps_raw = json.loads(row_data.get('timestamp', '[]'))
100
  timestamps = _flatten_and_unique_timestamps(timestamps_raw)
@@ -105,9 +114,17 @@ class SheetsClient:
105
  row_data['originalattachments'] = json.loads(row_data.get('originalattachments', '[]')) if row_data.get('originalattachments') else []
106
  row_data['isdone'] = str(row_data.get('isdone', 'false')).lower() == 'true'
107
  history.append(row_data)
 
 
 
 
 
 
 
 
108
  return history
109
  except Exception as e:
110
- logger.error(f"Error getting conversation history: {e}")
111
  return []
112
 
113
  @timing_decorator_sync
 
70
  @timing_decorator_sync
71
  def get_conversation_history(self, user_id: str, page_id: str) -> List[Dict[str, Any]]:
72
  """Lấy lịch sử hội thoại từ sheet, đảm bảo so sánh kiểu dữ liệu an toàn."""
73
+ # --- THÊM LOG ĐỂ DEBUG ---
74
+ logger.debug(f"[get_conversation_history] Bắt đầu lấy lịch sử cho user_id: '{user_id}' và page_id: '{page_id}'")
75
  try:
76
  if not self.service:
77
  self.authenticate()
 
83
  values = result.get('values', [])
84
  history = []
85
 
 
86
  if not values or len(values) < 2:
87
+ logger.warning(f"[get_conversation_history] Không tìm thấy dữ liệu hoặc chỉ có header trong sheet. Tổng số dòng: {len(values)}")
88
  return []
89
 
90
+ logger.debug(f"[get_conversation_history] Đã lấy được {len(values)} dòng từ sheet.")
91
  header = values[0]
92
+
93
+ for i, row in enumerate(values[1:], start=1):
94
  row_data = dict(zip(header, row + [""] * (len(header) - len(row))))
95
 
96
+ # --- THÊM LOG CHI TIẾT CHO TỪNG DÒNG ---
97
+ sheet_recipient_id = row_data.get('recipient_id', 'N/A')
98
+ sheet_page_id = row_data.get('page_id', 'N/A')
99
+
100
+ logger.trace(f"[get_conversation_history] Đang xử lý dòng {i+1}: recipient_id='{sheet_recipient_id}', page_id='{sheet_page_id}'")
101
+
102
+ is_recipient_match = str(sheet_recipient_id) == str(user_id)
103
+ is_page_match = str(sheet_page_id) == str(page_id)
104
+
105
+ if is_recipient_match and is_page_match:
106
+ logger.success(f"[get_conversation_history] >>> TÌM THẤY DÒNG KHỚP tại dòng {i+1}!")
107
  try:
108
  timestamps_raw = json.loads(row_data.get('timestamp', '[]'))
109
  timestamps = _flatten_and_unique_timestamps(timestamps_raw)
 
114
  row_data['originalattachments'] = json.loads(row_data.get('originalattachments', '[]')) if row_data.get('originalattachments') else []
115
  row_data['isdone'] = str(row_data.get('isdone', 'false')).lower() == 'true'
116
  history.append(row_data)
117
+ else:
118
+ # Log lý do không khớp
119
+ if not is_recipient_match:
120
+ logger.trace(f"[get_conversation_history] Dòng {i+1} không khớp: recipient_id (sheet: '{sheet_recipient_id}', input: '{user_id}')")
121
+ if not is_page_match:
122
+ logger.trace(f"[get_conversation_history] Dòng {i+1} không khớp: page_id (sheet: '{sheet_page_id}', input: '{page_id}')")
123
+
124
+ logger.debug(f"[get_conversation_history] Hoàn tất xử lý. Tìm thấy {len(history)} bản ghi lịch sử.")
125
  return history
126
  except Exception as e:
127
+ logger.error(f"Error getting conversation history: {e}", exc_info=True)
128
  return []
129
 
130
  @timing_decorator_sync