hualinxin8615 commited on
Commit
c56e0ef
·
verified ·
1 Parent(s): 95d7c47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -7
app.py CHANGED
@@ -343,22 +343,52 @@ def simple_push_allocation_record(repo, space_name, student_id):
343
  print(f"[simple_push_allocation_record] Allocation recorded locally, sync will happen eventually")
344
  return False
345
 
346
- def analyze_space_activity(available_spaces, repo_dir):
347
- """Analyze activity status of all spaces and return sorted list"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
  space_activity = []
349
  current_time = datetime.now()
350
 
351
  # Read allocation records to filter out recently allocated spaces
352
  active_allocations = read_allocation_records(repo_dir)
353
 
354
- print(f"[analyze_space_activity] Analyzing {len(available_spaces)} spaces...")
355
 
356
  for space_name in available_spaces:
357
  csv_file = os.path.join(repo_dir, f"{space_name}_query_log.csv")
358
- last_activity = get_last_activity_time(csv_file)
 
 
 
359
 
360
  # Calculate idle time in minutes
361
- if last_activity == datetime.min:
362
  idle_minutes = float('inf') # Never used
363
  status = "Never used"
364
  last_activity_str = "Never"
@@ -386,10 +416,11 @@ def analyze_space_activity(available_spaces, repo_dir):
386
  'idle_minutes': idle_minutes,
387
  'status': status,
388
  'is_recently_allocated_remote': is_recently_allocated_remote,
389
- 'is_recently_allocated_local': is_recently_allocated_local
 
390
  })
391
 
392
- print(f"[analyze_space_activity] {space_name}: {status}")
393
 
394
  # Sort by idle time (most idle first)
395
  space_activity.sort(key=lambda x: x['idle_minutes'], reverse=True)
 
343
  print(f"[simple_push_allocation_record] Allocation recorded locally, sync will happen eventually")
344
  return False
345
 
346
+ def get_last_activity_time_optimized(csv_file_path):
347
+ """优化版本:使用文件修改时间而不是读取文件内容"""
348
+ try:
349
+ if not os.path.exists(csv_file_path):
350
+ print(f"[get_last_activity_time_optimized] File not found: {csv_file_path}")
351
+ return datetime.min # Never used
352
+
353
+ # 获取文件的修改时间戳
354
+ file_mtime = os.path.getmtime(csv_file_path)
355
+ last_modified = datetime.fromtimestamp(file_mtime)
356
+
357
+ print(f"[get_last_activity_time_optimized] {os.path.basename(csv_file_path)} last modified: {last_modified}")
358
+ return last_modified
359
+
360
+ except Exception as e:
361
+ print(f"[get_last_activity_time_optimized] Error getting file mtime for {csv_file_path}: {e}")
362
+ return datetime.min
363
+
364
+ def get_file_size_for_validation(csv_file_path):
365
+ """获取文件大小,用于验证文件是否有实际内容"""
366
+ try:
367
+ if not os.path.exists(csv_file_path):
368
+ return 0
369
+ return os.path.getsize(csv_file_path)
370
+ except:
371
+ return 0
372
+
373
+ def analyze_space_activity_optimized(available_spaces, repo_dir):
374
+ """优化版本:使用文件修改时间分析空间活动"""
375
  space_activity = []
376
  current_time = datetime.now()
377
 
378
  # Read allocation records to filter out recently allocated spaces
379
  active_allocations = read_allocation_records(repo_dir)
380
 
381
+ print(f"[analyze_space_activity_optimized] Analyzing {len(available_spaces)} spaces using file modification time...")
382
 
383
  for space_name in available_spaces:
384
  csv_file = os.path.join(repo_dir, f"{space_name}_query_log.csv")
385
+
386
+ # 使用文件修改时间而不是读取文件内容
387
+ last_activity = get_last_activity_time_optimized(csv_file)
388
+ file_size = get_file_size_for_validation(csv_file)
389
 
390
  # Calculate idle time in minutes
391
+ if last_activity == datetime.min or file_size <= 100: # 小于100字节认为是空文件或只有header
392
  idle_minutes = float('inf') # Never used
393
  status = "Never used"
394
  last_activity_str = "Never"
 
416
  'idle_minutes': idle_minutes,
417
  'status': status,
418
  'is_recently_allocated_remote': is_recently_allocated_remote,
419
+ 'is_recently_allocated_local': is_recently_allocated_local,
420
+ 'file_size': file_size # 添加文件大小信息用于调试
421
  })
422
 
423
+ print(f"[analyze_space_activity_optimized] {space_name}: {status} (file size: {file_size} bytes)")
424
 
425
  # Sort by idle time (most idle first)
426
  space_activity.sort(key=lambda x: x['idle_minutes'], reverse=True)