VietCat commited on
Commit
55400cc
·
1 Parent(s): 996de23

quick fix timestamp

Browse files
Files changed (1) hide show
  1. app/sheets.py +12 -13
app/sheets.py CHANGED
@@ -69,7 +69,7 @@ class SheetsClient:
69
 
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."""
73
  try:
74
  if not self.service:
75
  self.authenticate()
@@ -80,15 +80,21 @@ class SheetsClient:
80
  ).execute()
81
  values = result.get('values', [])
82
  history = []
83
- if not values or len(values) < 2: # Cần có header và ít nhất 1 dòng dữ liệu
 
 
84
  return []
85
 
86
  header = values[0]
87
  for row in values[1:]:
88
- # Zip header với row để tạo dict, an toàn hơn
89
  row_data = dict(zip(header, row + [""] * (len(header) - len(row))))
90
 
91
- if row_data.get('recipient_id') == user_id and row_data.get('page_id') == page_id:
 
 
 
 
 
92
  try:
93
  timestamps_raw = json.loads(row_data.get('timestamp', '[]'))
94
  timestamps = _flatten_and_unique_timestamps(timestamps_raw)
@@ -133,20 +139,14 @@ class SheetsClient:
133
  ).execute()
134
  values = result.get('values', [])
135
 
136
- # ==================================================================
137
- # >>>>> SỬA LỖI LỒNG TIMESTAMP TẠI ĐÂY <<<<<
138
- # Bước 1: Xử lý `timestamp` đầu vào để nó LUÔN LUÔN là một danh sách phẳng.
139
  processed_timestamps = _flatten_and_unique_timestamps(timestamp or [])
140
 
141
- # Bước 2: Lấy timestamp mới nhất để kiểm tra trùng lặp. Nếu list rỗng thì tạo mới.
142
  if not processed_timestamps:
143
  current_ts_for_check = datetime.now().isoformat()
144
  processed_timestamps.append(current_ts_for_check)
145
  else:
146
  current_ts_for_check = processed_timestamps[-1]
147
- # ==================================================================
148
 
149
- # Logic kiểm tra trùng lặp của bạn được giữ nguyên
150
  for row in values:
151
  row = row + [""] * 14
152
  try:
@@ -180,7 +180,6 @@ class SheetsClient:
180
  'isdone': row[13].lower() == 'true' if len(row) > 13 else False
181
  }
182
 
183
- # Logic tạo dòng mới và append vào sheet được giữ nguyên
184
  if not conversation_id:
185
  conversation_id = generate_conversation_id(recipient_id, page_id, current_ts_for_check)
186
 
@@ -197,7 +196,7 @@ class SheetsClient:
197
  originalpurpose,
198
  originalquestion,
199
  systemresponse,
200
- json.dumps(processed_timestamps), # <<<< SỬ DỤNG BIẾN ĐÃ ĐƯỢC XỬ LÝ
201
  str(isdone).lower()
202
  ]
203
 
@@ -224,7 +223,7 @@ class SheetsClient:
224
  'originalpurpose': originalpurpose,
225
  'originalquestion': originalquestion,
226
  'systemresponse': systemresponse,
227
- 'timestamp': processed_timestamps, # <<<< TRẢ VỀ DANH SÁCH ĐÃ ĐƯỢC XỬ LÝ
228
  'isdone': isdone
229
  }
230
  except Exception as e:
 
69
 
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()
 
80
  ).execute()
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)
 
139
  ).execute()
140
  values = result.get('values', [])
141
 
 
 
 
142
  processed_timestamps = _flatten_and_unique_timestamps(timestamp or [])
143
 
 
144
  if not processed_timestamps:
145
  current_ts_for_check = datetime.now().isoformat()
146
  processed_timestamps.append(current_ts_for_check)
147
  else:
148
  current_ts_for_check = processed_timestamps[-1]
 
149
 
 
150
  for row in values:
151
  row = row + [""] * 14
152
  try:
 
180
  'isdone': row[13].lower() == 'true' if len(row) > 13 else False
181
  }
182
 
 
183
  if not conversation_id:
184
  conversation_id = generate_conversation_id(recipient_id, page_id, current_ts_for_check)
185
 
 
196
  originalpurpose,
197
  originalquestion,
198
  systemresponse,
199
+ json.dumps(processed_timestamps),
200
  str(isdone).lower()
201
  ]
202
 
 
223
  'originalpurpose': originalpurpose,
224
  'originalquestion': originalquestion,
225
  'systemresponse': systemresponse,
226
+ 'timestamp': processed_timestamps,
227
  'isdone': isdone
228
  }
229
  except Exception as e: