diff --git a/app/db_interface.py b/app/db_interface.py index f4d0de8..8227e86 100644 --- a/app/db_interface.py +++ b/app/db_interface.py @@ -637,6 +637,33 @@ def get_summary_counts(): return total_users, total_groups +def get_database_stats(): + conn = get_connection() + cursor = conn.cursor() + + stats = {} + + # Get total size of the database + cursor.execute(""" + SELECT table_schema AS db_name, + ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS total_mb + FROM information_schema.tables + WHERE table_schema = DATABASE() + GROUP BY table_schema + """) + row = cursor.fetchone() + stats["total_size_mb"] = row[1] if row else 0 + + # Optional: count total rows in key tables + cursor.execute("SELECT COUNT(*) FROM auth_logs") + stats["auth_logs_count"] = cursor.fetchone()[0] + + cursor.execute("SELECT COUNT(*) FROM users") + stats["users_count"] = cursor.fetchone()[0] + + conn.close() + return stats + # ------------------------------ # Maintenance Functions # ------------------------------ @@ -725,4 +752,6 @@ def get_table_stats(): return None finally: cursor.close() - conn.close() \ No newline at end of file + conn.close() + + diff --git a/app/templates/maintenance.html b/app/templates/maintenance.html index 5f91302..eaab71c 100644 --- a/app/templates/maintenance.html +++ b/app/templates/maintenance.html @@ -1,67 +1,93 @@ {% extends 'base.html' %} {% block title %}Maintenance{% endblock %} {% block content %} -

Database Maintenance

+
+

Database Maintenance

- {% with messages = get_flashed_messages(with_categories=true) %} - {% if messages %} -
- {% for category, message in messages %} -
{{ message }}
- {% endfor %} -
- {% endif %} - {% endwith %} - -

Perform common database maintenance tasks here.

- -
- -

Database Statistics

-
- {% if table_stats %} - {% for table, row_count in table_stats.items() %} -
-
{{ table }}
-
-

Number of rows: {{ row_count }}

-
-
- {% endfor %} - {% else %} -

Could not retrieve database statistics.

- {% endif %} + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} +
+ {% for category, message in messages %} +
{{ message }}
+ {% endfor %}
+ {% endif %} + {% endwith %} -
+
+
+
Database Overview
+
+ + + + + + + + + + + + + + + {% if table_stats %} + {% for table, row_count in table_stats.items() %} + {% if table != 'auth_logs' and table != 'users' %} + + + + + {% endif %} + {% endfor %} + {% endif %} + +
Database Size{{ db_stats.total_size_mb }} MB
auth_logs Rows{{ db_stats.auth_logs_count }}
users Rows{{ db_stats.users_count }}
{{ table }} Rows{{ row_count }}
+
+
+
-

Clear Authentication Logs

-

Permanently remove all authentication logs from the database. This action cannot be undone.

-
- -
+ + +
+
+ -
+
+
+
Backup Database
+
+

Dump the current SQL database to a downloadable file.

+

Warning: Backup size can be large if auth_logs has not been cleared.

+
+ +
+
+
+
-

Database Backup

-

Create a backup of the current database. The backup will be saved as a SQL file.

-

- Warning: Database backups can be very large if you do not clear the authentication logs first. -

-
- -
- -
- -

Database Restore

-

Restore the database from a previously created SQL backup file. This will overwrite the current database.

-
- - -
+ + + + + + {% endblock %} \ No newline at end of file diff --git a/app/views/maintenance_views.py b/app/views/maintenance_views.py index b6234a4..52322e9 100644 --- a/app/views/maintenance_views.py +++ b/app/views/maintenance_views.py @@ -1,16 +1,17 @@ from flask import Blueprint, render_template, request, send_file import mysql.connector import os -from db_interface import clear_auth_logs, backup_database, restore_database, get_table_stats # Import the functions from db_interface.py +from db_interface import get_database_stats, clear_auth_logs, backup_database, restore_database, get_table_stats # Import the functions from db_interface.py maintenance = Blueprint('maintenance', __name__, url_prefix='/maintenance') @maintenance.route('/') def maintenance_page(): - """Renders the maintenance page.""" + """Renders the maintenance page with table and DB stats.""" table_stats = get_table_stats() - return render_template('maintenance.html', table_stats=table_stats) + db_stats = get_database_stats() + return render_template('maintenance.html', table_stats=table_stats, db_stats=db_stats) @maintenance.route('/clear_auth_logs', methods=['POST']) def clear_auth_logs_route(): @@ -47,4 +48,4 @@ def restore_database_route(): message = restore_database(sql_content) return message except Exception as e: - return str(e), 500 + return str(e), 500 \ No newline at end of file