import { Column, Entity, Index, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm'; /** * 运行上报(现网 runtime_reports 表) */ @Entity('runtime_reports') export class RuntimeReportEntity { @PrimaryGeneratedColumn({ name: 'id', type: 'bigint', unsigned: true, comment: '记录自增ID' }) id: number; @Index() @Column({ name: 'account', type: 'varchar', length: 64, default: '', comment: '运行账号(工号)' }) account: string; @Column({ name: 'device_id', type: 'varchar', length: 64, default: '', comment: '执行机设备ID' }) deviceId: string; @Index() @Column({ name: 'plugin_id', type: 'varchar', length: 64, default: '', comment: '插件ID' }) pluginId: string; @Column({ name: 'version', type: 'varchar', length: 32, default: '', comment: '插件版本号' }) version: string; @Index() @Column({ name: 'status', type: 'varchar', length: 32, default: '', comment: 'start/done/success/error/failed' }) status: string; @Column({ name: 'msg', type: 'text', comment: '结果/错误信息' }) msg: string; @Column({ name: 'duration_sec', type: 'int', unsigned: true, default: 0, comment: '运行时长(秒)' }) durationSec: number; @Index() @Column({ name: 'at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', comment: '上报时间' }) at: Date; } /** * 执行机设备(现网 devices 表,主键 device_id) */ @Entity('devices') export class DeviceEntity { @PrimaryColumn({ name: 'device_id', type: 'varchar', length: 64, comment: '执行机设备ID' }) deviceId: string; @Index() @Column({ name: 'account', type: 'varchar', length: 64, default: '', comment: '当前登录账号' }) account: string; @Column({ name: 'hostname', type: 'varchar', length: 128, default: '', comment: '主机名' }) hostname: string; @Column({ name: 'os', type: 'varchar', length: 64, default: '', comment: '操作系统' }) os: string; @Column({ name: 'ip', type: 'varchar', length: 64, default: '', comment: 'IP 地址' }) ip: string; @Column({ name: 'chrome_installed', type: 'tinyint', width: 1, default: 0, comment: '是否已安装 Chrome' }) chromeInstalled: number; @Column({ name: 'chrome_version', type: 'varchar', length: 32, default: '', comment: 'Chrome 版本号' }) chromeVersion: string; @Column({ name: 'chrome_ok', type: 'tinyint', width: 1, default: 0, comment: 'Chrome 是否就绪' }) chromeOk: number; @Column({ name: 'client_version', type: 'varchar', length: 32, default: '', comment: '客户端(壳子)版本号,心跳上报' }) clientVersion: string; @Index() @Column({ name: 'last_seen', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', comment: '最后在线时间' }) lastSeen: Date; @Column({ name: 'status', type: 'tinyint', width: 1, default: 1, comment: '1=在线 0=离线' }) status: number; @Column({ name: 'created_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', comment: '首次上报时间' }) createdAt: Date; @Column({ name: 'updated_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP', comment: '更新时间' }) updatedAt: Date; } /** * 审计日志(现网 audit_logs 表) */ @Entity('audit_logs') export class AuditLogEntity { @PrimaryGeneratedColumn({ name: 'id', type: 'bigint', unsigned: true, comment: '日志自增ID' }) id: number; @Index() @Column({ name: 'account', type: 'varchar', length: 64, default: '', comment: '操作账号' }) account: string; @Index() @Column({ name: 'action', type: 'varchar', length: 64, comment: '动作' }) action: string; @Column({ name: 'target', type: 'varchar', length: 128, default: '', comment: '操作对象' }) target: string; @Column({ name: 'detail', type: 'text', comment: '操作详情(JSON)' }) detail: string; @Index() @Column({ name: 'at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', comment: '操作时间' }) at: Date; } /** * 通知(现网 notifications 表) */ @Entity('notifications') export class NotificationEntity { @PrimaryGeneratedColumn({ name: 'id', type: 'int', unsigned: true, comment: '通知ID' }) id: number; @Column({ name: 'title', type: 'varchar', length: 200, comment: '标题' }) title: string; @Column({ name: 'content', type: 'text', comment: '内容' }) content: string; @Column({ name: 'level', type: 'varchar', length: 16, default: 'info', comment: 'info/warning/important' }) level: string; @Column({ name: 'target', type: 'varchar', length: 16, default: 'all', comment: 'all/account' }) target: string; @Column({ name: 'account', type: 'varchar', length: 64, default: '', comment: 'target=account 时的目标账号' }) account: string; @Column({ name: 'created_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', comment: '创建时间' }) createdAt: Date; @Column({ name: 'expires_at', type: 'datetime', nullable: true, comment: '过期时间' }) expiresAt: Date; @Column({ name: 'active', type: 'tinyint', width: 1, default: 1, comment: '1=启用 0=停用' }) active: number; } /** * 运行日志(集中留存,客户端回传,按 run_id 幂等 upsert)。 * 注意:trigger_by 列原名 trigger(MySQL 保留字),改名避免 TypeORM 自动建表/查询语法错误; * 对外 JSON 字段仍为 trigger(SELECT ... AS trigger)。 */ @Entity('run_logs') export class RunLogEntity { @PrimaryGeneratedColumn({ name: 'id', type: 'bigint', unsigned: true, comment: '记录自增ID' }) id: number; @Index({ unique: true }) @Column({ name: 'run_id', type: 'varchar', length: 64, comment: '运行ID(客户端生成 32 位 hex,唯一)' }) runId: string; @Index() @Column({ name: 'account', type: 'varchar', length: 64, default: '', comment: '运行账号(工号)' }) account: string; @Index() @Column({ name: 'device_id', type: 'varchar', length: 64, default: '', comment: '执行机设备ID' }) deviceId: string; @Index() @Column({ name: 'plugin_id', type: 'varchar', length: 64, default: '', comment: '插件ID' }) pluginId: string; @Column({ name: 'name', type: 'varchar', length: 128, default: '', comment: '插件名称' }) name: string; @Column({ name: 'version', type: 'varchar', length: 32, default: '', comment: '插件版本号' }) version: string; @Column({ name: 'trigger_by', type: 'varchar', length: 16, default: 'manual', comment: '触发方式:manual=手动 schedule=定时' }) triggerBy: string; @Column({ name: 'schedule_id', type: 'varchar', length: 64, default: '', comment: '定时任务ID' }) scheduleId: string; @Index() @Column({ name: 'status', type: 'varchar', length: 32, default: '', comment: 'running/done/error' }) status: string; @Column({ name: 'ok', type: 'tinyint', width: 1, default: 0, comment: '是否成功:1=是 0=否' }) ok: number; @Column({ name: 'error', type: 'text', comment: '错误信息' }) error: string; @Column({ name: 'summary', type: 'text', comment: '运行结论摘要' }) summary: string; @Column({ name: 'started', type: 'bigint', unsigned: true, default: 0, comment: '开始时间(epoch 秒)' }) started: number; @Column({ name: 'finished', type: 'bigint', unsigned: true, default: 0, comment: '结束时间(epoch 秒)' }) finished: number; @Column({ name: 'duration_sec', type: 'int', unsigned: true, default: 0, comment: '运行时长(秒)' }) durationSec: number; @Column({ name: 'log_count', type: 'int', unsigned: true, default: 0, comment: '日志条数' }) logCount: number; @Column({ name: 'logs', type: 'mediumtext', comment: '运行日志全文(JSON 数组 [{t,level,msg}])' }) logs: string; @Index() @Column({ name: 'uploaded_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', comment: '回传时间' }) uploadedAt: Date; } /** * 客户端壳日志元数据(内容存 logs/shell/.log,增量追加) */ @Entity('device_logs') export class DeviceLogEntity { @PrimaryColumn({ name: 'device_id', type: 'varchar', length: 64, comment: '执行机设备ID(唯一)' }) deviceId: string; @Index() @Column({ name: 'account', type: 'varchar', length: 64, default: '', comment: '当前登录账号' }) account: string; @Column({ name: 'hostname', type: 'varchar', length: 128, default: '', comment: '主机名' }) hostname: string; @Column({ name: 'client_version', type: 'varchar', length: 32, default: '', comment: '客户端(壳子)版本号' }) clientVersion: string; @Column({ name: 'size_bytes', type: 'bigint', unsigned: true, default: 0, comment: '已回传壳日志累计字节' }) sizeBytes: number; @Column({ name: 'last_uploaded_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', comment: '最近一次回传时间' }) lastUploadedAt: Date; @Column({ name: 'updated_at', type: 'datetime', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP', comment: '更新时间' }) updatedAt: Date; }