Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import json
|
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
lightModeStyle = """
|
|
|
|
| 9 |
* {
|
| 10 |
margin: 0;
|
| 11 |
padding: 0;
|
|
@@ -106,8 +107,6 @@ body {
|
|
| 106 |
width: 20px;
|
| 107 |
height: 20px;
|
| 108 |
border-radius: 4px;
|
| 109 |
-
/* You might want to add: */
|
| 110 |
-
/* fill: currentColor; */
|
| 111 |
}
|
| 112 |
.metric-grid {
|
| 113 |
display: grid;
|
|
@@ -144,12 +143,10 @@ body {
|
|
| 144 |
}
|
| 145 |
.status-online {
|
| 146 |
background-color: #2ecc71;
|
| 147 |
-
/* color: #2ecc71; <-- This is redundant, as background-color sets the color */
|
| 148 |
box-shadow: 0 0 5px rgba(46, 204, 113, 0.4);
|
| 149 |
}
|
| 150 |
.status-offline {
|
| 151 |
background-color: #e74c3c;
|
| 152 |
-
/* color: #e74c3c; <-- Redundant */
|
| 153 |
box-shadow: 0 0 5px rgba(231, 76, 60, 0.4);
|
| 154 |
}
|
| 155 |
@keyframes fadeIn {
|
|
@@ -198,14 +195,17 @@ body {
|
|
| 198 |
}
|
| 199 |
"""
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
| 202 |
<!DOCTYPE html>
|
| 203 |
<html lang="zh">
|
| 204 |
<head>
|
| 205 |
<meta charset="UTF-8">
|
| 206 |
<title>HF Space Monitor</title>
|
| 207 |
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🚀</text></svg>">
|
| 208 |
-
<style>{}</style>
|
| 209 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" integrity="sha512-9usAa10IRO0HhonpyAIVpjrylPvoDwiPUiKdWk5t3PyolY1cOd4DSE0Ga+ri4AuTroPR5aQvXU9xC6qOPnzFeg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
|
| 210 |
</head>
|
| 211 |
<body>
|
|
@@ -224,7 +224,7 @@ htmlTemplate = """
|
|
| 224 |
</div>
|
| 225 |
</div>
|
| 226 |
<script>
|
| 227 |
-
const username = '{}';
|
| 228 |
|
| 229 |
async function fetchInstances() {{
|
| 230 |
try {{
|
|
@@ -240,7 +240,7 @@ htmlTemplate = """
|
|
| 240 |
class MetricsManager {{
|
| 241 |
constructor() {{
|
| 242 |
this.eventSources = new Map();
|
| 243 |
-
this.serversData = new Map();
|
| 244 |
this.instanceOwners = new Map();
|
| 245 |
this.spaceIds = new Map();
|
| 246 |
}}
|
|
@@ -271,7 +271,7 @@ htmlTemplate = """
|
|
| 271 |
eventSource.onerror = (error) => {{
|
| 272 |
console.error(`EventSource 错误 (${{instanceId}}):`, error);
|
| 273 |
eventSource.close();
|
| 274 |
-
this.eventSources.delete(instanceId);
|
| 275 |
}};
|
| 276 |
|
| 277 |
}}
|
|
@@ -279,7 +279,7 @@ htmlTemplate = """
|
|
| 279 |
disconnectAll() {{
|
| 280 |
this.eventSources.forEach(es => es.close());
|
| 281 |
this.eventSources.clear();
|
| 282 |
-
this.serversData.clear();
|
| 283 |
}}
|
| 284 |
}}
|
| 285 |
|
|
@@ -404,8 +404,7 @@ htmlTemplate = """
|
|
| 404 |
</script>
|
| 405 |
</body>
|
| 406 |
</html>
|
| 407 |
-
"""
|
| 408 |
-
|
| 409 |
|
| 410 |
|
| 411 |
@app.route('/')
|
|
@@ -424,7 +423,7 @@ def stream_metrics(username, instance_id):
|
|
| 424 |
def generate():
|
| 425 |
try:
|
| 426 |
response = requests.get(url, stream=True, headers={"Accept": "text/event-stream"}, timeout=15)
|
| 427 |
-
response.raise_for_status()
|
| 428 |
|
| 429 |
buffer = ""
|
| 430 |
for chunk in response.iter_content(chunk_size=1024, decode_unicode=True):
|
|
@@ -433,7 +432,7 @@ def stream_metrics(username, instance_id):
|
|
| 433 |
while "\n\n" in buffer:
|
| 434 |
event_data, buffer = buffer.split("\n\n", 1)
|
| 435 |
lines = event_data.split("\n")
|
| 436 |
-
event_type = "message"
|
| 437 |
data_lines = []
|
| 438 |
|
| 439 |
for line in lines:
|
|
@@ -444,7 +443,6 @@ def stream_metrics(username, instance_id):
|
|
| 444 |
|
| 445 |
if event_type == "metric":
|
| 446 |
try:
|
| 447 |
-
# Parse and re-serialize to ensure valid JSON
|
| 448 |
json_data = json.loads("".join(data_lines))
|
| 449 |
yield f"event: {event_type}\ndata: {json.dumps(json_data)}\n\n"
|
| 450 |
except json.JSONDecodeError as e:
|
|
@@ -453,15 +451,13 @@ def stream_metrics(username, instance_id):
|
|
| 453 |
|
| 454 |
except requests.exceptions.RequestException as e:
|
| 455 |
print(f"Request Exception: {e}")
|
| 456 |
-
yield f"event: error\ndata: Connection error\n\n"
|
| 457 |
except Exception as e:
|
| 458 |
print(f"An error occurred: {e}")
|
| 459 |
yield f"event: error\ndata: An unexpected error occurred\n\n"
|
| 460 |
|
| 461 |
-
|
| 462 |
return Response(generate(), mimetype='text/event-stream')
|
| 463 |
|
| 464 |
|
| 465 |
if __name__ == '__main__':
|
| 466 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
| 467 |
-
|
|
|
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
lightModeStyle = """
|
| 9 |
+
/* ... (Your CSS styles, same as before) ... */
|
| 10 |
* {
|
| 11 |
margin: 0;
|
| 12 |
padding: 0;
|
|
|
|
| 107 |
width: 20px;
|
| 108 |
height: 20px;
|
| 109 |
border-radius: 4px;
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
.metric-grid {
|
| 112 |
display: grid;
|
|
|
|
| 143 |
}
|
| 144 |
.status-online {
|
| 145 |
background-color: #2ecc71;
|
|
|
|
| 146 |
box-shadow: 0 0 5px rgba(46, 204, 113, 0.4);
|
| 147 |
}
|
| 148 |
.status-offline {
|
| 149 |
background-color: #e74c3c;
|
|
|
|
| 150 |
box-shadow: 0 0 5px rgba(231, 76, 60, 0.4);
|
| 151 |
}
|
| 152 |
@keyframes fadeIn {
|
|
|
|
| 195 |
}
|
| 196 |
"""
|
| 197 |
|
| 198 |
+
# Define USERNAME *before* using it in htmlTemplate
|
| 199 |
+
USERNAME = os.environ.get("USERNAME", "yangtb24")
|
| 200 |
+
|
| 201 |
+
htmlTemplate = f"""
|
| 202 |
<!DOCTYPE html>
|
| 203 |
<html lang="zh">
|
| 204 |
<head>
|
| 205 |
<meta charset="UTF-8">
|
| 206 |
<title>HF Space Monitor</title>
|
| 207 |
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🚀</text></svg>">
|
| 208 |
+
<style>{lightModeStyle}</style>
|
| 209 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" integrity="sha512-9usAa10IRO0HhonpyAIVpjrylPvoDwiPUiKdWk5t3PyolY1cOd4DSE0Ga+ri4AuTroPR5aQvXU9xC6qOPnzFeg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
|
| 210 |
</head>
|
| 211 |
<body>
|
|
|
|
| 224 |
</div>
|
| 225 |
</div>
|
| 226 |
<script>
|
| 227 |
+
const username = '{USERNAME}';
|
| 228 |
|
| 229 |
async function fetchInstances() {{
|
| 230 |
try {{
|
|
|
|
| 240 |
class MetricsManager {{
|
| 241 |
constructor() {{
|
| 242 |
this.eventSources = new Map();
|
| 243 |
+
this.serversData = new Map();
|
| 244 |
this.instanceOwners = new Map();
|
| 245 |
this.spaceIds = new Map();
|
| 246 |
}}
|
|
|
|
| 271 |
eventSource.onerror = (error) => {{
|
| 272 |
console.error(`EventSource 错误 (${{instanceId}}):`, error);
|
| 273 |
eventSource.close();
|
| 274 |
+
this.eventSources.delete(instanceId);
|
| 275 |
}};
|
| 276 |
|
| 277 |
}}
|
|
|
|
| 279 |
disconnectAll() {{
|
| 280 |
this.eventSources.forEach(es => es.close());
|
| 281 |
this.eventSources.clear();
|
| 282 |
+
this.serversData.clear();
|
| 283 |
}}
|
| 284 |
}}
|
| 285 |
|
|
|
|
| 404 |
</script>
|
| 405 |
</body>
|
| 406 |
</html>
|
| 407 |
+
"""
|
|
|
|
| 408 |
|
| 409 |
|
| 410 |
@app.route('/')
|
|
|
|
| 423 |
def generate():
|
| 424 |
try:
|
| 425 |
response = requests.get(url, stream=True, headers={"Accept": "text/event-stream"}, timeout=15)
|
| 426 |
+
response.raise_for_status()
|
| 427 |
|
| 428 |
buffer = ""
|
| 429 |
for chunk in response.iter_content(chunk_size=1024, decode_unicode=True):
|
|
|
|
| 432 |
while "\n\n" in buffer:
|
| 433 |
event_data, buffer = buffer.split("\n\n", 1)
|
| 434 |
lines = event_data.split("\n")
|
| 435 |
+
event_type = "message"
|
| 436 |
data_lines = []
|
| 437 |
|
| 438 |
for line in lines:
|
|
|
|
| 443 |
|
| 444 |
if event_type == "metric":
|
| 445 |
try:
|
|
|
|
| 446 |
json_data = json.loads("".join(data_lines))
|
| 447 |
yield f"event: {event_type}\ndata: {json.dumps(json_data)}\n\n"
|
| 448 |
except json.JSONDecodeError as e:
|
|
|
|
| 451 |
|
| 452 |
except requests.exceptions.RequestException as e:
|
| 453 |
print(f"Request Exception: {e}")
|
| 454 |
+
yield f"event: error\ndata: Connection error\n\n"
|
| 455 |
except Exception as e:
|
| 456 |
print(f"An error occurred: {e}")
|
| 457 |
yield f"event: error\ndata: An unexpected error occurred\n\n"
|
| 458 |
|
|
|
|
| 459 |
return Response(generate(), mimetype='text/event-stream')
|
| 460 |
|
| 461 |
|
| 462 |
if __name__ == '__main__':
|
| 463 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|