Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import time
|
| 3 |
import uuid
|
| 4 |
import psutil
|
|
|
|
| 5 |
import pymysql
|
| 6 |
from flask import Flask, jsonify, request, abort, render_template_string
|
| 7 |
|
|
@@ -21,23 +22,47 @@ TOTAL_MAX_MB = 25000
|
|
| 21 |
start_time = time.time()
|
| 22 |
db_registry = {}
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
DASHBOARD_TEMPLATE = """
|
| 25 |
<!doctype html>
|
| 26 |
<html>
|
| 27 |
<head>
|
| 28 |
<title>MySQL DB Manager Dashboard</title>
|
| 29 |
<style>
|
| 30 |
-
body { font-family: Arial, sans-serif; margin: 20px; }
|
| 31 |
-
h1, h2 { margin-bottom: 0.3em; }
|
| 32 |
.section { margin-bottom: 2em; }
|
| 33 |
-
table { border-collapse: collapse; width: 100%; margin-top: 0.5em; }
|
| 34 |
-
th, td { border: 1px solid #
|
| 35 |
-
th { background: #
|
| 36 |
-
code { background: #
|
| 37 |
-
.small { font-size: 12px; color: #
|
| 38 |
-
input[type="text"], input[type="number"] { padding: 4px; }
|
| 39 |
-
button { padding: 6px 10px; cursor: pointer; }
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
</style>
|
| 42 |
</head>
|
| 43 |
<body>
|
|
@@ -157,20 +182,9 @@ def get_system_stats():
|
|
| 157 |
disk_used_gb = disk.used / (1024**3)
|
| 158 |
disk_percent = disk.percent
|
| 159 |
return {
|
| 160 |
-
"cpu": {
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
},
|
| 164 |
-
"ram": {
|
| 165 |
-
"total_gb": ram_total_gb,
|
| 166 |
-
"used_gb": ram_used_gb,
|
| 167 |
-
"usage_percent": ram_percent,
|
| 168 |
-
},
|
| 169 |
-
"disk": {
|
| 170 |
-
"total_gb": disk_total_gb,
|
| 171 |
-
"used_gb": disk_used_gb,
|
| 172 |
-
"usage_percent": disk_percent,
|
| 173 |
-
},
|
| 174 |
}
|
| 175 |
|
| 176 |
def total_allocated_mb():
|
|
@@ -180,15 +194,7 @@ def total_allocated_mb():
|
|
| 180 |
def dashboard():
|
| 181 |
system_stats = get_system_stats()
|
| 182 |
uptime_seconds = int(time.time() - start_time)
|
| 183 |
-
dbs = [
|
| 184 |
-
{
|
| 185 |
-
"uuid": k,
|
| 186 |
-
"name": v["db_name"],
|
| 187 |
-
"size_mb": v["size_mb"],
|
| 188 |
-
"created_at": int(v["created_at"]),
|
| 189 |
-
}
|
| 190 |
-
for k, v in db_registry.items()
|
| 191 |
-
]
|
| 192 |
return render_template_string(
|
| 193 |
DASHBOARD_TEMPLATE,
|
| 194 |
system_stats=system_stats,
|
|
@@ -242,18 +248,18 @@ def create_db():
|
|
| 242 |
cur.close()
|
| 243 |
conn.close()
|
| 244 |
except Exception as e:
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
})
|
| 257 |
|
| 258 |
@app.route("/api/<db_uuid>/query", methods=["POST"])
|
| 259 |
def db_query(db_uuid):
|
|
@@ -268,24 +274,16 @@ def db_query(db_uuid):
|
|
| 268 |
conn = get_mysql_connection(database=db_name)
|
| 269 |
cur = conn.cursor()
|
| 270 |
cur.execute(query)
|
| 271 |
-
rows = None
|
| 272 |
-
if cur.description is not None:
|
| 273 |
-
rows = cur.fetchall()
|
| 274 |
try:
|
| 275 |
conn.commit()
|
| 276 |
except Exception:
|
| 277 |
conn.rollback()
|
| 278 |
cur.close()
|
| 279 |
conn.close()
|
| 280 |
-
return jsonify({
|
| 281 |
-
"status": "ok",
|
| 282 |
-
"rows": rows,
|
| 283 |
-
})
|
| 284 |
except Exception as e:
|
| 285 |
-
return jsonify({
|
| 286 |
-
"status": "error",
|
| 287 |
-
"error": str(e),
|
| 288 |
-
}), 500
|
| 289 |
|
| 290 |
if __name__ == "__main__":
|
| 291 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
import time
|
| 3 |
import uuid
|
| 4 |
import psutil
|
| 5 |
+
import subprocess
|
| 6 |
import pymysql
|
| 7 |
from flask import Flask, jsonify, request, abort, render_template_string
|
| 8 |
|
|
|
|
| 22 |
start_time = time.time()
|
| 23 |
db_registry = {}
|
| 24 |
|
| 25 |
+
def ensure_mysql_running():
|
| 26 |
+
try:
|
| 27 |
+
conn = pymysql.connect(
|
| 28 |
+
host=MYSQL_CONFIG["host"],
|
| 29 |
+
port=MYSQL_CONFIG["port"],
|
| 30 |
+
user=MYSQL_CONFIG["user"],
|
| 31 |
+
password=MYSQL_CONFIG["password"]
|
| 32 |
+
)
|
| 33 |
+
conn.close()
|
| 34 |
+
except Exception:
|
| 35 |
+
try:
|
| 36 |
+
subprocess.Popen(["mysqld_safe"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 37 |
+
time.sleep(5)
|
| 38 |
+
except Exception:
|
| 39 |
+
try:
|
| 40 |
+
subprocess.Popen(["service", "mysql", "start"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 41 |
+
time.sleep(5)
|
| 42 |
+
except Exception:
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
ensure_mysql_running()
|
| 46 |
+
|
| 47 |
DASHBOARD_TEMPLATE = """
|
| 48 |
<!doctype html>
|
| 49 |
<html>
|
| 50 |
<head>
|
| 51 |
<title>MySQL DB Manager Dashboard</title>
|
| 52 |
<style>
|
| 53 |
+
body { font-family: Arial, sans-serif; margin: 20px; background-color: #121212; color: #e0e0e0; }
|
| 54 |
+
h1, h2 { margin-bottom: 0.3em; color: #ffffff; }
|
| 55 |
.section { margin-bottom: 2em; }
|
| 56 |
+
table { border-collapse: collapse; width: 100%; margin-top: 0.5em; background-color: #1e1e1e; color: #e0e0e0; }
|
| 57 |
+
th, td { border: 1px solid #333; padding: 6px 8px; font-size: 14px; }
|
| 58 |
+
th { background: #2a2a2a; text-align: left; color: #fff; }
|
| 59 |
+
code { background: #2a2a2a; padding: 2px 4px; color: #f5c518; }
|
| 60 |
+
.small { font-size: 12px; color: #aaa; }
|
| 61 |
+
input[type="text"], input[type="number"] { padding: 4px; background: #1e1e1e; color: #e0e0e0; border: 1px solid #333; }
|
| 62 |
+
button { padding: 6px 10px; cursor: pointer; background: #1565c0; color: #fff; border: none; border-radius: 3px; }
|
| 63 |
+
button:hover { background: #1e88e5; }
|
| 64 |
+
.error { color: #ff5252; }
|
| 65 |
+
a, a:visited { color: #90caf9; }
|
| 66 |
</style>
|
| 67 |
</head>
|
| 68 |
<body>
|
|
|
|
| 182 |
disk_used_gb = disk.used / (1024**3)
|
| 183 |
disk_percent = disk.percent
|
| 184 |
return {
|
| 185 |
+
"cpu": {"usage_percent": cpu_percent, "cores_detected": cpu_count},
|
| 186 |
+
"ram": {"total_gb": ram_total_gb, "used_gb": ram_used_gb, "usage_percent": ram_percent},
|
| 187 |
+
"disk": {"total_gb": disk_total_gb, "used_gb": disk_used_gb, "usage_percent": disk_percent},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
def total_allocated_mb():
|
|
|
|
| 194 |
def dashboard():
|
| 195 |
system_stats = get_system_stats()
|
| 196 |
uptime_seconds = int(time.time() - start_time)
|
| 197 |
+
dbs = [{"uuid": k, "name": v["db_name"], "size_mb": v["size_mb"], "created_at": int(v["created_at"])} for k, v in db_registry.items()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
return render_template_string(
|
| 199 |
DASHBOARD_TEMPLATE,
|
| 200 |
system_stats=system_stats,
|
|
|
|
| 248 |
cur.close()
|
| 249 |
conn.close()
|
| 250 |
except Exception as e:
|
| 251 |
+
ensure_mysql_running()
|
| 252 |
+
try:
|
| 253 |
+
conn = get_mysql_connection()
|
| 254 |
+
conn.autocommit(True)
|
| 255 |
+
cur = conn.cursor()
|
| 256 |
+
cur.execute(f"CREATE DATABASE `{db_name}`")
|
| 257 |
+
cur.close()
|
| 258 |
+
conn.close()
|
| 259 |
+
except Exception as e2:
|
| 260 |
+
return jsonify({"error": f"Failed to create database: {e2}"}), 500
|
| 261 |
+
db_registry[db_uuid] = {"db_name": db_name, "size_mb": size_mb, "created_at": time.time()}
|
| 262 |
+
return jsonify({"uuid": db_uuid, "db_name": db_name, "size_mb": size_mb, "query_endpoint": f"/api/{db_uuid}/query"})
|
| 263 |
|
| 264 |
@app.route("/api/<db_uuid>/query", methods=["POST"])
|
| 265 |
def db_query(db_uuid):
|
|
|
|
| 274 |
conn = get_mysql_connection(database=db_name)
|
| 275 |
cur = conn.cursor()
|
| 276 |
cur.execute(query)
|
| 277 |
+
rows = cur.fetchall() if cur.description else None
|
|
|
|
|
|
|
| 278 |
try:
|
| 279 |
conn.commit()
|
| 280 |
except Exception:
|
| 281 |
conn.rollback()
|
| 282 |
cur.close()
|
| 283 |
conn.close()
|
| 284 |
+
return jsonify({"status": "ok", "rows": rows})
|
|
|
|
|
|
|
|
|
|
| 285 |
except Exception as e:
|
| 286 |
+
return jsonify({"status": "error", "error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
if __name__ == "__main__":
|
| 289 |
app.run(host="0.0.0.0", port=7860)
|