#!/usr/bin/env python3 """ Test script to verify data collection and summary generation Run this to check if your summaries are working correctly """ import sys from datetime import datetime, timedelta from pathlib import Path from config_multi_user import RAW_DATA_DIR, SUMMARIES_DIR, TIMEZONE, setup_logging from data_handler_team import get_team_handler from user_mapping import get_user_mapper def test_data_files(): """Test if data files are being created and can be read""" print("\n" + "="*60) print("TESTING DATA FILES") print("="*60) # Check if raw data directory exists and has files if not RAW_DATA_DIR.exists(): print("❌ Raw data directory does not exist!") return False # List all CSV files in raw data directory csv_files = list(RAW_DATA_DIR.glob("*.csv")) if not csv_files: print("❌ No CSV files found in raw data directory!") print(f" Directory: {RAW_DATA_DIR}") return False print(f"✅ Found {len(csv_files)} CSV files in raw data directory") # Show recent files recent_files = sorted(csv_files, key=lambda x: x.stat().st_mtime, reverse=True)[:5] print("\nMost recent files:") for f in recent_files: size = f.stat().st_size modified = datetime.fromtimestamp(f.stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S') print(f" - {f.name} ({size} bytes, modified: {modified})") # Test reading a file if csv_files: test_file = csv_files[0] print(f"\nTesting file read: {test_file.name}") try: import csv with open(test_file, 'r') as f: reader = csv.DictReader(f) rows = list(reader) if rows: print(f" ✅ Successfully read {len(rows)} rows") print(f" Columns: {', '.join(rows[0].keys())}") else: print(f" ⚠️ File is empty") except Exception as e: print(f" ❌ Failed to read file: {e}") return False return True def test_user_tracking(): """Test if users are being tracked correctly""" print("\n" + "="*60) print("TESTING USER TRACKING") print("="*60) team_handler = get_team_handler() mapper = get_user_mapper() # Get today's tracked users today = datetime.now(TIMEZONE).date() users = team_handler.get_all_tracked_users(today) if not users: print(f"⚠️ No users found for today ({today})") print(" Checking yesterday...") yesterday = today - timedelta(days=1) users = team_handler.get_all_tracked_users(yesterday) if users: print(f"✅ Found {len(users)} users for yesterday ({yesterday})") today = yesterday # Use yesterday for further tests else: print("❌ No users found for yesterday either") return False else: print(f"✅ Found {len(users)} users for today ({today})") # Display tracked users print("\nTracked users:") for user in users[:10]: # Show first 10 print(f" - {user['user_name']} (ID: {user['user_id']})") if len(users) > 10: print(f" ... and {len(users) - 10} more") # Test reading data for a specific user if users: test_user = users[0] print(f"\nTesting data read for user: {test_user['user_name']}") user_data = team_handler.read_user_daily_data( test_user['user_id'], test_user.get('sanitized_name'), today ) if user_data: print(f" ✅ Found {len(user_data)} data points") # Calculate activity active_count = sum(1 for row in user_data if row.get('presence') == 'active') print(f" Active: {active_count} minutes") print(f" Away: {len(user_data) - active_count} minutes") else: print(f" ⚠️ No data found for this user today") return True def test_daily_summary(): """Test daily summary generation""" print("\n" + "="*60) print("TESTING DAILY SUMMARY GENERATION") print("="*60) team_handler = get_team_handler() # Try today first today = datetime.now(TIMEZONE).date() print(f"Generating summary for today ({today})...") summary = team_handler.generate_daily_team_summary(today) if not summary: print("⚠️ No summary for today, trying yesterday...") yesterday = today - timedelta(days=1) summary = team_handler.generate_daily_team_summary(yesterday) if not summary: print("❌ Failed to generate summary for yesterday either") return False else: test_date = yesterday else: test_date = today # Display summary results print(f"\n✅ Successfully generated summary for {test_date}") print(f" Total users tracked: {summary['team_totals']['total_users']}") print(f" Active users: {summary['team_totals']['active_users']}") print(f" Total team hours: {summary['team_totals']['total_hours']:.2f}h") print(f" Average hours per active user: {summary['team_totals']['average_hours']:.2f}h") if summary['team_totals']['earliest_start']: print(f" Earliest start: {summary['team_totals']['earliest_start']}") if summary['team_totals']['latest_end']: print(f" Latest end: {summary['team_totals']['latest_end']}") # Show top 5 most active users if summary['users']: sorted_users = sorted(summary['users'].items(), key=lambda x: x[1]['total_hours'], reverse=True)[:5] print("\n Top 5 most active users:") for user_id, user_data in sorted_users: if user_data['total_hours'] > 0: print(f" - {user_data['name']}: {user_data['total_hours']:.2f}h") # Test saving to CSV print("\nTesting CSV save...") try: team_handler.save_team_summary_to_csv(summary) if team_handler.team_summary_file.exists(): print(f"✅ Summary saved to: {team_handler.team_summary_file}") else: print("❌ CSV file was not created") except Exception as e: print(f"❌ Failed to save CSV: {e}") return True def test_weekly_summary(): """Test weekly summary generation""" print("\n" + "="*60) print("TESTING WEEKLY SUMMARY GENERATION") print("="*60) team_handler = get_team_handler() # Generate for current week print("Generating weekly summary...") summary = team_handler.generate_weekly_summary() if not summary or not summary['users']: print("⚠️ No data in weekly summary") return False print(f"✅ Weekly summary generated") print(f" Week: {summary['week_start']} to {summary['week_end']}") print(f" Total team hours: {summary['team_totals']['total_hours']:.2f}h") if summary['daily_active_users']: print("\n Daily active users:") for day, count in summary['daily_active_users'].items(): print(f" {day}: {count} users") return True def main(): """Run all tests""" print("="*60) print("SLACK PRESENCE TRACKER - SYSTEM TEST") print("="*60) print(f"Current time: {datetime.now(TIMEZONE)}") print(f"Raw data directory: {RAW_DATA_DIR}") print(f"Summaries directory: {SUMMARIES_DIR}") # Setup logging logger = setup_logging('system_test') # Run tests tests_passed = 0 tests_total = 4 if test_data_files(): tests_passed += 1 if test_user_tracking(): tests_passed += 1 if test_daily_summary(): tests_passed += 1 if test_weekly_summary(): tests_passed += 1 # Final report print("\n" + "="*60) print("TEST RESULTS") print("="*60) print(f"Tests passed: {tests_passed}/{tests_total}") if tests_passed == tests_total: print("✅ All tests passed! Your system is working correctly.") elif tests_passed > 0: print("⚠️ Some tests failed. Check the output above for details.") else: print("❌ All tests failed. Please check your configuration and data collection.") # Recommendations print("\n" + "="*60) print("RECOMMENDATIONS") print("="*60) if not (SUMMARIES_DIR / "team_summary.csv").exists(): print("• Run daily summary: python3 team_summary.py --type daily --csv") # Check if cron is running import subprocess try: result = subprocess.run(['crontab', '-l'], capture_output=True, text=True) if 'check_presence_team.py' in result.stdout: print("✅ Cron job for presence checking is configured") else: print("⚠️ Cron job for presence checking not found") print(" Run: ./setup_cron_fresh.sh") except: print("⚠️ Could not check cron status") return 0 if tests_passed == tests_total else 1 if __name__ == "__main__": sys.exit(main())