"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeviceLogController = void 0; const deviceLog_1 = require("../models/deviceLog"); const authLog_1 = require("../models/authLog"); const systemLog_1 = require("../models/systemLog"); const helpers_1 = require("../utils/helpers"); class DeviceLogController { static async getDeviceLogs(req, res) { try { const clientid = (0, helpers_1.toString)(req.params.clientid); const page = Number((0, helpers_1.toString)(req.query.page)) || 1; const limit = Number((0, helpers_1.toString)(req.query.limit)) || 50; const offset = (page - 1) * limit; const event_type = (0, helpers_1.toString)(req.query.event_type); const start_time = (0, helpers_1.toString)(req.query.start_time); const end_time = (0, helpers_1.toString)(req.query.end_time); const filters = {}; if (event_type) { filters.event_type = event_type; } if (start_time) { filters.start_time = new Date(start_time); } if (end_time) { filters.end_time = new Date(end_time); } const { logs, total } = await deviceLog_1.DeviceLogModel.getByClientId(clientid, filters, limit, offset); res.status(200).json({ success: true, data: logs, pagination: { page, limit, total, pages: Math.ceil(total / limit) } }); } catch (error) { console.error('获取设备日志失败:', error); res.status(500).json({ success: false, message: '获取设备日志失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceConnectLogs(req, res) { try { const { clientid } = req.params; const limit = Number((0, helpers_1.toString)(req.query.limit)) || 100; const clientidStr = (0, helpers_1.toString)(clientid); const logs = await deviceLog_1.DeviceLogModel.getConnectDisconnectLogs(clientidStr, limit); res.status(200).json({ success: true, data: logs }); } catch (error) { console.error('获取设备连接日志失败:', error); res.status(500).json({ success: false, message: '获取设备连接日志失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDevicePublishLogs(req, res) { try { const { clientid } = req.params; const limit = Number((0, helpers_1.toString)(req.query.limit)) || 100; const clientidStr = (0, helpers_1.toString)(clientid); const logs = await deviceLog_1.DeviceLogModel.getPublishLogs(clientidStr, limit); res.status(200).json({ success: true, data: logs }); } catch (error) { console.error('获取设备发布日志失败:', error); res.status(500).json({ success: false, message: '获取设备发布日志失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceSubscribeLogs(req, res) { try { const { clientid } = req.params; const limit = Number((0, helpers_1.toString)(req.query.limit)) || 100; const clientidStr = (0, helpers_1.toString)(clientid); const logs = await deviceLog_1.DeviceLogModel.getSubscribeLogs(clientidStr, limit); res.status(200).json({ success: true, data: logs }); } catch (error) { console.error('获取设备订阅日志失败:', error); res.status(500).json({ success: false, message: '获取设备订阅日志失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceLogStats(req, res) { try { const { clientid } = req.params; const clientidStr = (0, helpers_1.toString)(clientid); const eventStats = await deviceLog_1.DeviceLogModel.getEventTypesStats(clientidStr); res.status(200).json({ success: true, data: eventStats }); } catch (error) { console.error('获取设备日志统计失败:', error); res.status(500).json({ success: false, message: '获取设备日志统计失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceDailyStats(req, res) { try { const { clientid } = req.params; const days = Number((0, helpers_1.toString)(req.query.days)) || 7; const clientidStr = (0, helpers_1.toString)(clientid); const stats = await deviceLog_1.DeviceLogModel.getDailyStats(clientidStr, days); res.status(200).json({ success: true, data: stats }); } catch (error) { console.error('获取设备每日统计失败:', error); res.status(500).json({ success: false, message: '获取设备每日统计失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceAuthLogs(req, res) { try { const clientid = (0, helpers_1.toString)(req.params.clientid); const page = Number((0, helpers_1.toString)(req.query.page)) || 1; const limit = Number((0, helpers_1.toString)(req.query.limit)) || 50; const offset = (page - 1) * limit; const operation_type = (0, helpers_1.toString)(req.query.operation_type); const result = (0, helpers_1.toString)(req.query.result); const filters = { clientid }; if (operation_type) { filters.operation_type = operation_type; } if (result) { filters.result = result; } const logs = await authLog_1.AuthLogModel.getByMultipleConditions(filters, undefined, undefined, limit, offset); const total = await authLog_1.AuthLogModel.getCountByClientid(clientid); res.status(200).json({ success: true, data: logs, pagination: { page, limit, total, pages: Math.ceil(total / limit) } }); } catch (error) { console.error('获取设备认证日志失败:', error); res.status(500).json({ success: false, message: '获取设备认证日志失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceSystemLogs(req, res) { try { const { clientid } = req.params; const page = Number((0, helpers_1.toString)(req.query.page)) || 1; const limit = Number((0, helpers_1.toString)(req.query.limit)) || 50; const offset = (page - 1) * limit; const level = (0, helpers_1.toString)(req.query.level); const source = (0, helpers_1.toString)(req.query.source); const filters = {}; if (level) { filters.level = level; } if (source) { filters.source = source; } const logs = await systemLog_1.SystemLogModel.getByMultipleConditions(filters, undefined, undefined, limit, offset, ['message', 'source', 'module', 'details']); const total = await systemLog_1.SystemLogModel.getCount(); res.status(200).json({ success: true, data: logs, pagination: { page, limit, total, pages: Math.ceil(total / limit) } }); } catch (error) { console.error('获取系统日志失败:', error); res.status(500).json({ success: false, message: '获取系统日志失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceOverview(req, res) { try { const { clientid } = req.params; const clientidStr = (0, helpers_1.toString)(clientid); const [eventStats, authLogs, recentLogs] = await Promise.all([ deviceLog_1.DeviceLogModel.getEventTypesStats(clientidStr), authLog_1.AuthLogModel.getByClientid(clientidStr, 10), deviceLog_1.DeviceLogModel.getByClientId(clientidStr, undefined, 10, 0) ]); const authSuccess = authLogs.filter((log) => log.result === 'success').length; const authFailure = authLogs.filter((log) => log.result === 'failure').length; res.status(200).json({ success: true, data: { event_stats: eventStats, auth_stats: { success: authSuccess, failure: authFailure, total: authLogs.length }, recent_logs: recentLogs.logs, total_logs: recentLogs.total } }); } catch (error) { console.error('获取设备概览失败:', error); res.status(500).json({ success: false, message: '获取设备概览失败', error: error instanceof Error ? error.message : '未知错误' }); } } static async getDeviceAuthStats(req, res) { try { const { clientid } = req.params; const clientidStr = (0, helpers_1.toString)(clientid); const authLogs = await authLog_1.AuthLogModel.getByClientid(clientidStr); const success = authLogs.filter((log) => log.result === 'success').length; const failure = authLogs.filter((log) => log.result === 'failure').length; const operationStats = await authLog_1.AuthLogModel.getOperationTypeStats(); res.status(200).json({ success: true, data: { success, failure, total: authLogs.length, operation_stats: operationStats } }); } catch (error) { console.error('获取设备认证统计失败:', error); res.status(500).json({ success: false, message: '获取设备认证统计失败', error: error instanceof Error ? error.message : '未知错误' }); } } } exports.DeviceLogController = DeviceLogController; //# sourceMappingURL=deviceLogController.js.map