From dbc1c882eb37c8eeca4b22405cafdfc4e02a219b Mon Sep 17 00:00:00 2001 From: ske087 Date: Fri, 26 Jun 2026 10:11:44 +0300 Subject: [PATCH] updated to allow config of the new server --- app/__init__.py | 3 + app/api/logs.py | 69 +++- app/api/wmt.py | 32 ++ app/web/main.py | 2 + app/web/wmt.py | 45 +-- templates/base.html | 44 +-- templates/dashboard_old.html | 263 -------------- templates/device_edit.html | 21 ++ templates/device_management_old.html | 507 --------------------------- templates/devices.html | 0 templates/wmt/devices.html | 83 ----- templates/wmt/index.html | 183 ---------- templates/wmt/requests.html | 70 ++-- templates/wmt/settings.html | 2 +- 14 files changed, 197 insertions(+), 1127 deletions(-) delete mode 100644 templates/dashboard_old.html delete mode 100644 templates/device_management_old.html delete mode 100644 templates/devices.html delete mode 100644 templates/wmt/devices.html delete mode 100644 templates/wmt/index.html diff --git a/app/__init__.py b/app/__init__.py index bfe3210..ceb98bc 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -217,10 +217,13 @@ def _register_context_processors(app): try: with get_db().get_session() as session: pending_wmt_count = session.query(WMTUpdateRequest).filter_by(status='pending').count() + unconfigured_count = session.query(WMTUpdateRequest).filter_by(status='to_be_configured').count() except Exception: pending_wmt_count = 0 + unconfigured_count = 0 return { 'app_name': 'Enhanced Server Monitoring', 'app_version': '2.0.0', 'pending_wmt_count': pending_wmt_count, + 'unconfigured_count': unconfigured_count, } \ No newline at end of file diff --git a/app/api/logs.py b/app/api/logs.py index 9dc3f58..95caa10 100644 --- a/app/api/logs.py +++ b/app/api/logs.py @@ -8,10 +8,12 @@ import hashlib from datetime import datetime from app.services.log_service import LogCompressionService from app.services.file_service import FileUploadService -from app.models import Device, LogEntry, FileUpload +from app.models import Device, LogEntry, FileUpload, WMTUpdateRequest from config.database_config import get_db import logging +logger = logging.getLogger(__name__) + # Create blueprint logs_bp = Blueprint('logs', __name__, url_prefix='/api/logs') @@ -68,6 +70,14 @@ def submit_log(): 'mac_address': data.get('mac_address') or (data.get('metadata') or {}).get('mac_address'), } + # ── Unconfigured device interception ────────────────────────────────── + # A device with work_place=notconfig must NOT be added to the devices + # table. Instead, create/refresh a "to_be_configured" update request so + # it appears in the dedicated section on the Update Requests page. + if data['nume_masa'].strip().lower() == 'notconfig': + return _handle_notconfig_device(device_info, data.get('log_message', '')) + # ───────────────────────────────────────────────────────────────────── + # Process log with compression result = log_service.process_log_message( device_info=device_info, @@ -204,14 +214,67 @@ def upload_log_file(): 'success': False }), 500 +def _handle_notconfig_device(device_info: dict, log_message: str): + """ + A device reporting work_place='notconfig' is awaiting admin configuration. + We must NOT create a Device record for it. Instead we create / refresh a + WMTUpdateRequest with status='to_be_configured' so it surfaces under the + dedicated tab on the Update Requests page. + """ + mac = (device_info.get('mac_address') or '').strip().lower() + hostname = device_info.get('hostname', '') + ip = device_info.get('device_ip', '') + + try: + with get_db().get_session() as session: + existing = ( + session.query(WMTUpdateRequest) + .filter_by(mac_address=mac, status='to_be_configured') + .order_by(WMTUpdateRequest.submitted_at.desc()) + .first() + ) if mac else None + + if existing: + # Refresh the timestamp and any identity fields that may have changed + existing.submitted_at = datetime.utcnow() + existing.proposed_hostname = hostname or existing.proposed_hostname + existing.proposed_device_ip = ip or existing.proposed_device_ip + if log_message: + existing.admin_notes = f"Last log: {log_message[:200]}" + logger.debug(f'notconfig heartbeat refreshed for {mac or hostname}') + else: + req = WMTUpdateRequest( + mac_address=mac or None, + device_id=None, + proposed_device_name=None, + proposed_hostname=hostname or None, + proposed_device_ip=ip or None, + client_config_mtime=datetime.utcnow().isoformat(), + submitted_at=datetime.utcnow(), + status='to_be_configured', + admin_notes=f"Last log: {log_message[:200]}" if log_message else None, + ) + session.add(req) + logger.info(f'New unconfigured device queued: {hostname} / {mac or "no-mac"}') + + return jsonify({ + 'success': True, + 'status': 'to_be_configured', + 'message': 'Device is not configured. Awaiting admin assignment.', + }), 202 + except Exception as e: + logger.error(f'Error handling notconfig device: {e}') + return jsonify({'success': False, 'error': str(e)}), 500 + + @logs_bp.route('/query', methods=['GET']) def query_logs(): """ Query logs with filters and pagination - + Query parameters: - device_id: Filter by device ID - - hostname: Filter by hostname + - hostname: Filter by hostname - severity: Filter by severity level - start_time: Start time (ISO format) - end_time: End time (ISO format) diff --git a/app/api/wmt.py b/app/api/wmt.py index e6f76e2..2bd6e3b 100644 --- a/app/api/wmt.py +++ b/app/api/wmt.py @@ -197,6 +197,38 @@ def submit_update_request(): # ── Outcome 3: unknown device ───────────────────────────── if not device: + # Devices with work_place=notconfig are unconfigured and must not + # be registered. Route them to their own "to_be_configured" bucket. + if not proposed_name or proposed_name.lower() == 'notconfig': + existing_tbc = ( + session.query(WMTUpdateRequest) + .filter_by(mac_address=mac, status='to_be_configured') + .order_by(WMTUpdateRequest.submitted_at.desc()) + .first() + ) + if existing_tbc: + existing_tbc.submitted_at = datetime.utcnow() + existing_tbc.proposed_hostname = proposed_hostname or existing_tbc.proposed_hostname + existing_tbc.proposed_device_ip = proposed_ip or existing_tbc.proposed_device_ip + logger.debug(f'WMT notconfig heartbeat refreshed for {mac}') + else: + req = WMTUpdateRequest( + mac_address=mac, + device_id=None, + proposed_device_name=None, + proposed_hostname=proposed_hostname or None, + proposed_device_ip=proposed_ip or None, + client_config_mtime=data.get('client_config_mtime'), + submitted_at=datetime.utcnow(), + status='to_be_configured', + ) + session.add(req) + logger.info(f'WMT unconfigured device queued: {proposed_hostname} / {mac}') + return jsonify({ + 'status': 'to_be_configured', + 'message': 'Device has no work_place set. Awaiting admin assignment.', + }), 202 + # Check if a pending request with the same data already exists existing = ( session.query(WMTUpdateRequest) diff --git a/app/web/main.py b/app/web/main.py index 885eb79..8051361 100644 --- a/app/web/main.py +++ b/app/web/main.py @@ -454,6 +454,8 @@ def device_edit(device_id): device.device_type = request.form.get('device_type', '').strip() or 'unknown' device.description = request.form.get('description', '').strip() or None device.os_version = request.form.get('os_version', '').strip() or None + custom_url = request.form.get('custom_chrome_url', '').strip() + device.custom_chrome_url = custom_url if custom_url else None device.config_updated_at = datetime.utcnow() device.info_reviewed_at = datetime.utcnow() flash('Device updated.', 'success') diff --git a/app/web/wmt.py b/app/web/wmt.py index 23ede5a..449d75b 100644 --- a/app/web/wmt.py +++ b/app/web/wmt.py @@ -37,31 +37,8 @@ def _get_or_create_global_config(session): @wmt_web_bp.route('/') def index(): - """WMT management dashboard.""" - try: - with get_db().get_session() as session: - global_cfg = _get_or_create_global_config(session) - devices = session.query(Device).filter(Device.mac_address.isnot(None)).order_by(Device.nume_masa).all() - pending_count = session.query(WMTUpdateRequest).filter_by(status='pending').count() - recent_requests = ( - session.query(WMTUpdateRequest) - .order_by(WMTUpdateRequest.submitted_at.desc()) - .limit(5) - .all() - ) - return render_template( - 'wmt/index.html', - global_cfg=global_cfg, - devices=devices, - pending_count=pending_count, - recent_requests=recent_requests, - breadcrumbs=[{'url': url_for('wmt_web.index'), 'title': 'WMT Management'}], - ) - except Exception as e: - logger.error(f'WMT dashboard error: {e}') - flash(f'Error loading dashboard: {e}', 'error') - return render_template('wmt/index.html', global_cfg=None, devices=[], - pending_count=0, recent_requests=[]) + """Retired: the WMT dashboard is merged into the unified Devices page.""" + return redirect(url_for('main.devices')) # --------------------------------------------------------------------------- @@ -94,14 +71,14 @@ def settings(): 'wmt/settings.html', cfg=cfg, breadcrumbs=[ - {'url': url_for('wmt_web.index'), 'title': 'WMT Management'}, - {'url': url_for('wmt_web.settings'), 'title': 'Global Settings'}, + {'url': url_for('main.devices'), 'title': 'Devices'}, + {'url': url_for('wmt_web.settings'), 'title': 'WMT Settings'}, ], ) except Exception as e: logger.error(f'WMT settings error: {e}') flash(f'Error: {e}', 'error') - return redirect(url_for('wmt_web.index')) + return redirect(url_for('main.devices')) # --------------------------------------------------------------------------- @@ -119,21 +96,23 @@ def update_requests(): query = query.filter_by(status=status_filter) req_list = query.order_by(WMTUpdateRequest.submitted_at.desc()).all() pending_count = session.query(WMTUpdateRequest).filter_by(status='pending').count() + unconfigured_count = session.query(WMTUpdateRequest).filter_by(status='to_be_configured').count() return render_template( 'wmt/requests.html', requests=req_list, status_filter=status_filter, pending_count=pending_count, + unconfigured_count=unconfigured_count, breadcrumbs=[ - {'url': url_for('wmt_web.index'), 'title': 'WMT Management'}, + {'url': url_for('main.devices'), 'title': 'Devices'}, {'url': url_for('wmt_web.update_requests'), 'title': 'Update Requests'}, ], ) except Exception as e: logger.error(f'WMT requests list error: {e}') flash(f'Error: {e}', 'error') - return redirect(url_for('wmt_web.index')) + return redirect(url_for('main.devices')) @wmt_web_bp.route('/requests//accept', methods=['POST']) @@ -256,8 +235,7 @@ def device_new(): 'wmt/device_form.html', device=None, breadcrumbs=[ - {'url': url_for('wmt_web.index'), 'title': 'WMT Management'}, - {'url': url_for('wmt_web.devices'), 'title': 'Devices'}, + {'url': url_for('main.devices'), 'title': 'Devices'}, {'url': url_for('wmt_web.device_new'), 'title': 'New Device'}, ], ) @@ -291,8 +269,7 @@ def device_edit(device_id): 'wmt/device_form.html', device=device, breadcrumbs=[ - {'url': url_for('wmt_web.index'), 'title': 'WMT Management'}, - {'url': url_for('wmt_web.devices'), 'title': 'Devices'}, + {'url': url_for('main.devices'), 'title': 'Devices'}, {'url': url_for('wmt_web.device_edit', device_id=device_id), 'title': 'Edit'}, ], ) diff --git a/templates/base.html b/templates/base.html index a157185..bdff9ce 100644 --- a/templates/base.html +++ b/templates/base.html @@ -867,33 +867,28 @@