| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- """
- Dashboard API endpoints.
- Provides endpoints for:
- - GET /api/dashboard/stats - Get dashboard statistics
- Requirements: 7.2
- """
- from flask import jsonify
- from app.api import api_bp
- from app.models import Task, Report
- from app.services import login_required, get_current_user_from_context
- from app import db
- @api_bp.route('/dashboard/stats', methods=['GET'])
- @login_required
- def get_dashboard_stats():
- """
- Get dashboard statistics including task counts and recent reports.
-
- Returns:
- JSON with task statistics and recent reports
-
- Requirements:
- - 7.2: Show task status summary and recent reports
- """
- current_user = get_current_user_from_context()
-
- # Build base query based on user role
- if current_user.role in ['admin', 'power_user']:
- task_query = Task.query
- else:
- # Regular users can only see their own tasks
- task_query = Task.query.filter_by(created_by=current_user.id)
-
- # Get task counts by status
- total_tasks = task_query.count()
- pending_tasks = task_query.filter_by(status='pending').count()
- running_tasks = task_query.filter_by(status='running').count()
- completed_tasks = task_query.filter_by(status='completed').count()
- failed_tasks = task_query.filter_by(status='failed').count()
-
- # Get recent reports (last 5)
- if current_user.role in ['admin', 'power_user']:
- recent_reports_query = db.session.query(Report).join(Task)
- else:
- recent_reports_query = db.session.query(Report).join(Task).filter(
- Task.created_by == current_user.id
- )
-
- recent_reports = recent_reports_query.order_by(
- Report.created_at.desc()
- ).limit(5).all()
-
- # Get total reports count
- total_reports = recent_reports_query.count()
-
- return jsonify({
- 'tasks': {
- 'total': total_tasks,
- 'pending': pending_tasks,
- 'running': running_tasks,
- 'completed': completed_tasks,
- 'failed': failed_tasks
- },
- 'reports': {
- 'total': total_reports,
- 'recent': [report.to_dict() for report in recent_reports]
- }
- }), 200
|