Optimize system
This commit is contained in:
@@ -18,6 +18,8 @@ from payroll_excel_exporter import PayrollExcelExporter
|
||||
# Load environment variables in .env
|
||||
load_dotenv()
|
||||
from turnstile_utils import turnstile_utils
|
||||
from db_performance_optimization import initialize_performance_optimizations
|
||||
from app_performance_middleware import PerformanceMonitor
|
||||
|
||||
# Initialize Flask application
|
||||
app = Flask(__name__)
|
||||
@@ -1621,6 +1623,102 @@ def dashboard():
|
||||
flash('Error loading dashboard. Please try again.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
@app.route('/api/dashboard/stats')
|
||||
@login_required
|
||||
def dashboard_stats_api():
|
||||
"""API endpoint for dashboard statistics"""
|
||||
try:
|
||||
# Get current stats
|
||||
total_qr_codes = QRCode.query.filter_by(active_status=True).count()
|
||||
|
||||
# Today's check-ins
|
||||
today = datetime.utcnow().date()
|
||||
today_checkins = AttendanceData.query.filter(
|
||||
AttendanceData.check_in_date == today
|
||||
).count()
|
||||
|
||||
# Active projects
|
||||
active_projects = Project.query.filter_by(active_status=True).count()
|
||||
|
||||
# Unique locations
|
||||
unique_locations = db.session.query(
|
||||
AttendanceData.location_name
|
||||
).distinct().count()
|
||||
|
||||
# Calculate trends (compared to last month)
|
||||
last_month = datetime.utcnow() - timedelta(days=30)
|
||||
|
||||
# QR codes trend
|
||||
old_qr_count = QRCode.query.filter(
|
||||
QRCode.created_date <= last_month,
|
||||
QRCode.active_status == True
|
||||
).count()
|
||||
qr_change = ((total_qr_codes - old_qr_count) / max(old_qr_count, 1)) * 100
|
||||
|
||||
# Check-ins trend (yesterday)
|
||||
yesterday = today - timedelta(days=1)
|
||||
yesterday_checkins = AttendanceData.query.filter(
|
||||
AttendanceData.check_in_date == yesterday
|
||||
).count()
|
||||
checkin_change = ((today_checkins - yesterday_checkins) / max(yesterday_checkins, 1)) * 100
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'total_qr_codes': total_qr_codes,
|
||||
'today_checkins': today_checkins,
|
||||
'active_projects': active_projects,
|
||||
'unique_locations': unique_locations,
|
||||
'qr_change': round(qr_change, 1),
|
||||
'checkin_change': round(checkin_change, 1),
|
||||
'project_change': 0, # You can calculate this based on your needs
|
||||
'location_change': 0 # You can calculate this based on your needs
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
logger_handler.log_database_error('dashboard_stats_api', e)
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': 'Failed to fetch dashboard statistics'
|
||||
}), 500
|
||||
|
||||
@app.route('/api/dashboard/realtime')
|
||||
@login_required
|
||||
def dashboard_realtime_api():
|
||||
"""API endpoint for real-time dashboard data"""
|
||||
try:
|
||||
# Get recent activity (last 10 check-ins)
|
||||
recent_activity = db.session.query(
|
||||
AttendanceData.employee_id,
|
||||
AttendanceData.location_name,
|
||||
AttendanceData.check_in_time,
|
||||
AttendanceData.check_in_date
|
||||
).order_by(
|
||||
AttendanceData.check_in_date.desc(),
|
||||
AttendanceData.check_in_time.desc()
|
||||
).limit(10).all()
|
||||
|
||||
activity_data = [
|
||||
{
|
||||
'employee_id': activity.employee_id,
|
||||
'location': activity.location_name,
|
||||
'time': activity.check_in_time.strftime('%H:%M'),
|
||||
'date': activity.check_in_date.strftime('%Y-%m-%d')
|
||||
}
|
||||
for activity in recent_activity
|
||||
]
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'recent_activity': activity_data
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
logger_handler.log_database_error('dashboard_realtime_api', e)
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': 'Failed to fetch real-time data'
|
||||
}), 500
|
||||
|
||||
# USER MANAGEMENT ROUTES
|
||||
@app.route('/profile', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@@ -6048,6 +6146,7 @@ def employee_detail(employee_index):
|
||||
|
||||
create_location_logging_routes(app, db, logger_handler)
|
||||
|
||||
|
||||
# Jinja2 filters for better template functionality
|
||||
@app.template_filter('days_since')
|
||||
def days_since_filter(date):
|
||||
@@ -6440,22 +6539,16 @@ if __name__ == '__main__':
|
||||
# Initialize database and logging
|
||||
create_tables()
|
||||
|
||||
# Add performance optimizations
|
||||
create_performance_indexes()
|
||||
create_audit_triggers()
|
||||
# Initialize performance optimizations
|
||||
print("🚀 Initializing performance optimizations...")
|
||||
cached_query = initialize_performance_optimizations(app, db, logger_handler)
|
||||
performance_monitor = PerformanceMonitor(app, db, logger_handler)
|
||||
|
||||
if cached_query:
|
||||
print("✅ Performance optimizations completed successfully")
|
||||
else:
|
||||
print("⚠️ Performance optimizations completed with warnings")
|
||||
|
||||
# Log optimization completion
|
||||
logger_handler.log_system_event(
|
||||
event_type="database_optimization_complete",
|
||||
description="Database performance optimization completed successfully",
|
||||
severity="INFO",
|
||||
additional_data={
|
||||
"indexes_created": True,
|
||||
"triggers_created": True,
|
||||
"optimization_timestamp": datetime.utcnow().isoformat()
|
||||
}
|
||||
)
|
||||
print("🚀 Database optimization completed successfully")
|
||||
# Log application startup
|
||||
logger_handler.logger.info("QR Attendance Management System started successfully")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user