PhilipL commited on
Commit
fcdc619
·
verified ·
1 Parent(s): f4fc485

time input bug in plot

Browse files
Files changed (1) hide show
  1. app.py +18 -6
app.py CHANGED
@@ -179,7 +179,9 @@ def get_plot_data(selected_key):
179
 
180
  df_live = pd.DataFrame(log_history)
181
  if not df_live.empty:
182
- df_live['timestamp'] = pd.to_datetime(df_live['timestamp'])
 
 
183
  df_live = df_live[df_live['action'] == action_code].copy()
184
 
185
  if not df_live.empty:
@@ -359,8 +361,18 @@ def check_anomaly(ip, account, action, log_time):
359
 
360
  def record_log(ip, cookie, account, role, action, status, time_mode, custom_time):
361
  global baseline_X
362
- if time_mode == "自訂時間 (模擬過去/未來)" and custom_time and str(custom_time).strip() != "": log_time = str(custom_time)
363
- else: log_time = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8))).strftime("%Y-%m-%d %H:%M:%S")
 
 
 
 
 
 
 
 
 
 
364
 
365
  if status == "success":
366
  ai_judgment = check_anomaly(ip, account, action, log_time)
@@ -376,7 +388,7 @@ def record_log(ip, cookie, account, role, action, status, time_mode, custom_time
376
  except: pass
377
 
378
  global_stats["total_count"] += 1
379
- if "異常" in str(status): global_stats["abnormal_count"] += 1
380
 
381
  log_history.insert(0, new_log)
382
 
@@ -389,10 +401,10 @@ def record_log(ip, cookie, account, role, action, status, time_mode, custom_time
389
  except: pass
390
 
391
  df_all = pd.DataFrame(log_history)
392
- abnormal_logs = [log for log in log_history if "異常" in str(log.get("status", ""))]
393
  df_abnormal = pd.DataFrame(abnormal_logs) if abnormal_logs else pd.DataFrame(columns=df_all.columns)
394
 
395
- input_update = gr.update(value=f"時間: {log_time} | IP: {ip} | 帳號: {account} | 動作: {action} | 狀態: {status}") if "異常" in str(status) else gr.update()
396
  return df_all, df_abnormal, input_update
397
 
398
  def fast_login(target_account, ip, cookie, time_mode, custom_time):
 
179
 
180
  df_live = pd.DataFrame(log_history)
181
  if not df_live.empty:
182
+ # 👉 雙重防護 2:加入 errors='coerce' 容錯機制,剔除壞資料避免圖表破圖
183
+ df_live['timestamp'] = pd.to_datetime(df_live['timestamp'], errors='coerce')
184
+ df_live = df_live.dropna(subset=['timestamp'])
185
  df_live = df_live[df_live['action'] == action_code].copy()
186
 
187
  if not df_live.empty:
 
361
 
362
  def record_log(ip, cookie, account, role, action, status, time_mode, custom_time):
363
  global baseline_X
364
+
365
+ # 👉 雙重防護 1:驗證自訂時間格式
366
+ if time_mode == "自訂時間 (模擬過去/未來)" and custom_time and str(custom_time).strip() != "":
367
+ try:
368
+ valid_time = pd.to_datetime(str(custom_time))
369
+ log_time = valid_time.strftime("%Y-%m-%d %H:%M:%S")
370
+ except Exception:
371
+ # 🚨 解析失敗,退回當前時間並標記狀態
372
+ log_time = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8))).strftime("%Y-%m-%d %H:%M:%S")
373
+ status = "⚠️格式錯誤_無效的自訂時間"
374
+ else:
375
+ log_time = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8))).strftime("%Y-%m-%d %H:%M:%S")
376
 
377
  if status == "success":
378
  ai_judgment = check_anomaly(ip, account, action, log_time)
 
388
  except: pass
389
 
390
  global_stats["total_count"] += 1
391
+ if "異常" in str(status) or "錯誤" in str(status): global_stats["abnormal_count"] += 1
392
 
393
  log_history.insert(0, new_log)
394
 
 
401
  except: pass
402
 
403
  df_all = pd.DataFrame(log_history)
404
+ abnormal_logs = [log for log in log_history if "異常" in str(log.get("status", "")) or "錯誤" in str(log.get("status", ""))]
405
  df_abnormal = pd.DataFrame(abnormal_logs) if abnormal_logs else pd.DataFrame(columns=df_all.columns)
406
 
407
+ input_update = gr.update(value=f"時間: {log_time} | IP: {ip} | 帳號: {account} | 動作: {action} | 狀態: {status}") if "異常" in str(status) or "錯誤" in str(status) else gr.update()
408
  return df_all, df_abnormal, input_update
409
 
410
  def fast_login(target_account, ip, cookie, time_mode, custom_time):