dashboard fix/online off , and warehouse

This commit is contained in:
2026-04-20 12:01:09 +03:00
parent 31ab577c25
commit 2cce95d557
2 changed files with 137 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
from flask import Blueprint, render_template, request, jsonify
from db import get_db
import json
from datetime import datetime
from datetime import datetime, timedelta
dashboard = Blueprint('dashboard', __name__)
@@ -55,6 +55,33 @@ def get_towns():
return jsonify(towns)
# ------------------------------------------------------------------
# GET /dashboard/client-status
# Returns whether the Tampermonkey client is considered online.
# Online = at least one town_state row updated within the last 60 s.
# ------------------------------------------------------------------
@dashboard.route('/dashboard/client-status', methods=['GET'])
def client_status():
conn = get_db()
row = conn.execute('''
SELECT MAX(updated_at) AS last_seen FROM town_state
''').fetchone()
conn.close()
last_seen = row['last_seen'] if row else None
if last_seen:
try:
dt = datetime.fromisoformat(last_seen)
age_s = (datetime.utcnow() - dt).total_seconds()
online = age_s <= 60
except Exception:
online = False
else:
online = False
return jsonify({'online': online, 'last_seen': last_seen})
# ------------------------------------------------------------------
# GET /dashboard/commands
# Returns command history (last 50) for the log panel.
@@ -93,7 +120,22 @@ def create_command():
if cmd_type not in ('build', 'recruit'):
return jsonify({'error': 'type must be build or recruit'}), 400
# Reject if the Tampermonkey client is offline (no state push in last 60 s)
conn = get_db()
row = conn.execute('SELECT MAX(updated_at) AS last_seen FROM town_state').fetchone()
last_seen = row['last_seen'] if row else None
client_online = False
if last_seen:
try:
dt = datetime.fromisoformat(last_seen)
client_online = (datetime.utcnow() - dt).total_seconds() <= 60
except Exception:
pass
if not client_online:
conn.close()
return jsonify({'error': 'client_offline', 'message': 'Το script είναι offline — δεν μπορείτε να στείλετε εντολές.'}), 503
c = conn.cursor()
c.execute('''
INSERT INTO commands (town_id, town_name, type, payload, status, created_at, updated_at)
@@ -120,9 +162,29 @@ def create_command():
@dashboard.route('/dashboard/commands/<int:cmd_id>', methods=['DELETE'])
def cancel_command(cmd_id):
conn = get_db()
conn.execute('''
DELETE FROM commands WHERE id = ?
''', (cmd_id,))
conn.execute('DELETE FROM commands WHERE id = ?', (cmd_id,))
conn.commit()
conn.close()
return jsonify({'ok': True})
# ------------------------------------------------------------------
# POST /dashboard/commands/fail-stale
# Mark all pending/executing commands as failed (called by dashboard
# when it detects the client has gone offline).
# ------------------------------------------------------------------
@dashboard.route('/dashboard/commands/fail-stale', methods=['POST'])
def fail_stale_commands():
conn = get_db()
c = conn.cursor()
c.execute('''
UPDATE commands
SET status = 'failed',
result_msg = 'Client went offline',
updated_at = ?
WHERE status IN ('pending', 'executing')
''', (datetime.utcnow().isoformat(),))
affected = c.rowcount
conn.commit()
conn.close()
return jsonify({'ok': True, 'failed': affected})