plokmii commited on
Commit
cf15a03
·
verified ·
1 Parent(s): d4128b8

Upload db.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. db.py +22 -7
db.py CHANGED
@@ -1037,27 +1037,42 @@ def get_storage_stats():
1037
 
1038
  def list_uploads():
1039
  """列出 uploads/ 所有檔案。上傳時間優先取 reports.created_at(持久值),
1040
- orphan 檔案才 fallback 到 file mtime。"""
 
1041
  d = get_upload_dir()
1042
  files = list(d.glob('*.xls')) + list(d.glob('*.xlsx')) + list(d.glob('*.csv'))
1043
  conn = get_conn()
1044
- rows = conn.execute("""SELECT upload_file, MAX(COALESCE(updated_at, created_at)) as ts
1045
- FROM reports WHERE upload_file != ''
1046
- GROUP BY upload_file""").fetchall()
 
 
 
1047
  conn.close()
1048
- upload_times = {r['upload_file']: r['ts'] for r in rows if r['upload_file']}
 
 
 
 
 
 
 
 
 
 
1049
  out = []
1050
  for f in files:
1051
  st = f.stat()
1052
- ts = upload_times.get(f.name)
1053
  if not ts:
1054
  ts = datetime.fromtimestamp(st.st_mtime).strftime('%Y-%m-%d %H:%M')
1055
  else:
1056
- ts = ts[:16] # YYYY-MM-DD HH:MM
1057
  out.append({
1058
  'name': f.name,
1059
  'size_kb': round(st.st_size / 1024, 1),
1060
  'mtime': ts,
 
1061
  })
1062
  out.sort(key=lambda x: x['mtime'], reverse=True)
1063
  return out
 
1037
 
1038
  def list_uploads():
1039
  """列出 uploads/ 所有檔案。上傳時間優先取 reports.created_at(持久值),
1040
+ orphan 檔案才 fallback 到 file mtime。每筆附 source ('PRA' / 'DSA')。
1041
+ 判定優先序:reports → dsa_reports → 檔名 (LS?A* / *PRA1* / *PRA2*) → 預設 PRA。"""
1042
  d = get_upload_dir()
1043
  files = list(d.glob('*.xls')) + list(d.glob('*.xlsx')) + list(d.glob('*.csv'))
1044
  conn = get_conn()
1045
+ pra_rows = conn.execute("""SELECT upload_file, MAX(COALESCE(updated_at, created_at)) as ts
1046
+ FROM reports WHERE upload_file != ''
1047
+ GROUP BY upload_file""").fetchall()
1048
+ dsa_rows = conn.execute("""SELECT upload_file, MAX(COALESCE(updated_at, created_at)) as ts
1049
+ FROM dsa_reports WHERE upload_file != ''
1050
+ GROUP BY upload_file""").fetchall()
1051
  conn.close()
1052
+ pra_times = {r['upload_file']: r['ts'] for r in pra_rows if r['upload_file']}
1053
+ dsa_times = {r['upload_file']: r['ts'] for r in dsa_rows if r['upload_file']}
1054
+
1055
+ def classify(name):
1056
+ if name in pra_times: return 'PRA'
1057
+ if name in dsa_times: return 'DSA'
1058
+ u = name.upper()
1059
+ if 'LS1A' in u or 'LS2A' in u: return 'DSA'
1060
+ if 'PRA1' in u or 'PRA2' in u: return 'PRA'
1061
+ return 'PRA'
1062
+
1063
  out = []
1064
  for f in files:
1065
  st = f.stat()
1066
+ ts = pra_times.get(f.name) or dsa_times.get(f.name)
1067
  if not ts:
1068
  ts = datetime.fromtimestamp(st.st_mtime).strftime('%Y-%m-%d %H:%M')
1069
  else:
1070
+ ts = ts[:16]
1071
  out.append({
1072
  'name': f.name,
1073
  'size_kb': round(st.st_size / 1024, 1),
1074
  'mtime': ts,
1075
+ 'source': classify(f.name),
1076
  })
1077
  out.sort(key=lambda x: x['mtime'], reverse=True)
1078
  return out