52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
from flask import Blueprint, jsonify
|
|
from ..db import get_db_connection, release_db_connection
|
|
from ..middleware import token_required
|
|
|
|
dashboard_blueprint = Blueprint('dashboard', __name__)
|
|
|
|
@dashboard_blueprint.route('/dashboard/stats', methods=['GET'])
|
|
@token_required
|
|
def get_stats(current_user_id):
|
|
conn = get_db_connection()
|
|
cur = conn.cursor()
|
|
|
|
# 1. Total cases
|
|
cur.execute("SELECT COUNT(*) FROM cases WHERE user_id = %s", (current_user_id,))
|
|
total_cases = cur.fetchone()[0]
|
|
|
|
# 2. Cases by status
|
|
cur.execute("SELECT status, COUNT(*) FROM cases WHERE user_id = %s GROUP BY status", (current_user_id,))
|
|
status_rows = cur.fetchall()
|
|
cases_by_status = {row[0]: row[1] for row in status_rows}
|
|
|
|
# 3. Total active integrations
|
|
cur.execute("SELECT COUNT(*) FROM credentials WHERE user_id = %s", (current_user_id,))
|
|
total_integrations = cur.fetchone()[0]
|
|
|
|
# 4. List of connected platforms
|
|
cur.execute("SELECT platform FROM credentials WHERE user_id = %s", (current_user_id,))
|
|
platforms = [row[0] for row in cur.fetchall()]
|
|
|
|
# 5. Recent activity (last 5 cases)
|
|
cur.execute("SELECT id, platform, violator_article, status, created_at FROM cases WHERE user_id = %s ORDER BY created_at DESC LIMIT 5", (current_user_id,))
|
|
recent_rows = cur.fetchall()
|
|
recent_activity = [
|
|
{
|
|
"id": r[0],
|
|
"platform": r[1],
|
|
"article": r[2],
|
|
"status": r[3],
|
|
"date": r[4].isoformat() if r[4] else None
|
|
} for r in recent_rows
|
|
]
|
|
|
|
release_db_connection(conn)
|
|
|
|
return jsonify({
|
|
"total_cases": total_cases,
|
|
"cases_by_status": cases_by_status,
|
|
"total_integrations": total_integrations,
|
|
"platforms": platforms,
|
|
"recent_activity": recent_activity
|
|
})
|