updated to allow config of the new server

This commit is contained in:
ske087
2026-06-26 10:11:44 +03:00
parent a48b3afb83
commit dbc1c882eb
14 changed files with 197 additions and 1127 deletions
+66 -3
View File
@@ -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)