16 Commits

Author SHA1 Message Date
Misaka
018d524fe8 feat(logging-p0): add structured logging to database driver services
Add createLogger/trackDuration logging to mysql.ts, sql-server.ts, and
data-source.ts — the only database layer files without observability.
Connect/disconnect, query execution (with duration tracking), and
transaction lifecycle events are now logged. Passwords and parameter
values are excluded; SQL statements are capped at 100 chars.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-04 11:43:02 +08:00
Misaka
42b76c4de5 docs(logging): add comprehensive logging operations guide 2026-04-04 10:54:18 +08:00
Misaka
cfb80376ce feat(logging-p0): Wave 3 - Database DAO layer transformed with enhanced logging 2026-04-04 10:52:55 +08:00
Misaka
78a3066904 feat(logging-p0): complete Wave 2 - Auth/Extractor/Cleaner services transformed 2026-04-04 10:39:06 +08:00
Misaka
24d9bfebaf fix(logger): use app.isPackaged for log dir detection and add logging docs
Previously getLogDir() only checked app.isReady(), which caused
development builds to write logs to the user data directory instead
of the local project logs/ folder. Now uses app.isPackaged to
correctly distinguish production from development environments.

Also adds comprehensive logging system documentation and a debug
utility for verifying Electron environment detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-04 09:22:20 +08:00
Misaka
ba436cf374 feat(extraction): read headless mode from config instead of hardcoding
Add headless field to extraction config schema (default: true).
Extractor handler now reads globalConfig.extraction.headless instead
of hardcoding true. Users can set headless: false in config.yaml
to show the browser window during extraction for debugging.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 21:53:21 +08:00
Misaka
6413eef5b8 fix(logger): address code review findings
- Remove misleading await from audit-logger tests (functions are sync)
- Add cleanup() to LoggerAPI type definition in index.d.ts
- Fix circular reference fallback to preserve null values

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 21:37:59 +08:00
Misaka
6a9d144bbc fix(logger): batch fix audit-logger, circular meta, and sync callers
- Make logAudit and closeAuditLogger synchronous (were async for no reason)
- Set audit-logger silent:true initially, enable on applyAuditConfig()
- Add try-catch for circular references in consoleFormat meta JSON
- Update all callers to remove unnecessary await/.catch() on sync functions
- Add comment to shared.ts explaining acceptable sync FS usage
- Fix audit-logger test for sync closeAuditLogger

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 21:25:06 +08:00
Misaka
a2e3681c8f feat(logger): sync renderer log level cache and support verbose IPC
- Add LOGGER_LEVEL_CHANGED IPC channel for broadcasting level changes
- setLogLevel() now notifies all BrowserWindows when level changes
- Add verbose case in IPC forwardToWinston (was falling through to info)
- Renderer logger API listens for level changes and updates cached level
- Add cleanup() method to remove level change listener

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 21:18:56 +08:00
Misaka
21359b31c6 fix(logger): cache isProduction and prevent error double-serialization
Cache isProduction() result at module load to avoid repeated property
lookups. Add isSerializedError() check in format functions to skip
re-serialization when error objects have already been processed by
logError/formatErrorForLogging.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 21:16:04 +08:00
Misaka
0a1181fecd fix(logger): use will-quit instead of before-quit and remove redundant console.error
Move logger close from before-quit to will-quit to keep the logger available
for uncaughtException handlers that may fire during shutdown. Remove 4
redundant console.error calls that duplicate Winston logger output.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 21:13:36 +08:00
Misaka
883f98065a refactor(logger): optimize logging architecture with 6 improvements
1. Extract shared module: consolidate getLogDir() and isProduction()
   into shared.ts, eliminate duplication across logger modules
2. Make retention config effective: delay file transport creation
   until config is loaded, apply appRetention/auditRetention from config.yaml
3. Add before-quit log flush: close logger and audit logger on
   app exit to prevent log loss
4. Unify logError entry point: remove duplicate logError from index.ts,
   re-export from error-utils.ts with richer error context
5. Renderer log level filtering: add client-side level check in preload
   to skip IPC for filtered-out messages
6. Child logger cache + audit cleanup: cache child loggers in IPC
   handler for performance, remove redundant timestamp format in audit logger

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 20:53:53 +08:00
Misaka_Company
020bbcdccc fix(auth): retry silent login after logout to show user selection for Admin
When Admin switches user and the switched user logs out, instead of
showing the login dialog, re-run silent login to detect if the
computer belongs to an Admin user and show user selection dialog.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-01 16:28:26 +08:00
Misaka_Company
63a292c5f9 1.8.0 2026-04-01 15:18:31 +08:00
Misaka_Company
51f8e0a6e7 docs: add release notes for version 1.8.0
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-01 15:18:02 +08:00
Misaka_Company
c8ab58d390 feat(history): add Admin-only delete button and multi-user filter with chips
- Add Admin-only delete button in operation history modal
- Replace dropdown with multi-select chip filters for Admin users
- Support filtering by multiple usernames using IN clause
- Fix user state propagation by passing currentUser via props
- Change GetBatchesOptions.username to usernames (array)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-01 15:17:20 +08:00
56 changed files with 6419 additions and 1001 deletions

View File

@@ -40,6 +40,7 @@ extraction:
autoConvert: true
mergeBatches: true
enableDbPersistence: true
headless: true # 浏览器无头模式true=后台运行false=显示浏览器窗口(调试用)
validation:
dataSource: database_full

547
docs/LOGGING_GUIDE.md Normal file
View File

@@ -0,0 +1,547 @@
# ERPAuto 日志查询指南
## 概述
> 本指南用于帮助运维和开发人员使用日志系统快速排查问题。
>
> **P0 升级**:日志系统已增强 requestId 追踪、性能监控、完整错误上下文。
---
## 日志字段说明
### 新增核心字段P0 升级)
| 字段 | 类型 | 说明 | 示例 |
| --------------- | ------- | ------------------------- | ---------------------------------------- |
| `requestId` | string | 请求唯一标识符UUID v4 | `"f833980c-7b11-4c13-9c39-7c8890eb8b2f"` |
| `userId` | string | 执行操作的用户 ID | `"admin"` |
| `operation` | string | 操作类型 | `"extract"`, `"clean"`, `"validate"` |
| `duration` | number | 操作耗时(毫秒) | `1523` |
| `slow` | boolean | 是否为慢操作(> 阈值) | `true` |
| `batchId` | string | 批次 ID | `"B20260404-001"` |
| `tableName` | string | 数据库表名 | `"DiscreteMaterialPlan"` |
| `operationType` | string | 数据库操作类型 | `"INSERT"`, `"DELETE"`, `"UPDATE"` |
| `recordCount` | number | 记录数 | `150` |
| `fileSize` | number | 文件大小(字节) | `1048576` |
### 业务上下文字段
| 字段 | 场景 | 说明 |
| ------------------------ | ----------------- | ---------------------------------- |
| `orderNumbers` | Extractor/Cleaner | 订单号列表 |
| `materialCodes` | Cleaner | 物料代码列表 |
| `downloadDir` | Extractor | 下载目录路径 |
| `dryRun` | Cleaner | 是否为干运行模式 |
| `mode` | Validation | 验证模式(`database_filtered` 等) |
| `useSharedProductionIds` | Validation | 是否使用共享 Production ID |
| `configPath` | Config | 配置文件路径 |
| `isDev` | Config | 是否为开发环境 |
| `version` | Update | 应用版本号 |
| `channel` | Update | 更新通道(`stable`/`preview` |
---
## 日志查询工具与脚本
### PowerShell 查询脚本
#### 1. 按 requestId 追踪完整请求链路
```powershell
# 查找特定 requestId 的所有日志
$requestId = "f833980c-7b11-4c13-9c39-7c8890eb8b2f"
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\app-*.log" |
ConvertFrom-Json |
Where-Object { $_.requestId -eq $requestId } |
Sort-Object timestamp |
Format-Table timestamp, level, message, context -AutoSize
```
**用途**:完整追踪一个请求的所有操作
---
#### 2. 查找慢操作(> 2 秒)
```powershell
# 查找所有慢操作
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\app-*.log" |
ConvertFrom-Json |
Where-Object { $_.duration -gt 2000 } |
Format-Table timestamp, operation, duration, message -AutoSize
```
**用途**:识别性能瓶颈
---
#### 3. 查找特定用户的所有操作
```powershell
# 按 userId 筛选日志
$userId = "admin"
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\app-*.log" |
ConvertFrom-Json |
Where-Object { $_.userId -eq $userId } |
Sort-Object timestamp |
Format-Table timestamp, operation, level, message -AutoSize
```
**用途**:审计用户操作
---
#### 4. 查找特定时间段内的错误
```powershell
# 查找最近 1 小时的错误
$startTime = (Get-Date).AddHours(-1)
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\error-*.log" |
ConvertFrom-Json |
Where-Object { [datetime]::Parse($_.timestamp) -gt $startTime } |
Format-Table timestamp, message, error -AutoSize
```
**用途**:故障排查
---
#### 5. 按 operation 统计操作频率
```powershell
# 统计各 operation 的执行次数
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\app-*.log" |
ConvertFrom-Json |
Where-Object { $_.operation } |
Group-Object operation |
Sort-Object Count -Descending |
Format-Table Name, Count -AutoSize
```
**用途**:了解系统使用情况
---
### Linux/Mac Bash 查询
```bash
# 按 requestId 过滤
cat app-*.log | jq 'select(.requestId == "f833980c-7b11-4c13-9c39-7c8890eb8b2f")'
# 查找错误日志
cat error-*.log | jq '.'
# 查找慢操作
cat app-*.log | jq 'select(.duration > 2000)'
# 统计 operation 频率
cat app-*.log | jq -r '.operation' | sort | uniq -c | sort -rn
```
---
## 常见故障排查场景
### 场景 1数据提取失败
**症状**:用户报告 "提取任务失败"
**排查步骤**
```mermaid
flowchart TD
A[用户报告提取失败] --> B[定位 requestId]
B --> C[查看完整请求链路]
C --> D{错误类型?}
D -->|网络错误 | E[检查 ERP 连接]
D -->|数据库错误 | F[检查数据库连接]
D -->|文件错误 | G[检查文件权限]
E --> H[修复网络问题]
F --> H
G --> H
H --> I[重新执行提取]
```
**日志查询**
```powershell
# 1. 找到提取相关的错误日志
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\error-*.log" |
ConvertFrom-Json |
Where-Object { $_.operation -eq "extract" -and $_.message -like "*失败*" } |
Format-List timestamp, requestId, error, orderNumbers
```
**排查要点**
1. 查找 `operation: "extract"`的日志
2. 提取`requestId`用于全链路追踪
3. 检查`error`字段的具体错误信息
4. 查看`orderNumbers`确定哪些订单失败
---
### 场景 2物料清理执行缓慢
**症状**:用户报告 "清理任务太慢"
**排查步骤**
```mermaid
flowchart TD
A[清理缓慢报告] --> B[查找慢操作]
B --> C{哪个阶段慢?}
C -->|批量处理 | D[检查订单数量/物料数量]
C -->|重试操作 | E[检查 ERP 响应时间]
C -->|数据库操作 | F[检查数据库性能]
D --> G[优化批量大小]
E --> G
F --> G
```
**日志查询**
```powershell
# 1. 查找清理相关的慢操作
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\app-*.log" |
ConvertFrom-Json |
Where-Object { $_.operation -eq "cleaner" -and $_.duration -gt 5000 } |
Format-List timestamp, requestId, duration, slow, totalOrders, totalMaterials
```
**排查要点**
1. 查找 `duration > 5000ms` 的清理操作
2. 检查`totalOrders``totalMaterials` 确认数据量
3. 查看 `slow: true` 的批处理日志
---
### 场景 3登录失败
**症状**:用户无法登录
**排查步骤**
```mermaid
flowchart TD
A[登录失败] --> B[查找认证错误]
B --> C{错误类型?}
C -->|凭证错误 | D[检查用户名/密码]
C -->|ERP 连接错误 | E[检查 ERP 服务状态]
C -->|会话错误 | F[检查会话管理]
D --> G[修正登录信息]
E --> G
F --> G
```
**日志查询**
```powershell
# 1. 查找认证相关的错误
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\error-*.log" |
ConvertFrom-Json |
Where-Object { $_.userId -eq "admin" -and $_.message -like "*login*" } |
Format-List timestamp, requestId, error, userId, username
```
**排查要点**
1. 查找 `operation: "login"``message` 包含"login"的日志
2. 检查 `userId``username`
3. 查看`error`字段的具体错误信息
---
### 场景 4数据库插入失败
**症状**:数据无法保存到数据库
**排查步骤**
```mermaid
flowchart TD
A[数据库插入失败] --> B[查找数据库错误]
B --> C{错误类型?}
C -->|连接错误 | D[检查数据库服务]
C -->|SQL 语法错误 | E[检查 SQL 语句]
C -->|约束错误 | F[检查数据完整性]
D --> G[修复数据库问题]
E --> G
F --> G
```
**日志查询**
```powershell
# 1. 查找数据库相关的错误
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\error-*.log" |
ConvertFrom-Json |
Where-Object { $_.operationType -eq "INSERT" } |
Format-List timestamp, requestId, operationType, tableName, error
```
**排查要点**
1. 查找 `operationType: "INSERT"`的日志
2. 检查`tableName` 确定哪个表失败
3. 查看`error`字段的具体错误信息
---
### 场景 5配置文件读取失败
**症状**:应用启动失败,提示配置错误
**排查步骤**
```mermaid
flowchart TD
A[配置读取失败] --> B[查找配置相关错误]
B --> C{错误类型?}
C -->|文件不存在 | D[检查配置文件路径]
C -->|解析错误 | E[检查 YAML 格式]
C -->|验证错误 | F[检查配置字段]
D --> G[修复配置问题]
E --> G
F --> G
```
**日志查询**
```powershell
# 1. 查找配置相关的错误
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\error-*.log" |
ConvertFrom-Json |
Where-Object { $_.configPath } |
Format-List timestamp, requestId, configPath, isDev, error
```
**排查要点**
1. 查找 `configPath` 字段的日志
2. 检查 `isDev` 确定环境(开发/生产)
3. 查看`error`字段的具体错误信息
---
### 场景 6文件上传失败
**症状**:文件无法上传到 RustFS
**排查步骤**
**日志查询**
```powershell
# 1. 查找上传相关的错误
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\error-*.log" |
ConvertFrom-Json |
Where-Object { $_.fileSize -or $_.message -like "*upload*" } |
Format-List timestamp, requestId, fileSize, endpoint, bucket, error
```
**排查要点**
1. 查找 `fileSize` 字段的日志(表示文件操作)
2. 检查 `endpoint``bucket` 配置
3. 查看`error`字段的具体错误信息
---
### 场景 7验证任务无数据返回
**症状**:验证任务执行成功但无数据
**排查步骤**
**日志查询**
```powershell
# 1. 查找验证相关的日志
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\app-*.log" |
ConvertFrom-Json |
Where-Object { $_.operation -eq "validate" } |
Format-List timestamp, requestId, mode, useSharedProductionIds, recordCount
```
**排查要点**
1. 查找 `operation: "validate"`的日志
2. 检查 `mode`字段(数据来源)
3. 查看`useSharedProductionIds``recordCount`
---
## 日志最佳实践
### 1. 开发环境 vs 生产环境
```mermaid
flowchart LR
A[日志级别配置] --> B{环境?}
B -->|开发 | C[DEBUG 级别<br/>详细信息]
B -->|生产 | D[INFO 级别<br/>业务操作]
C --> E[调试问题]
D --> F[监控运行]
```
**配置示例**
```yaml
# config.yaml
logging:
level: debug # 开发环境
# level: info # 生产环境
```
---
### 2. 敏感信息保护
**永远不要记录**
- ❌ 密码
- ❌ Token/密钥
- ❌ 数据库连接字符串
- ❌ 用户个人信息
**正确做法**
```typescript
// ❌ 错误:记录敏感信息
log.error('Login failed', { password: userPassword })
// ✅ 正确:使用脱敏信息
log.error('Login failed', {
userId: 'admin',
reason: 'invalid_credentials' // 仅记录原因
})
```
---
### 3. 错误日志应该包含
**完整上下文**
```typescript
log.error('Database insert failed', {
requestId: getRequestId(), // 自动注入
operation: 'insert-materials',
userId: 'admin',
tableName: 'DiscreteMaterialPlan',
recordCount: 150,
error: error.message,
orderNumbers: ['SO001', 'SO002']
})
```
---
### 4. 性能监控
**关键指标**
- `duration > 1000ms`:一般警告
- `duration > 5000ms`:严重警告
- `duration > 10000ms`:需要立即调查
**监控脚本**
```powershell
# 每小时生成性能报告
Get-Content "C:\Users\pengq\AppData\Roaming\erpauto\logs\app-*.log" |
ConvertFrom-Json |
Where-Object { $_.duration -gt 1000 } |
Group-Object operation |
ForEach-Object {
[PSCustomObject]@{
Operation = $_.Name
SlowOperations = $_.Count
AvgDuration = [math]::Round(($_.Group | Measure-Object duration -Average).Average, 2)
MaxDuration = [math]::Round(($_.Group | Measure-Object duration -Maximum).Maximum, 2)
}
} | Format-Table -AutoSize
```
---
## 日志文件管理
### 文件位置
| 环境 | 路径 |
| -------- | ------------------------------------------------- |
| **开发** | `D:\FileLib\Projects\CodeMigration\ERPAuto\logs\` |
| **生产** | `C:\Users\<user>\AppData\Roaming\erpauto\logs\` |
### 文件命名
| 类型 | 命名格式 | 说明 |
| -------- | ------------------------ | ------------------ |
| 应用日志 | `app-YYYY-MM-DD.log` | 所有业务日志 |
| 错误日志 | `error-YYYY-MM-DD.log` | 仅错误级别日志 |
| 审计日志 | `audit-YYYY-MM-DD.jsonl` | 用户操作审计 |
| 压缩归档 | `*.log.gz` | 超过保留期限的日志 |
### 保留策略
```yaml
# config.yaml
logging:
appRetention: 14 # 应用日志保留 14 天
auditRetention: 30 # 审计日志保留 30 天
```
---
## 故障排查流程图
### 通用排查流程
```mermaid
flowchart TD
A[收到故障报告] --> B[确定故障类型]
B --> C{故障类型?}
C -->|功能错误 | D[查找相关 error 日志]
C -->|性能问题 | E[查找慢操作日志]
C -->|数据问题 | F[查找数据操作日志]
D --> G[定位 requestId]
E --> G
F --> G
G --> H[追踪完整请求链路]
H --> I[分析错误根因]
I --> J[制定修复方案]
J --> K[执行修复]
K --> L[验证修复效果]
```
---
## 总结
### 快速参考
| 需求 | 查询字段 |
| ---------- | ------------------------ |
| 完整追踪 | `requestId` |
| 性能排查 | `duration`, `slow` |
| 用户审计 | `userId` |
| 错误分析 | `error`, `operationType` |
| 数据库问题 | `tableName`, `records` |
| 文件问题 | `fileSize`, `filePath` |
### 联系支持
如遇日志相关问题,请联系技术支持团队并提供:
1. 故障时间段
2. 相关 `requestId`
3. 错误日志内容
---
_文档版本P0 Enhanced Logging_
_更新日期2026-04-04_

View File

@@ -0,0 +1,752 @@
# ERPAuto 日志系统实现文档
## 概述
ERPAuto 使用 **Winston** 作为核心日志库,实现了统一的主进程 - 渲染进程日志系统。系统支持日志级别管理、文件轮转、审计日志、错误全链路追踪等功能。
---
## 架构总览
```mermaid
graph TB
subgraph Renderer Process
RC[React Components]
UH[useLogger Hook]
LA[Logger API]
end
subgraph Preload Layer
PL[Preload Bridge]
LC[Level Cache]
end
subgraph Main Process
LH[Logger Handler]
IL[IPC Router]
WL[Winston Logger]
FT[File Transports]
CT[Console Transport]
AL[Audit Logger]
end
subgraph Storage
ALF[app-YYYY-MM-DD.log]
ELF[error-YYYY-MM-DD.log]
AUF[audit-YYYY-MM-DD.jsonl]
end
RC --> UH
UH --> LA
LA --> LC
LC -->|IPC Send| PL
PL -->|logger:forward| IL
IL --> LH
LH --> WL
WL --> CT
WL --> FT
FT --> ALF
FT --> ELF
AL --> AUF
style WL fill:#f9f,stroke:#333
style LH fill:#bbf,stroke:#333
style AL fill:#bfb,stroke:#333
```
---
## 核心组件
### 1. 主进程日志服务 (`src/main/services/logger/`)
#### 1.1 核心日志器 (`index.ts`)
```typescript
// 日志器创建与配置
import winston from 'winston'
import DailyRotateFile from 'winston-daily-rotate-file'
const logger = winston.createLogger({
level: 'info',
defaultMeta: { service: 'erpauto' },
transports: [new winston.transports.Console({ format: consoleFormat })]
})
```
**关键特性:**
- **双格式输出**:控制台(彩色文本)+ 文件JSON
- **每日轮转**:日志文件按日期拆分,自动压缩归档
- **错误序列化**:完整捕获 stack trace 和自定义属性
- **环境感知**:生产环境自动脱敏敏感信息
#### 1.2 日志级别与优先级
```typescript
export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose'
export const LOG_LEVEL_PRIORITY: Record<string, number> = {
verbose: 0,
debug: 1,
info: 2,
warn: 3,
error: 4
}
```
#### 1.3 错误工具类 (`error-utils.ts`)
```mermaid
flowchart LR
A[Error Occurs] --> B{Error Type?}
B -->|Error Instance| C[serializeError]
B -->|Error-like| C
B -->|Other| D[Wrap as UnknownError]
C --> E{Production?}
D --> E
E -->|Yes| F[sanitizeError]
E -->|No| G[Keep Full Details]
F --> H[Redact Sensitive Keys]
G --> I[Preserve Stack Trace]
H --> J[Log Output]
I --> J
```
**序列化流程:**
1. 捕获所有 enumerable 和 non-enumerable 属性
2. 递归处理 error cause 链
3. 生产环境脱敏 password/token/secret 等敏感字段
4. 提取堆栈中的文件/行号/列号信息
---
### 2. 审计日志服务 (`audit-logger.ts`)
**用途**:记录用户操作审计日志,满足合规要求
```typescript
interface AuditEntry {
timestamp: string // ISO 8601 时间戳
action: string // 操作类型LOGIN, EXTRACT, DELETE
userId: string // 用户 ID
username: string // 用户名
computerName: string // 计算机名
resource: string // 受影响的资源
status: 'success' | 'failure' | 'partial'
metadata: Record<string, unknown>
}
```
**格式特点:**
- **JSONL 格式**:每行一个 JSON 对象,便于流式解析
- **30 天轮转**:默认保留 30 天审计日志
- **独立文件**`audit-YYYY-MM-DD.jsonl`
---
### 3. IPC 日志处理器 (`src/main/ipc/logger-handler.ts`)
```mermaid
sequenceDiagram
participant R as Renderer
participant B as Buffer State
participant W as Winston
participant F as File
R->>B: Send Log Entry
Note over B: Circuit Breaker Check
alt Error Level
B->>B: Always Buffer
else Non-Error & Buffer < 500
B->>B: Buffer Entry
else Buffer >= 500
B->>B: Discard + Count
end
Note over B: Batch Processing
B->>B: 100ms Debounce OR 50 entries
B->>W: Flush Batch
W->>F: Write to File
```
**批处理策略:**
| 参数 | 值 | 说明 |
|------|-----|------|
| `DEBOUNCE_MS` | 100ms | 防抖等待时间 |
| `MAX_BATCH_SIZE` | 50 | 最大批次大小 |
| `CIRCUIT_BREAKER_THRESHOLD` | 500 | 熔断阈值 |
**熔断机制:**
- 当缓冲区 > 500 条时,丢弃非错误日志
- 错误日志始终绕过熔断器
- 每丢弃 100 条记录一次警告
---
### 4. 渲染进程日志 Hook (`src/renderer/src/hooks/useLogger.ts`)
```typescript
// 使用示例
function MyComponent() {
const logger = useLogger('MyComponent')
const handleClick = () => {
logger.info('User clicked button', { buttonId: 'submit' })
}
const handleError = (err: Error) => {
logger.error('Operation failed', { error: err.message })
}
}
```
**客户端级别过滤:**
```typescript
// 在发送 IPC 前检查日志级别,避免无效 IPC 调用
if (!shouldLog(level)) return
ipcRenderer.send(IPC_CHANNELS.LOGGER_FORWARD, { ... })
```
**FPS 监控:**
- 检测因过度日志导致的 UI 卡顿
- 当 FPS < 30 时发出警告
- 5 秒冷却期避免重复警告
---
### 5. 预加载层 API (`src/preload/api/logger.ts`)
```typescript
// 级别缓存机制
let cachedLevel: LogLevel = 'info'
// 监听主进程级别变更广播
ipcRenderer.on(IPC_CHANNELS.LOGGER_LEVEL_CHANGED, (level) => {
cachedLevel = level
})
// 客户端过滤
function shouldLog(level: LogLevel): boolean {
return priorities[level] >= priorities[cachedLevel]
}
```
---
### 6. 配置管理 (`src/main/services/config/config-manager.ts`)
```yaml
# config.yaml 配置示例
logging:
level: info # 日志级别
auditRetention: 30 # 审计日志保留天数
appRetention: 14 # 应用日志保留天数
```
**配置加载时机:**
1. 应用启动时加载 `config.yaml`
2. 调用 `applyLoggingConfig()` 配置 Winston
3. 调用 `applyAuditConfig()` 配置审计日志
---
## 日志数据流
```mermaid
flowchart TD
subgraph 渲染进程
A[Component] --> B[useLogger Hook]
B --> C{Level Check}
C -->|Pass| D[loggerApi.log]
C -->|Skip| E[Drop]
end
subgraph IPC 传输
D --> F[logger:forward]
F --> G[Context Bridge]
end
subgraph 主进程
G --> H[Logger Handler]
H --> I{Circuit Breaker}
I -->|Pass| J[Batch Buffer]
I -->|Block| K[Discard Counter]
J --> L{Debounce Timer}
L -->|100ms| M[Flush to Winston]
J -->|50 entries| M
end
subgraph Winston
M --> N[Console Transport]
M --> O[File Transport]
O --> P{Error Level?}
P -->|Yes| Q[error-DATE.log]
P -->|All| R[app-DATE.log]
end
subgraph 审计日志
S[logAudit] --> T[Audit Logger]
T --> U[audit-DATE.jsonl]
end
```
---
## 日志文件组织
### 目录结构
```
AppData/Roaming/erpauto/logs/
├── app-2024-04-01.log
├── app-2024-04-01.log.gz # 压缩归档
├── app-2024-04-02.log
├── error-2024-04-01.log # 仅错误级别
├── error-2024-04-01.log.gz
├── audit-2024-04-01.jsonl # 审计日志
└── audit-2024-04-01.jsonl.gz
```
### 文件格式
**应用日志 (JSON 格式):**
```json
{
"level": "info",
"message": "Extractor started",
"timestamp": "2024-04-01 10:30:00",
"service": "erpauto",
"context": "Extractor",
"orders": ["SO001", "SO002"]
}
```
**错误日志 (含堆栈):**
```json
{
"level": "error",
"message": "Database connection failed",
"timestamp": "2024-04-01 10:31:00",
"error": {
"name": "ConnectionError",
"message": "ECONNREFUSED",
"stack": "ConnectionError: ECONNREFUSED\n at TCP.connectWrap (...)",
"code": "ECONNREFUSED"
}
}
```
**审计日志 (JSONL 格式):**
```jsonl
{"timestamp":"2024-04-01T10:30:00Z","action":"LOGIN","userId":"1","username":"admin","computerName":"DESKTOP-001","resource":"/auth","status":"success","metadata":{}}
{"timestamp":"2024-04-01T10:35:00Z","action":"EXTRACT","userId":"1","username":"admin","computerName":"DESKTOP-001","resource":"orders","status":"success","metadata":{"orderCount":50}}
```
---
## IPC 通道定义
```typescript
// src/shared/ipc-channels.ts
export const IPC_CHANNELS = {
// 日志转发renderer → main
LOGGER_FORWARD: 'logger:forward',
// 获取当前日志级别
LOGGER_GET_LEVEL: 'logger:getLevel',
// 级别变更广播main → renderer
LOGGER_LEVEL_CHANGED: 'logger:levelChanged'
}
```
---
## 使用指南
### 在主进程中记录日志
```typescript
import { createLogger } from '@/main/services/logger'
const log = createLogger('MyService')
// 基础用法
log.info('Operation started')
log.warn('Disk space low')
log.error('Failed to connect', { error: err })
// 带上下文的日志
log.info('Processing batch', {
batchId: 'B001',
itemCount: 100,
estimatedTime: '5min'
})
// 错误日志(自动序列化堆栈)
try {
await riskyOperation()
} catch (error) {
log.error('Operation failed', { error })
}
```
### 在渲染进程中记录日志
```typescript
import { useLogger } from '@/renderer/src/hooks/useLogger'
function MyComponent() {
const logger = useLogger('MyComponent')
useEffect(() => {
logger.info('Component mounted')
return () => logger.debug('Component unmounted')
}, [])
const handleAction = async () => {
try {
await api.doSomething()
logger.info('Action succeeded')
} catch (err) {
logger.error('Action failed', { error: err.message })
}
}
}
```
### 记录审计日志
```typescript
import { logAudit } from '@/main/services/logger/audit-logger'
// 用户登录审计
logAudit('LOGIN', userId, {
username: 'admin',
computerName: 'DESKTOP-001',
resource: '/auth',
status: 'success',
metadata: { loginMethod: 'password' }
})
// 数据提取审计
logAudit('EXTRACT', userId, {
username: 'user1',
computerName: 'DESKTOP-002',
resource: 'materials',
status: 'success',
metadata: { orderCount: 50, materialCount: 1200 }
})
```
---
## 高级功能
### 1. 日志级别动态切换
```mermaid
sequenceDiagram
participant U as User (UI)
participant C as ConfigManager
participant M as Main Logger
participant R as Renderer
participant L as Level Cache
U->>C: Update logging.level
C->>M: applyLoggingConfig(newLevel)
M->>M: logger.level = newLevel
M->>R: Broadcast levelChanged
R->>L: cachedLevel = newLevel
Note over L: Future logs filtered at client
```
**代码示例:**
```typescript
// 主进程设置级别
import { setLogLevel } from '@/main/services/logger'
setLogLevel('debug')
// 渲染进程自动同步
// useLogger Hook 会自动接收级别变更广播
// 客户端过滤自动生效
```
### 2. 生产环境错误脱敏
```typescript
// 自动脱敏以下关键字段
const sensitiveKeys = [
'password', 'secret', 'token', 'apiKey',
'credentials', 'authorization', 'privateKey'
]
// 生产环境错误消息
{
"name": "AuthError",
"message": "An error occurred due to invalid credentials or configuration"
// 原始错误消息被脱敏
}
```
### 3. 错误上下文提取
```typescript
// 从堆栈跟踪提取位置信息
const errorContext = extractErrorContext(serializedError)
// 输出:
{
fileName: 'extractor.ts',
lineNumber: 142,
columnName: 15,
functionName: 'runExtraction'
}
```
---
## 最佳实践
### ✅ 推荐做法
```typescript
// 1. 使用 createLogger 创建带上下文的子日志器
const log = createLogger('DatabaseService')
// 2. 记录错误时传递完整 Error 对象
log.error('Query failed', { error })
// 3. 使用结构化元数据
log.info('Batch processed', {
batchId: 'B001',
duration: 1250,
itemCount: 100
})
// 4. 渲染进程使用 useLogger Hook
const logger = useLogger('LoginForm')
// 5. 敏感信息使用审计日志
logAudit('DELETE', userId, { ... })
```
### ❌ 避免的做法
```typescript
// 1. 避免直接 console.log
console.log('debug') // ❌ 不会被 Winston 捕获
// 2. 避免只记录错误消息
log.error(err.message) // ❌ 丢失堆栈和类型
// 3. 避免循环引用元数据
const obj: any = {}
obj.self = obj
log.info('test', { obj }) // ❌ 序列化失败
// 4. 避免过度日志
for (let i = 0; i < 1000; i++) {
logger.info(`Item ${i}`) // ❌ 触发熔断
}
```
---
## 故障排查
### 问题:日志文件不生成
**检查清单:**
1. 确认 `config.yaml` 中 logging 配置正确
2. 检查日志目录权限
3. 查看控制台输出是否有 Winston 错误
4. 验证 `applyLoggingConfig()` 是否被调用
### 问题:渲染进程日志未到达主进程
**调试步骤:**
```typescript
// 1. 检查 IPC 通道是否注册
// src/main/ipc/index.ts 应包含:
registerLoggerHandlers()
// 2. 检查 preload 暴露
// src/preload/index.ts 应暴露:
contextBridge.exposeInMainWorld('electron', api)
// 3. 检查级别过滤
console.log(window.electron.logger) // 应存在
```
### 问题:生产环境错误信息不完整
**原因**:生产环境自动脱敏
**解决方案**
- 查看 `error-DATE.log` 获取完整错误
- 开发环境禁用脱敏:设置开发模式构建
---
## 测试支持
### 单元测试示例
```typescript
import { createLogger } from '@/main/services/logger'
describe('Logger', () => {
it('should log with context', () => {
const log = createLogger('TestService')
// 测试逻辑...
expect(log).toBeDefined()
})
})
```
### 集成测试
```typescript
// tests/integration/ipc-logging.test.ts
import { loggerApi } from '@/preload/api/logger'
test('Renderer logs should reach Winston', async () => {
// Mock Winston transport
// Send log via IPC
// Assert log appears in main process
})
```
---
## 配置参考
### config.yaml 完整配置
```yaml
logging:
# 日志级别error | warn | info | debug | verbose
level: info
# 审计日志保留天数
auditRetention: 30
# 应用日志保留天数
appRetention: 14
```
### 日志级别说明
| 级别 | 使用场景 | 示例 |
| --------- | -------------- | ---------------------------- |
| `error` | 系统错误、异常 | 数据库连接失败、文件写入错误 |
| `warn` | 可恢复的警告 | 磁盘空间不足、重试操作 |
| `info` | 业务操作记录 | 用户登录、提取开始/结束 |
| `debug` | 技术调试信息 | API 请求参数、SQL 语句 |
| `verbose` | 详细跟踪 | 循环迭代、中间状态 |
---
## 相关文件索引
| 文件路径 | 职责 |
| -------------------------------------------- | ------------------ |
| `src/main/services/logger/index.ts` | Winston 日志器核心 |
| `src/main/services/logger/shared.ts` | 共享工具函数 |
| `src/main/services/logger/error-utils.ts` | 错误序列化/脱敏 |
| `src/main/services/logger/audit-logger.ts` | 审计日志服务 |
| `src/main/ipc/logger-handler.ts` | IPC 批处理与熔断 |
| `src/renderer/src/hooks/useLogger.ts` | React Hook |
| `src/preload/api/logger.ts` | Preload API |
| `src/shared/ipc-channels.ts` | IPC 通道定义 |
| `src/main/services/config/config-manager.ts` | 配置管理 |
---
## 架构图附录
### 完整日志系统架构
```mermaid
graph TB
subgraph 渲染进程 Renderer
UI[UI Components]
HL[useLogger Hook]
CF[Client Filter]
LC[Level Cache]
end
subgraph 预加载层 Preload
CB[Context Bridge]
IR[IPC Renderer]
LA[Logger API]
end
subgraph 主进程 Main
IH[IPC Handler]
BB[Batch Buffer]
CB2[Circuit Breaker]
WL[Winston Logger]
AC[Audit Logger]
CM[Config Manager]
end
subgraph 传输层 Transports
CT[Console]
AFT[App File]
EFT[Error File]
ATF[Audit File]
end
subgraph 文件系统 File System
ALF[app-DATE.log]
ELF[error-DATE.log]
AUF[audit-DATE.jsonl]
GZ[.gz Archive]
end
UI --> HL
HL --> CF
CF --> LC
LC --> LA
LA --> IR
IR --> CB
CB --> IH
IH --> CB2
CB2 --> BB
BB --> WL
WL --> CT
WL --> AFT
WL --> EFT
AC --> ATF
CM --> WL
AFT --> ALF
EFT --> ELF
ATF --> AUF
ALF --> GZ
ELF --> GZ
AUF --> GZ
style WL fill:#f9f,stroke:#333
style BB fill:#bbf,stroke:#333
style CB2 fill:#fbb,stroke:#333
style AC fill:#bfb,stroke:#333
```
---
_文档生成日期2026-04-04_
_项目版本ERPAuto v1.x_

View File

@@ -48,13 +48,13 @@ graph TD
## 文档职责一览
| 文档 | 主要回答的问题 |
| --- | --- |
| `overview.md` | 这个项目整体是什么、做什么、核心目录和主链路是什么 |
| `runtime-architecture.md` | `main / preload / renderer` 如何协作 |
| `data-flow.md` | 核心业务数据如何在各层之间流动 |
| `file-map.md` | 关键文件在哪里、应该先看哪些入口 |
| `decision-log.md` | 最近几轮重要重构和架构决策是什么 |
| 文档 | 主要回答的问题 |
| ------------------------- | -------------------------------------------------- |
| `overview.md` | 这个项目整体是什么、做什么、核心目录和主链路是什么 |
| `runtime-architecture.md` | `main / preload / renderer` 如何协作 |
| `data-flow.md` | 核心业务数据如何在各层之间流动 |
| `file-map.md` | 关键文件在哪里、应该先看哪些入口 |
| `decision-log.md` | 最近几轮重要重构和架构决策是什么 |
## 按问题选择阅读路径

View File

@@ -66,13 +66,13 @@ flowchart TD
## 当前文档一览
| 文档 | 主要内容 |
| --- | --- |
| `local-development.md` | 环境准备、启动、构建、常用命令、本地验证 |
| `debugging.md` | 分层调试思路、调试入口、主链路定位方法 |
| 文档 | 主要内容 |
| ------------------------- | ---------------------------------------- |
| `local-development.md` | 环境准备、启动、构建、常用命令、本地验证 |
| `debugging.md` | 分层调试思路、调试入口、主链路定位方法 |
| `renderer-development.md` | React 渲染层开发方式、页面/hook/组件边界 |
| `ipc-development.md` | 新增或修改 IPC 能力的推荐实现路径 |
| `release-process.md` | 构建、发布、更新产物与上传流程 |
| `ipc-development.md` | 新增或修改 IPC 能力的推荐实现路径 |
| `release-process.md` | 构建、发布、更新产物与上传流程 |
## 与其他文档目录的关系

View File

@@ -59,14 +59,14 @@ graph TD
## 模块目录一览
| 模块 | 文档 | 核心职责 |
| --- | --- | --- |
| Auth | `auth.md` | 登录、silent login、管理员代切用户、用户上下文同步 |
| Extractor | `extractor.md` | 订单号输入、提取执行、日志与共享订单号同步 |
| 模块 | 文档 | 核心职责 |
| ---------- | --------------- | --------------------------------------------------------- |
| Auth | `auth.md` | 登录、silent login、管理员代切用户、用户上下文同步 |
| Extractor | `extractor.md` | 订单号输入、提取执行、日志与共享订单号同步 |
| Validation | `validation.md` | 共享 Production IDs、校验查询、结果富化、Cleaner 数据准备 |
| Cleaner | `cleaner.md` | 物料校验展示、删除计划保存、ERP 清理执行、报告展示 |
| Update | `update.md` | 更新目录、状态广播、下载、安装、用户/管理员更新视图 |
| Settings | `settings.md` | ERP 凭据加载与保存、当前用户配置管理 |
| Cleaner | `cleaner.md` | 物料校验展示、删除计划保存、ERP 清理执行、报告展示 |
| Update | `update.md` | 更新目录、状态广播、下载、安装、用户/管理员更新视图 |
| Settings | `settings.md` | ERP 凭据加载与保存、当前用户配置管理 |
## 模块入口地图

12
docs/releases/1.8.0.md Normal file
View File

@@ -0,0 +1,12 @@
# 1.8.0
## 权限控制
- 操作历史删除按钮仅对管理员可见,普通用户无法删除历史记录。
- 修复用户状态传递问题,确保权限判断正确生效。
## 界面与交互
- 管理员可使用多选标签Chip按用户筛选操作历史。
- 支持同时选择多个用户查看记录,点击标签即可切换选中状态。
- 添加"清空筛选"按钮,一键恢复显示所有用户记录。

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "erpauto",
"version": "1.7.2",
"version": "1.8.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "erpauto",
"version": "1.7.2",
"version": "1.8.0",
"hasInstallScript": true,
"dependencies": {
"@aws-sdk/client-s3": "^3.929.0",

View File

@@ -1,6 +1,6 @@
{
"name": "erpauto",
"version": "1.7.2",
"version": "1.8.0",
"description": "An Electron application with React and TypeScript",
"main": "./out/main/index.js",
"author": "example.com",

View File

@@ -1,40 +1,45 @@
import { app } from 'electron'
import logger from '../services/logger/index'
import { logAudit } from '../services/logger/audit-logger'
import { logAudit, closeAuditLogger } from '../services/logger/audit-logger'
export function setupProcessGuards(): void {
process.on('uncaughtException', async (err) => {
process.on('uncaughtException', (err) => {
logger.error('Uncaught exception', { error: err })
await logAudit('SYSTEM_CRASH', 'system', {
logAudit('SYSTEM_CRASH', 'system', {
username: 'system',
computerName: process.env.COMPUTERNAME || 'unknown',
resource: 'main-process',
status: 'failure',
metadata: { error: err.message, stack: err.stack }
})
console.error('Uncaught exception:', err)
setTimeout(() => process.exit(1), 1000)
})
process.on('unhandledRejection', async (reason) => {
process.on('unhandledRejection', (reason) => {
logger.error('Unhandled Rejection', { reason: String(reason) })
await logAudit('SYSTEM_ERROR', 'system', {
logAudit('SYSTEM_ERROR', 'system', {
username: 'system',
computerName: process.env.COMPUTERNAME || 'unknown',
resource: 'main-process',
status: 'failure',
metadata: { reason: String(reason) }
})
console.error('Unhandled Rejection:', reason)
})
app.on('render-process-gone', (_, webContents, details) => {
logger.error('Render process gone', { details, webContentsId: webContents.id })
console.error('Render process gone:', details)
})
app.on('child-process-gone', (_, details) => {
logger.error('Child process gone', { details })
console.error('Child process gone:', details)
})
// Flush and close loggers on will-quit (fires after all windows are closed,
// but before the event loop stops). Using will-quit instead of before-quit
// ensures the logger remains available for uncaughtException handlers that
// may fire between before-quit and actual process exit.
app.on('will-quit', () => {
logger.close()
closeAuditLogger()
})
}

View File

@@ -90,6 +90,10 @@ export function registerExtractorHandlers(): void {
log.info('Fetching ERP configuration from database...')
const erpConfig = await getErpConfig()
// Read headless setting from global config
const configManager = ConfigManager.getInstance()
const globalConfig = configManager.getConfig()
log.info('ERP config retrieved', {
url: erpConfig.url ? 'configured' : 'EMPTY',
username: erpConfig.username ? 'configured' : 'EMPTY'
@@ -188,7 +192,7 @@ export function registerExtractorHandlers(): void {
url: erpConfig.url,
username: erpConfig.username,
password: erpConfig.password,
headless: true
headless: globalConfig.extraction.headless
})
sendProgress(sender, '登录 ERP 系统...', 9.99, {
@@ -259,7 +263,13 @@ export function registerExtractorHandlers(): void {
// Write per-order record counts
for (const { orderNumber, recordCount } of result.orderRecordCounts) {
await historyDao.updateRecordStatus(batchId, orderNumber, status, undefined, recordCount)
await historyDao.updateRecordStatus(
batchId,
orderNumber,
status,
undefined,
recordCount
)
}
// Update batch status without recordCount (per-order counts are set individually)
@@ -286,7 +296,7 @@ export function registerExtractorHandlers(): void {
recordCount: result.recordCount,
errorCount: result.errors.length
}
}).catch((err) => log.warn('Failed to write audit log', { err }))
})
}
return result

View File

@@ -68,15 +68,21 @@ export function withErrorHandling<T>(
}
if (isBaseError(error)) {
logError(log, `[${context}] ${error.name}`, error, {
code,
cause: getErrorCauseMessage(error),
handler: context
logError(log, error, {
message: `[${context}] ${error.name}`,
context: {
code,
cause: getErrorCauseMessage(error),
handler: context
}
})
} else {
logError(log, `[${context}] Error`, error, {
code,
handler: context
logError(log, error, {
message: `[${context}] Error`,
context: {
code,
handler: context
}
})
}

View File

@@ -10,7 +10,9 @@
*/
import { ipcMain } from 'electron'
import winston from 'winston'
import { createLogger } from '../services/logger'
import logger from '../services/logger'
import { IPC_CHANNELS, type LogLevel } from '../../shared/ipc-channels'
const log = createLogger('LoggerHandler')
@@ -41,6 +43,7 @@ class LoggerHandlerState {
private buffer: LogEntry[] = []
private debounceTimer: NodeJS.Timeout | null = null
private discardedCount = 0
private childLoggerCache = new Map<string, winston.Logger>()
/**
* Add log entry to buffer
@@ -131,22 +134,36 @@ class LoggerHandlerState {
}
}
/**
* Get or create a cached child logger for a component
* Avoids creating a new child logger for every log entry
* @param component - Component name for the child logger
*/
private getChildLogger(component: string): winston.Logger {
let child = this.childLoggerCache.get(component)
if (!child) {
child = log.child({ source: 'renderer', component })
this.childLoggerCache.set(component, child)
}
return child
}
/**
* Forward a single log entry to Winston logger
* @param entry - Log entry to forward
*/
private forwardToWinston(entry: LogEntry): void {
const context = (entry.context?.component as string) || 'renderer'
const childLogger = log.child({
source: 'renderer',
component: context
})
const childLogger = this.getChildLogger(context)
const message = entry.context?.message
? `[${entry.context.message}] ${entry.message}`
: entry.message
switch (entry.level) {
case 'verbose':
childLogger.verbose(message, entry.context)
break
case 'debug':
childLogger.debug(message, entry.context)
break
@@ -187,6 +204,7 @@ class LoggerHandlerState {
}
this.buffer = []
this.discardedCount = 0
this.childLoggerCache.clear()
}
}
@@ -197,6 +215,11 @@ const state = new LoggerHandlerState()
* Register IPC handlers for logger
*/
export function registerLoggerHandlers(): void {
// Return current log level to preload for client-side filtering
ipcMain.handle(IPC_CHANNELS.LOGGER_GET_LEVEL, () => {
return logger.level as LogLevel
})
// Use ipcMain.on with send() - fire-and-forget, non-blocking
ipcMain.on(IPC_CHANNELS.LOGGER_FORWARD, (_event, entry: LogEntry) => {
// Validate entry

View File

@@ -27,16 +27,13 @@ export function registerSettingsHandlers(): void {
const erpConfigService = UserErpConfigService.getInstance()
ipcMain.handle(IPC_CHANNELS.SETTINGS_GET_USER_TYPE, async (): Promise<IpcResult<UserType>> => {
return withErrorHandling(
async () => {
const userType = sessionManager.getUserType()
if (!userType) {
throw new ValidationError('未找到用户类型', 'VAL_INVALID_INPUT')
}
return userType as UserType
},
'settings:getUserType'
)
return withErrorHandling(async () => {
const userType = sessionManager.getUserType()
if (!userType) {
throw new ValidationError('未找到用户类型', 'VAL_INVALID_INPUT')
}
return userType as UserType
}, 'settings:getUserType')
})
ipcMain.handle(
@@ -77,7 +74,7 @@ export function registerSettingsHandlers(): void {
resource: 'ERP_CONFIG',
status: 'success',
metadata: { changeType: 'erp_credentials', usernameChanged: !!settings.erp.username }
}).catch((err) => log.warn('Failed to write audit log', { err }))
})
}
return { success: true }

View File

@@ -1,7 +1,7 @@
import { hostname } from 'os'
import { SessionManager } from '../user/session-manager'
import { UpdateService } from '../update/update-service'
import { createLogger } from '../logger'
import { createLogger, run, getRequestId, getContext } from '../logger'
import { logAudit } from '../logger/audit-logger'
import { ValidationError } from '../../types/errors'
import type { UserInfo } from '../../types/user.types'
@@ -23,120 +23,212 @@ export class AuthApplicationService {
) {}
async getComputerName(): Promise<string> {
const requestId = getRequestId()
if (requestId) {
log.debug('Get computer name', { requestId })
}
return hostname()
}
async silentLogin(): Promise<SilentLoginResponse> {
if (this.silentLoginPromise) {
log.debug('Reusing in-flight silent login request')
log.debug('Reusing in-flight silent login request', { requestId: getRequestId() })
return this.silentLoginPromise
}
this.silentLoginPromise = this.performSilentLogin()
try {
return await this.silentLoginPromise
} finally {
this.silentLoginPromise = null
}
}
this.silentLoginPromise = run(
async (): Promise<SilentLoginResponse> => {
const requestId = getRequestId()
const context = getContext()
const startTime = performance.now()
private async performSilentLogin(): Promise<SilentLoginResponse> {
log.info('Attempting silent login')
const success = await this.sessionManager.loginByComputerName()
const userInfo = this.sessionManager.getUserInfo()
try {
log.info('Attempting silent login', { requestId, operation: context?.operation })
const success = await this.sessionManager.loginByComputerName()
const userInfo = this.sessionManager.getUserInfo()
if (!success || !userInfo) {
await this.updateService.setUserContext(null)
throw new ValidationError('无感登录失败:未找到匹配用户', 'VAL_INVALID_INPUT')
}
if (!success || !userInfo) {
await this.updateService.setUserContext(null)
const error = new ValidationError('无感登录失败:未找到匹配用户', 'VAL_INVALID_INPUT')
log.error('Silent login failed - user not found', {
operation: 'silentLogin',
requestId,
userId: userInfo?.id,
username: userInfo?.username,
computerName: hostname(),
error
})
throw error
}
await this.updateService.setUserContext(userInfo.userType)
await this.updateService.setUserContext(userInfo.userType)
const requiresUserSelection = userInfo.userType === 'Admin'
log.info('Silent login successful', {
username: userInfo.username,
userType: userInfo.userType,
requiresUserSelection
})
const requiresUserSelection = userInfo.userType === 'Admin'
log.info('Silent login successful', {
requestId,
operation: context?.operation,
username: userInfo.username,
userType: userInfo.userType,
requiresUserSelection,
userId: userInfo.id
})
this.writeAuditLog('LOGIN', String(userInfo.id), {
username: userInfo.username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'success',
metadata: { loginType: 'silent', userType: userInfo.userType }
})
this.writeAuditLog('LOGIN', String(userInfo.id), {
username: userInfo.username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'success',
metadata: { loginType: 'silent', userType: userInfo.userType }
})
return {
success: true,
userInfo,
requiresUserSelection
}
return {
success: true,
userInfo,
requiresUserSelection
}
} finally {
const durationMs = performance.now() - startTime
if (durationMs > 1000) {
log.warn(`Silent login took ${durationMs.toFixed(2)}ms (SLOW)`, {
operation: 'silentLogin',
requestId,
durationMs
})
} else {
log.debug(`Silent login completed in ${durationMs.toFixed(2)}ms`, {
operation: 'silentLogin',
requestId,
durationMs
})
}
}
},
{ operation: 'silentLogin' }
)
return this.silentLoginPromise
}
async login(username: string, password: string): Promise<LoginResponse> {
if (!username || !password) {
log.warn('Login attempt with missing credentials')
log.warn('Login attempt with missing credentials', { requestId: getRequestId() })
throw new ValidationError('请输入用户名和密码', 'VAL_MISSING_REQUIRED')
}
log.info('Login attempt', { username })
const success = await this.sessionManager.login(username, password)
const userInfo = this.sessionManager.getUserInfo()
return run(
async (): Promise<LoginResponse> => {
const requestId = getRequestId()
const context = getContext()
if (!success || !userInfo) {
this.writeAuditLog('LOGIN', '0', {
username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'failure',
metadata: { loginType: 'credentials', reason: 'invalid_credentials' }
})
const startTime = performance.now()
log.warn('Login failed - invalid credentials', { username })
await this.updateService.setUserContext(null)
throw new ValidationError('用户名或密码错误', 'VAL_INVALID_INPUT')
}
try {
log.info('Login attempt', { username, requestId, operation: context?.operation })
const success = await this.sessionManager.login(username, password)
const userInfo = this.sessionManager.getUserInfo()
log.info('Login successful', { username, userType: userInfo.userType })
await this.updateService.setUserContext(userInfo.userType)
if (!success || !userInfo) {
this.writeAuditLog('LOGIN', '0', {
username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'failure',
metadata: { loginType: 'credentials', reason: 'invalid_credentials' }
})
this.writeAuditLog('LOGIN', String(userInfo.id), {
username: userInfo.username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'success',
metadata: { loginType: 'credentials', userType: userInfo.userType }
})
const error = new ValidationError('用户名或密码错误', 'VAL_INVALID_INPUT')
log.warn('Login failed - invalid credentials', {
username,
requestId,
operation: context?.operation,
error
})
await this.updateService.setUserContext(null)
throw error
}
return {
success: true,
userInfo
}
log.info('Login successful', {
requestId,
operation: context?.operation,
username,
userType: userInfo.userType,
userId: userInfo.id
})
await this.updateService.setUserContext(userInfo.userType)
this.writeAuditLog('LOGIN', String(userInfo.id), {
username: userInfo.username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'success',
metadata: { loginType: 'credentials', userType: userInfo.userType }
})
return {
success: true,
userInfo
}
} finally {
const durationMs = performance.now() - startTime
if (durationMs > 1000) {
log.warn(`Login took ${durationMs.toFixed(2)}ms (SLOW)`, {
operation: 'login',
requestId,
durationMs,
username
})
} else {
log.debug(`Login completed in ${durationMs.toFixed(2)}ms`, {
operation: 'login',
requestId,
durationMs
})
}
}
},
{ operation: 'login' }
)
}
async logout(): Promise<void> {
const userInfo = this.sessionManager.getUserInfo()
log.info('User logout', { username: userInfo?.username })
return run(
async () => {
const requestId = getRequestId()
const context = getContext()
const userInfo = this.sessionManager.getUserInfo()
if (userInfo) {
this.writeAuditLog('LOGOUT', String(userInfo.id), {
username: userInfo.username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'success',
metadata: { userType: userInfo.userType }
})
}
log.info('User logout', {
requestId,
operation: context?.operation,
username: userInfo?.username,
userId: userInfo?.id
})
this.sessionManager.logout()
await this.updateService.setUserContext(null)
if (userInfo) {
this.writeAuditLog('LOGOUT', String(userInfo.id), {
username: userInfo.username,
computerName: hostname(),
resource: 'ERP_SYSTEM',
status: 'success',
metadata: { userType: userInfo.userType }
})
}
this.sessionManager.logout()
await this.updateService.setUserContext(null)
},
{ operation: 'logout' }
)
}
getCurrentUser(): CurrentUserResponse {
const requestId = getRequestId()
const isAuthenticated = this.sessionManager.isAuthenticated()
const userInfo = this.sessionManager.getUserInfo()
if (requestId) {
log.debug('Get current user', { requestId, isAuthenticated, userId: userInfo?.id })
}
return {
isAuthenticated,
userInfo: userInfo ?? undefined
@@ -144,27 +236,73 @@ export class AuthApplicationService {
}
async getAllUsers(): Promise<UserInfo[]> {
log.debug('Fetching all users for admin selection')
const requestId = getRequestId()
log.debug('Fetching all users for admin selection', { requestId })
return this.sessionManager.getAllUsers()
}
async switchUser(userInfo: UserInfo): Promise<UserSelectionResponse> {
log.info('User switch attempt', { targetUser: userInfo.username })
const success = this.sessionManager.switchUser(userInfo)
return run(
async (): Promise<UserSelectionResponse> => {
const requestId = getRequestId()
const context = getContext()
const startTime = performance.now()
if (!success) {
log.warn('User switch failed')
throw new ValidationError('用户切换失败', 'VAL_INVALID_INPUT')
}
try {
log.info('User switch attempt', {
requestId,
operation: context?.operation,
targetUser: userInfo.username,
targetUserId: userInfo.id
})
const success = this.sessionManager.switchUser(userInfo)
const newUser = this.sessionManager.getUserInfo()
log.info('User switch successful', { newUsername: newUser?.username })
await this.updateService.setUserContext(newUser?.userType ?? null)
if (!success) {
const error = new ValidationError('用户切换失败', 'VAL_INVALID_INPUT')
log.warn('User switch failed', {
requestId,
operation: context?.operation,
targetUser: userInfo.username,
targetUserId: userInfo.id,
error
})
throw error
}
return {
success: true,
userInfo: newUser ?? undefined
}
const newUser = this.sessionManager.getUserInfo()
log.info('User switch successful', {
requestId,
operation: context?.operation,
newUsername: newUser?.username,
newUserId: newUser?.id,
newUserType: newUser?.userType
})
await this.updateService.setUserContext(newUser?.userType ?? null)
return {
success: true,
userInfo: newUser ?? undefined
}
} finally {
const durationMs = performance.now() - startTime
if (durationMs > 1000) {
log.warn(`User switch took ${durationMs.toFixed(2)}ms (SLOW)`, {
operation: 'switchUser',
requestId,
durationMs,
targetUser: userInfo.username
})
} else {
log.debug(`User switch completed in ${durationMs.toFixed(2)}ms`, {
operation: 'switchUser',
requestId,
durationMs
})
}
}
},
{ operation: 'switchUser', userId: String(userInfo.id) }
)
}
isAdmin(): boolean {
@@ -176,8 +314,6 @@ export class AuthApplicationService {
actorId: string,
payload: Parameters<typeof logAudit>[2]
): void {
logAudit(action, actorId, payload).catch((err) =>
log.warn('Failed to write audit log', { err })
)
logAudit(action, actorId, payload)
}
}

View File

@@ -274,7 +274,7 @@ export class CleanerApplicationService {
? 'failure'
: 'success'
await logAudit('CLEAN', String(currentUser.id), {
logAudit('CLEAN', String(currentUser.id), {
username: currentUser.username,
computerName: (await import('os')).hostname(),
resource: 'MATERIAL_PLAN',
@@ -288,7 +288,7 @@ export class CleanerApplicationService {
materialsSkipped: result.materialsSkipped,
errorCount: result.errors.length
}
}).catch((err) => log.warn('Failed to write audit log', { err }))
})
}
private async generateAndUploadReport(

View File

@@ -20,7 +20,8 @@ import { dirname } from 'path'
import { app } from 'electron'
import yaml from 'js-yaml'
import { z } from 'zod'
import { createLogger, setLogLevel } from '../logger'
import { createLogger, applyLoggingConfig, trackDuration } from '../logger'
import { applyAuditConfig } from '../logger/audit-logger'
import {
fullConfigSchema,
type FullConfig,
@@ -77,7 +78,8 @@ const DEFAULT_CONFIG: FullConfig = {
verbose: true,
autoConvert: true,
mergeBatches: true,
enableDbPersistence: true
enableDbPersistence: true,
headless: true
},
validation: {
dataSource: 'database_full',
@@ -139,14 +141,22 @@ export class ConfigManager {
// 开发环境:配置文件放在项目根目录,方便编辑和调试
this.configPath = path.resolve(__dirname, '../../config.yaml')
this.backupPath = path.resolve(__dirname, '../../config.yaml.backup')
log.info('Running in development mode', { configPath: this.configPath })
log.info('Running in development mode', {
configPath: this.configPath,
isDev: true,
environment: process.env.NODE_ENV || 'not-set'
})
} else {
// 生产环境(包括安装版和便携版):配置文件放在用户数据目录
// Windows: C:\Users\<user>\AppData\Roaming\erpauto\config.yaml
// 这样配置会在应用升级时保留,且不会暴露在应用目录中
this.configPath = path.join(app.getPath('userData'), 'config.yaml')
this.backupPath = path.join(app.getPath('userData'), 'config.yaml.backup')
log.info('Running in production mode', { configPath: this.configPath })
log.info('Running in production mode', {
configPath: this.configPath,
isDev: false,
userDataPath: app.getPath('userData')
})
}
this.initialized = true
@@ -166,11 +176,18 @@ export class ConfigManager {
*/
public async initialize(): Promise<void> {
if (!fs.existsSync(this.configPath)) {
log.info('Config file not found, creating default config.yaml')
log.info('Config file not found, creating default config.yaml', {
configPath: this.configPath
})
await this.saveConfig(DEFAULT_CONFIG)
this.config = DEFAULT_CONFIG
// Apply logging configuration from default config
setLogLevel(DEFAULT_CONFIG.logging.level)
applyLoggingConfig(DEFAULT_CONFIG.logging)
applyAuditConfig(DEFAULT_CONFIG.logging.auditRetention)
log.info('Default configuration created and applied', {
configPath: this.configPath,
logLevel: DEFAULT_CONFIG.logging.level
})
return
}
@@ -190,16 +207,29 @@ export class ConfigManager {
this.config = validated
// Apply logging configuration
setLogLevel(validated.logging.level)
applyLoggingConfig(validated.logging)
applyAuditConfig(validated.logging.auditRetention)
log.info('Configuration loaded and validated successfully')
log.info('Configuration loaded and validated successfully', {
configPath: this.configPath,
logLevel: validated.logging.level,
auditRetention: validated.logging.auditRetention,
appRetention: validated.logging.appRetention,
isDev: process.env.NODE_ENV === 'development' || !(app?.isPackaged ?? false)
})
} catch (error) {
if (error instanceof z.ZodError) {
const messages = error.issues.map(formatZodIssue)
log.error('Configuration validation failed', { errors: messages })
throw new Error(`配置文件验证失败:\n${messages.join('\n')}`)
log.error('Configuration validation failed', {
configPath: this.configPath,
errors: messages
})
throw new Error(`配置文件验证失败:\n${messages.join('\n')}`)
}
log.error('Failed to load configuration', { error })
log.error('Failed to load configuration', {
configPath: this.configPath,
error
})
throw error
}
}
@@ -212,6 +242,7 @@ export class ConfigManager {
// 备份现有配置
if (fs.existsSync(this.configPath)) {
fs.copyFileSync(this.configPath, this.backupPath)
log.debug('Config backup created', { backupPath: this.backupPath })
}
// 转换为 YAML
@@ -226,13 +257,21 @@ export class ConfigManager {
fs.writeFileSync(this.configPath, content, 'utf-8')
this.config = config
log.info('Configuration saved successfully')
log.info('Configuration saved successfully', {
configPath: this.configPath,
logLevel: config.logging.level,
auditRetention: config.logging.auditRetention
})
return true
} catch (error) {
log.error('Failed to save configuration', { error })
log.error('Failed to save configuration', {
configPath: this.configPath,
error
})
// 恢复备份
if (fs.existsSync(this.backupPath)) {
fs.copyFileSync(this.backupPath, this.configPath)
log.warn('Configuration restored from backup', { backupPath: this.backupPath })
}
return false
}
@@ -291,6 +330,11 @@ export class ConfigManager {
await this.loadConfig()
}
log.info('Updating configuration', {
configPath: this.configPath,
updateKeys: Object.keys(updates)
})
// 深合并
const merged = this.deepMerge(this.config!, updates)
@@ -302,12 +346,24 @@ export class ConfigManager {
return { success: false, error: '保存配置失败' }
}
log.info('Configuration update completed', {
configPath: this.configPath,
updatedKeys: Object.keys(updates)
})
return { success: true }
} catch (error) {
if (error instanceof z.ZodError) {
const messages = error.issues.map(formatZodIssue)
return { success: false, error: `配置验证失败:\n${messages.join('\n')}` }
log.error('Configuration update validation failed', {
configPath: this.configPath,
errors: messages
})
return { success: false, error: `配置验证失败:\n${messages.join('\n')}` }
}
log.error('Failed to update configuration', {
configPath: this.configPath,
error
})
return { success: false, error: error instanceof Error ? error.message : '未知错误' }
}
}

View File

@@ -11,6 +11,9 @@
import 'reflect-metadata'
import { DataSource, DataSourceOptions } from 'typeorm'
import { ConfigManager } from '../config/config-manager'
import { createLogger } from '../logger'
const log = createLogger('DataSource')
/**
* Get database type from config manager
@@ -26,6 +29,7 @@ function getDatabaseType(): 'mysql' | 'mssql' {
*/
function buildDataSourceOptions(): DataSourceOptions {
const type = getDatabaseType()
log.debug('Building DataSource options', { type })
const configManager = ConfigManager.getInstance()
const config = configManager.getConfig()
@@ -74,7 +78,11 @@ let dataSource: DataSource | null = null
*/
export function getDataSource(): DataSource {
if (!dataSource) {
const type = getDatabaseType()
log.info('Creating new TypeORM DataSource', { type })
dataSource = new DataSource(buildDataSourceOptions())
} else {
log.debug('Reusing existing DataSource')
}
return dataSource
}
@@ -85,7 +93,14 @@ export function getDataSource(): DataSource {
export async function initializeDataSource(): Promise<DataSource> {
const ds = getDataSource()
if (!ds.isInitialized) {
await ds.initialize()
try {
await ds.initialize()
const type = getDatabaseType()
log.info('TypeORM DataSource initialized', { type })
} catch (error) {
log.error('Failed to initialize DataSource', { error })
throw error
}
}
return ds
}
@@ -95,8 +110,13 @@ export async function initializeDataSource(): Promise<DataSource> {
*/
export async function destroyDataSource(): Promise<void> {
if (dataSource && dataSource.isInitialized) {
await dataSource.destroy()
dataSource = null
try {
await dataSource.destroy()
dataSource = null
log.info('TypeORM DataSource destroyed')
} catch (error) {
log.error('Failed to destroy DataSource', { error })
}
}
}

View File

@@ -9,7 +9,7 @@
*/
import { create, type IDatabaseService } from './index'
import { createLogger } from '../logger'
import { createLogger, run, getRequestId, trackDuration } from '../logger'
const log = createLogger('DiscreteMaterialPlanDAO')
@@ -138,11 +138,17 @@ export class DiscreteMaterialPlanDAO {
const tableName = this.getTableName()
const sqlString = `SELECT * FROM ${tableName}`
const result = await dbService.query(sqlString)
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'DiscreteMaterialPlanDAO.queryAll',
context: { tableName, operationType: 'SELECT' }
})
return result.rows
return result.result.rows
} catch (error) {
log.error('Query all error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -182,10 +188,16 @@ export class DiscreteMaterialPlanDAO {
WHERE rn = 1
`
const result = await dbService.query(sqlString)
return result.rows
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'DiscreteMaterialPlanDAO.queryAllDistinctByMaterialCode',
context: { tableName: this.getTableName(), operationType: 'SELECT' }
})
return result.result.rows
} catch (error) {
log.error('Query all distinct by material code error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -221,13 +233,25 @@ export class DiscreteMaterialPlanDAO {
WHERE SourceNumber IN (${placeholders})
`
const result = await dbService.query(sqlString, batch)
allResults.push(...result.rows)
const result = await trackDuration(async () => await dbService.query(sqlString, batch), {
operationName: 'DiscreteMaterialPlanDAO.queryBySourceNumbers',
context: {
tableName,
operationType: 'SELECT',
batchNumber: Math.floor(i / batchSize) + 1,
batchSize: batch.length
}
})
allResults.push(...result.result.rows)
}
return allResults
} catch (error) {
log.error('Query by source numbers error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
recordCount: sourceNumbers.length,
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -280,13 +304,25 @@ export class DiscreteMaterialPlanDAO {
WHERE rn = 1
`
const result = await dbService.query(sqlString, batch)
allResults.push(...result.rows)
const result = await trackDuration(async () => await dbService.query(sqlString, batch), {
operationName: 'DiscreteMaterialPlanDAO.queryBySourceNumbersDistinct',
context: {
tableName,
operationType: 'SELECT',
batchNumber: Math.floor(i / batchSize) + 1,
batchSize: batch.length
}
})
allResults.push(...result.result.rows)
}
return allResults
} catch (error) {
log.error('Query by source numbers distinct error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
recordCount: sourceNumbers.length,
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -311,10 +347,19 @@ export class DiscreteMaterialPlanDAO {
WHERE SourceNumber = ${placeholder}
`
const result = await dbService.query(sqlString, [sourceNumber])
return result.rows
const result = await trackDuration(
async () => await dbService.query(sqlString, [sourceNumber]),
{
operationName: 'DiscreteMaterialPlanDAO.queryBySourceNumber',
context: { tableName, operationType: 'SELECT' }
}
)
return result.result.rows
} catch (error) {
log.error('Query by source number error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -341,10 +386,19 @@ export class DiscreteMaterialPlanDAO {
WHERE PlanNumber = ${placeholder}
`
const result = await dbService.query(sqlString, [planNumber])
return result.rows
const result = await trackDuration(
async () => await dbService.query(sqlString, [planNumber]),
{
operationName: 'DiscreteMaterialPlanDAO.queryByPlanNumber',
context: { tableName, operationType: 'SELECT' }
}
)
return result.result.rows
} catch (error) {
log.error('Query by plan number error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -378,13 +432,25 @@ export class DiscreteMaterialPlanDAO {
WHERE PlanNumber IN (${placeholders})
`
const result = await dbService.query(sqlString, batch)
allResults.push(...result.rows)
const result = await trackDuration(async () => await dbService.query(sqlString, batch), {
operationName: 'DiscreteMaterialPlanDAO.queryByPlanNumbers',
context: {
tableName,
operationType: 'SELECT',
batchNumber: Math.floor(i / batchSize) + 1,
batchSize: batch.length
}
})
allResults.push(...result.result.rows)
}
return allResults
} catch (error) {
log.error('Query by plan numbers error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
recordCount: planNumbers.length,
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -404,18 +470,31 @@ export class DiscreteMaterialPlanDAO {
return 0
}
const batchId = getRequestId() || `delete-${Date.now()}`
let totalDeleted = 0
try {
const dbService = await this.getDatabaseService()
const tableName = this.getTableName()
const isSqlServer = dbService.type === 'sqlserver'
const batchSize = 2000
let totalDeleted = 0
// Get unique source numbers
const uniqueSourceNumbers = [...new Set(sourceNumbers.filter(Boolean))]
const totalBatches = Math.ceil(uniqueSourceNumbers.length / batchSize)
log.info('Starting batch delete operation', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: batchId,
totalRecords: uniqueSourceNumbers.length,
batchSize,
totalBatches
})
for (let i = 0; i < uniqueSourceNumbers.length; i += batchSize) {
const batch = uniqueSourceNumbers.slice(i, i + batchSize)
const batchNumber = Math.floor(i / batchSize) + 1
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
const sqlString = `
@@ -423,16 +502,32 @@ export class DiscreteMaterialPlanDAO {
WHERE SourceNumber IN (${placeholders})
`
const result = await dbService.query(sqlString, batch)
totalDeleted += result.rowCount || 0
const result = await trackDuration(async () => await dbService.query(sqlString, batch), {
operationName: 'DiscreteMaterialPlanDAO.deleteBySourceNumbers',
context: {
tableName,
operationType: 'DELETE',
batchId,
batchNumber,
totalBatches,
batchSize: batch.length
}
})
const deletedCount = result.result.rowCount || 0
totalDeleted += deletedCount
log.debug('Deleted batch', {
batch: i / batchSize + 1,
count: result.rowCount
batch: batchNumber,
totalBatches,
count: deletedCount,
batchId
})
}
log.info('Deleted records by source numbers', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: batchId,
totalDeleted,
sourceNumberCount: uniqueSourceNumbers.length
})
@@ -440,6 +535,11 @@ export class DiscreteMaterialPlanDAO {
return totalDeleted
} catch (error) {
log.error('Delete by source numbers error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: batchId,
totalDeleted,
recordCount: sourceNumbers.length,
error: error instanceof Error ? error.message : String(error)
})
throw error
@@ -459,11 +559,13 @@ export class DiscreteMaterialPlanDAO {
return 0
}
const batchId = getRequestId() || `insert-${Date.now()}`
let totalInserted = 0
try {
const dbService = await this.getDatabaseService()
const tableName = this.getTableName()
const isSqlServer = dbService.type === 'sqlserver'
let totalInserted = 0
// SQL Server has a limit of 2100 parameters per query
// Each record has 28 columns, so max rows per batch = 2100 / 28 = 75
@@ -473,35 +575,61 @@ export class DiscreteMaterialPlanDAO {
const effectiveBatchSize = isSqlServer
? Math.min(batchSize, Math.floor(sqlServerMaxParams / columnsPerRow))
: batchSize
const totalBatches = Math.ceil(records.length / effectiveBatchSize)
log.info('Batch insert parameters', {
log.info('Batch insert started', {
tableName,
operationType: 'INSERT',
requestId: batchId,
isSqlServer,
dbType: dbService.type,
columnsPerRow,
effectiveBatchSize,
totalRecords: records.length
totalRecords: records.length,
totalBatches
})
// Process in batches
for (let i = 0; i < records.length; i += effectiveBatchSize) {
const batch = records.slice(i, i + effectiveBatchSize)
const inserted = await this.insertBatch(dbService, tableName, batch, isSqlServer)
const batchNumber = Math.floor(i / effectiveBatchSize) + 1
const inserted = await this.insertBatchWithTracking(
dbService,
tableName,
batch,
isSqlServer,
batchId,
batchNumber,
totalBatches
)
totalInserted += inserted
log.debug('Inserted batch', {
batch: Math.floor(i / effectiveBatchSize) + 1,
count: inserted
batch: batchNumber,
totalBatches,
count: inserted,
batchId
})
}
log.info('Batch insert completed', {
tableName,
operationType: 'INSERT',
requestId: batchId,
totalInserted,
batchSize: effectiveBatchSize
batchSize: effectiveBatchSize,
totalBatches
})
return totalInserted
} catch (error) {
log.error('Batch insert error', {
tableName: this.getTableName(),
operationType: 'INSERT',
requestId: batchId,
totalInserted,
recordCount: records.length,
error: error instanceof Error ? error.message : String(error)
})
throw error
@@ -509,13 +637,16 @@ export class DiscreteMaterialPlanDAO {
}
/**
* Insert a single batch of records
* Insert a single batch of records with tracking
*/
private async insertBatch(
private async insertBatchWithTracking(
dbService: IDatabaseService,
tableName: string,
records: MaterialPlanRecord[],
isSqlServer: boolean
isSqlServer: boolean,
batchId: string,
batchNumber: number,
totalBatches: number
): Promise<number> {
if (records.length === 0) {
return 0
@@ -567,8 +698,30 @@ export class DiscreteMaterialPlanDAO {
VALUES ${rowPlaceholders.join(', ')}
`
const result = await dbService.query(sqlString, values)
return result.rowCount || records.length
const result = await trackDuration(async () => await dbService.query(sqlString, values), {
operationName: 'DiscreteMaterialPlanDAO.insertBatch',
context: {
tableName,
operationType: 'INSERT',
batchId,
batchNumber,
totalBatches,
recordCount: records.length
}
})
return result.result.rowCount || records.length
}
/**
* Insert a single batch of records (legacy method - kept for compatibility)
*/
private async insertBatch(
dbService: IDatabaseService,
tableName: string,
records: MaterialPlanRecord[],
isSqlServer: boolean
): Promise<number> {
return this.insertBatchWithTracking(dbService, tableName, records, isSqlServer, 'unknown', 1, 1)
}
/**
@@ -660,11 +813,17 @@ export class DiscreteMaterialPlanDAO {
const tableName = this.getTableName()
const sqlString = `SELECT COUNT(*) as count FROM ${tableName}`
const result = await dbService.query(sqlString)
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'DiscreteMaterialPlanDAO.countAll',
context: { tableName, operationType: 'SELECT' }
})
return result.rows.length > 0 ? (result.rows[0].count as number) : 0
return result.result.rows.length > 0 ? (result.result.rows[0].count as number) : 0
} catch (error) {
log.error('Count all error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return 0
@@ -689,10 +848,19 @@ export class DiscreteMaterialPlanDAO {
WHERE PlanNumber = ${placeholder}
`
const result = await dbService.query(sqlString, [planNumber])
return result.rows.length > 0 ? (result.rows[0].count as number) : 0
const result = await trackDuration(
async () => await dbService.query(sqlString, [planNumber]),
{
operationName: 'DiscreteMaterialPlanDAO.countByPlanNumber',
context: { tableName, operationType: 'SELECT' }
}
)
return result.result.rows.length > 0 ? (result.result.rows[0].count as number) : 0
} catch (error) {
log.error('Count by plan number error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return 0
@@ -725,8 +893,18 @@ export class DiscreteMaterialPlanDAO {
AND MaterialName IS NOT NULL
`
const result = await dbService.query(sqlString, batch)
allNames.push(...result.rows.map((row) => row.MaterialName as string).filter(Boolean))
const result = await trackDuration(async () => await dbService.query(sqlString, batch), {
operationName: 'DiscreteMaterialPlanDAO.getUniqueMaterialNames',
context: {
tableName,
operationType: 'SELECT',
batchNumber: Math.floor(i / batchSize) + 1,
batchSize: batch.length
}
})
allNames.push(
...result.result.rows.map((row) => row.MaterialName as string).filter(Boolean)
)
}
return allNames
@@ -737,11 +915,18 @@ export class DiscreteMaterialPlanDAO {
WHERE MaterialName IS NOT NULL
`
const result = await dbService.query(sqlString)
return result.rows.map((row) => row.MaterialName as string).filter(Boolean)
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'DiscreteMaterialPlanDAO.getUniqueMaterialNames',
context: { tableName, operationType: 'SELECT' }
})
return result.result.rows.map((row) => row.MaterialName as string).filter(Boolean)
}
} catch (error) {
log.error('Get unique material names error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
recordCount: sourceNumbers?.length || 0,
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -767,10 +952,16 @@ export class DiscreteMaterialPlanDAO {
FROM ${tableName}
`
const result = await dbService.query(sqlString)
return result.rows.length > 0 ? result.rows[0] : {}
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'DiscreteMaterialPlanDAO.getStatistics',
context: { tableName, operationType: 'SELECT' }
})
return result.result.rows.length > 0 ? result.result.rows[0] : {}
} catch (error) {
log.error('Get statistics error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return {}

View File

@@ -10,7 +10,7 @@
*/
import { create, type IDatabaseService } from './index'
import { createLogger } from '../logger'
import { createLogger, run, getRequestId, trackDuration } from '../logger'
import type {
OperationHistoryRecord,
BatchStats,
@@ -106,15 +106,31 @@ export class ExtractorOperationHistoryDAO {
records: InsertBatchRecordInput[]
): Promise<boolean> {
if (!records || records.length === 0) {
log.warn('No records to insert')
log.warn('No records to insert', {
batchId,
tableName: this.getTableName(),
requestId: getRequestId()
})
return false
}
const requestId = getRequestId() || `insert-${Date.now()}`
try {
const dbService = await this.getDatabaseService()
const tableName = this.getTableName()
const isSqlServer = dbService.type === 'sqlserver'
log.info('Batch records insertion started', {
tableName,
operationType: 'INSERT',
requestId,
batchId,
userId,
username,
recordCount: records.length
})
for (const record of records) {
try {
if (isSqlServer) {
@@ -124,13 +140,20 @@ export class ExtractorOperationHistoryDAO {
VALUES
(@p0, @p1, @p2, @p3, @p4, GETDATE(), 'pending')
`
await dbService.query(sqlString, [
batchId,
userId,
username,
record.productionId || null,
record.orderNumber
])
await trackDuration(
async () =>
await dbService.query(sqlString, [
batchId,
userId,
username,
record.productionId || null,
record.orderNumber
]),
{
operationName: 'ExtractorOperationHistoryDAO.insertBatchRecords',
context: { tableName, operationType: 'INSERT', batchId }
}
)
} else {
const sqlString = `
INSERT INTO ${tableName}
@@ -138,16 +161,26 @@ export class ExtractorOperationHistoryDAO {
VALUES
(?, ?, ?, ?, ?, NOW(), 'pending')
`
await dbService.query(sqlString, [
batchId,
userId,
username,
record.productionId || null,
record.orderNumber
])
await trackDuration(
async () =>
await dbService.query(sqlString, [
batchId,
userId,
username,
record.productionId || null,
record.orderNumber
]),
{
operationName: 'ExtractorOperationHistoryDAO.insertBatchRecords',
context: { tableName, operationType: 'INSERT', batchId }
}
)
}
} catch (error) {
log.error('Error inserting individual record', {
tableName,
operationType: 'INSERT',
requestId,
batchId,
orderNumber: record.orderNumber,
error: error instanceof Error ? error.message : String(error)
@@ -155,10 +188,21 @@ export class ExtractorOperationHistoryDAO {
}
}
log.info('Batch records inserted', { batchId, count: records.length })
log.info('Batch records inserted', {
tableName,
operationType: 'INSERT',
requestId,
batchId,
count: records.length
})
return true
} catch (error) {
log.error('Insert batch records error', {
tableName: this.getTableName(),
operationType: 'INSERT',
requestId,
batchId,
recordCount: records.length,
error: error instanceof Error ? error.message : String(error)
})
return false
@@ -173,10 +217,7 @@ export class ExtractorOperationHistoryDAO {
* @param status - New status (success, failed, partial)
* @returns Update result
*/
async updateBatchStatus(
batchId: string,
status: string
): Promise<UpdateBatchStatusResult> {
async updateBatchStatus(batchId: string, status: string): Promise<UpdateBatchStatusResult> {
try {
const dbService = await this.getDatabaseService()
const tableName = this.getTableName()
@@ -189,12 +230,24 @@ export class ExtractorOperationHistoryDAO {
`
const params = [status, batchId]
await dbService.query(sqlString, params)
const result = await trackDuration(async () => await dbService.query(sqlString, params), {
operationName: 'ExtractorOperationHistoryDAO.updateBatchStatus',
context: { tableName, operationType: 'UPDATE', batchId }
})
log.info('Batch status updated', { batchId, status })
log.info('Batch status updated', {
tableName,
operationType: 'UPDATE',
requestId: getRequestId(),
batchId,
status
})
return { success: true, updatedCount: 1 }
} catch (error) {
log.error('Update batch status error', {
tableName: this.getTableName(),
operationType: 'UPDATE',
requestId: getRequestId(),
batchId,
error: error instanceof Error ? error.message : String(error)
})
@@ -247,11 +300,17 @@ export class ExtractorOperationHistoryDAO {
params = [status, errorMessage || null, batchId, orderNumber]
}
await dbService.query(sqlString, params)
await trackDuration(async () => await dbService.query(sqlString, params), {
operationName: 'ExtractorOperationHistoryDAO.updateRecordStatus',
context: { tableName, operationType: 'UPDATE', batchId }
})
return true
} catch (error) {
log.error('Update record status error', {
tableName: this.getTableName(),
operationType: 'UPDATE',
requestId: getRequestId(),
batchId,
orderNumber,
error: error instanceof Error ? error.message : String(error)
@@ -265,7 +324,7 @@ export class ExtractorOperationHistoryDAO {
/**
* Get batch statistics with optional user filtering
* @param userId - Optional user ID for filtering (Admin gets all, User gets own)
* @param options - Query options (limit, offset)
* @param options - Query options (limit, offset, usernames)
* @returns Array of batch statistics
*/
async getBatches(userId?: number, options?: GetBatchesOptions): Promise<BatchStats[]> {
@@ -293,6 +352,10 @@ export class ExtractorOperationHistoryDAO {
if (userId !== undefined) {
sqlString += isSqlServer ? ` WHERE UserId = @p0 ` : ` WHERE UserId = ? `
params.push(userId)
} else if (options?.usernames && options.usernames.length > 0) {
const placeholders = this.buildPlaceholders(options.usernames.length, isSqlServer)
sqlString += ` WHERE Username IN (${placeholders}) `
params.push(...options.usernames)
}
sqlString += `
@@ -305,7 +368,6 @@ export class ExtractorOperationHistoryDAO {
const safeOffset = options.offset !== undefined ? Math.floor(options.offset) : undefined
if (isSqlServer) {
// SQL Server: use parameterized OFFSET/FETCH
const offsetIndex = params.length
if (safeOffset !== undefined) {
params.push(safeOffset)
@@ -318,9 +380,6 @@ export class ExtractorOperationHistoryDAO {
sqlString += ` OFFSET 0 ROWS FETCH NEXT @p${offsetIndex} ROWS ONLY`
}
} else {
// MySQL: embed validated integer values directly.
// connection.execute() uses binary protocol prepared statements,
// which do not reliably support ? placeholders in LIMIT/OFFSET clauses.
if (safeOffset !== undefined) {
sqlString += ` LIMIT ${safeLimit} OFFSET ${safeOffset}`
} else {
@@ -329,9 +388,12 @@ export class ExtractorOperationHistoryDAO {
}
}
const result = await dbService.query(sqlString, params)
const result = await trackDuration(async () => await dbService.query(sqlString, params), {
operationName: 'ExtractorOperationHistoryDAO.getBatches',
context: { tableName, operationType: 'SELECT', userId }
})
return result.rows.map((row) => ({
return result.result.rows.map((row) => ({
batchId: row.BatchId as string,
userId: row.UserId as number,
username: row.Username as string,
@@ -344,6 +406,10 @@ export class ExtractorOperationHistoryDAO {
}))
} catch (error) {
log.error('Get batches error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
userId,
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -379,9 +445,12 @@ export class ExtractorOperationHistoryDAO {
ORDER BY ID
`
const result = await dbService.query(sqlString, [batchId])
const result = await trackDuration(async () => await dbService.query(sqlString, [batchId]), {
operationName: 'ExtractorOperationHistoryDAO.getBatchDetails',
context: { tableName, operationType: 'SELECT', batchId }
})
return result.rows.map((row) => ({
return result.result.rows.map((row) => ({
id: row.ID as number,
batchId: row.BatchId as string,
userId: row.UserId as number,
@@ -395,6 +464,9 @@ export class ExtractorOperationHistoryDAO {
}))
} catch (error) {
log.error('Get batch details error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
batchId,
error: error instanceof Error ? error.message : String(error)
})
@@ -430,13 +502,16 @@ export class ExtractorOperationHistoryDAO {
GROUP BY BatchId, UserId, Username
`
const result = await dbService.query(sqlString, [batchId])
const result = await trackDuration(async () => await dbService.query(sqlString, [batchId]), {
operationName: 'ExtractorOperationHistoryDAO.getBatchStats',
context: { tableName, operationType: 'SELECT', batchId }
})
if (result.rows.length === 0) {
if (result.result.rows.length === 0) {
return null
}
const row = result.rows[0]
const row = result.result.rows[0]
return {
batchId: row.BatchId as string,
userId: row.UserId as number,
@@ -450,6 +525,9 @@ export class ExtractorOperationHistoryDAO {
}
} catch (error) {
log.error('Get batch stats error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
batchId,
error: error instanceof Error ? error.message : String(error)
})
@@ -471,6 +549,8 @@ export class ExtractorOperationHistoryDAO {
requestingUserId: number,
isAdmin: boolean
): Promise<{ success: boolean; error?: string }> {
const requestId = getRequestId() || `delete-${Date.now()}`
try {
const dbService = await this.getDatabaseService()
const tableName = this.getTableName()
@@ -495,12 +575,24 @@ export class ExtractorOperationHistoryDAO {
WHERE BatchId = ${placeholder}
`
const result = await dbService.query(sqlString, [batchId])
const result = await trackDuration(async () => await dbService.query(sqlString, [batchId]), {
operationName: 'ExtractorOperationHistoryDAO.deleteBatch',
context: { tableName, operationType: 'DELETE', batchId, requestingUserId }
})
log.info('Batch deleted', { batchId, rowCount: result.rowCount })
log.info('Batch deleted', {
tableName,
operationType: 'DELETE',
requestId,
batchId,
rowCount: result.result.rowCount
})
return { success: true }
} catch (error) {
log.error('Delete batch error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId,
batchId,
error: error instanceof Error ? error.message : String(error)
})
@@ -528,10 +620,16 @@ export class ExtractorOperationHistoryDAO {
WHERE UserId = ${placeholder}
`
const result = await dbService.query(sqlString, [userId])
return result.rowCount
const result = await trackDuration(async () => await dbService.query(sqlString, [userId]), {
operationName: 'ExtractorOperationHistoryDAO.deleteByUser',
context: { tableName, operationType: 'DELETE', userId }
})
return result.result.rowCount
} catch (error) {
log.error('Delete by user error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: getRequestId(),
userId,
error: error instanceof Error ? error.message : String(error)
})
@@ -559,10 +657,16 @@ export class ExtractorOperationHistoryDAO {
WHERE BatchId = ${placeholder}
`
const result = await dbService.query(sqlString, [batchId])
return result.rows.length > 0 && (result.rows[0].count as number) > 0
const result = await trackDuration(async () => await dbService.query(sqlString, [batchId]), {
operationName: 'ExtractorOperationHistoryDAO.batchExists',
context: { tableName, operationType: 'SELECT', batchId }
})
return result.result.rows.length > 0 && (result.result.rows[0].count as number) > 0
} catch (error) {
log.error('Batch exists error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
batchId,
error: error instanceof Error ? error.message : String(error)
})
@@ -573,9 +677,10 @@ export class ExtractorOperationHistoryDAO {
/**
* Count total batches with optional user filtering
* @param userId - Optional user ID for filtering
* @param usernames - Optional usernames filter for Admin users
* @returns Total number of batches
*/
async countBatches(userId?: number): Promise<number> {
async countBatches(userId?: number, usernames?: string[]): Promise<number> {
try {
const dbService = await this.getDatabaseService()
const tableName = this.getTableName()
@@ -586,17 +691,27 @@ export class ExtractorOperationHistoryDAO {
FROM ${tableName}
`
const params: number[] = []
const params: (number | string)[] = []
if (userId !== undefined) {
sqlString += isSqlServer ? ` WHERE UserId = @p0 ` : ` WHERE UserId = ? `
params.push(userId)
} else if (usernames && usernames.length > 0) {
const placeholders = this.buildPlaceholders(usernames.length, isSqlServer)
sqlString += ` WHERE Username IN (${placeholders}) `
params.push(...usernames)
}
const result = await dbService.query(sqlString, params)
return result.rows.length > 0 ? (result.rows[0].count as number) : 0
const result = await trackDuration(async () => await dbService.query(sqlString, params), {
operationName: 'ExtractorOperationHistoryDAO.countBatches',
context: { tableName, operationType: 'SELECT', userId }
})
return result.result.rows.length > 0 ? (result.result.rows[0].count as number) : 0
} catch (error) {
log.error('Count batches error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return 0

View File

@@ -9,7 +9,7 @@
*/
import { create, type IDatabaseService } from './index'
import { createLogger } from '../logger'
import { createLogger, run, getRequestId, trackDuration } from '../logger'
const log = createLogger('MaterialsToBeDeletedDAO')
@@ -100,7 +100,11 @@ export class MaterialsToBeDeletedDAO {
*/
async upsertMaterial(materialCode: string, managerName: string): Promise<boolean> {
if (!materialCode || !materialCode.trim()) {
log.error('MaterialCode cannot be empty')
log.error('MaterialCode cannot be empty', {
tableName: this.getTableName(),
operationType: 'UPSERT',
requestId: getRequestId()
})
return false
}
@@ -112,7 +116,6 @@ export class MaterialsToBeDeletedDAO {
const isSqlServer = dbService.type === 'sqlserver'
if (isSqlServer) {
// SQL Server MERGE statement
const sqlString = `
MERGE ${tableName} AS target
USING (VALUES (@p0, @p1)) AS source (MaterialCode, ManagerName)
@@ -121,21 +124,30 @@ export class MaterialsToBeDeletedDAO {
WHEN NOT MATCHED THEN INSERT (MaterialCode, ManagerName) VALUES (source.MaterialCode, source.ManagerName);
`
await dbService.query(sqlString, [code, manager])
await trackDuration(async () => await dbService.query(sqlString, [code, manager]), {
operationName: 'MaterialsToBeDeletedDAO.upsertMaterial',
context: { tableName, operationType: 'MERGE' }
})
} else {
// MySQL ON DUPLICATE KEY UPDATE
const sqlString = `
INSERT INTO ${tableName} (MaterialCode, ManagerName)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
`
await dbService.query(sqlString, [code, manager])
await trackDuration(async () => await dbService.query(sqlString, [code, manager]), {
operationName: 'MaterialsToBeDeletedDAO.upsertMaterial',
context: { tableName, operationType: 'INSERT' }
})
}
return true
} catch (error) {
log.error('Upsert material error', {
tableName: this.getTableName(),
operationType: 'UPSERT',
requestId: getRequestId(),
materialCode: materialCode.trim(),
error: error instanceof Error ? error.message : String(error)
})
return false
@@ -154,6 +166,7 @@ export class MaterialsToBeDeletedDAO {
return { total: 0, success: 0, failed: 0 }
}
const batchId = getRequestId() || `upsert-${Date.now()}`
const stats: UpsertStats = {
total: materials.length,
success: 0,
@@ -165,6 +178,14 @@ export class MaterialsToBeDeletedDAO {
const tableName = this.getTableName()
const isSqlServer = dbService.type === 'sqlserver'
log.info('Batch upsert started', {
tableName,
operationType: 'UPSERT',
requestId: batchId,
totalRecords: materials.length,
dbType: dbService.type
})
for (const material of materials) {
const materialCode = material.materialCode?.trim()
const managerName = material.managerName?.trim() || ''
@@ -176,7 +197,6 @@ export class MaterialsToBeDeletedDAO {
try {
if (isSqlServer) {
// SQL Server MERGE statement
const sqlString = `
MERGE ${tableName} AS target
USING (VALUES (@p0, @p1)) AS source (MaterialCode, ManagerName)
@@ -185,29 +205,56 @@ export class MaterialsToBeDeletedDAO {
WHEN NOT MATCHED THEN INSERT (MaterialCode, ManagerName) VALUES (source.MaterialCode, source.ManagerName);
`
await dbService.query(sqlString, [materialCode, managerName || null])
await trackDuration(
async () => await dbService.query(sqlString, [materialCode, managerName || null]),
{
operationName: 'MaterialsToBeDeletedDAO.upsertBatch',
context: { tableName, operationType: 'MERGE', batchId }
}
)
} else {
// MySQL ON DUPLICATE KEY UPDATE
const sqlString = `
INSERT INTO ${tableName} (MaterialCode, ManagerName)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
`
await dbService.query(sqlString, [materialCode, managerName || null])
await trackDuration(
async () => await dbService.query(sqlString, [materialCode, managerName || null]),
{
operationName: 'MaterialsToBeDeletedDAO.upsertBatch',
context: { tableName, operationType: 'INSERT', batchId }
}
)
}
stats.success++
} catch (error) {
log.error('Error upserting material', {
tableName,
operationType: 'UPSERT',
requestId: batchId,
materialCode,
error: error instanceof Error ? error.message : String(error)
})
stats.failed++
}
}
log.info('Batch upsert completed', {
tableName,
operationType: 'UPSERT',
requestId: batchId,
success: stats.success,
failed: stats.failed,
total: stats.total
})
} catch (error) {
log.error('Batch upsert error', {
tableName: this.getTableName(),
operationType: 'UPSERT',
requestId: batchId,
totalRecords: materials.length,
error: error instanceof Error ? error.message : String(error)
})
stats.failed = stats.total - stats.success
@@ -279,10 +326,16 @@ export class MaterialsToBeDeletedDAO {
WHERE MaterialCode IS NOT NULL
`
const result = await dbService.query(sqlString)
return new Set(result.rows.map((row) => row.MaterialCode as string).filter(Boolean))
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'MaterialsToBeDeletedDAO.getAllMaterialCodes',
context: { tableName, operationType: 'SELECT' }
})
return new Set(result.result.rows.map((row) => row.MaterialCode as string).filter(Boolean))
} catch (error) {
log.error('Get all material codes error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return new Set()
@@ -305,14 +358,20 @@ export class MaterialsToBeDeletedDAO {
ORDER BY ManagerName, MaterialCode
`
const result = await dbService.query(sqlString)
return result.rows.map((row) => ({
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'MaterialsToBeDeletedDAO.getAllRecords',
context: { tableName, operationType: 'SELECT' }
})
return result.result.rows.map((row) => ({
id: row.ID as number,
materialCode: row.MaterialCode as string,
managerName: row.ManagerName as string
}))
} catch (error) {
log.error('Get all records error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -338,14 +397,23 @@ export class MaterialsToBeDeletedDAO {
ORDER BY MaterialCode
`
const result = await dbService.query(sqlString, [managerName])
return result.rows.map((row) => ({
const result = await trackDuration(
async () => await dbService.query(sqlString, [managerName]),
{
operationName: 'MaterialsToBeDeletedDAO.getMaterialsByManager',
context: { tableName, operationType: 'SELECT' }
}
)
return result.result.rows.map((row) => ({
id: row.ID as number,
materialCode: row.MaterialCode as string,
managerName: row.ManagerName as string
}))
} catch (error) {
log.error('Get materials by manager error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -368,10 +436,16 @@ export class MaterialsToBeDeletedDAO {
ORDER BY ManagerName
`
const result = await dbService.query(sqlString)
return result.rows.map((row) => row.ManagerName as string).filter(Boolean)
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'MaterialsToBeDeletedDAO.getManagers',
context: { tableName, operationType: 'SELECT' }
})
return result.result.rows.map((row) => row.ManagerName as string).filter(Boolean)
} catch (error) {
log.error('Get managers error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -397,13 +471,16 @@ export class MaterialsToBeDeletedDAO {
WHERE MaterialCode = ${placeholder}
`
const result = await dbService.query(sqlString, [code])
const result = await trackDuration(async () => await dbService.query(sqlString, [code]), {
operationName: 'MaterialsToBeDeletedDAO.getRecordByMaterialCode',
context: { tableName, operationType: 'SELECT' }
})
if (result.rows.length === 0) {
if (result.result.rows.length === 0) {
return null
}
const row = result.rows[0]
const row = result.result.rows[0]
return {
id: row.ID as number,
materialCode: row.MaterialCode as string,
@@ -411,6 +488,9 @@ export class MaterialsToBeDeletedDAO {
}
} catch (error) {
log.error('Get record by material code error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return null
@@ -437,10 +517,16 @@ export class MaterialsToBeDeletedDAO {
WHERE MaterialCode = ${placeholder}
`
const result = await dbService.query(sqlString, [code])
return result.rowCount > 0
const result = await trackDuration(async () => await dbService.query(sqlString, [code]), {
operationName: 'MaterialsToBeDeletedDAO.deleteByMaterialCode',
context: { tableName, operationType: 'DELETE' }
})
return result.result.rowCount > 0
} catch (error) {
log.error('Delete by material code error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return false
@@ -464,10 +550,19 @@ export class MaterialsToBeDeletedDAO {
WHERE ManagerName = ${placeholder}
`
const result = await dbService.query(sqlString, [managerName])
return result.rowCount
const result = await trackDuration(
async () => await dbService.query(sqlString, [managerName]),
{
operationName: 'MaterialsToBeDeletedDAO.deleteByManager',
context: { tableName, operationType: 'DELETE' }
}
)
return result.result.rowCount
} catch (error) {
log.error('Delete by manager error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return 0
@@ -484,10 +579,16 @@ export class MaterialsToBeDeletedDAO {
const tableName = this.getTableName()
const sqlString = `DELETE FROM ${tableName}`
const result = await dbService.query(sqlString)
return result.rowCount
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'MaterialsToBeDeletedDAO.deleteAllMaterials',
context: { tableName, operationType: 'DELETE' }
})
return result.result.rowCount
} catch (error) {
log.error('Delete all materials error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return 0
@@ -504,6 +605,7 @@ export class MaterialsToBeDeletedDAO {
return 0
}
const batchId = getRequestId() || `delete-${Date.now()}`
let totalDeleted = 0
const batchSize = 1000
@@ -511,9 +613,20 @@ export class MaterialsToBeDeletedDAO {
const dbService = await this.getDatabaseService()
const tableName = this.getTableName()
const isSqlServer = dbService.type === 'sqlserver'
const totalBatches = Math.ceil(materialCodes.length / batchSize)
log.info('Batch delete started', {
tableName,
operationType: 'DELETE',
requestId: batchId,
totalRecords: materialCodes.length,
batchSize,
totalBatches
})
for (let i = 0; i < materialCodes.length; i += batchSize) {
const batch = materialCodes.slice(i, i + batchSize)
const batchNumber = Math.floor(i / batchSize) + 1
const placeholders = this.buildPlaceholders(batch.length, isSqlServer)
const sqlString = `
@@ -521,14 +634,41 @@ export class MaterialsToBeDeletedDAO {
WHERE MaterialCode IN (${placeholders})
`
const result = await dbService.query(
sqlString,
batch.map((c) => c.trim())
const result = await trackDuration(
async () =>
await dbService.query(
sqlString,
batch.map((c) => c.trim())
),
{
operationName: 'MaterialsToBeDeletedDAO.deleteByMaterialCodes',
context: {
tableName,
operationType: 'DELETE',
batchId,
batchNumber,
totalBatches,
batchSize: batch.length
}
}
)
totalDeleted += result.rowCount
totalDeleted += result.result.rowCount
}
log.info('Batch delete completed', {
tableName,
operationType: 'DELETE',
requestId: batchId,
totalDeleted,
totalRecords: materialCodes.length
})
} catch (error) {
log.error('Delete by material codes error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: batchId,
totalDeleted,
recordCount: materialCodes.length,
error: error instanceof Error ? error.message : String(error)
})
}
@@ -557,10 +697,16 @@ export class MaterialsToBeDeletedDAO {
WHERE MaterialCode = ${placeholder}
`
const result = await dbService.query(sqlString, [code])
return result.rows.length > 0 && (result.rows[0].count as number) > 0
const result = await trackDuration(async () => await dbService.query(sqlString, [code]), {
operationName: 'MaterialsToBeDeletedDAO.materialExists',
context: { tableName, operationType: 'SELECT' }
})
return result.result.rows.length > 0 && (result.result.rows[0].count as number) > 0
} catch (error) {
log.error('Material exists error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return false
@@ -577,11 +723,17 @@ export class MaterialsToBeDeletedDAO {
const tableName = this.getTableName()
const sqlString = `SELECT COUNT(*) as count FROM ${tableName}`
const result = await dbService.query(sqlString)
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'MaterialsToBeDeletedDAO.countAll',
context: { tableName, operationType: 'SELECT' }
})
return result.rows.length > 0 ? (result.rows[0].count as number) : 0
return result.result.rows.length > 0 ? (result.result.rows[0].count as number) : 0
} catch (error) {
log.error('Count all error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return 0
@@ -606,10 +758,19 @@ export class MaterialsToBeDeletedDAO {
WHERE ManagerName = ${placeholder}
`
const result = await dbService.query(sqlString, [managerName])
return result.rows.length > 0 ? (result.rows[0].count as number) : 0
const result = await trackDuration(
async () => await dbService.query(sqlString, [managerName]),
{
operationName: 'MaterialsToBeDeletedDAO.countByManager',
context: { tableName, operationType: 'SELECT' }
}
)
return result.result.rows.length > 0 ? (result.result.rows[0].count as number) : 0
} catch (error) {
log.error('Count by manager error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return 0
@@ -634,8 +795,11 @@ export class MaterialsToBeDeletedDAO {
WHERE MaterialCode IS NOT NULL
`
const statsResult = await dbService.query(statsSql)
const stats = statsResult.rows[0] || {}
const statsResult = await trackDuration(async () => await dbService.query(statsSql), {
operationName: 'MaterialsToBeDeletedDAO.getStatistics',
context: { tableName, operationType: 'SELECT' }
})
const stats = statsResult.result.rows[0] || {}
// Get materials per manager
const managerSql = `
@@ -646,8 +810,11 @@ export class MaterialsToBeDeletedDAO {
ORDER BY count DESC
`
const managerResult = await dbService.query(managerSql)
const materialsPerManager = managerResult.rows.map((row) => ({
const managerResult = await trackDuration(async () => await dbService.query(managerSql), {
operationName: 'MaterialsToBeDeletedDAO.getStatistics.managers',
context: { tableName, operationType: 'SELECT' }
})
const materialsPerManager = managerResult.result.rows.map((row) => ({
[row.ManagerName as string]: row.count as number
}))
@@ -658,6 +825,9 @@ export class MaterialsToBeDeletedDAO {
}
} catch (error) {
log.error('Get statistics error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return {

View File

@@ -6,7 +6,7 @@
*/
import { create, type IDatabaseService } from './index'
import { createLogger } from '../logger'
import { createLogger, run, getRequestId, trackDuration } from '../logger'
const log = createLogger('MaterialsTypeToBeDeletedDAO')
@@ -87,14 +87,20 @@ export class MaterialsTypeToBeDeletedDAO {
ORDER BY ManagerName, MaterialName
`
const result = await dbService.query(sqlString)
return result.rows.map((row) => ({
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'MaterialsTypeToBeDeletedDAO.getAllMaterials',
context: { tableName, operationType: 'SELECT' }
})
return result.result.rows.map((row) => ({
id: row.ID as number,
materialName: row.MaterialName as string,
managerName: row.ManagerName as string
}))
} catch (error) {
log.error('Get all materials error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -120,14 +126,23 @@ export class MaterialsTypeToBeDeletedDAO {
ORDER BY MaterialName
`
const result = await dbService.query(sqlString, [managerName])
return result.rows.map((row) => ({
const result = await trackDuration(
async () => await dbService.query(sqlString, [managerName]),
{
operationName: 'MaterialsTypeToBeDeletedDAO.getMaterialsByManager',
context: { tableName, operationType: 'SELECT' }
}
)
return result.result.rows.map((row) => ({
id: row.ID as number,
materialName: row.MaterialName as string,
managerName: row.ManagerName as string
}))
} catch (error) {
log.error('Get materials by manager error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -150,10 +165,16 @@ export class MaterialsTypeToBeDeletedDAO {
ORDER BY ManagerName
`
const result = await dbService.query(sqlString)
return result.rows.map((row) => row.ManagerName as string).filter(Boolean)
const result = await trackDuration(async () => await dbService.query(sqlString), {
operationName: 'MaterialsTypeToBeDeletedDAO.getManagers',
context: { tableName, operationType: 'SELECT' }
})
return result.result.rows.map((row) => row.ManagerName as string).filter(Boolean)
} catch (error) {
log.error('Get managers error', {
tableName: this.getTableName(),
operationType: 'SELECT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return []
@@ -170,7 +191,11 @@ export class MaterialsTypeToBeDeletedDAO {
*/
async upsertMaterial(materialName: string, managerName: string): Promise<boolean> {
if (!materialName || !materialName.trim()) {
log.error('MaterialName cannot be empty')
log.error('MaterialName cannot be empty', {
tableName: this.getTableName(),
operationType: 'UPSERT',
requestId: getRequestId()
})
return false
}
@@ -182,7 +207,6 @@ export class MaterialsTypeToBeDeletedDAO {
const isSqlServer = dbService.type === 'sqlserver'
if (isSqlServer) {
// SQL Server MERGE statement
const sqlString = `
MERGE ${tableName} AS target
USING (VALUES (@p0, @p1)) AS source (MaterialName, ManagerName)
@@ -191,21 +215,29 @@ export class MaterialsTypeToBeDeletedDAO {
WHEN NOT MATCHED THEN INSERT (MaterialName, ManagerName) VALUES (source.MaterialName, source.ManagerName);
`
await dbService.query(sqlString, [name, manager])
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
context: { tableName, operationType: 'MERGE' }
})
} else {
// MySQL ON DUPLICATE KEY UPDATE
const sqlString = `
INSERT INTO ${tableName} (MaterialName, ManagerName)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE ManagerName = VALUES(ManagerName)
`
await dbService.query(sqlString, [name, manager])
await trackDuration(async () => await dbService.query(sqlString, [name, manager]), {
operationName: 'MaterialsTypeToBeDeletedDAO.upsertMaterial',
context: { tableName, operationType: 'INSERT' }
})
}
return true
} catch (error) {
log.error('Upsert material error', {
tableName: this.getTableName(),
operationType: 'UPSERT',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return false
@@ -247,10 +279,16 @@ export class MaterialsTypeToBeDeletedDAO {
params = [name]
}
const result = await dbService.query(sqlString, params)
return result.rowCount > 0
const result = await trackDuration(async () => await dbService.query(sqlString, params), {
operationName: 'MaterialsTypeToBeDeletedDAO.deleteMaterial',
context: { tableName, operationType: 'DELETE' }
})
return result.result.rowCount > 0
} catch (error) {
log.error('Delete material error', {
tableName: this.getTableName(),
operationType: 'DELETE',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return false
@@ -284,29 +322,46 @@ export class MaterialsTypeToBeDeletedDAO {
SET MaterialName = @p0, ManagerName = @p1
WHERE MaterialName = @p2 AND ManagerName = @p3
`
const result = await dbService.query(sqlString, [
newName.trim(),
newManager.trim(),
oldName.trim(),
oldManager.trim()
])
return result.rowCount > 0
const result = await trackDuration(
async () =>
await dbService.query(sqlString, [
newName.trim(),
newManager.trim(),
oldName.trim(),
oldManager.trim()
]),
{
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
context: { tableName, operationType: 'UPDATE' }
}
)
return result.result.rowCount > 0
} else {
const sqlString = `
UPDATE ${tableName}
SET MaterialName = ?, ManagerName = ?
WHERE MaterialName = ? AND ManagerName = ?
`
const result = await dbService.query(sqlString, [
newName.trim(),
newManager.trim(),
oldName.trim(),
oldManager.trim()
])
return result.rowCount > 0
const result = await trackDuration(
async () =>
await dbService.query(sqlString, [
newName.trim(),
newManager.trim(),
oldName.trim(),
oldManager.trim()
]),
{
operationName: 'MaterialsTypeToBeDeletedDAO.updateMaterial',
context: { tableName, operationType: 'UPDATE' }
}
)
return result.result.rowCount > 0
}
} catch (error) {
log.error('Update material error', {
tableName: this.getTableName(),
operationType: 'UPDATE',
requestId: getRequestId(),
error: error instanceof Error ? error.message : String(error)
})
return false
@@ -323,9 +378,24 @@ export class MaterialsTypeToBeDeletedDAO {
async upsertBatch(
request: MaterialTypeBatchRequest
): Promise<{ total: number; success: number; failed: number }> {
const batchId = getRequestId() || `batch-${Date.now()}`
const stats = { total: 0, success: 0, failed: 0 }
try {
const tableName = this.getTableName()
const totalOperations =
request.toInsert.length + request.toUpdate.length + request.toDelete.length
log.info('Batch upsert started', {
tableName,
operationType: 'BATCH',
requestId: batchId,
totalOperations,
inserts: request.toInsert.length,
updates: request.toUpdate.length,
deletes: request.toDelete.length
})
// Process inserts
for (const record of request.toInsert) {
stats.total++
@@ -355,9 +425,24 @@ export class MaterialsTypeToBeDeletedDAO {
else stats.failed++
}
log.info('Batch upsert completed', {
tableName,
operationType: 'BATCH',
requestId: batchId,
success: stats.success,
failed: stats.failed,
total: stats.total
})
return stats
} catch (error) {
log.error('Batch upsert error', {
tableName: this.getTableName(),
operationType: 'BATCH',
requestId: batchId,
total: stats.total,
success: stats.success,
failed: stats.failed,
error: error instanceof Error ? error.message : String(error)
})
return stats

View File

@@ -5,6 +5,9 @@ import type {
QueryResult,
MySqlConfig
} from '../../types/database.types'
import { createLogger, trackDuration } from '../logger'
const log = createLogger('MySqlService')
export type { MySqlConfig } from '../../types/database.types'
@@ -24,6 +27,7 @@ export class MySqlService implements IDatabaseService {
*/
async connect(): Promise<void> {
if (this.connection) {
log.warn('Already connected to MySQL')
throw new Error('Already connected to MySQL')
}
@@ -38,7 +42,18 @@ export class MySqlService implements IDatabaseService {
// Test connection
await this.connection.ping()
log.info('Connected to MySQL', {
host: this.config.host,
port: this.config.port,
database: this.config.database
})
} catch (error) {
log.error('Failed to connect to MySQL', {
host: this.config.host,
port: this.config.port,
database: this.config.database,
error
})
throw new Error(`Failed to connect to MySQL: ${(error as Error).message}`)
}
}
@@ -54,7 +69,9 @@ export class MySqlService implements IDatabaseService {
try {
await this.connection.end()
this.connection = null
log.info('Disconnected from MySQL')
} catch (error) {
log.error('Failed to disconnect from MySQL', { error })
throw new Error(`Failed to disconnect from MySQL: ${(error as Error).message}`)
}
}
@@ -74,32 +91,40 @@ export class MySqlService implements IDatabaseService {
throw new Error('Not connected to MySQL. Call connect() first.')
}
const sqlPreview = sql.substring(0, 100)
const paramCount = params?.length ?? 0
try {
const [result, fields] = await this.connection.execute(sql, params)
const { result: queryResult } = await trackDuration(
async () => {
const [result, fields] = await this.connection!.execute(sql, params)
// Convert to plain objects and extract column names
const columns = Array.isArray(fields) ? fields.map((field) => field.name) : []
// Convert to plain objects and extract column names
const columns = Array.isArray(fields) ? fields.map((field) => field.name) : []
// Handle different result types
let rows: Record<string, unknown>[] = []
let rowCount = 0
// Handle different result types
let rows: Record<string, unknown>[] = []
let rowCount = 0
if (Array.isArray(result)) {
// SELECT query - result is an array of rows
rows = result as Record<string, unknown>[]
rowCount = rows.length
} else if (typeof result === 'object' && result !== null) {
// INSERT/UPDATE/DELETE query - result is OkPacket
const okPacket = result as any
rowCount = okPacket.affectedRows || okPacket.changedRows || 0
}
if (Array.isArray(result)) {
// SELECT query - result is an array of rows
rows = result as Record<string, unknown>[]
rowCount = rows.length
} else if (typeof result === 'object' && result !== null) {
// INSERT/UPDATE/DELETE query - result is OkPacket
const okPacket = result as any
rowCount = okPacket.affectedRows || okPacket.changedRows || 0
}
return {
rows,
columns,
rowCount
}
return { rows, columns, rowCount }
},
{ operationName: 'MySqlService.query' }
)
log.debug('Query executed', { sqlPreview, rowCount: queryResult.rowCount, paramCount })
return queryResult
} catch (error) {
log.error('MySQL query failed', { sqlPreview, paramCount, error })
throw new Error(`MySQL query failed: ${(error as Error).message}`)
}
}
@@ -112,17 +137,24 @@ export class MySqlService implements IDatabaseService {
throw new Error('Not connected to MySQL. Call connect() first.')
}
const queryCount = queries.length
log.info('Transaction started', { queryCount })
try {
await this.connection.beginTransaction()
for (const { sql, params } of queries) {
for (let i = 0; i < queries.length; i++) {
const { sql, params } = queries[i]
await this.connection.execute(sql, params)
log.debug('Transaction query executed', { index: i, sqlPreview: sql.substring(0, 100) })
}
await this.connection.commit()
log.info('Transaction committed', { queryCount })
} catch (error) {
if (this.connection) {
await this.connection.rollback()
log.warn('Transaction rolled back', { queryCount, error })
}
throw new Error(`MySQL transaction failed: ${(error as Error).message}`)
}

View File

@@ -5,6 +5,9 @@ import type {
QueryResult,
SqlServerConfig
} from '../../types/database.types'
import { createLogger, trackDuration } from '../logger'
const log = createLogger('SqlServerService')
export type { SqlServerConfig } from '../../types/database.types'
@@ -24,6 +27,7 @@ export class SqlServerService implements IDatabaseService {
*/
async connect(): Promise<void> {
if (this.pool) {
log.warn('Already connected to SQL Server')
throw new Error('Already connected to SQL Server')
}
@@ -42,7 +46,18 @@ export class SqlServerService implements IDatabaseService {
this.pool = new sql.ConnectionPool(poolConfig)
await this.pool.connect()
log.info('Connected to SQL Server', {
server: this.config.server,
port: this.config.port,
database: this.config.database
})
} catch (error) {
log.error('Failed to connect to SQL Server', {
server: this.config.server,
port: this.config.port,
database: this.config.database,
error
})
throw new Error(`Failed to connect to SQL Server: ${(error as Error).message}`)
}
}
@@ -58,7 +73,9 @@ export class SqlServerService implements IDatabaseService {
try {
await this.pool.close()
this.pool = null
log.info('Disconnected from SQL Server')
} catch (error) {
log.error('Failed to disconnect from SQL Server', { error })
throw new Error(`Failed to disconnect from SQL Server: ${(error as Error).message}`)
}
}
@@ -80,29 +97,41 @@ export class SqlServerService implements IDatabaseService {
throw new Error('Not connected to SQL Server. Call connect() first.')
}
const sqlPreview = sqlString.substring(0, 100)
const paramCount = params?.length ?? 0
try {
const request = this.pool.request()
const { result: queryResult } = await trackDuration(
async () => {
const request = this.pool!.request()
// Add parameters if provided - convert array to @p0, @p1, ... format
if (params && params.length > 0) {
params.forEach((value, index) => {
request.input(`p${index}`, value)
})
}
// Add parameters if provided - convert array to @p0, @p1, ... format
if (params && params.length > 0) {
params.forEach((value, index) => {
request.input(`p${index}`, value)
})
}
const result = await request.query(sqlString)
const result = await request.query(sqlString)
// Convert recordset to array of objects (may be undefined for DELETE/INSERT/UPDATE)
const rows = (result.recordset as Record<string, unknown>[]) || []
// Extract column names from the first row if available
const columns = rows.length > 0 ? Object.keys(rows[0]) : []
// Convert recordset to array of objects (may be undefined for DELETE/INSERT/UPDATE)
const rows = (result.recordset as Record<string, unknown>[]) || []
// Extract column names from the first row if available
const columns = rows.length > 0 ? Object.keys(rows[0]) : []
return {
rows,
columns,
rowCount: result.rowsAffected?.[0] || rows.length
}
return {
rows,
columns,
rowCount: result.rowsAffected?.[0] || rows.length
}
},
{ operationName: 'SqlServerService.query' }
)
log.debug('Query executed', { sqlPreview, rowCount: queryResult.rowCount, paramCount })
return queryResult
} catch (error) {
log.error('SQL Server query failed', { sqlPreview, paramCount, error })
throw new Error(`SQL Server query failed: ${(error as Error).message}`)
}
}
@@ -126,31 +155,47 @@ export class SqlServerService implements IDatabaseService {
throw new Error('Not connected to SQL Server. Call connect() first.')
}
const sqlPreview = sqlString.substring(0, 100)
const paramNames = Object.keys(params)
try {
const request = this.pool.request()
const { result: queryResult } = await trackDuration(
async () => {
const request = this.pool!.request()
// Add parameters with explicit types
for (const [key, { value, type }] of Object.entries(params)) {
if (type) {
request.input(key, type, value)
} else {
request.input(key, value)
}
}
// Add parameters with explicit types
for (const [key, { value, type }] of Object.entries(params)) {
if (type) {
request.input(key, type, value)
} else {
request.input(key, value)
}
}
const result = await request.query(sqlString)
const result = await request.query(sqlString)
// Convert recordset to array of objects
const rows = result.recordset as Record<string, unknown>[]
// Extract column names from the first row if available
const columns = rows.length > 0 ? Object.keys(rows[0]) : []
// Convert recordset to array of objects
const rows = result.recordset as Record<string, unknown>[]
// Extract column names from the first row if available
const columns = rows.length > 0 ? Object.keys(rows[0]) : []
return {
rows,
columns,
rowCount: result.rowsAffected?.[0] || rows.length
}
return {
rows,
columns,
rowCount: result.rowsAffected?.[0] || rows.length
}
},
{ operationName: 'SqlServerService.queryWithParams' }
)
log.debug('Query with params executed', {
sqlPreview,
rowCount: queryResult.rowCount,
paramNames
})
return queryResult
} catch (error) {
log.error('SQL Server query with params failed', { sqlPreview, paramNames, error })
throw new Error(`SQL Server query failed: ${(error as Error).message}`)
}
}
@@ -164,12 +209,15 @@ export class SqlServerService implements IDatabaseService {
throw new Error('Not connected to SQL Server. Call connect() first.')
}
const queryCount = queries.length
const transaction = new sql.Transaction(this.pool)
log.info('Transaction started', { queryCount })
try {
await transaction.begin()
for (const { sql: sqlString, params } of queries) {
for (let i = 0; i < queries.length; i++) {
const { sql: sqlString, params } = queries[i]
const request = new sql.Request(transaction)
// Add parameters if provided - convert array to @p0, @p1, ... format
@@ -180,11 +228,17 @@ export class SqlServerService implements IDatabaseService {
}
await request.query(sqlString)
log.debug('Transaction query executed', {
index: i,
sqlPreview: sqlString.substring(0, 100)
})
}
await transaction.commit()
log.info('Transaction committed', { queryCount })
} catch (error) {
await transaction.rollback()
log.warn('Transaction rolled back', { queryCount, error })
throw new Error(`SQL Server transaction failed: ${(error as Error).message}`)
}
}

View File

@@ -3,7 +3,7 @@ import { ErpAuthService } from './erp-auth'
import type { CleanerInput, CleanerResult, OrderCleanDetail } from '../../types/cleaner.types'
import type { ErpSession } from '../../types/erp.types'
import type { FrameLocator, Locator, Page } from 'playwright'
import { createLogger } from '../logger'
import { createLogger, run, trackDuration } from '../logger'
const log = createLogger('CleanerService')
@@ -173,6 +173,15 @@ export class CleanerService {
}
async clean(input: CleanerInput): Promise<CleanerResult> {
return run(
async () => {
return await this.performCleanup(input)
},
{ operation: 'cleaner' }
)
}
private async performCleanup(input: CleanerInput): Promise<CleanerResult> {
const result: CleanerResult = {
ordersProcessed: 0,
materialsDeleted: 0,
@@ -184,6 +193,7 @@ export class CleanerService {
}
const totalOrders = input.orderNumbers.length
const totalMaterials = input.materialCodes.length
const dryRun = input.dryRun ?? this.dryRun
const queryBatchSize = clampNumber(
input.queryBatchSize,
@@ -200,10 +210,12 @@ export class CleanerService {
log.info('Starting cleaner', {
totalOrders,
materialCount: input.materialCodes.length,
totalMaterials,
dryRun,
queryBatchSize,
processConcurrency
processConcurrency,
orderNumbers: input.orderNumbers,
materialCodes: input.materialCodes
})
const deleteSet = new Set(input.materialCodes)
@@ -231,56 +243,75 @@ export class CleanerService {
log.info('Processing cleaner batch', {
batchIndex: batchIndex + 1,
totalBatches: orderBatches.length,
batchSize: batchOrders.length
batchSize: batchOrders.length,
totalOrders,
totalMaterials
})
await this.queryOrders(workFrame, batchOrders)
await this.waitForLoading(workFrame)
// Track batch processing duration with 5s slow threshold
await trackDuration(
async () => {
await this.queryOrders(workFrame, batchOrders)
await this.waitForLoading(workFrame)
const queriedRows = await this.collectQueryResultRows(workFrame)
const queriedOrderNumbersInBatch = new Set(queriedRows.map((row) => row.orderNumber))
const queriedRows = await this.collectQueryResultRows(workFrame)
const queriedOrderNumbersInBatch = new Set(queriedRows.map((row) => row.orderNumber))
await runWithConcurrency(queriedRows, processConcurrency, async (row) => {
const { rowIndex, orderNumber } = row
const openedDetailPage = await popupMutex.runExclusive(async () => {
return await this.openDetailPageFromRow(workFrame, popupPage!, rowIndex)
})
await runWithConcurrency(queriedRows, processConcurrency, async (row) => {
const { rowIndex, orderNumber } = row
const openedDetailPage = await popupMutex.runExclusive(async () => {
return await this.openDetailPageFromRow(workFrame, popupPage!, rowIndex)
})
let detail: OrderCleanDetail
try {
detail = await this.processDetailPage({
detailPage: openedDetailPage,
deleteSet,
dryRun,
expectedOrderNumber: orderNumber,
progressState,
onProgress: input.onProgress
let detail: OrderCleanDetail
try {
detail = await this.processDetailPage({
detailPage: openedDetailPage,
deleteSet,
dryRun,
expectedOrderNumber: orderNumber,
progressState,
onProgress: input.onProgress
})
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
detail = this.createErrorDetail(orderNumber, message)
} finally {
progressState.completedOrders += 1
}
result.details.push(detail)
if (detail.errors.length > 0) {
result.errors.push(`Order ${detail.orderNumber}: ${detail.errors.join('; ')}`)
return
}
result.ordersProcessed += 1
result.materialsDeleted += detail.materialsDeleted
result.materialsSkipped += detail.materialsSkipped
})
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
detail = this.createErrorDetail(orderNumber, message)
} finally {
progressState.completedOrders += 1
const missingOrders = getMissingOrders(batchOrders, queriedOrderNumbersInBatch)
for (const missingOrder of missingOrders) {
const missingMessage = '订单未出现在查询结果中'
result.errors.push(`Order ${missingOrder}: ${missingMessage}`)
result.details.push(this.createErrorDetail(missingOrder, missingMessage))
}
},
{
operationName: `batch-${batchIndex + 1}-${orderBatches[batchIndex].length}-orders`,
message: `Batch ${batchIndex + 1}/${orderBatches.length}`,
slowThresholdMs: 5000,
context: {
batchIndex: batchIndex + 1,
totalBatches: orderBatches.length,
batchSize: batchOrders.length,
totalOrders,
totalMaterials
}
}
result.details.push(detail)
if (detail.errors.length > 0) {
result.errors.push(`Order ${detail.orderNumber}: ${detail.errors.join('; ')}`)
return
}
result.ordersProcessed += 1
result.materialsDeleted += detail.materialsDeleted
result.materialsSkipped += detail.materialsSkipped
})
const missingOrders = getMissingOrders(batchOrders, queriedOrderNumbersInBatch)
for (const missingOrder of missingOrders) {
const missingMessage = '订单未出现在查询结果中'
result.errors.push(`Order ${missingOrder}: ${missingMessage}`)
result.details.push(this.createErrorDetail(missingOrder, missingMessage))
}
)
}
const retryResult = await this.retryFailedOrders({
@@ -321,11 +352,21 @@ export class CleanerService {
ordersProcessed: result.ordersProcessed,
materialsDeleted: result.materialsDeleted,
materialsSkipped: result.materialsSkipped,
errorCount: result.errors.length
errorCount: result.errors.length,
totalOrders,
totalMaterials,
dryRun
})
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.error('Cleaner failed', { error: message })
log.error('Cleaner failed', {
error: message,
totalOrders,
totalMaterials,
dryRun,
orderNumbers: input.orderNumbers,
materialCodes: input.materialCodes
})
result.errors.push(`Clean failed: ${message}`)
} finally {
if (popupPage) {
@@ -780,86 +821,112 @@ export class CleanerService {
return result
}
log.info('Starting retry for failed orders', { count: failedDetails.length })
log.info('Starting retry for failed orders', {
count: failedDetails.length,
totalOrders: params.failedDetails.length
})
const MAX_RETRIES = 2
for (let detailIndex = 0; detailIndex < failedDetails.length; detailIndex++) {
const failedDetail = failedDetails[detailIndex]
const orderNumber = failedDetail.orderNumber
const retryAttempts: import('../../types/cleaner.types').RetryAttempt[] = []
// Track overall retry process duration
const trackedResult = await trackDuration(
async () => {
const retryResult: RetryResult = {
retriedOrders: 0,
successfulRetries: 0,
updatedDetails: []
}
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
log.info(`Retrying order ${orderNumber} (attempt ${attempt}/${MAX_RETRIES})`)
for (let detailIndex = 0; detailIndex < failedDetails.length; detailIndex++) {
const failedDetail = failedDetails[detailIndex]
const orderNumber = failedDetail.orderNumber
const retryAttempts: import('../../types/cleaner.types').RetryAttempt[] = []
await this.queryOrders(workFrame, [orderNumber])
await this.waitForLoading(workFrame)
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
log.info(`Retrying order ${orderNumber} (attempt ${attempt}/${MAX_RETRIES})`)
const rows = workFrame.locator('tbody tr')
const rowCount = await rows.count()
if (rowCount === 0) {
throw new Error('订单重试查询无结果')
}
await this.queryOrders(workFrame, [orderNumber])
await this.waitForLoading(workFrame)
const detailPage = await this.openDetailPageFromCurrentQuery(workFrame, popupPage)
const retryDetail = await this.processDetailPage({
detailPage,
deleteSet,
dryRun,
expectedOrderNumber: orderNumber,
progressState: {
completedOrders: detailIndex,
totalOrders: failedDetails.length
},
onProgress: (message, progress, extra) => {
onProgress?.(
`[重试 ${attempt}/${MAX_RETRIES}] ${message}`,
progress,
extra ? { ...extra, phase: 'processing' as const } : undefined
)
const rows = workFrame.locator('tbody tr')
const rowCount = await rows.count()
if (rowCount === 0) {
throw new Error('订单重试查询无结果')
}
const detailPage = await this.openDetailPageFromCurrentQuery(workFrame, popupPage)
const retryDetail = await this.processDetailPage({
detailPage,
deleteSet,
dryRun,
expectedOrderNumber: orderNumber,
progressState: {
completedOrders: detailIndex,
totalOrders: failedDetails.length
},
onProgress: (message, progress, extra) => {
onProgress?.(
`[重试 ${attempt}/${MAX_RETRIES}] ${message}`,
progress,
extra ? { ...extra, phase: 'processing' as const } : undefined
)
}
})
retryResult.successfulRetries += 1
retryResult.updatedDetails.push({
...retryDetail,
retryCount: attempt,
retriedAt: Date.now(),
retrySuccess: true,
retryAttempts
})
retryResult.retriedOrders += 1
break
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.warn(`Retry attempt ${attempt} failed for order ${orderNumber}: ${message}`)
retryAttempts.push({
attempt,
error: message,
timestamp: Date.now()
})
if (attempt === MAX_RETRIES) {
retryResult.updatedDetails.push({
...failedDetail,
retryCount: MAX_RETRIES,
retryAttempts,
retriedAt: Date.now(),
retrySuccess: false
})
retryResult.retriedOrders += 1
}
}
})
result.successfulRetries += 1
result.updatedDetails.push({
...retryDetail,
retryCount: attempt,
retriedAt: Date.now(),
retrySuccess: true,
retryAttempts
})
result.retriedOrders += 1
break
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.warn(`Retry attempt ${attempt} failed for order ${orderNumber}: ${message}`)
retryAttempts.push({
attempt,
error: message,
timestamp: Date.now()
})
if (attempt === MAX_RETRIES) {
result.updatedDetails.push({
...failedDetail,
retryCount: MAX_RETRIES,
retryAttempts,
retriedAt: Date.now(),
retrySuccess: false
})
result.retriedOrders += 1
}
}
log.info('Retry process completed', {
retriedOrders: retryResult.retriedOrders,
successfulRetries: retryResult.successfulRetries,
totalRetryOrders: failedDetails.length
})
return retryResult
},
{
operationName: 'retry-failed-orders',
message: 'Retry failed orders',
slowThresholdMs: 5000,
context: {
totalRetryOrders: failedDetails.length,
dryRun
}
}
}
)
log.info('Retry process completed', {
retriedOrders: result.retriedOrders,
successfulRetries: result.successfulRetries
})
return result
return trackedResult.result
}
}

View File

@@ -10,7 +10,8 @@ import type {
LogLevel
} from '../../types/extractor.types'
import { DataImportService } from '../database/data-importer'
import { createLogger } from '../logger'
import { createLogger, withRequestContext, getRequestId } from '../logger'
import { trackDuration } from '../logger/performance-monitor'
const log = createLogger('ExtractorService')
@@ -50,70 +51,105 @@ export class ExtractorService {
orderRecordCounts: []
}
try {
const session = this.authService.getSession()
// Call ExtractorCore to execute web page operations
const core = new ExtractorCore()
const coreResult = await core.downloadAllBatches({
session,
orderNumbers: input.orderNumbers,
downloadDir: this.downloadDir,
batchSize: input.batchSize || 100,
onProgress: input.onProgress
})
result.downloadedFiles = coreResult.downloadedFiles
result.errors = coreResult.errors
// Merge downloaded files (original logic preserved)
if (result.downloadedFiles.length > 0) {
const totalBatches = result.downloadedFiles.length
const totalPoints = 1 + totalBatches + 2
const progressPerPoint = 100 / totalPoints
const mergeProgress = (1 + totalBatches) * progressPerPoint
input.onProgress?.('正在合并文件...', mergeProgress, {
phase: 'merging',
totalBatches
// Wrap entire extraction in request context for unified logging
return withRequestContext(
async () => {
const requestId = getRequestId()
log.info('Starting extraction', {
orderCount: input.orderNumbers.length,
batchSize: input.batchSize || 100,
downloadDir: this.downloadDir,
requestId
})
const mergeResult = await this.mergeFiles(result.downloadedFiles)
result.mergedFile = mergeResult.mergedFile
result.recordCount = mergeResult.recordCount
result.orderRecordCounts = mergeResult.orderRecordCounts
// Add merge error to result if any
if (mergeResult.error) {
result.errors.push(mergeResult.error)
}
try {
const session = this.authService.getSession()
// Always clean up temporary files regardless of merge success
await this.cleanupTempFiles(result.downloadedFiles)
// Auto-import to database if merge was successful
if (result.mergedFile) {
const importProgress = (1 + totalBatches + 1) * progressPerPoint
input.onProgress?.('正在写入数据库...', importProgress, {
phase: 'importing',
totalBatches
})
const importResult = await this.importToDatabaseWithLogging(
result.mergedFile,
input.onLog
// Call ExtractorCore to execute web page operations with timing
const core = new ExtractorCore()
const coreResult = await trackDuration(
async () =>
core.downloadAllBatches({
session,
orderNumbers: input.orderNumbers,
downloadDir: this.downloadDir,
batchSize: input.batchSize || 100,
onProgress: input.onProgress
}),
{
operationName: 'Batch Download',
context: {
orderCount: input.orderNumbers.length,
batchSize: input.batchSize || 100
}
}
)
result.importResult = importResult
if (!importResult.success && importResult.errors.length > 0) {
result.errors.push(...importResult.errors)
result.downloadedFiles = coreResult.result.downloadedFiles
result.errors = coreResult.result.errors
// Merge downloaded files (original logic preserved)
if (result.downloadedFiles.length > 0) {
const totalBatches = result.downloadedFiles.length
const totalPoints = 1 + totalBatches + 2
const progressPerPoint = 100 / totalPoints
const mergeProgress = (1 + totalBatches) * progressPerPoint
input.onProgress?.('正在合并文件...', mergeProgress, {
phase: 'merging',
totalBatches
})
const mergeResult = await this.mergeFiles(result.downloadedFiles, input.orderNumbers)
result.mergedFile = mergeResult.mergedFile
result.recordCount = mergeResult.recordCount
result.orderRecordCounts = mergeResult.orderRecordCounts
// Add merge error to result if any
if (mergeResult.error) {
result.errors.push(mergeResult.error)
}
// Always clean up temporary files regardless of merge success
await this.cleanupTempFiles(result.downloadedFiles, input.orderNumbers)
// Auto-import to database if merge was successful
if (result.mergedFile) {
const importProgress = (1 + totalBatches + 1) * progressPerPoint
input.onProgress?.('正在写入数据库...', importProgress, {
phase: 'importing',
totalBatches
})
const importResult = await this.importToDatabaseWithLogging(
result.mergedFile,
input.onLog
)
result.importResult = importResult
if (!importResult.success && importResult.errors.length > 0) {
result.errors.push(...importResult.errors)
}
}
}
}
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
result.errors.push(`Extraction failed: ${message}`)
}
return result
log.info('Extraction completed successfully', {
recordCount: result.recordCount,
fileCount: result.downloadedFiles.length
})
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.error('Extraction failed', {
error: message,
orderNumbers: input.orderNumbers,
downloadDir: this.downloadDir,
requestId: getRequestId()
})
result.errors.push(`Extraction failed: ${message}`)
}
return result
},
{ operation: 'extract' }
)
}
/**
@@ -121,10 +157,12 @@ export class ExtractorService {
* Uses ExcelParser to parse and combine all material plans
*
* @param filePaths - Array of downloaded Excel file paths
* @param orderNumbers - Order numbers for context logging
* @returns Merged file path, total record count, and optional error message
*/
private async mergeFiles(
filePaths: string[]
filePaths: string[],
orderNumbers: string[]
): Promise<{
mergedFile: string | null
recordCount: number
@@ -135,70 +173,101 @@ export class ExtractorService {
return { mergedFile: null, recordCount: 0, orderRecordCounts: [] }
}
log.info('Starting merge', { fileCount: filePaths.length })
const parser = new ExcelParser()
log.info('Starting merge', { fileCount: filePaths.length, orderCount: orderNumbers.length })
// Collect all orders with full order info and materials
// Each order has: { orderInfo: OrderHeader, materials: MaterialRow[] }
const allOrders: Array<{ orderInfo: any; materials: any[] }> = []
// Track merge operation duration and unwrap result
const trackedResult = await trackDuration(
async () => {
const parser = new ExcelParser()
// Parse each downloaded file and collect orders
for (const filePath of filePaths) {
try {
log.debug('Parsing file', { filePath })
await parser.parse(filePath)
// After parse(), the parser store orders internally as lastOrders
const orders = (parser as any).lastOrders
log.debug('File parsed', { filePath, orderCount: orders?.length || 0 })
if (orders && Array.isArray(orders)) {
allOrders.push(...orders)
// Collect all orders with full order info and materials
// Each order has: { orderInfo: OrderHeader, materials: MaterialRow[] }
const allOrders: Array<{ orderInfo: any; materials: any[] }> = []
// Parse each downloaded file and collect orders
for (const filePath of filePaths) {
try {
log.debug('Parsing file', { filePath })
await parser.parse(filePath)
// After parse(), the parser store orders internally as lastOrders
const orders = (parser as any).lastOrders
log.debug('File parsed', { filePath, orderCount: orders?.length || 0 })
if (orders && Array.isArray(orders)) {
allOrders.push(...orders)
}
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error)
log.error('Failed to parse file', {
filePath,
error: errorMsg,
orderNumbers,
batchId: filePaths.indexOf(filePath)
})
}
}
// Calculate total record count (total material rows)
let recordCount = 0
const orderRecordCounts: Array<{ orderNumber: string; recordCount: number }> = []
for (const order of allOrders) {
const count = order.materials.length
recordCount += count
orderRecordCounts.push({
orderNumber: order.orderInfo.productionOrder || '',
recordCount: count
})
}
log.info('Merge summary', { orderCount: allOrders.length, recordCount })
if (recordCount === 0) {
log.warn('No records found in any downloaded files', { orderNumbers })
return { mergedFile: null, recordCount: 0, orderRecordCounts }
}
// Generate output filename with timestamp
const timestamp = new Date()
.toISOString()
.replace(/[-:T]/g, '')
.replace(/\..+/, '')
.slice(0, 14)
const outputPath = path.join(this.downloadDir, `merged_${timestamp}.xlsx`)
// Save with error handling
try {
log.info('Saving merged file', { outputPath })
await this.saveMergedOrders(allOrders, outputPath)
log.info('Merged file saved successfully', { recordCount })
return { mergedFile: outputPath, recordCount, orderRecordCounts }
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error)
const errorStack = error instanceof Error ? error.stack : ''
log.error('Failed to save merged file', {
error: errorMsg,
stack: errorStack,
orderNumbers,
downloadDir: this.downloadDir
})
// Return parsed record count and error info even if save fails
return {
mergedFile: null,
recordCount,
orderRecordCounts,
error: `保存合并文件失败:${errorMsg}`
}
}
},
{
operationName: 'File Merge',
context: {
fileCount: filePaths.length,
orderCount: orderNumbers.length,
orderNumbers
}
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error)
log.error('Failed to parse file', { filePath, error: errorMsg })
}
}
)
// Calculate total record count (total material rows)
let recordCount = 0
const orderRecordCounts: Array<{ orderNumber: string; recordCount: number }> = []
for (const order of allOrders) {
const count = order.materials.length
recordCount += count
orderRecordCounts.push({
orderNumber: order.orderInfo.productionOrder || '',
recordCount: count
})
}
log.info('Merge summary', { orderCount: allOrders.length, recordCount })
if (recordCount === 0) {
log.warn('No records found in any downloaded files')
return { mergedFile: null, recordCount: 0, orderRecordCounts }
}
// Generate output filename with timestamp
const timestamp = new Date()
.toISOString()
.replace(/[-:T]/g, '')
.replace(/\..+/, '')
.slice(0, 14)
const outputPath = path.join(this.downloadDir, `merged_${timestamp}.xlsx`)
// Save with error handling
try {
log.info('Saving merged file', { outputPath })
await this.saveMergedOrders(allOrders, outputPath)
log.info('Merged file saved successfully', { recordCount })
return { mergedFile: outputPath, recordCount, orderRecordCounts }
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error)
const errorStack = error instanceof Error ? error.stack : ''
log.error('Failed to save merged file', { error: errorMsg, stack: errorStack })
// Return parsed record count and error info even if save fails
return { mergedFile: null, recordCount, orderRecordCounts, error: `保存合并文件失败:${errorMsg}` }
}
return trackedResult.result
}
/**
@@ -305,14 +374,19 @@ export class ExtractorService {
* Clean up temporary batch files after merging
* @param filePaths - Array of temporary file paths to delete
*/
private async cleanupTempFiles(filePaths: string[]): Promise<void> {
private async cleanupTempFiles(filePaths: string[], orderNumbers?: string[]): Promise<void> {
for (const filePath of filePaths) {
try {
await fs.unlink(filePath)
log.debug('Deleted temporary file', { filePath })
} catch (error) {
// Log error but don't fail the main process
log.error('Failed to delete temporary file', { filePath, error })
log.error('Failed to delete temporary file', {
filePath,
error,
orderNumbers,
downloadDir: this.downloadDir
})
}
}
}
@@ -330,41 +404,58 @@ export class ExtractorService {
log.info('Starting database import', { filePath })
onLog?.('info', `开始导入数据到数据库...`)
const importService = new DataImportService()
// Track import operation duration and unwrap result
const trackedResult = await trackDuration(
async () => {
const importService = new DataImportService()
try {
const result = await importService.importFromExcel(filePath, 1000)
try {
const result = await importService.importFromExcel(filePath, 1000)
log.info('Import completed', {
success: result.success,
recordsRead: result.recordsRead,
recordsDeleted: result.recordsDeleted,
recordsImported: result.recordsImported
})
log.info('Import completed', {
success: result.success,
recordsRead: result.recordsRead,
recordsDeleted: result.recordsDeleted,
recordsImported: result.recordsImported
})
if (result.success) {
onLog?.(
'success',
`导入完成:读取 ${result.recordsRead} 条,删除 ${result.recordsDeleted} 条,导入 ${result.recordsImported}`
)
} else if (result.errors.length > 0) {
result.errors.forEach((err) => onLog?.('error', err))
if (result.success) {
onLog?.(
'success',
`导入完成:读取 ${result.recordsRead} 条,删除 ${result.recordsDeleted} 条,导入 ${result.recordsImported}`
)
} else if (result.errors.length > 0) {
result.errors.forEach((err) => onLog?.('error', err))
}
return result
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error)
log.error('Import failed', {
error: errorMsg,
filePath,
downloadDir: this.downloadDir
})
onLog?.('error', `导入失败:${errorMsg}`)
return {
success: false,
recordsRead: 0,
recordsDeleted: 0,
recordsImported: 0,
uniqueSourceNumbers: 0,
errors: [errorMsg]
}
}
},
{
operationName: 'Database Import',
context: {
filePath
}
}
)
return result
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error)
log.error('Import failed', { error: errorMsg })
onLog?.('error', `导入失败:${errorMsg}`)
return {
success: false,
recordsRead: 0,
recordsDeleted: 0,
recordsImported: 0,
uniqueSourceNumbers: 0,
errors: [errorMsg]
}
}
return trackedResult.result
}
}

View File

@@ -6,8 +6,7 @@
import winston from 'winston'
import DailyRotateFile from 'winston-daily-rotate-file'
import path from 'path'
import { app } from 'electron'
import fs from 'fs'
import { getLogDir } from './shared'
/**
* Audit log entry structure
@@ -32,22 +31,6 @@ export interface AuditEntry {
metadata: Record<string, unknown>
}
/**
* Get the log directory for audit logs
* Uses app.getPath('logs') in production, local logs dir in development
*/
function getLogDir(): string {
if (app && app.isReady()) {
return app.getPath('logs')
}
// Fallback for development or before app is ready
const devLogDir = path.join(process.cwd(), 'logs')
if (!fs.existsSync(devLogDir)) {
fs.mkdirSync(devLogDir, { recursive: true })
}
return devLogDir
}
/**
* JSONL formatter - outputs one JSON object per line
* This is the key difference from the standard JSON formatter
@@ -59,26 +42,43 @@ const jsonlFormat = winston.format.printf(({ message }) => {
/**
* Create the audit logger instance with daily rotation
* Configured for 30-day retention as per requirements
* Initially silent (no transports). Call applyAuditConfig() after config is loaded.
*/
const auditLogger = winston.createLogger({
level: 'info',
silent: false,
transports: [
silent: true,
transports: []
})
/**
* Apply audit log retention configuration
* Creates the DailyRotateFile transport with the configured retention period
*
* @param retentionDays - Number of days to retain audit logs
*/
export function applyAuditConfig(retentionDays: number): void {
// Enable logging now that config is loaded
auditLogger.silent = false
// Remove existing DailyRotateFile transports
const existingTransports = auditLogger.transports.filter((t) => t instanceof DailyRotateFile)
for (const transport of existingTransports) {
auditLogger.remove(transport)
}
// Add audit transport with configured retention
auditLogger.add(
new DailyRotateFile({
filename: path.join(getLogDir(), 'audit-%DATE%.jsonl'),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '30d', // 30-day retention
maxFiles: `${retentionDays}d`,
level: 'info',
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DDTHH:mm:ss.SSSZ' }),
jsonlFormat
)
format: jsonlFormat
})
]
})
)
}
/**
* Log an audit event
@@ -86,9 +86,8 @@ const auditLogger = winston.createLogger({
* @param action - The action that was performed
* @param userId - User ID who performed the action
* @param details - Additional details including username, computerName, resource, status, and optional metadata
* @returns Promise that resolves when the log is written (non-blocking)
*/
export async function logAudit(
export function logAudit(
action: string,
userId: string,
details: {
@@ -98,7 +97,7 @@ export async function logAudit(
status: 'success' | 'failure' | 'partial'
metadata?: Record<string, unknown>
}
): Promise<void> {
): void {
const entry: AuditEntry = {
timestamp: new Date().toISOString(),
action,
@@ -118,8 +117,7 @@ export async function logAudit(
/**
* Flush and close the audit logger (call on app shutdown)
*/
export async function closeAuditLogger(): Promise<void> {
// Winston logger.close() is synchronous
export function closeAuditLogger(): void {
auditLogger.close()
}

View File

@@ -3,9 +3,13 @@
*
* Provides comprehensive error serialization and formatting for logging.
* Captures full error context including stack traces, causes, and custom properties.
*
* Enhanced with request context tracking for distributed tracing support.
*/
import type { ErrorLike, SerializedError } from '../../types/errors'
import { isProduction } from './shared'
import { getRequestId } from './request-context'
/**
* Check if value is an Error or Error-like object
@@ -84,7 +88,7 @@ export function sanitizeError(error: SerializedError): SerializedError {
const sanitized: SerializedError = { ...error }
// Sanitize message in production
if (process.env.NODE_ENV === 'production') {
if (isProduction()) {
// Keep error name and structure, but sanitize message
if (sensitiveKeys.some((key) => error.message?.toLowerCase().includes(key))) {
sanitized.message = 'An error occurred due to invalid credentials or configuration'
@@ -151,6 +155,29 @@ export function extractErrorContext(error: SerializedError): {
/**
* Format error for console/file logging
* Returns a formatted string with all error details
*
* @param error - The error to format (Error object or Error-like)
* @param context - Optional context for logging
* @param context.operation - Business operation being performed (e.g., 'extract', 'clean', 'validate')
* @param context.module - Module/Service name where error occurred
* @param context.userId - User ID performing the operation
* @param context.requestId - Request/trace ID for distributed tracing (auto-injected if not provided)
* @param context.batchId - Batch identifier for batch operations
* @param context.duration - Operation duration in milliseconds
* @param context.orderNumbers - Order numbers related to the operation
* @param context.materialCodes - Material codes related to the operation
* @returns Object with formatted message and metadata for logging
*
* @example
* ```typescript
* const { message, metadata } = formatErrorForLogging(error, {
* operation: 'extract',
* userId: 'user123',
* batchId: 'batch-001',
* duration: 1500
* })
* logger.error(message, metadata)
* ```
*/
export function formatErrorForLogging(
error: unknown,
@@ -158,6 +185,11 @@ export function formatErrorForLogging(
operation?: string
module?: string
userId?: string
requestId?: string
batchId?: string
duration?: number
orderNumbers?: string[]
materialCodes?: string[]
[key: string]: unknown
}
): {
@@ -165,15 +197,31 @@ export function formatErrorForLogging(
metadata: Record<string, unknown>
} {
const serialized = serializeError(error)
const isProd = process.env.NODE_ENV === 'production'
const isProd = isProduction()
const errorToLog = isProd ? sanitizeError(serialized) : serialized
const errorContext = extractErrorContext(errorToLog)
// Auto-inject requestId from async context if not explicitly provided
const autoRequestId = getRequestId()
const requestId = context?.requestId || autoRequestId
const metadata: Record<string, unknown> = {
error: errorToLog,
...(requestId && { requestId }),
...context
}
// Remove undefined context fields to keep logs clean
if (context) {
const cleanMetadata: Record<string, unknown> = {}
for (const [key, value] of Object.entries(metadata)) {
if (value !== undefined) {
cleanMetadata[key] = value
}
}
Object.assign(metadata, cleanMetadata)
}
// Add error location context if available
if (errorContext.fileName) {
metadata.errorLocation = {
@@ -201,6 +249,28 @@ export function formatErrorForLogging(
/**
* Log error with full context
* Wrapper for logger.error that ensures complete error information is captured
*
* @param logger - Logger instance with error method
* @param error - The error to log (Error object or Error-like)
* @param options - Logging options
* @param options.message - Custom message to prepend to error message
* @param options.operation - Business operation being performed
* @param options.module - Module/Service name
* @param options.userId - User ID performing the operation
* @param options.requestId - Request/trace ID (auto-injected if not provided)
* @param options.batchId - Batch identifier for batch operations
* @param options.duration - Operation duration in milliseconds
* @param options.context - Additional custom context fields
*
* @example
* ```typescript
* logError(logger, error, {
* operation: 'extract',
* userId: 'user123',
* message: 'Failed to process order',
* duration: 1500
* })
* ```
*/
export function logError(
logger: { error: (message: string, meta?: Record<string, unknown>) => void },
@@ -210,14 +280,29 @@ export function logError(
operation?: string
module?: string
userId?: string
requestId?: string
batchId?: string
duration?: number
context?: Record<string, unknown>
} = {}
): void {
const { message: customMessage, operation, module: moduleName, userId, context } = options
const {
message: customMessage,
operation,
module: moduleName,
userId,
requestId,
batchId,
duration,
context
} = options
const { message, metadata } = formatErrorForLogging(error, {
operation,
module: moduleName,
userId,
requestId,
batchId,
duration,
...context
})
@@ -240,3 +325,77 @@ export function throwAfterLogging(
logError(logger, error, options)
throw error
}
/**
* Enhanced error logging helper with automatic context injection
*
* Simplifies error logging by automatically injecting requestId from async context
* and providing a concise API for common logging scenarios.
*
* @param logger - Logger instance with error method
* @param error - The error to log (Error object or Error-like)
* @param context - Business context for the error
* @param context.operation - Business operation (REQUIRED for enhanced logging)
* @param context.userId - User ID performing the operation
* @param context.batchId - Batch identifier for batch operations
* @param context.duration - Operation duration in milliseconds (e.g., from performance monitoring)
* @param context.orderNumbers - Order numbers related to the operation
* @param context.materialCodes - Material codes related to the operation
* @param context.module - Module/Service name (defaults to 'unknown' if not provided)
* @param customMessage - Optional custom message to prepend (if not provided, uses error message)
*
* @example
* ```typescript
* import { enhancedLogError } from './error-utils'
*
* // Simple usage with auto-injected requestId
* enhancedLogError(logger, error, { operation: 'extract', userId: 'user123' })
*
* // With performance metrics
* const duration = Date.now() - startTime
* enhancedLogError(logger, error, {
* operation: 'clean',
* userId: 'user456',
* batchId: 'batch-001',
* duration,
* orderNumbers: ['ORD-123', 'ORD-124']
* })
* ```
*/
export function enhancedLogError(
logger: { error: (message: string, meta?: Record<string, unknown>) => void },
error: unknown,
context: {
operation: string
userId?: string
batchId?: string
duration?: number
orderNumbers?: string[]
materialCodes?: string[]
module?: string
},
customMessage?: string
): void {
const {
operation,
userId,
batchId,
duration,
orderNumbers,
materialCodes,
module: moduleName
} = context
logError(logger, error, {
message: customMessage,
operation,
module: moduleName,
userId,
batchId,
duration,
context: {
...(orderNumbers && { orderNumbers }),
...(materialCodes && { materialCodes })
}
})
}

View File

@@ -11,66 +11,124 @@
import winston from 'winston'
import DailyRotateFile from 'winston-daily-rotate-file'
import path from 'path'
import { app } from 'electron'
import fs from 'fs'
import { BrowserWindow } from 'electron'
import { serializeError, sanitizeError } from './error-utils'
import { getLogDir, isProduction } from './shared'
import { IPC_CHANNELS } from '../../../shared/ipc-channels'
import { getContext, run } from './request-context'
// Get log directory - use app.getPath('logs') in production, or local logs dir in development
function getLogDir(): string {
if (app && app.isReady()) {
return app.getPath('logs')
}
// Fallback for development or before app is ready
const devLogDir = path.join(process.cwd(), 'logs')
if (!fs.existsSync(devLogDir)) {
fs.mkdirSync(devLogDir, { recursive: true })
}
return devLogDir
// Cache isProduction() at module load — app.isPackaged never changes at runtime
const IS_PROD = isProduction()
/**
* Check if an error has already been serialized (plain object with name/message but not an Error instance).
* Prevents double-serialization when logError() output passes through the format pipeline.
*/
function isSerializedError(value: unknown): boolean {
return (
typeof value === 'object' &&
value !== null &&
!(value instanceof Error) &&
'name' in value &&
'message' in value
)
}
// Check if running in production
const isProduction = app?.isPackaged ?? process.env.NODE_ENV === 'production'
// Custom format for console output - includes full error details
const consoleFormat = winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.colorize(),
winston.format.printf(({ timestamp, level, message, context, error, ...meta }) => {
const contextStr = context ? `[${context}]` : ''
// Format error with full stack trace
let errorStr = ''
if (error) {
const serialized = isProduction ? sanitizeError(serializeError(error)) : serializeError(error)
if (serialized.stack) {
errorStr = `\n${serialized.stack}`
} else {
errorStr = ` ${serialized.message}`
// Auto-inject requestId from async context
winston.format((info) => {
const context = getContext()
if (context) {
info.requestId = context.requestId
if (context.userId) {
info.userId = context.userId
}
if (context.operation) {
info.operation = context.operation
}
}
return info
})(),
winston.format.printf(
({ timestamp, level, message, context, error, requestId, userId, operation, ...meta }) => {
const contextStr = context ? `[${context}]` : ''
const requestIdStr = requestId ? ` [${requestId}]` : ''
const userStr = userId ? ` (user:${userId})` : ''
const opStr = operation ? ` op:${operation}` : ''
const metaStr = Object.keys(meta).length > 0 ? ` ${JSON.stringify(meta, null, 2)}` : ''
return `${timestamp} [${level}]${contextStr} ${message}${errorStr}${metaStr}`
})
// Format error with full stack trace
let errorStr = ''
if (error) {
// Skip re-serialization if already a serialized error object
const serialized: { stack?: string; message: string } = isSerializedError(error)
? (error as { stack?: string; message: string })
: IS_PROD
? sanitizeError(serializeError(error))
: serializeError(error)
if (serialized.stack) {
errorStr = `\n${serialized.stack}`
} else {
errorStr = ` ${serialized.message}`
}
}
let metaStr = ''
if (Object.keys(meta).length > 0) {
try {
metaStr = ` ${JSON.stringify(meta, null, 2)}`
} catch {
// Fallback for circular references: stringify primitives, replace complex objects with placeholder
metaStr = ` ${JSON.stringify(
Object.fromEntries(
Object.entries(meta).map(([k, v]) => [
k,
v !== null && typeof v === 'object' ? `[Object]` : v
])
),
null,
2
)}`
}
}
return `${timestamp} [${level}]${contextStr}${requestIdStr}${userStr}${opStr} ${message}${errorStr}${metaStr}`
}
)
)
// Custom format for file output - JSON with full error details
const fileFormat = winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
// Auto-inject requestId from async context for file logs
winston.format((info) => {
// Serialize errors in metadata
const context = getContext()
if (context) {
info.requestId = context.requestId
if (context.userId) {
info.userId = context.userId
}
if (context.operation) {
info.operation = context.operation
}
}
return info
})(),
winston.format((info) => {
// Serialize errors in metadata (skip if already serialized)
if (info.error) {
info.error = isProduction
? sanitizeError(serializeError(info.error))
: serializeError(info.error)
if (!isSerializedError(info.error)) {
info.error = IS_PROD
? sanitizeError(serializeError(info.error))
: serializeError(info.error)
}
}
// Serialize any error in meta fields
// Serialize any error in meta fields (skip if already serialized)
for (const key of Object.keys(info)) {
if (key !== 'error' && info[key] instanceof Error) {
info[key] = isProduction
? sanitizeError(serializeError(info[key]))
: serializeError(info[key])
info[key] = IS_PROD ? sanitizeError(serializeError(info[key])) : serializeError(info[key])
}
}
@@ -80,19 +138,20 @@ const fileFormat = winston.format.combine(
)
// Daily rotate file transport configuration
const createFileTransport = (level?: string): DailyRotateFile => {
const createFileTransport = (level?: string, maxFiles?: string): DailyRotateFile => {
return new DailyRotateFile({
filename: path.join(getLogDir(), 'app-%DATE%.log'),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
maxFiles: maxFiles || '14d',
level,
format: fileFormat
})
}
// Create the logger instance with default level
// Create the logger instance with default level - Console only initially
// File transports are added after config is loaded via applyLoggingConfig()
const logger = winston.createLogger({
level: 'info', // Default level, can be updated via setLogLevel()
defaultMeta: { service: 'erpauto' },
@@ -100,33 +159,59 @@ const logger = winston.createLogger({
// Console transport - always enabled
new winston.transports.Console({
format: consoleFormat
}),
// File transport for all levels
createFileTransport()
})
]
})
/**
* Update the logger level dynamically
* Update the logger level dynamically and notify renderer processes
* @param level - The new log level
*/
export function setLogLevel(level: string): void {
logger.level = level
// Broadcast level change to all renderer windows so they update their cached level
for (const win of BrowserWindow.getAllWindows()) {
if (!win.isDestroyed()) {
win.webContents.send(IPC_CHANNELS.LOGGER_LEVEL_CHANGED, level)
}
}
}
// Add error-specific file transport in production
if (app?.isPackaged) {
logger.add(
new DailyRotateFile({
filename: path.join(getLogDir(), 'error-%DATE%.log'),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
level: 'error',
format: fileFormat
})
)
/**
* Apply logging configuration from config file
* Removes existing DailyRotateFile transports and recreates them with config values
*
* @param config - Logging configuration from config.yaml
*/
export function applyLoggingConfig(config: { level: string; appRetention: number }): void {
// Update log level
setLogLevel(config.level)
// Remove existing DailyRotateFile transports
const existingFileTransports = logger.transports.filter((t) => t instanceof DailyRotateFile)
for (const transport of existingFileTransports) {
logger.remove(transport)
}
// Add app log transport with configured retention
const retentionStr = `${config.appRetention}d`
logger.add(createFileTransport(undefined, retentionStr))
// Add error-specific file transport in production
if (IS_PROD) {
logger.add(
new DailyRotateFile({
filename: path.join(getLogDir(), 'error-%DATE%.log'),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '20m',
maxFiles: retentionStr,
level: 'error',
format: fileFormat
})
)
}
}
/**
@@ -139,25 +224,47 @@ export function createLogger(context: string): winston.Logger {
}
/**
* Log an error with full context and stack trace
* This is the recommended way to log errors in the application
* Execute a function with automatic request-scoped logging
*
* @param log - Logger instance
* @param message - Error message
* @param error - The error object (Error, BaseError, or any)
* @param meta - Additional metadata to include
* This wrapper ensures all logging within the function has access to the request context.
* It's a convenience wrapper around RequestContext.run() that also ensures the logger
* properly captures the context.
*
* @param fn - The async function to execute within the context
* @param context - Optional business context (userId, operation)
* @returns Promise resolving to the function's return value
*
* @example
* ```typescript
* await withRequestContext(async () => {
* logger.info('Processing order') // Will include requestId, userId, operation
* await processOrder()
* }, { userId: 'user123', operation: 'process-order' })
* ```
*/
export function logError(
log: winston.Logger,
message: string,
error: unknown,
meta?: Record<string, unknown>
): void {
log.error(message, { error, ...meta })
export async function withRequestContext<T>(
fn: () => Promise<T>,
context?: { userId?: string; operation?: string }
): Promise<T> {
return run(fn, context)
}
// Re-export error utilities for convenience
export { logError, formatErrorForLogging, serializeError, extractErrorContext } from './error-utils'
// Export request context management for async-context logging
export { run, getRequestId, getContext, withContext, type LoggerContext } from './request-context'
// Export the main logger for direct use
export default logger
// Export performance monitoring utilities
export {
trackDuration,
PerformanceTracker,
createPerformanceTracker,
DEFAULT_SLOW_THRESHOLD_MS
} from './performance-monitor'
// Export log level types for convenience
export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose'

View File

@@ -0,0 +1,340 @@
/**
* Logger Performance Monitoring Utilities
*
* Provides performance tracking and timing utilities that integrate with the Winston logger.
*
* Features:
* - trackDuration: Wrap async functions and auto-log execution time
* - PerformanceTracker: Track multiple metrics over time
* - Slow operation detection with configurable thresholds
* - Performance warnings for operations exceeding thresholds
*/
import type { Logger } from 'winston'
import logger from './index'
/**
* Default threshold for slow operation warnings (in milliseconds)
*/
export const DEFAULT_SLOW_THRESHOLD_MS = 1000
/**
* Result of a tracked operation
*/
export interface TrackDurationResult<T> {
/** The result value from the operation */
result: T
/** Execution duration in milliseconds */
durationMs: number
/** Whether the operation exceeded the slow threshold */
isSlow: boolean
}
/**
* Configuration for trackDuration
*/
export interface TrackDurationOptions {
/** Operation name for logging */
operationName: string
/** Custom log message (optional) */
message?: string
/** Slow threshold in ms (overrides default) */
slowThresholdMs?: number
/** Log level for duration info (default: 'debug') */
logLevel?: 'debug' | 'info' | 'verbose'
/** Additional context to include in logs */
context?: Record<string, unknown>
}
/**
* Metrics tracked by PerformanceTracker
*/
export interface PerformanceMetrics {
/** Total number of operations tracked */
count: number
/** Total duration of all operations in milliseconds */
totalDurationMs: number
/** Minimum duration in milliseconds */
minDurationMs: number
/** Maximum duration in milliseconds */
maxDurationMs: number
/** Average duration in milliseconds */
avgDurationMs: number
/** Number of operations exceeding slow threshold */
slowOperationCount: number
}
/**
* Track the duration of an async operation and log the result
*
* @param fn - The async function to track
* @param options - Configuration options including operation name and threshold
* @returns Promise resolving to TrackDurationResult with result, duration, and slow flag
*
* @example
* ```typescript
* const result = await trackDuration(
* async () => await someAsyncOperation(),
* { operationName: 'Database Query', slowThresholdMs: 500 }
* );
* console.log(result.result, result.durationMs);
* ```
*/
export async function trackDuration<T>(
fn: () => Promise<T>,
options: TrackDurationOptions
): Promise<TrackDurationResult<T>> {
const {
operationName,
message = `Operation "${operationName}"`,
slowThresholdMs = DEFAULT_SLOW_THRESHOLD_MS,
logLevel = 'debug',
context = {}
} = options
const startTime = performance.now()
try {
const result = await fn()
const durationMs = performance.now() - startTime
const isSlow = durationMs > slowThresholdMs
// Log the result
const logMessage = `${message} completed in ${durationMs.toFixed(2)}ms`
if (isSlow) {
logger.warn(`${logMessage} (SLOW - exceeded ${slowThresholdMs}ms threshold)`, {
operation: operationName,
durationMs,
slowThresholdMs,
...context
})
} else {
logger[logLevel](logMessage, {
operation: operationName,
durationMs,
...context
})
}
return { result, durationMs, isSlow }
} catch (error) {
const durationMs = performance.now() - startTime
const isSlow = durationMs > slowThresholdMs
// Log the error with duration
logger.error(`${message} failed after ${durationMs.toFixed(2)}ms`, {
operation: operationName,
durationMs,
slowThresholdMs,
error,
...context
})
throw error
}
}
/**
* PerformanceTracker class for tracking multiple metrics over time
*
* Tracks operation counts, durations, and identifies slow operations.
* Useful for monitoring service-level performance and identifying bottlenecks.
*
* @example
* ```typescript
* const tracker = new PerformanceTracker('DataService', 500);
*
* // Track individual operations
* await tracker.track('fetchData', async () => fetchData());
* await tracker.track('saveData', async () => saveData());
*
* // Get metrics
* const metrics = tracker.getMetrics();
* console.log(`Avg duration: ${metrics.avgDurationMs}ms`);
*
* // Log summary
* tracker.logSummary();
* ```
*/
export class PerformanceTracker {
private operationName: string
private slowThresholdMs: number
private durations: number[] = []
private slowCount = 0
private log: Logger
/**
* Create a new PerformanceTracker
*
* @param operationName - Name of the operation/category being tracked
* @param slowThresholdMs - Custom slow threshold in ms (default: 1000)
* @param customLogger - Optional custom logger instance (default: main logger)
*/
constructor(
operationName: string,
slowThresholdMs: number = DEFAULT_SLOW_THRESHOLD_MS,
customLogger?: Logger
) {
this.operationName = operationName
this.slowThresholdMs = slowThresholdMs
this.log = customLogger ?? logger
}
/**
* Track an async operation and record its duration
*
* @param name - Specific name of this operation instance
* @param fn - The async function to track
* @param context - Optional context to log with the operation
* @returns Promise resolving to the function's result
*
* @example
* ```typescript
* const result = await tracker.track('getUserById', async () => getUserById(id), {
* userId: id
* });
* ```
*/
async track<T>(
name: string,
fn: () => Promise<T>,
context?: Record<string, unknown>
): Promise<T> {
const startTime = performance.now()
try {
const result = await fn()
const durationMs = performance.now() - startTime
this.recordDuration(durationMs)
if (durationMs > this.slowThresholdMs) {
this.log.warn(`[${this.operationName}] ${name} took ${durationMs.toFixed(2)}ms (SLOW)`, {
operation: name,
durationMs,
slowThresholdMs: this.slowThresholdMs,
...context
})
} else {
this.log.debug(`[${this.operationName}] ${name} completed in ${durationMs.toFixed(2)}ms`, {
operation: name,
durationMs,
...context
})
}
return result
} catch (error) {
const durationMs = performance.now() - startTime
this.recordDuration(durationMs)
this.log.error(`[${this.operationName}] ${name} failed after ${durationMs.toFixed(2)}ms`, {
operation: name,
durationMs,
error,
...context
})
throw error
}
}
/**
* Record a duration measurement (for manual tracking)
*
* @param durationMs - Duration in milliseconds
*/
recordDuration(durationMs: number): void {
this.durations.push(durationMs)
if (durationMs > this.slowThresholdMs) {
this.slowCount++
}
}
/**
* Get current performance metrics
*
* @returns PerformanceMetrics with aggregated statistics
*/
getMetrics(): PerformanceMetrics {
const count = this.durations.length
if (count === 0) {
return {
count: 0,
totalDurationMs: 0,
minDurationMs: 0,
maxDurationMs: 0,
avgDurationMs: 0,
slowOperationCount: 0
}
}
const totalDurationMs = this.durations.reduce((sum, d) => sum + d, 0)
const minDurationMs = Math.min(...this.durations)
const maxDurationMs = Math.max(...this.durations)
const avgDurationMs = totalDurationMs / count
return {
count,
totalDurationMs,
minDurationMs,
maxDurationMs,
avgDurationMs,
slowOperationCount: this.slowCount
}
}
/**
* Log a summary of performance metrics
*
* @param level - Log level for summary (default: 'info')
* @param message - Custom message prefix (optional)
*/
logSummary(level: 'info' | 'warn' | 'debug' = 'info', message?: string): void {
const metrics = this.getMetrics()
const summaryMessage = message || `[${this.operationName}] Performance Summary`
this.log[level](summaryMessage, {
totalOperations: metrics.count,
avgDurationMs: `${metrics.avgDurationMs.toFixed(2)}ms`,
minDurationMs: `${metrics.minDurationMs.toFixed(2)}ms`,
maxDurationMs: `${metrics.maxDurationMs.toFixed(2)}ms`,
slowOperations: metrics.slowOperationCount,
slowPercentage:
metrics.count > 0
? ((metrics.slowOperationCount / metrics.count) * 100).toFixed(1) + '%'
: '0%',
totalDurationMs: `${metrics.totalDurationMs.toFixed(2)}ms`
})
}
/**
* Reset all tracked metrics
*/
reset(): void {
this.durations = []
this.slowCount = 0
}
}
/**
* Create a performance tracker for a specific service or module
*
* Convenience function that returns a new PerformanceTracker instance.
* Useful for creating trackers with consistent naming conventions.
*
* @param context - Context/module name (e.g., 'DatabaseService', 'ERPExtractor')
* @param slowThresholdMs - Optional custom slow threshold
* @returns New PerformanceTracker instance
*
* @example
* ```typescript
* const dbTracker = createPerformanceTracker('DatabaseService', 200);
* ```
*/
export function createPerformanceTracker(
context: string,
slowThresholdMs?: number
): PerformanceTracker {
return new PerformanceTracker(context, slowThresholdMs)
}

View File

@@ -0,0 +1,152 @@
/**
* Request Context Management using AsyncLocalStorage
*
* Provides async-context propagation for request-scoped logging metadata.
* Uses Node.js AsyncLocalStorage to maintain isolated context across async/await boundaries.
*
* Features:
* - Automatic requestId generation with crypto.randomUUID()
* - Support for userId and operation tracking
* - Complete context isolation between concurrent requests
* - Backward compatible with non-request logging scenarios
*
* @example
* ```typescript
* import { run, getRequestId, getContext } from './request-context'
*
* await run(async () => {
* const requestId = getRequestId() // Available throughout async chain
* await someAsyncOperation()
* }, { userId: 'user123', operation: 'extract' })
* ```
*/
import { AsyncLocalStorage } from 'async_hooks'
import { randomUUID } from 'crypto'
/**
* Logger context structure containing request-scoped metadata
*/
export interface LoggerContext {
/** Unique identifier for this request (auto-generated UUID v4) */
requestId: string
/** User ID performing the operation (optional, set by caller) */
userId?: string
/** Operation being performed (optional, e.g., 'extract', 'clean', 'validate') */
operation?: string
}
/**
* AsyncLocalStorage instance for request context
* Each async execution scope has its own isolated context
*/
const storage = new AsyncLocalStorage<LoggerContext>()
/**
* Execute a function within a request context scope
*
* Creates a new context with auto-generated requestId and optional business metadata.
* All async operations within the callback can access this context via getRequestId() or getContext().
*
* @param fn - The async function to execute within the context
* @param context - Optional business context (userId, operation)
* @returns Promise resolving to the function's return value
*
* @example
* ```typescript
* await run(async () => {
* // requestId is available here and in all nested async calls
* const id = getRequestId()
* await processOrder()
* }, { userId: 'user123', operation: 'extract' })
* ```
*/
export function run<T>(
fn: () => Promise<T>,
context?: Omit<LoggerContext, 'requestId'>
): Promise<T> {
const fullContext: LoggerContext = {
requestId: randomUUID(),
userId: context?.userId,
operation: context?.operation
}
return storage.run(fullContext, fn)
}
/**
* Get the current request ID from the async context
*
* @returns The current requestId, or undefined if not in a request context
*
* @example
* ```typescript
* function logSomething() {
* const requestId = getRequestId()
* logger.info(`Processing...`, { requestId })
* }
* ```
*/
export function getRequestId(): string | undefined {
const context = storage.getStore()
return context?.requestId
}
/**
* Get the full logger context from the current async scope
*
* @returns The complete LoggerContext, or undefined if not in a request context
*
* @example
* ```typescript
* const context = getContext()
* if (context) {
* logger.info('Operation', {
* requestId: context.requestId,
* userId: context.userId,
* operation: context.operation
* })
* }
* ```
*/
export function getContext(): LoggerContext | undefined {
return storage.getStore()
}
/**
* Execute a function with a modified context
*
* Creates a new context scope based on the current context with selective overrides.
* Useful for nested operations that need to change specific context fields.
*
* @param fn - The async function to execute
* @param overrides - Context fields to override
* @returns Promise resolving to the function's return value
*
* @example
* ```typescript
* await run(async () => {
* // Outer context: operation='extract'
* await withContext(async () => {
* // Inner context: operation='validate-subtask'
* }, { operation: 'validate-subtask' })
* }, { operation: 'extract' })
* ```
*/
export function withContext<T>(
fn: () => Promise<T>,
overrides: Partial<Omit<LoggerContext, 'requestId'>>
): Promise<T> {
const currentContext = storage.getStore()
const newContext: LoggerContext = currentContext
? {
...currentContext,
...overrides
}
: {
requestId: randomUUID(),
...overrides
}
return storage.run(newContext, fn)
}

View File

@@ -0,0 +1,60 @@
/**
* Shared Logger Utilities
* Common functions used across logger modules
*/
import path from 'path'
import fs from 'fs'
import { app } from 'electron'
/**
* Get log directory path
* Uses app.getPath('logs') in production, local logs dir in development
* Production = app.isPackaged === true
*/
export function getLogDir(): string {
// Check if running in production (packed app)
// This must be checked BEFORE app.getPath('logs') because Electron
// always returns the user data logs path regardless of environment
if (app && app.isReady() && app.isPackaged) {
return app.getPath('logs')
}
// Development environment: use logs directory in project root
// Note: synchronous FS calls are acceptable here because this branch
// executes in dev environments or before app is ready.
const devLogDir = path.join(process.cwd(), 'logs')
if (!fs.existsSync(devLogDir)) {
fs.mkdirSync(devLogDir, { recursive: true })
}
return devLogDir
}
/**
* Check if running in production environment
* Uses app.isPackaged as the single source of truth
*/
export function isProduction(): boolean {
return app?.isPackaged ?? false
}
/**
* Log level priority mapping (higher number = more severe)
*/
export const LOG_LEVEL_PRIORITY: Record<string, number> = {
verbose: 0,
debug: 1,
info: 2,
warn: 3,
error: 4
}
/**
* Check if a log level should be logged given a threshold
* @param level - The log level of the message
* @param threshold - The minimum log level threshold
* @returns true if the message should be logged
*/
export function isLoggable(level: string, threshold: string): boolean {
return (LOG_LEVEL_PRIORITY[level] ?? 0) >= (LOG_LEVEL_PRIORITY[threshold] ?? 2)
}

View File

@@ -15,7 +15,7 @@ import {
type GetObjectCommandInput,
type DeleteObjectCommandInput
} from '@aws-sdk/client-s3'
import { createLogger } from '../logger'
import { createLogger, run, trackDuration } from '../logger'
import type { RustfsConfig } from '../../types/config.schema'
import * as fs from 'fs'
import * as path from 'path'
@@ -77,6 +77,7 @@ export class RustfsService {
try {
// Validate configuration
if (!this.config.enabled) {
log.warn('RustFS upload skipped - disabled in config', { filePath, key })
return {
success: false,
key,
@@ -86,6 +87,7 @@ export class RustfsService {
// Check if file exists
if (!fs.existsSync(filePath)) {
log.warn('RustFS upload skipped - file not found', { filePath, key })
return {
success: false,
key,
@@ -103,7 +105,9 @@ export class RustfsService {
filePath,
key,
contentType: mimeType,
size: fileContent.length
fileSize: fileContent.length,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
const input: PutObjectCommandInput = {
@@ -116,9 +120,12 @@ export class RustfsService {
const command = new PutObjectCommand(input)
const response = await this.client.send(command)
log.info('File uploaded successfully', {
log.info('File uploaded successfully to RustFS', {
key,
etag: response.ETag
fileSize: fileContent.length,
etag: response.ETag,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
@@ -131,7 +138,9 @@ export class RustfsService {
log.error('Failed to upload file to RustFS', {
filePath,
key,
error: errorMessage
error: errorMessage,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
@@ -151,6 +160,10 @@ export class RustfsService {
async uploadString(content: string, key: string, contentType?: string): Promise<UploadResult> {
try {
if (!this.config.enabled) {
log.warn('RustFS string upload skipped - disabled in config', {
key,
endpoint: this.config.endpoint
})
return {
success: false,
key,
@@ -163,7 +176,9 @@ export class RustfsService {
log.info('Uploading string content to RustFS', {
key,
contentType: mimeType,
size: content.length
fileSize: content.length,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
const input: PutObjectCommandInput = {
@@ -176,9 +191,12 @@ export class RustfsService {
const command = new PutObjectCommand(input)
const response = await this.client.send(command)
log.info('String content uploaded successfully', {
log.info('String content uploaded successfully to RustFS', {
key,
etag: response.ETag
fileSize: content.length,
etag: response.ETag,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
@@ -190,7 +208,9 @@ export class RustfsService {
const errorMessage = error instanceof Error ? error.message : 'Unknown upload error'
log.error('Failed to upload string to RustFS', {
key,
error: errorMessage
error: errorMessage,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
@@ -208,6 +228,10 @@ export class RustfsService {
async downloadFile(key: string): Promise<DownloadResult> {
try {
if (!this.config.enabled) {
log.warn('RustFS download skipped - disabled in config', {
key,
endpoint: this.config.endpoint
})
return {
success: false,
content: Buffer.alloc(0),
@@ -215,7 +239,11 @@ export class RustfsService {
}
}
log.info('Downloading file from RustFS', { key })
log.info('Downloading file from RustFS', {
key,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
const input: GetObjectCommandInput = {
Bucket: this.config.bucket,
@@ -232,9 +260,11 @@ export class RustfsService {
const content = Buffer.concat(chunks)
log.info('File downloaded successfully', {
log.info('File downloaded successfully from RustFS', {
key,
size: content.length
fileSize: content.length,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
@@ -245,7 +275,9 @@ export class RustfsService {
const errorMessage = error instanceof Error ? error.message : 'Unknown download error'
log.error('Failed to download file from RustFS', {
key,
error: errorMessage
error: errorMessage,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
@@ -263,13 +295,21 @@ export class RustfsService {
async deleteFile(key: string): Promise<{ success: boolean; error?: string }> {
try {
if (!this.config.enabled) {
log.warn('RustFS delete skipped - disabled in config', {
key,
endpoint: this.config.endpoint
})
return {
success: false,
error: 'RustFS is not enabled in configuration'
}
}
log.info('Deleting file from RustFS', { key })
log.info('Deleting file from RustFS', {
key,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
const input: DeleteObjectCommandInput = {
Bucket: this.config.bucket,
@@ -279,7 +319,11 @@ export class RustfsService {
const command = new DeleteObjectCommand(input)
await this.client.send(command)
log.info('File deleted successfully', { key })
log.info('File deleted successfully from RustFS', {
key,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
success: true
@@ -288,7 +332,9 @@ export class RustfsService {
const errorMessage = error instanceof Error ? error.message : 'Unknown delete error'
log.error('Failed to delete file from RustFS', {
key,
error: errorMessage
error: errorMessage,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
@@ -341,7 +387,8 @@ export class RustfsService {
try {
log.info('Testing RustFS connection', {
endpoint: this.config.endpoint,
bucket: this.config.bucket
bucket: this.config.bucket,
region: this.config.region
})
// Try to list objects in the bucket (head bucket operation)
@@ -354,7 +401,10 @@ export class RustfsService {
const command = new ListObjectsV2Command(input)
await this.client.send(command)
log.info('RustFS connection test successful')
log.info('RustFS connection test successful', {
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {
success: true,
@@ -363,7 +413,9 @@ export class RustfsService {
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown connection error'
log.error('RustFS connection test failed', {
error: errorMessage
error: errorMessage,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
return {

View File

@@ -1,6 +1,6 @@
import * as fs from 'fs'
import { ConfigManager } from '../config/config-manager'
import { createLogger } from '../logger'
import { createLogger, run, trackDuration } from '../logger'
import type { UpdateConfig } from '../../types/config.schema'
import type { UserType } from '../../types/user.types'
import type {
@@ -67,7 +67,10 @@ export class UpdateService {
enabled,
supported: supportState.supported,
currentVersion: this.status.currentVersion,
currentChannel: this.status.currentChannel
currentChannel: this.status.currentChannel,
endpoint: this.config?.endpoint,
bucket: this.config?.bucket,
checkIntervalMinutes: this.config?.checkIntervalMinutes
})
this.initialized = true
@@ -88,17 +91,35 @@ export class UpdateService {
public async getChangelog(release: DownloadReleaseRequest): Promise<string> {
this.ensureInitialized()
if (!this.status.enabled || !this.storageClient) {
log.warn('Changelog request rejected - auto update disabled', {
version: release.version,
channel: release.channel
})
throw new Error('自动更新不可用')
}
const cacheKey = `${release.channel}:${release.version}`
const cached = this.changelogCache.get(cacheKey)
if (cached) {
log.debug('Changelog returned from cache', {
version: release.version,
channel: release.channel
})
return cached
}
log.info('Fetching changelog from storage', {
version: release.version,
channel: release.channel,
changelogKey: release.changelogKey
})
const markdown = await this.storageClient.readText(release.changelogKey)
this.changelogCache.set(cacheKey, markdown)
log.info('Changelog fetched successfully', {
version: release.version,
channel: release.channel,
cacheSize: this.changelogCache.size
})
return markdown
}
@@ -107,6 +128,10 @@ export class UpdateService {
this.status.currentUserType = userType
if (!this.status.enabled || !userType) {
log.info('Update service context cleared', {
userType,
reason: this.status.enabled ? 'user logged out' : 'auto-update disabled'
})
this.clearPolling()
this.catalog = { stable: [], preview: [] }
this.publishStatus({
@@ -124,10 +149,18 @@ export class UpdateService {
return
}
log.info('Update service user context set', {
userType,
enabled: this.status.enabled,
currentVersion: this.status.currentVersion
})
// 启动异步更新检查,不阻塞登录流程
void this.checkForUpdates().catch((error) => {
const message = error instanceof Error ? error.message : String(error)
log.warn('Async update check failed', {
error: error instanceof Error ? error.message : String(error)
userType,
error: message
})
})
this.startPolling()
@@ -136,6 +169,11 @@ export class UpdateService {
public async checkForUpdates(): Promise<UpdateStatus> {
this.ensureInitialized()
if (!this.status.enabled || !this.storageClient || !this.catalogService) {
log.debug('Update check skipped - service not enabled or not initialized', {
enabled: this.status.enabled,
hasStorageClient: !!this.storageClient,
hasCatalogService: !!this.catalogService
})
return this.getStatus()
}
@@ -147,6 +185,12 @@ export class UpdateService {
try {
const currentUserType = this.status.currentUserType
log.info('Checking for updates', {
userType: currentUserType,
currentVersion: this.status.currentVersion,
currentChannel: this.status.currentChannel
})
this.catalog = await this.catalogService.loadCatalog(currentUserType)
if (currentUserType === 'User') {
@@ -154,10 +198,19 @@ export class UpdateService {
this.publishStatus(nextStatus)
if (nextStatus.phase === 'available' && nextStatus.recommendedRelease) {
const release = nextStatus.recommendedRelease
log.info('Update available for user', {
version: release.version,
channel: release.channel
})
// 异步下载,不阻塞更新检查流程
void this.downloadRelease(nextStatus.recommendedRelease).catch((error) => {
void this.downloadRelease(release).catch((error) => {
const message = error instanceof Error ? error.message : '下载更新失败'
log.warn('Async update download failed', { error: message })
log.warn('Async update download failed', {
version: release.version,
channel: release.channel,
error: message
})
this.publishStatus({
phase: 'error',
error: message,
@@ -166,6 +219,10 @@ export class UpdateService {
})
}
} else if (currentUserType === 'Admin') {
log.info('Update check completed for admin', {
stableReleases: this.catalog.stable.length,
previewReleases: this.catalog.preview.length
})
this.publishStatus(this.catalogService.resolveAdminStatus(this.status, this.catalog))
} else {
this.publishStatus({
@@ -176,7 +233,10 @@ export class UpdateService {
}
} catch (error) {
const message = error instanceof Error ? error.message : '检查更新失败'
log.error('Failed to check for updates', { error: message })
log.error('Failed to check for updates', {
userType: this.status.currentUserType,
error: message
})
this.publishStatus({
phase: 'error',
error: message,
@@ -190,9 +250,19 @@ export class UpdateService {
public async downloadRelease(request: DownloadReleaseRequest): Promise<UpdateStatus> {
this.ensureInitialized()
if (!this.status.enabled || !this.storageClient) {
log.warn('Download request rejected - auto update disabled', {
version: request.version,
channel: request.channel
})
throw new Error('自动更新不可用')
}
log.info('Starting update download', {
version: request.version,
channel: request.channel,
artifactKey: request.artifactKey
})
this.publishStatus({
phase: 'downloading',
progress: 0,
@@ -208,10 +278,22 @@ export class UpdateService {
const hash = await this.installer.calculateSha256(downloadPath)
if (hash.toLowerCase() !== request.sha256.toLowerCase()) {
log.error('Update package hash mismatch', {
version: request.version,
channel: request.channel,
expectedHash: request.sha256,
actualHash: hash
})
await fs.promises.rm(downloadPath, { force: true })
throw new Error('更新包校验失败,文件哈希不匹配')
}
log.info('Update download completed and verified', {
version: request.version,
channel: request.channel,
downloadPath
})
this.publishStatus({
phase: 'downloaded',
progress: 100,
@@ -233,9 +315,19 @@ export class UpdateService {
const downloaded = this.status.downloadedRelease
if (!this.status.enabled || !downloaded) {
log.warn('Install request rejected - no update package available', {
enabled: this.status.enabled,
hasDownloadedRelease: !!downloaded
})
throw new Error('没有可安装的更新包')
}
log.info('Installing update package', {
version: downloaded.version,
channel: downloaded.channel,
localPath: downloaded.localPath
})
this.publishStatus({
phase: 'installing',
latestVersion: downloaded.version,
@@ -245,6 +337,10 @@ export class UpdateService {
})
await this.installer.installDownloadedRelease(downloaded)
log.info('Update installation completed', {
version: downloaded.version,
channel: downloaded.channel
})
}
private ensureInitialized(): void {
@@ -264,14 +360,23 @@ export class UpdateService {
private startPolling(): void {
this.clearPolling()
if (!this.config) {
log.warn('Polling not started - no update configuration')
return
}
log.info('Update polling started', {
intervalMinutes: this.config.checkIntervalMinutes,
endpoint: this.config.endpoint,
bucket: this.config.bucket
})
this.intervalHandle = setInterval(
() => {
this.checkForUpdates().catch((error) => {
const message = error instanceof Error ? error.message : String(error)
log.warn('Periodic update check failed', {
error: error instanceof Error ? error.message : String(error)
channel: this.status.currentChannel,
error: message
})
})
},

View File

@@ -163,10 +163,10 @@ export class BIPUsersDAO {
return null
}
} catch (error) {
logError(log, 'Authenticate failed', error, {
logError(log, error, {
message: 'Authenticate failed',
operation: 'authenticate',
username,
dbType: this.dbType
context: { username, dbType: this.dbType }
})
return null
}
@@ -222,10 +222,10 @@ export class BIPUsersDAO {
return null
}
} catch (error) {
logError(log, 'Silent login failed', error, {
logError(log, error, {
message: 'Silent login failed',
operation: 'authenticateByComputerName',
computerName,
dbType: this.dbType
context: { computerName, dbType: this.dbType }
})
return null
}
@@ -258,9 +258,10 @@ export class BIPUsersDAO {
createTime: row.CreateTime as Date | undefined
}))
} catch (error) {
logError(log, 'Get all users failed', error, {
logError(log, error, {
message: 'Get all users failed',
operation: 'getAllUsers',
dbType: this.dbType
context: { dbType: this.dbType }
})
return []
}
@@ -345,11 +346,10 @@ export class BIPUsersDAO {
return true
}
} catch (error) {
logError(log, 'Create user failed', error, {
logError(log, error, {
message: 'Create user failed',
operation: 'createUser',
username,
userType,
dbType: this.dbType
context: { username, userType, dbType: this.dbType }
})
return false
}
@@ -389,11 +389,10 @@ export class BIPUsersDAO {
return true
}
} catch (error) {
logError(log, 'Update user type failed', error, {
logError(log, error, {
message: 'Update user type failed',
operation: 'updateUserType',
username,
userType,
dbType: this.dbType
context: { username, userType, dbType: this.dbType }
})
return false
}
@@ -433,10 +432,10 @@ export class BIPUsersDAO {
return true
}
} catch (error) {
logError(log, 'Update password failed', error, {
logError(log, error, {
message: 'Update password failed',
operation: 'updatePassword',
username,
dbType: this.dbType
context: { username, dbType: this.dbType }
})
return false
}
@@ -472,10 +471,10 @@ export class BIPUsersDAO {
return true
}
} catch (error) {
logError(log, 'Delete user failed', error, {
logError(log, error, {
message: 'Delete user failed',
operation: 'deleteUser',
username,
dbType: this.dbType
context: { username, dbType: this.dbType }
})
return false
}
@@ -513,10 +512,10 @@ export class BIPUsersDAO {
return result.rows.length > 0 && (result.rows[0].count as number) > 0
}
} catch (error) {
logError(log, 'Check user exists failed', error, {
logError(log, error, {
message: 'Check user exists failed',
operation: 'userExists',
username,
dbType: this.dbType
context: { username, dbType: this.dbType }
})
return false
}
@@ -574,10 +573,10 @@ export class BIPUsersDAO {
return null
}
} catch (error) {
logError(log, 'Get user ERP credentials failed', error, {
logError(log, error, {
message: 'Get user ERP credentials failed',
operation: 'getUserErpCredentials',
username,
dbType: this.dbType
context: { username, dbType: this.dbType }
})
return null
}
@@ -626,10 +625,10 @@ export class BIPUsersDAO {
return true
}
} catch (error) {
logError(log, 'Update user ERP credentials failed', error, {
logError(log, error, {
message: 'Update user ERP credentials failed',
operation: 'updateUserErpCredentials',
username,
dbType: this.dbType
context: { username, dbType: this.dbType }
})
return false
}
@@ -668,9 +667,10 @@ export class BIPUsersDAO {
erpUsername: (row[cols.ERP_USERNAME] as string) || ''
}))
} catch (error) {
logError(log, 'Get all users ERP config failed', error, {
logError(log, error, {
message: 'Get all users ERP config failed',
operation: 'getAllUsersErpConfig',
dbType: this.dbType
context: { dbType: this.dbType }
})
return []
}

View File

@@ -1,7 +1,7 @@
import { DiscreteMaterialPlanDAO } from '../database/discrete-material-plan-dao'
import { MaterialsToBeDeletedDAO } from '../database/materials-to-be-deleted-dao'
import { SqlServerService } from '../database/sql-server'
import { createLogger } from '../logger'
import { createLogger, withRequestContext, trackDuration, getRequestId } from '../logger'
import type {
MaterialRecordSummary,
ValidationRequest,
@@ -30,109 +30,202 @@ export class ValidationApplicationService {
userInfo: UserInfo,
senderId: number
): Promise<ValidationResponse> {
let dbService: ValidationDatabaseService | null = null
return withRequestContext(
async () => {
const requestId = getRequestId()
let dbService: ValidationDatabaseService | null = null
try {
const isAdmin = userInfo.userType === 'Admin'
const username = userInfo.username
try {
const isAdmin = userInfo.userType === 'Admin'
const username = userInfo.username
log.info('Starting validation', { mode: request.mode, user: username, isAdmin })
log.info('Starting validation workflow', {
mode: request.mode,
useSharedProductionIds: request.useSharedProductionIds,
userId: userInfo.id,
username,
isAdmin,
requestId
})
dbService = await createValidationDatabaseService()
// Track data query duration
const dataQueryResult = await trackDuration(
async () => {
dbService = await createValidationDatabaseService()
let sourceNumbers: string[] | null = null
let sourceNumbers: string[] | null = null
if (request.mode === 'database_filtered') {
if (request.useSharedProductionIds) {
const sharedIds = sharedProductionIdsStore.get(senderId)
log.info(`Using ${sharedIds.length} shared Production IDs`)
if (request.mode === 'database_filtered') {
if (request.useSharedProductionIds) {
const sharedIds = sharedProductionIdsStore.get(senderId)
log.info(`Using ${sharedIds.length} shared Production IDs`, {
userId: userInfo.id,
mode: request.mode,
useSharedProductionIds: true
})
if (sharedIds.length === 0) {
return this.emptyFailure(
'没有可用的共享 Production ID。请在数据提取页面输入 Production ID。'
)
if (sharedIds.length === 0) {
return {
sourceNumbers: null,
failure: this.emptyFailure(
'没有可用的共享 Production ID。请在数据提取页面输入 Production ID。'
)
}
}
sourceNumbers = await getSourceNumbersFromInputs(sharedIds, dbService)
log.info(
`Got ${sourceNumbers.length} source numbers from shared Production IDs`,
{
userId: userInfo.id,
sourceCount: sourceNumbers.length
}
)
if (sourceNumbers.length === 0) {
return {
sourceNumbers: null,
failure: this.emptyFailure(
'共享的 Production ID 没有找到对应的订单数据。请确保在数据提取页面输入了有效的 Production ID 并成功获取了订单数据。'
)
}
}
} else if (request.productionIdFile) {
const inputs = readProductionIds(request.productionIdFile)
log.info(`Read ${inputs.length} inputs from file`, {
userId: userInfo.id,
fileMode: !request.useSharedProductionIds
})
sourceNumbers = await getSourceNumbersFromInputs(inputs, dbService)
log.info(`Got ${sourceNumbers.length} source numbers`, {
userId: userInfo.id,
sourceCount: sourceNumbers.length
})
if (sourceNumbers.length === 0) {
return {
sourceNumbers: null,
failure: this.emptyFailure(
'文件中的 Production ID 没有找到对应的订单数据。请检查 Production ID 是否正确,或确保数据库中有对应的订单数据。'
)
}
}
}
}
const materialDao = new DiscreteMaterialPlanDAO()
let materialRecords: any[] = []
if (request.mode === 'database_full') {
materialRecords = await materialDao.queryAllDistinctByMaterialCode()
} else if (sourceNumbers && sourceNumbers.length > 0) {
materialRecords = await materialDao.queryBySourceNumbersDistinct(sourceNumbers)
}
if (materialRecords.length === 0) {
return {
sourceNumbers: null,
failure: this.emptyFailure(
'未找到物料记录。请检查数据库中是否有对应订单的物料数据。'
)
}
}
return { sourceNumbers, materialRecords, failure: null }
},
{
operationName: 'data-query',
message: 'Data query phase',
context: { mode: request.mode, userId: userInfo.id }
}
)
// Check for failure
if (dataQueryResult.result.failure) {
return dataQueryResult.result.failure
}
sourceNumbers = await getSourceNumbersFromInputs(sharedIds, dbService)
log.info(`Got ${sourceNumbers.length} source numbers from shared Production IDs`)
// Track validation duration
const validationResult = await trackDuration(
async () => {
const typeKeywords = await this.loadTypeKeywords(dbService!)
const markedCodes = await this.loadMarkedCodes(dbService!)
const results = this.buildValidationResults(
(dataQueryResult.result as any).materialRecords,
typeKeywords,
markedCodes,
{ isAdmin, username }
)
if (sourceNumbers.length === 0) {
return this.emptyFailure(
'共享的 Production ID 没有找到对应的订单数据。请确保在数据提取页面输入了有效的 Production ID 并成功获取了订单数据。'
)
const markedCount = results.filter((result) => result.isMarkedForDeletion).length
const matchedCount = results.filter((result) => result.managerName).length
log.info('Validation completed', {
totalRecords: results.length,
matchedCount,
markedCount,
userId: userInfo.id,
mode: request.mode
})
return {
success: true,
results,
stats: {
totalRecords: results.length,
matchedCount,
markedCount
}
}
},
{
operationName: 'validation-processing',
message: 'Validation processing phase',
context: { mode: request.mode, userId: userInfo.id }
}
)
return validationResult.result
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.error('Validation workflow failed', {
error: message,
mode: request.mode,
useSharedProductionIds: request.useSharedProductionIds,
userId: userInfo.id,
username: userInfo.username,
requestId
})
return {
success: false,
error: `Validation failed: ${message}`
}
} else if (request.productionIdFile) {
const inputs = readProductionIds(request.productionIdFile)
log.info(`Read ${inputs.length} inputs from file`)
sourceNumbers = await getSourceNumbersFromInputs(inputs, dbService)
log.info(`Got ${sourceNumbers.length} source numbers`)
if (sourceNumbers.length === 0) {
return this.emptyFailure(
'文件中的 Production ID 没有找到对应的订单数据。请检查 Production ID 是否正确,或确保数据库中有对应的订单数据。'
)
} finally {
if (dbService) {
await this.disconnectQuietly(dbService)
}
}
}
const materialDao = new DiscreteMaterialPlanDAO()
let materialRecords: any[] = []
if (request.mode === 'database_full') {
materialRecords = await materialDao.queryAllDistinctByMaterialCode()
} else if (sourceNumbers && sourceNumbers.length > 0) {
materialRecords = await materialDao.queryBySourceNumbersDistinct(sourceNumbers)
}
if (materialRecords.length === 0) {
return this.emptyFailure('未找到物料记录。请检查数据库中是否有对应订单的物料数据。')
}
const typeKeywords = await this.loadTypeKeywords(dbService)
const markedCodes = await this.loadMarkedCodes(dbService)
const results = this.buildValidationResults(materialRecords, typeKeywords, markedCodes, {
isAdmin,
username
})
const markedCount = results.filter((result) => result.isMarkedForDeletion).length
const matchedCount = results.filter((result) => result.managerName).length
return {
success: true,
results,
stats: {
totalRecords: results.length,
matchedCount,
markedCount
}
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.error('Validation error', { error: message })
return {
success: false,
error: `Validation failed: ${message}`
}
} finally {
if (dbService) {
await this.disconnectQuietly(dbService)
}
}
},
{ userId: userInfo.id.toString(), operation: 'validate' }
)
}
async getMaterialsByManager(managerName: string): Promise<MaterialRecordSummary[]> {
log.info(`Getting materials by manager: ${managerName}`)
const dao = new MaterialsToBeDeletedDAO()
const materials = await dao.getMaterialsByManager(managerName)
const markedCodes = await dao.getAllMaterialCodes()
log.info(`Found ${materials.length} materials for manager: ${managerName}`)
return this.enrichMaterials(materials, markedCodes)
}
async getAllMaterials(): Promise<MaterialRecordSummary[]> {
log.info('Getting all materials')
const dao = new MaterialsToBeDeletedDAO()
const materials = await dao.getAllRecords()
const markedCodes = await dao.getAllMaterialCodes()
log.info(`Found ${materials.length} total materials`)
return this.enrichMaterials(materials, markedCodes)
}
@@ -145,43 +238,70 @@ export class ValidationApplicationService {
materialCodes?: string[]
error?: string
}> {
let dbService: ValidationDatabaseService | null = null
return withRequestContext(
async () => {
const requestId = getRequestId()
let dbService: ValidationDatabaseService | null = null
try {
const isAdmin = userInfo.userType === 'Admin'
const username = userInfo.username
log.info(`User: ${username}, isAdmin: ${isAdmin}`)
try {
const isAdmin = userInfo.userType === 'Admin'
const username = userInfo.username
log.info('Getting cleaner data', {
userId: userInfo.id,
username,
isAdmin,
requestId
})
dbService = await createValidationDatabaseService()
dbService = await createValidationDatabaseService()
const sharedIds = sharedProductionIdsStore.get(senderId)
let orderNumbers: string[] = []
const sharedIds = sharedProductionIdsStore.get(senderId)
let orderNumbers: string[] = []
if (sharedIds.length > 0) {
log.info(`Using ${sharedIds.length} shared Production IDs`)
orderNumbers = await getSourceNumbersFromInputs(sharedIds, dbService)
log.info(`Got ${orderNumbers.length} order numbers`)
}
if (sharedIds.length > 0) {
log.info(`Using ${sharedIds.length} shared Production IDs`, {
userId: userInfo.id,
sharedCount: sharedIds.length
})
orderNumbers = await getSourceNumbersFromInputs(sharedIds, dbService)
log.info(`Got ${orderNumbers.length} order numbers`, {
userId: userInfo.id,
orderCount: orderNumbers.length
})
}
const materialCodes = await this.loadMaterialCodesForCleaner(dbService, username, isAdmin)
const materialCodes = await this.loadMaterialCodesForCleaner(dbService, username, isAdmin)
log.info('Cleaner data retrieved', {
userId: userInfo.id,
orderCount: orderNumbers.length,
materialCodeCount: materialCodes.length
})
return {
success: true,
orderNumbers,
materialCodes
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.error('CleanerData error', { error: message })
return {
success: false,
error: `获取清理数据失败:${message}`
}
} finally {
if (dbService) {
await this.disconnectQuietly(dbService)
}
}
return {
success: true,
orderNumbers,
materialCodes
}
} catch (error) {
const message = error instanceof Error ? error.message : 'Unknown error'
log.error('CleanerData error', {
error: message,
userId: userInfo.id,
username: userInfo.username,
requestId
})
return {
success: false,
error: `获取清理数据失败:${message}`
}
} finally {
if (dbService) {
await this.disconnectQuietly(dbService)
}
}
},
{ userId: userInfo.id.toString(), operation: 'getCleanerData' }
)
}
private emptyFailure(error: string): ValidationResponse {
@@ -291,6 +411,8 @@ export class ValidationApplicationService {
const detailTableName = getValidationTableName('dbo_DiscreteMaterialPlanData')
const enrichedMaterials: MaterialRecordSummary[] = []
log.info(`Enriching ${materials.length} materials with details`)
for (const material of materials) {
const detailResult = await this.queryMaterialDetail(
dbService,
@@ -309,6 +431,11 @@ export class ValidationApplicationService {
})
}
log.info(`Material enrichment completed`, {
totalMaterials: materials.length,
enrichedCount: enrichedMaterials.length
})
return enrichedMaterials
} finally {
if (dbService) {
@@ -363,7 +490,11 @@ export class ValidationApplicationService {
`
)
const materialCodes = result.rows.map((row) => row.MaterialCode as string).filter(Boolean)
log.info(`Admin user: got ${materialCodes.length} materials`)
log.info(`Admin user: got ${materialCodes.length} materials`, {
userId: username,
isAdmin: true,
materialCount: materialCodes.length
})
return materialCodes
}
@@ -382,7 +513,11 @@ export class ValidationApplicationService {
const materialCodes = result.rows
.map((row: Record<string, unknown>) => row.MaterialCode as string)
.filter(Boolean)
log.info(`Regular user: got ${materialCodes.length} materials`)
log.info(`Regular user: got ${materialCodes.length} materials`, {
userId: username,
isAdmin: false,
materialCount: materialCodes.length
})
return materialCodes
}
@@ -395,7 +530,11 @@ export class ValidationApplicationService {
[username]
)
const materialCodes = result.rows.map((row) => row.MaterialCode as string).filter(Boolean)
log.info(`Regular user: got ${materialCodes.length} materials`)
log.info(`Regular user: got ${materialCodes.length} materials`, {
userId: username,
isAdmin: false,
materialCount: materialCodes.length
})
return materialCodes
}

View File

@@ -0,0 +1,27 @@
/**
* Debug script to verify Electron environment detection
*/
import { app } from 'electron'
console.log('=== Electron Environment Debug ===\n')
console.log('1. app.isPackaged:', app.isPackaged)
console.log('2. app.getPath("userData"):', app.getPath('userData'))
console.log('3. app.getPath("logs"):', app.getPath('logs'))
console.log('4. NODE_ENV:', process.env.NODE_ENV)
console.log('5. process.cwd():', process.cwd())
console.log('6. __dirname:', __dirname)
// Predict log dir
function getLogDir(): string {
if (app && app.isReady()) {
return app.getPath('logs')
}
const devLogDir = `${process.cwd()}\\logs`
return devLogDir
}
console.log('\n7. Predicted log dir:', getLogDir())
console.log('\n=== END DEBUG ===')
app.quit()

View File

@@ -83,7 +83,8 @@ export const extractionConfigSchema = z.object({
verbose: z.boolean().default(true),
autoConvert: z.boolean().default(true),
mergeBatches: z.boolean().default(true),
enableDbPersistence: z.boolean().default(true)
enableDbPersistence: z.boolean().default(true),
headless: z.boolean().default(true)
})
/**

View File

@@ -0,0 +1,244 @@
/**
* Enhanced Logger Type Definitions
*
* Provides comprehensive type safety for the logging system,
* including request context, performance metrics, and structured log metadata.
*
* @packageDocumentation
*/
import type { LogLevel } from '../../shared/ipc-channels'
/**
* Core log context interface for request tracing
*
* This type is re-exported from the logger service but defined here
* for type sharing across the application without creating circular dependencies.
*/
export interface LogContext {
/** Unique identifier for the request/operation (UUID v4) */
requestId: string
/** User ID performing the operation (optional) */
userId?: string
/** Operation name being performed (e.g., 'extract', 'clean', 'validate') */
operation?: string
/** Sub-operation or step within the main operation (optional) */
subOperation?: string
/** Batch identifier for batch operations (optional) */
batchId?: string
}
/**
* Enhanced log metadata for structured logging
*
* Provides rich context for log entries, enabling better
* filtering, analysis, and debugging capabilities.
*/
export interface EnhancedLogMeta {
/** Request context for correlation */
context?: LogContext
/** Performance metrics (if applicable) */
performance?: PerformanceMetrics
/** Error information (if applicable) */
error?: {
/** Error name/type */
name: string
/** Error message */
message: string
/** Error code for programmatic handling */
code?: string
/** Stack trace (in development) */
stack?: string
/** Serialized cause chain */
cause?: string | Record<string, unknown>
}
/** Database query information (if applicable) */
database?: {
/** Database type (mysql, sqlserver) */
type: 'mysql' | 'sqlserver'
/** Query executed (sanitized in production) */
query?: string
/** Execution time in milliseconds */
duration: number
/** Number of rows affected/returned */
rowsAffected?: number
}
/** File operation information (if applicable) */
file?: {
/** File path (sanitized in production) */
path: string
/** Operation type (read, write, delete, exists) */
operation: 'read' | 'write' | 'delete' | 'exists' | 'list'
/** File size in bytes (if applicable) */
size?: number
/** Result of the operation */
success: boolean
}
/** HTTP/ERP API call information (if applicable) */
http?: {
/** HTTP method used */
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
/** URL or endpoint called */
url: string
/** HTTP status code received */
statusCode: number
/** Request duration in milliseconds */
duration: number
/** Request payload size in bytes */
requestSize?: number
/** Response payload size in bytes */
responseSize?: number
}
/** Custom key-value pairs for additional metadata */
custom?: Record<string, unknown>
}
/**
* Performance metrics for timing and resource tracking
*
* Captures timing information for operations, enabling
* performance monitoring and bottleneck identification.
*/
export interface PerformanceMetrics {
/** Operation start timestamp (ISO 8601 format or Date) */
startTime: Date | string
/** Operation end timestamp (ISO 8601 format or Date) */
endTime?: Date | string
/** Total duration in milliseconds */
duration: number
/** Breakdown of time spent in different phases (optional) */
phases?: {
/** Phase name (e.g., 'connect', 'query', 'process', 'write') */
[phaseName: string]: {
/** Duration of this phase in milliseconds */
duration: number
/** Additional phase-specific metadata */
meta?: Record<string, unknown>
}
}
/** Memory usage snapshot (optional, Node.js specific) */
memory?: {
/** Heap used in bytes */
heapUsed: number
/** Heap total in bytes */
heapTotal: number
/** RSS (Resident Set Size) in bytes */
rss: number
/** External memory in bytes */
external: number
}
/** CPU usage snapshot (optional) */
cpu?: {
/** User CPU time in milliseconds */
user: number
/** System CPU time in milliseconds */
system: number
}
}
/**
* Log entry structure for structured logging
*
* Represents a complete log entry with all metadata,
* suitable for JSON serialization and log aggregation systems.
*/
export interface StructuredLogEntry {
/** Log level */
level: LogLevel
/** Log message */
message: string
/** Timestamp (ISO 8601 format) */
timestamp: string
/** Service/application identifier */
service: string
/** Module or component context */
context?: string
/** Environment (development, production) */
environment?: string
/** Enhanced metadata */
meta?: EnhancedLogMeta
/** Process information */
process?: {
/** Process ID */
pid: number
/** Process uptime in seconds */
uptime: number
}
}
/**
* Logger configuration interface
*
* Used for type-safe configuration of the logging system.
*/
export interface LoggerConfig {
/** Log level threshold */
level: LogLevel
/** Number of days to retain application logs */
appRetention: number
/** Enable console output (default: true) */
console?: boolean
/** Enable file output (default: true in production) */
file?: boolean
/** Maximum log file size before rotation (e.g., '20m') */
maxSize?: string
/** Log format ('json' | 'pretty') */
format?: 'json' | 'pretty'
}
/**
* Result type for logger operations
*
* Provides type-safe error handling for logger methods.
*/
export interface LoggerOperationResult {
/** Whether the operation succeeded */
success: boolean
/** Error message if operation failed */
error?: string
/** Additional data from the operation */
data?: Record<string, unknown>
}
/**
* Log transport configuration
*/
export interface LogTransportConfig {
/** Transport type */
type: 'console' | 'file' | 'http'
/** Transport-specific options */
options?: {
/** Log level for this transport */
level?: LogLevel
/** Maximum number of files to retain (for file transport) */
maxFiles?: string
/** Maximum file size before rotation */
maxSize?: string
/** Compression for old logs */
zippedArchive?: boolean
}
}

View File

@@ -83,4 +83,6 @@ export interface GetBatchesOptions {
limit?: number
/** Number of batches to skip (for pagination) */
offset?: number
/** Optional username filter for Admin users (supports multiple) */
usernames?: string[]
}

View File

@@ -2,13 +2,56 @@ import type { LogLevel } from '../../shared/ipc-channels'
import { IPC_CHANNELS } from '../../shared/ipc-channels'
import { ipcRenderer } from '../lib/ipc'
// Cached log level for client-side filtering (avoids IPC for filtered-out messages)
let cachedLevel: LogLevel = 'info'
/**
* Check if a message at the given level should be logged
* Based on level priority: error > warn > info > debug > verbose
*/
function shouldLog(level: LogLevel): boolean {
const priorities: Record<LogLevel, number> = {
verbose: 0,
debug: 1,
info: 2,
warn: 3,
error: 4
}
return (priorities[level] ?? 0) >= (priorities[cachedLevel] ?? 2)
}
// Listener for level change broadcasts from main process
function onLevelChanged(_event: Electron.IpcRendererEvent, level: LogLevel): void {
cachedLevel = level
}
export const loggerApi = {
log: (level: LogLevel, message: string, context?: Record<string, unknown>): void => {
// Drop messages below the configured log level
if (!shouldLog(level)) return
ipcRenderer.send(IPC_CHANNELS.LOGGER_FORWARD, {
level,
message,
context,
timestamp: Date.now()
})
},
/**
* Fetch the current log level from main process and cache it.
* Also registers a listener for future level changes.
* Should be called early in renderer initialization.
*/
fetchLevel: async (): Promise<void> => {
cachedLevel = (await ipcRenderer.invoke(IPC_CHANNELS.LOGGER_GET_LEVEL)) as LogLevel
ipcRenderer.on(IPC_CHANNELS.LOGGER_LEVEL_CHANGED, onLevelChanged)
},
/**
* Remove the level change listener (call on cleanup/unmount)
*/
cleanup: (): void => {
ipcRenderer.removeListener(IPC_CHANNELS.LOGGER_LEVEL_CHANGED, onLevelChanged)
}
} as const

View File

@@ -129,6 +129,8 @@ export interface ConfigAPI {
export interface LoggerAPI {
log: (level: LogLevel, message: string, context?: Record<string, unknown>) => void
fetchLevel: () => Promise<void>
cleanup: () => void
}
export interface UpdateAPI {

View File

@@ -80,6 +80,8 @@ export const ExtractorOperationHistoryModal: React.FC<ExtractorOperationHistoryM
const [expandedBatches, setExpandedBatches] = useState<Set<string>>(new Set())
const [batchDetails, setBatchDetails] = useState<Map<string, OperationHistoryRecord[]>>(new Map())
const [deleting, setDeleting] = useState<Set<string>>(new Set())
const [allUsers, setAllUsers] = useState<string[]>([])
const [selectedUsers, setSelectedUsers] = useState<string[]>([])
const isAdmin = user?.userType === 'Admin'
@@ -87,7 +89,13 @@ export const ExtractorOperationHistoryModal: React.FC<ExtractorOperationHistoryM
setLoading(true)
setError(null)
try {
const result = await window.electron.operationHistory.getBatches({ limit: 100 })
// Admin user can pass usernames filter
const options =
isAdmin && selectedUsers.length > 0
? { limit: 100, usernames: selectedUsers }
: { limit: 100 }
const result = await window.electron.operationHistory.getBatches(options)
if (result.success && result.data) {
setBatches(result.data)
} else {
@@ -98,6 +106,18 @@ export const ExtractorOperationHistoryModal: React.FC<ExtractorOperationHistoryM
} finally {
setLoading(false)
}
}, [isAdmin, selectedUsers])
const fetchAllUsers = useCallback(async () => {
try {
const result = await window.electron.auth.getAllUsers()
if (result.success && result.data) {
const usernames = result.data.map((u: UserInfo) => u.username)
setAllUsers(usernames)
}
} catch (err) {
console.error('Failed to fetch users:', err)
}
}, [])
const fetchBatchDetails = useCallback(
@@ -123,8 +143,11 @@ export const ExtractorOperationHistoryModal: React.FC<ExtractorOperationHistoryM
useEffect(() => {
if (isOpen) {
void fetchBatches()
if (isAdmin) {
void fetchAllUsers()
}
}
}, [isOpen, fetchBatches])
}, [isOpen, fetchBatches, fetchAllUsers, isAdmin])
const toggleBatchExpansion = (batchId: string) => {
setExpandedBatches((prev) => {
@@ -196,27 +219,75 @@ export const ExtractorOperationHistoryModal: React.FC<ExtractorOperationHistoryM
}
}
const toggleUserFilter = (username: string) => {
setSelectedUsers((prev) =>
prev.includes(username) ? prev.filter((u) => u !== username) : [...prev, username]
)
}
const clearUserFilters = () => {
setSelectedUsers([])
}
if (!isOpen) return null
return (
<Modal isOpen={isOpen} onClose={onClose} title="操作历史" size="3xl">
<div className="flex flex-col h-[70vh]">
{/* Toolbar */}
<div className="flex items-center justify-between mb-4 pb-4 border-b border-gray-200">
<div className="flex items-center gap-4">
<span className="text-sm text-gray-600">
{isAdmin ? (
<span className="text-amber-600 font-medium"></span>
) : (
<span></span>
)}
</span>
{batches.length > 0 && (
<span className="text-sm text-gray-500"> {batches.length} </span>
<div className="flex items-start justify-between mb-4 pb-4 border-b border-gray-200">
<div className="flex-1">
{isAdmin && allUsers.length > 0 && (
<div className="mb-3">
<div className="text-xs text-gray-500 mb-2"></div>
<div className="flex flex-wrap gap-2">
{allUsers.map((username) => {
const isSelected = selectedUsers.includes(username)
return (
<button
key={username}
onClick={() => toggleUserFilter(username)}
className={`inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full text-sm font-medium transition-all ${
isSelected
? 'bg-blue-600 text-white shadow-sm'
: 'bg-gray-100 text-gray-700 hover:bg-gray-200'
}`}
>
{username}
</button>
)
})}
{selectedUsers.length > 0 && (
<button
onClick={clearUserFilters}
className="inline-flex items-center gap-1 px-3 py-1.5 rounded-full text-sm font-medium bg-red-50 text-red-600 hover:bg-red-100 transition-all"
>
</button>
)}
</div>
</div>
)}
<div className="flex items-center gap-4">
<span className="text-sm text-gray-600">
{isAdmin ? (
<span className="text-amber-600 font-medium">
{selectedUsers.length > 0
? `已选择 ${selectedUsers.length} 个用户`
: '显示所有用户记录'}
</span>
) : (
<span></span>
)}
</span>
{batches.length > 0 && (
<span className="text-sm text-gray-500"> {batches.length} </span>
)}
</div>
</div>
<button
className="p-2 hover:bg-gray-100 rounded-lg transition-colors disabled:opacity-50"
className="p-2 hover:bg-gray-100 rounded-lg transition-colors disabled:opacity-50 flex-shrink-0"
onClick={() => void fetchBatches()}
disabled={loading}
title="刷新"
@@ -309,17 +380,19 @@ export const ExtractorOperationHistoryModal: React.FC<ExtractorOperationHistoryM
</div>
</div>
<button
className="p-2 hover:bg-red-50 text-gray-400 hover:text-red-600 rounded transition-colors disabled:opacity-50"
onClick={(e) => {
e.stopPropagation()
void handleDeleteBatch(batch.batchId)
}}
disabled={isDeleting}
title="删除批次"
>
<Trash2 size={16} className={isDeleting ? 'animate-pulse' : ''} />
</button>
{isAdmin && (
<button
className="p-2 hover:bg-red-50 text-gray-400 hover:text-red-600 rounded transition-colors disabled:opacity-50"
onClick={(e) => {
e.stopPropagation()
void handleDeleteBatch(batch.batchId)
}}
disabled={isDeleting}
title="删除批次"
>
<Trash2 size={16} className={isDeleting ? 'animate-pulse' : ''} />
</button>
)}
</div>
{/* Batch details */}

View File

@@ -185,7 +185,7 @@ export function AuthenticatedAppShell({
</div>
</div>
)}
{currentPage === 'extractor' && <ExtractorPage />}
{currentPage === 'extractor' && <ExtractorPage currentUser={currentUser} />}
{currentPage === 'cleaner' && <CleanerPage />}
{currentPage === 'settings' && <SettingsPage />}
</main>

View File

@@ -47,7 +47,10 @@ export const ComparisonTooltip = React.memo(
{user || '未分配'}:
</span>
<span className="font-medium text-slate-900">
{firstMetric === 'executionTimeSecs' ? Number(userEntry.value).toFixed(1) : userEntry.value} {firstMetric === 'executionTimeSecs' ? '秒' : ''}
{firstMetric === 'executionTimeSecs'
? Number(userEntry.value).toFixed(1)
: userEntry.value}{' '}
{firstMetric === 'executionTimeSecs' ? '秒' : ''}
</span>
</div>
)

View File

@@ -40,7 +40,10 @@ export const CustomTooltip = React.memo(
{entry.name}:
</span>
<span className="font-medium text-slate-900">
{entry.dataKey === 'executionTimeSecs' ? Number(entry.value).toFixed(1) : entry.value} {entry.dataKey === 'executionTimeSecs' ? '秒' : ''}
{entry.dataKey === 'executionTimeSecs'
? Number(entry.value).toFixed(1)
: entry.value}{' '}
{entry.dataKey === 'executionTimeSecs' ? '秒' : ''}
</span>
</div>
))}

View File

@@ -68,6 +68,10 @@ export function useAppBootstrap() {
const initializeAuth = useCallback(async () => {
logger.info('=== Starting initializeAuth ===')
// Fetch log level early so client-side filtering takes effect
await window.electron.logger.fetchLevel()
try {
logger.debug('Getting computer name...')
const computerNameResult = await window.electron.auth.getComputerName()
@@ -246,12 +250,19 @@ export function useAppBootstrap() {
}, [])
const handleLogout = useCallback(async () => {
// 退出登录,清空后端状态
await window.electron.auth.logout()
// 清空前端状态
setIsAuthenticated(false)
setCurrentUser(null)
setIsSwitchedByAdmin(false)
setShowLoginDialog(true)
}, [])
setShowUserSelection(false)
setShowLoginDialog(false)
// 重新进行静默登录,如果是 Admin 会自动弹出用户选择界面
await initializeAuth()
}, [initializeAuth])
const openUpdateDialog = useCallback(async () => {
await Promise.all([refreshUpdateCatalog(), refreshUpdateState()])

View File

@@ -7,12 +7,28 @@ import { useSharedProductionIds } from '../hooks/useSharedProductionIds'
import LogPanel from '../components/ui/LogPanel'
import { SegmentedProgressBar } from '../components/ui/SegmentedProgressBar'
import ExtractorOperationHistoryModal from '../components/ExtractorOperationHistoryModal'
import { useUserStore } from '../stores/useUserStore'
import type { CurrentUser } from '../hooks/useAppBootstrap'
const ExtractorPage: React.FC = () => {
interface ExtractorPageProps {
currentUser: CurrentUser | null
}
const ExtractorPage: React.FC<ExtractorPageProps> = ({ currentUser }) => {
const [orderNumbers, setOrderNumbers] = usePersistentTextState('extractor_orderNumbers')
const [showHistoryModal, setShowHistoryModal] = React.useState(false)
const user = useUserStore((state) => state.user)
// Convert currentUser to UserInfo format for the modal
const user = React.useMemo(
() =>
currentUser
? {
id: 0, // ID is not needed for modal display logic
username: currentUser.username,
userType: currentUser.userType
}
: null,
[currentUser]
)
const {
isRunning,

View File

@@ -92,6 +92,8 @@ export const IPC_CHANNELS = {
// Logger
LOGGER_FORWARD: 'logger:forward',
LOGGER_GET_LEVEL: 'logger:getLevel',
LOGGER_LEVEL_CHANGED: 'logger:levelChanged',
// Report
REPORT_LIST_ALL: 'report:listAll',
@@ -122,4 +124,4 @@ export const IPC_CHANNELS = {
/**
* Log level for logger service
*/
export type LogLevel = 'debug' | 'info' | 'warn' | 'error'
export type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'verbose'

72
test-output.txt Normal file
View File

@@ -0,0 +1,72 @@
> erpauto@1.8.0 test:run
> vitest run cleaner
 RUN  v4.0.18 D:/FileLib/Projects/CodeMigration/ERPAuto
stdout | tests/unit/cleaner-handler.test.ts
Test suite starting...
stdout | tests/unit/cleaner-helpers.test.ts
Test suite starting...
stdout | tests/unit/cleaner-helpers.test.ts
Test suite completed.
鉁?[39m tests/unit/cleaner-helpers.test.ts (3 tests) 6ms
stdout | tests/unit/cleaner-handler.test.ts
Test suite completed.
鉁?[39m tests/unit/cleaner-handler.test.ts (2 tests) 120ms
stdout | tests/unit/cleaner.test.ts
Test suite starting...
stdout | tests/unit/cleaner.test.ts
Test suite completed.
鉁?[39m tests/unit/cleaner.test.ts (8 tests) 64ms
stdout | tests/integration/cleaner.test.ts
Test suite starting...
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Dry-run mode > should initialize with dry-run mode
Skipping test: ERP credentials not configured
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Dry-run mode > should track materials to delete without actually deleting (dry-run)
Skipping test: ERP credentials not configured
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Order processing > should process single order and return details
Skipping test: ERP credentials not configured
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Order processing > should handle order with "瀹℃壒閫氳繃" status
Skipping test: ERP credentials not configured
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Order processing > should handle multiple orders with progress callback
Skipping test: ERP credentials not configured
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Error handling > should continue processing after order error
Skipping test: ERP credentials not configured
stderr | tests/integration/cleaner.test.ts > Cleaner Service (Integration) > Navigation > should navigate to discrete production order maintenance page
Skipping test: ERP credentials not configured
stdout | tests/integration/cleaner.test.ts
Test suite completed.
鉁?[39m tests/integration/cleaner.test.ts (7 tests) 10ms
stdout | tests/manual/cleaner-slow-motion.test.ts
Test suite starting...
stderr | tests/manual/cleaner-slow-motion.test.ts > Cleaner Slow Motion Test > should run cleaner in slow motion mode
Please set ERP_URL, ERP_USERNAME, ERP_PASSWORD in .env file
stdout | tests/manual/cleaner-slow-motion.test.ts
Test suite completed.
鉁?[39m tests/manual/cleaner-slow-motion.test.ts (1 test) 5ms
 Test Files  5 passed (5)
 Tests  21 passed (21)
 Start at  10:36:50
 Duration  1.50s (transform 729ms, setup 228ms, import 2.41s, tests 204ms, environment 1ms)

View File

@@ -0,0 +1,381 @@
/**
* Performance Monitor Unit Tests
*
* Tests for trackDuration helper and PerformanceTracker class
*/
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest'
import {
trackDuration,
PerformanceTracker,
createPerformanceTracker,
DEFAULT_SLOW_THRESHOLD_MS,
type TrackDurationOptions
} from '../../src/main/services/logger/performance-monitor'
import logger from '../../src/main/services/logger/index'
// Mock the logger to avoid noisy output during tests
vi.mock('../../src/main/services/logger/index', () => ({
default: {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
child: vi.fn().mockReturnThis()
}
}))
describe('Performance Monitor', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.clearAllMocks()
})
describe('trackDuration', () => {
it('should track duration of successful async operation', async () => {
const mockFn = vi.fn().mockResolvedValue('test result')
const result = await trackDuration(mockFn, {
operationName: 'TestOperation'
})
expect(result.result).toBe('test result')
expect(result.durationMs).toBeGreaterThanOrEqual(0)
expect(result.isSlow).toBe(false)
expect(mockFn).toHaveBeenCalledTimes(1)
})
it('should mark operation as slow when exceeding threshold', async () => {
const slowFn = vi
.fn()
.mockImplementation(() => new Promise((resolve) => setTimeout(() => resolve('slow'), 50)))
const result = await trackDuration(slowFn, {
operationName: 'SlowOperation',
slowThresholdMs: 10
})
expect(result.result).toBe('slow')
expect(result.durationMs).toBeGreaterThan(10)
expect(result.isSlow).toBe(true)
})
it('should log with custom log level', async () => {
const mockFn = vi.fn().mockResolvedValue('result')
await trackDuration(mockFn, {
operationName: 'CustomLevelOp',
logLevel: 'info'
})
expect(logger.info).toHaveBeenCalled()
expect(logger.warn).not.toHaveBeenCalled()
})
it('should log warning for slow operations', async () => {
const slowFn = vi
.fn()
.mockImplementation(() => new Promise((resolve) => setTimeout(() => resolve('slow'), 100)))
await trackDuration(slowFn, {
operationName: 'SlowOp',
slowThresholdMs: 50
})
expect(logger.warn).toHaveBeenCalled()
const warnCall = (logger.warn as Mock).mock.calls[0][0]
expect(warnCall).toContain('SLOW')
expect(warnCall).toContain('50ms threshold')
})
it('should include context in logs', async () => {
const mockFn = vi.fn().mockResolvedValue('result')
await trackDuration(mockFn, {
operationName: 'ContextOp',
context: { userId: 123, customField: 'test' }
})
expect(logger.debug).toHaveBeenCalled()
const contextCall = (logger.debug as Mock).mock.calls[0][1]
expect(contextCall).toMatchObject({
operation: 'ContextOp',
userId: 123
})
// Also check the custom field is included
expect(contextCall.customField).toBe('test')
})
it('should log error and duration when operation fails', async () => {
const errorFn = vi.fn().mockRejectedValue(new Error('Test error'))
await expect(
trackDuration(errorFn, {
operationName: 'ErrorOp'
})
).rejects.toThrow('Test error')
expect(logger.error).toHaveBeenCalled()
const errorCall = (logger.error as Mock).mock.calls[0][0]
expect(errorCall).toContain('failed after')
expect(errorCall).toContain('ErrorOp')
})
it('should use custom message in logs', async () => {
const mockFn = vi.fn().mockResolvedValue('result')
await trackDuration(mockFn, {
operationName: 'TestOp',
message: 'Custom message for this operation'
})
expect(logger.debug).toHaveBeenCalled()
const messageCall = (logger.debug as Mock).mock.calls[0][0]
expect(messageCall).toContain('Custom message for this operation')
})
it('should have default threshold of 1000ms', async () => {
const slowFn = vi
.fn()
.mockImplementation(() => new Promise((resolve) => setTimeout(() => resolve('slow'), 100)))
const result = await trackDuration(slowFn, {
operationName: 'DefaultThresholdOp'
})
// 100ms should NOT be slow with default 1000ms threshold
expect(result.isSlow).toBe(false)
expect(result.durationMs).toBeLessThan(1000)
})
})
describe('PerformanceTracker', () => {
it('should track multiple operations', async () => {
const tracker = new PerformanceTracker('TestService', 1000)
const mockFn1 = vi.fn().mockResolvedValue('result1')
const mockFn2 = vi.fn().mockResolvedValue('result2')
const result1 = await tracker.track('Operation1', mockFn1)
const result2 = await tracker.track('Operation2', mockFn2)
expect(result1).toBe('result1')
expect(result2).toBe('result2')
const metrics = tracker.getMetrics()
expect(metrics.count).toBe(2)
expect(metrics.minDurationMs).toBeGreaterThanOrEqual(0)
expect(metrics.maxDurationMs).toBeGreaterThanOrEqual(0)
})
it('should calculate correct metrics', async () => {
const tracker = new PerformanceTracker('TestService', 1000)
// Track operations with known durations
tracker.recordDuration(100)
tracker.recordDuration(200)
tracker.recordDuration(300)
const metrics = tracker.getMetrics()
expect(metrics.count).toBe(3)
expect(metrics.totalDurationMs).toBe(600)
expect(metrics.minDurationMs).toBe(100)
expect(metrics.maxDurationMs).toBe(300)
expect(metrics.avgDurationMs).toBe(200)
})
it('should track slow operations count', async () => {
const tracker = new PerformanceTracker('TestService', 50)
tracker.recordDuration(30) // Normal
tracker.recordDuration(100) // Slow
tracker.recordDuration(40) // Normal
tracker.recordDuration(150) // Slow
const metrics = tracker.getMetrics()
expect(metrics.slowOperationCount).toBe(2)
})
it('should log warnings for slow operations', async () => {
const tracker = new PerformanceTracker('TestService', 10)
const slowFn = vi
.fn()
.mockImplementation(() => new Promise((resolve) => setTimeout(() => resolve('slow'), 50)))
await tracker.track('SlowOp', slowFn)
expect(logger.warn).toHaveBeenCalled()
const warnCall = (logger.warn as Mock).mock.calls[0][0]
expect(warnCall).toContain('[TestService]')
expect(warnCall).toContain('SLOW')
})
it('should include context in operation logs', async () => {
const tracker = new PerformanceTracker('DatabaseService', 1000)
const mockFn = vi.fn().mockResolvedValue('data')
await tracker.track('getUser', mockFn, { userId: 456, table: 'users' })
expect(logger.debug).toHaveBeenCalled()
const contextCall = (logger.debug as Mock).mock.calls[0][1]
expect(contextCall).toMatchObject({
operation: 'getUser',
userId: 456
})
})
it('should log summary with aggregated metrics', () => {
const tracker = new PerformanceTracker('TestService', 1000)
tracker.recordDuration(100)
tracker.recordDuration(200)
tracker.recordDuration(300)
tracker.logSummary('info', 'Test Summary')
expect(logger.info).toHaveBeenCalled()
const summaryCall = (logger.info as Mock).mock.calls[0]
expect(summaryCall[0]).toContain('Test Summary')
expect(summaryCall[1]).toMatchObject({
totalOperations: 3,
slowOperations: 0
})
})
it('should include slow percentage in summary', () => {
const tracker = new PerformanceTracker('TestService', 50)
tracker.recordDuration(30) // Normal
tracker.recordDuration(100) // Slow
tracker.logSummary()
expect(logger.info).toHaveBeenCalled()
const summaryCall = (logger.info as Mock).mock.calls[0][1]
expect(summaryCall.slowPercentage).toContain('%')
})
it('should reset metrics when reset() is called', () => {
const tracker = new PerformanceTracker('TestService', 1000)
tracker.recordDuration(100)
tracker.recordDuration(200)
tracker.reset()
const metrics = tracker.getMetrics()
expect(metrics.count).toBe(0)
expect(metrics.totalDurationMs).toBe(0)
expect(metrics.slowOperationCount).toBe(0)
})
it('should return zero metrics when no operations tracked', () => {
const tracker = new PerformanceTracker('EmptyService')
const metrics = tracker.getMetrics()
expect(metrics.count).toBe(0)
expect(metrics.totalDurationMs).toBe(0)
expect(metrics.minDurationMs).toBe(0)
expect(metrics.maxDurationMs).toBe(0)
expect(metrics.avgDurationMs).toBe(0)
expect(metrics.slowOperationCount).toBe(0)
})
it('should use custom logger if provided', () => {
const customLogger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn()
} as unknown as typeof logger
const tracker = new PerformanceTracker('TestService', 1000, customLogger)
tracker.recordDuration(100)
// Should use custom logger
expect(customLogger.debug).not.toHaveBeenCalled() // We called recordDuration directly
tracker.logSummary()
expect(customLogger.info).toHaveBeenCalled()
})
})
describe('createPerformanceTracker', () => {
it('should create a tracker with default threshold', () => {
const tracker = createPerformanceTracker('MyService')
expect(tracker).toBeInstanceOf(PerformanceTracker)
const metrics = tracker.getMetrics()
expect(metrics.count).toBe(0)
})
it('should create a tracker with custom threshold', () => {
const tracker = createPerformanceTracker('FastService', 100)
tracker.recordDuration(150)
const metrics = tracker.getMetrics()
expect(metrics.slowOperationCount).toBe(1) // 150 > 100
})
it('should use operation name in logs', async () => {
const tracker = createPerformanceTracker('MyCustomService', 1000)
const mockFn = vi.fn().mockResolvedValue('result')
await tracker.track('TestOperation', mockFn)
expect(logger.debug).toHaveBeenCalled()
const call = (logger.debug as Mock).mock.calls[0][0]
expect(call).toContain('[MyCustomService]')
expect(call).toContain('TestOperation')
})
})
describe('Integration scenarios', () => {
it('should track a sequence of operations with varying speeds', async () => {
const tracker = new PerformanceTracker('DataPipeline', 100)
const fastOp = vi.fn().mockResolvedValue('fast')
const mediumOp = vi
.fn()
.mockImplementation(() => new Promise((resolve) => setTimeout(() => resolve('medium'), 50)))
const slowOp = vi
.fn()
.mockImplementation(() => new Promise((resolve) => setTimeout(() => resolve('slow'), 200)))
await tracker.track('FastExtract', fastOp)
await tracker.track('MediumTransform', mediumOp)
await tracker.track('SlowLoad', slowOp)
const metrics = tracker.getMetrics()
expect(metrics.count).toBe(3)
expect(metrics.slowOperationCount).toBe(1) // Only SlowLoad > 100ms
expect(metrics.maxDurationMs).toBeGreaterThan(150)
})
it('should handle errors gracefully in tracker', async () => {
const tracker = new PerformanceTracker('ErrorProneService', 1000)
const errorFn = vi.fn().mockRejectedValue(new Error('Expected error'))
await expect(tracker.track('FailingOp', errorFn)).rejects.toThrow('Expected error')
expect(logger.error).toHaveBeenCalled()
const errorCall = (logger.error as Mock).mock.calls[0][0]
expect(errorCall).toContain('[ErrorProneService]')
expect(errorCall).toContain('failed after')
})
})
describe('Constants', () => {
it('should export DEFAULT_SLOW_THRESHOLD_MS as 1000', () => {
expect(DEFAULT_SLOW_THRESHOLD_MS).toBe(1000)
})
})
})

View File

@@ -92,7 +92,7 @@ describe('Audit Logger - Real File Integration', () => {
const entry = createTestEntry()
await logAudit(entry.action as string, entry.userId as string, {
logAudit(entry.action as string, entry.userId as string, {
username: entry.username as string,
computerName: entry.computerName as string,
resource: entry.resource as string,
@@ -101,7 +101,7 @@ describe('Audit Logger - Real File Integration', () => {
})
// Close logger to flush writes
await closeAuditLogger()
closeAuditLogger()
// Find the audit log file (should be today's file)
const today = new Date().toISOString().split('T')[0]
@@ -122,7 +122,7 @@ describe('Audit Logger - Real File Integration', () => {
await import('../../src/main/services/logger/audit-logger')
// Test success status
await logAudit('EXTRACT', 'user1', {
logAudit('EXTRACT', 'user1', {
username: 'extractor',
computerName: 'PC-001',
resource: 'materials',
@@ -130,7 +130,7 @@ describe('Audit Logger - Real File Integration', () => {
})
// Test failure status
await logAudit('DELETE', 'user2', {
logAudit('DELETE', 'user2', {
username: 'cleaner',
computerName: 'PC-002',
resource: 'temp_files',
@@ -139,7 +139,7 @@ describe('Audit Logger - Real File Integration', () => {
})
// Test partial status
await logAudit('UPDATE', 'user3', {
logAudit('UPDATE', 'user3', {
username: 'updater',
computerName: 'PC-003',
resource: 'config',
@@ -147,7 +147,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: { updated: 5, failed: 2 }
})
await closeAuditLogger()
closeAuditLogger()
// Verify all entries were processed
expect(true).toBe(true) // Logger accepted all status types without error
@@ -158,7 +158,7 @@ describe('Audit Logger - Real File Integration', () => {
await import('../../src/main/services/logger/audit-logger')
// Without metadata
await logAudit('LOGIN', 'user-no-meta', {
logAudit('LOGIN', 'user-no-meta', {
username: 'no.meta',
computerName: 'PC-001',
resource: 'ERP',
@@ -166,7 +166,7 @@ describe('Audit Logger - Real File Integration', () => {
})
// With metadata
await logAudit('LOGOUT', 'user-with-meta', {
logAudit('LOGOUT', 'user-with-meta', {
username: 'with.meta',
computerName: 'PC-002',
resource: 'ERP',
@@ -174,7 +174,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: { sessionDuration: 3600, actionsPerformed: 15 }
})
await closeAuditLogger()
closeAuditLogger()
// Both entries should be processed successfully
expect(true).toBe(true)
@@ -186,14 +186,14 @@ describe('Audit Logger - Real File Integration', () => {
const beforeLog = Date.now()
await logAudit('TEST', 'timestamp-user', {
logAudit('TEST', 'timestamp-user', {
username: 'timestamp.test',
computerName: 'PC-TS',
resource: 'test_resource',
status: 'success'
})
await closeAuditLogger()
closeAuditLogger()
const afterLog = Date.now()
@@ -204,15 +204,15 @@ describe('Audit Logger - Real File Integration', () => {
it('should close audit logger without errors', async () => {
const { closeAuditLogger } = await import('../../src/main/services/logger/audit-logger')
// Should resolve without throwing
await expect(closeAuditLogger()).resolves.toBeUndefined()
// Should complete without throwing
expect(() => closeAuditLogger()).not.toThrow()
})
it('should handle special characters in fields', async () => {
const { logAudit, closeAuditLogger } =
await import('../../src/main/services/logger/audit-logger')
await logAudit('LOGIN_ATTEMPT', 'user-special', {
logAudit('LOGIN_ATTEMPT', 'user-special', {
username: 'user.name+test@example.com',
computerName: 'DESKTOP-特殊字符-001',
resource: 'ERP/子系统',
@@ -220,7 +220,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: { reason: '密码错误', attempt: 3 }
})
await closeAuditLogger()
closeAuditLogger()
// Should handle without errors
expect(true).toBe(true)
@@ -230,7 +230,7 @@ describe('Audit Logger - Real File Integration', () => {
const { logAudit, closeAuditLogger } =
await import('../../src/main/services/logger/audit-logger')
await logAudit('PING', 'ping-user', {
logAudit('PING', 'ping-user', {
username: 'pinger',
computerName: 'PC-PING',
resource: 'health_check',
@@ -238,7 +238,7 @@ describe('Audit Logger - Real File Integration', () => {
metadata: {}
})
await closeAuditLogger()
closeAuditLogger()
// Should handle empty metadata
expect(true).toBe(true)

View File

@@ -0,0 +1,54 @@
/**
* Logger Integration Tests - RequestContext Integration
* Verifies RequestContext is properly integrated with Logger
*/
import { describe, it, expect } from 'vitest'
describe('Logger RequestContext Integration', () => {
it('should export run from request-context', async () => {
const { run } = await import('../../src/main/services/logger/index')
expect(run).toBeDefined()
expect(typeof run).toBe('function')
})
it('should export getRequestId from request-context', async () => {
const { getRequestId } = await import('../../src/main/services/logger/index')
expect(getRequestId).toBeDefined()
expect(typeof getRequestId).toBe('function')
})
it('should export getContext from request-context', async () => {
const { getContext } = await import('../../src/main/services/logger/index')
expect(getContext).toBeDefined()
expect(typeof getContext).toBe('function')
})
it('should export withContext from request-context', async () => {
const { withContext } = await import('../../src/main/services/logger/index')
expect(withContext).toBeDefined()
expect(typeof withContext).toBe('function')
})
it('should export withRequestContext wrapper', async () => {
const { withRequestContext } = await import('../../src/main/services/logger/index')
expect(withRequestContext).toBeDefined()
expect(typeof withRequestContext).toBe('function')
})
it('should export createLogger', async () => {
const { createLogger } = await import('../../src/main/services/logger/index')
expect(createLogger).toBeDefined()
expect(typeof createLogger).toBe('function')
})
it('should have all exports available from LoggerContext type', async () => {
const loggerModule = await import('../../src/main/services/logger/index')
expect(loggerModule.run).toBeDefined()
expect(loggerModule.getRequestId).toBeDefined()
expect(loggerModule.getContext).toBeDefined()
expect(loggerModule.withContext).toBeDefined()
expect(loggerModule.withRequestContext).toBeDefined()
expect(loggerModule.createLogger).toBeDefined()
})
})

View File

@@ -48,34 +48,43 @@ vi.mock('winston', () => {
})
}
const formatFn = vi.fn((fn: any) => fn && fn()) as any
formatFn.combine = vi.fn((...args) => args)
formatFn.timestamp = vi.fn(() => ({ type: 'timestamp' }))
formatFn.colorize = vi.fn(() => ({ type: 'colorize' }))
formatFn.printf = vi.fn((fn: any) => fn)
formatFn.json = vi.fn(() => ({ type: 'json' }))
return {
default: {
createLogger: vi.fn(() => createLoggerInstance),
format: {
combine: vi.fn((...args) => args),
timestamp: vi.fn(() => ({ type: 'timestamp' })),
colorize: vi.fn(() => ({ type: 'colorize' })),
printf: vi.fn((fn) => fn),
json: vi.fn(() => ({ type: 'json' }))
},
format: formatFn,
transports: {
Console: vi.fn()
Console: vi.fn() as any,
DailyRotateFile: vi.fn() as any
}
}
}
})
vi.mock('winston-daily-rotate-file', () => ({
default: vi.fn()
default: vi.fn() as any
}))
vi.mock('electron', () => ({
app: {
isReady: vi.fn(() => false),
getPath: vi.fn(() => './logs'),
isPackaged: false
}
}))
vi.mock(
'electron',
() =>
({
BrowserWindow: {
getAllWindows: vi.fn(() => [])
},
app: {
isReady: vi.fn(() => false),
getPath: vi.fn(() => './logs'),
isPackaged: false
}
}) as any
)
describe('Logger', () => {
beforeEach(() => {

View File

@@ -0,0 +1,432 @@
/**
* RequestContext Unit Tests
*
* Tests for AsyncLocalStorage-based request context management:
* - Context propagation across async/await
* - Concurrent request isolation
* - Nested contexts
* - Non-request scenarios
* - userId and operation fields
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import {
run,
getRequestId,
getContext,
withContext,
LoggerContext
} from '../../src/main/services/logger/request-context'
describe('RequestContext', () => {
beforeEach(() => {
// Clear any existing context before each test
})
afterEach(() => {
// Context is automatically cleaned up when async scope exits
})
describe('run()', () => {
it('should create context with auto-generated requestId', async () => {
let capturedRequestId: string | undefined
await run(async () => {
capturedRequestId = getRequestId()
})
expect(capturedRequestId).toBeDefined()
expect(typeof capturedRequestId).toBe('string')
// UUID v4 format check (basic)
expect(capturedRequestId).toMatch(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
)
})
it('should include userId when provided', async () => {
let context: LoggerContext | undefined
await run(
async () => {
context = getContext()
},
{ userId: 'user-123' }
)
expect(context).toBeDefined()
expect(context?.userId).toBe('user-123')
expect(context?.requestId).toBeDefined()
})
it('should include operation when provided', async () => {
let context: LoggerContext | undefined
await run(
async () => {
context = getContext()
},
{ operation: 'extract' }
)
expect(context).toBeDefined()
expect(context?.operation).toBe('extract')
expect(context?.requestId).toBeDefined()
})
it('should include both userId and operation when provided', async () => {
let context: LoggerContext | undefined
await run(
async () => {
context = getContext()
},
{ userId: 'user-456', operation: 'clean' }
)
expect(context).toBeDefined()
expect(context?.userId).toBe('user-456')
expect(context?.operation).toBe('clean')
expect(context?.requestId).toBeDefined()
})
it('should work without optional context', async () => {
let context: LoggerContext | undefined
await run(async () => {
context = getContext()
})
expect(context).toBeDefined()
expect(context?.requestId).toBeDefined()
expect(context?.userId).toBeUndefined()
expect(context?.operation).toBeUndefined()
})
})
describe('Context Propagation', () => {
it('should propagate context across async/await', async () => {
const requestIds: (string | undefined)[] = []
async function nestedOperation() {
requestIds.push(getRequestId())
await Promise.resolve() // Simulate async operation
requestIds.push(getRequestId())
}
await run(async () => {
requestIds.push(getRequestId())
await nestedOperation()
requestIds.push(getRequestId())
})
// All should have the same requestId
expect(requestIds).toHaveLength(4)
expect(new Set(requestIds).size).toBe(1)
expect(requestIds[0]).toBeDefined()
})
it('should propagate context through Promise.all', async () => {
const requestIds: (string | undefined)[] = []
await run(async () => {
const outerId = getRequestId()
requestIds.push(outerId)
await Promise.all([
(async () => {
requestIds.push(getRequestId())
await Promise.resolve()
requestIds.push(getRequestId())
})(),
(async () => {
requestIds.push(getRequestId())
await Promise.resolve()
requestIds.push(getRequestId())
})()
])
requestIds.push(getRequestId())
})
// All should have the same requestId (6 total: 1 outer + 2 from each parallel + 1 final)
expect(requestIds).toHaveLength(6)
expect(new Set(requestIds).size).toBe(1)
})
})
describe('Concurrent Request Isolation', () => {
it('should maintain separate contexts for concurrent requests', async () => {
const request1Ids: (string | undefined)[] = []
const request2Ids: (string | undefined)[] = []
const promise1 = run(
async () => {
request1Ids.push(getRequestId())
await new Promise((resolve) => setTimeout(resolve, 10))
request1Ids.push(getRequestId())
},
{ userId: 'user-1', operation: 'extract' }
)
const promise2 = run(
async () => {
request2Ids.push(getRequestId())
await new Promise((resolve) => setTimeout(resolve, 5))
request2Ids.push(getRequestId())
},
{ userId: 'user-2', operation: 'clean' }
)
await Promise.all([promise1, promise2])
// Each request should have consistent internal context
expect(request1Ids).toHaveLength(2)
expect(request2Ids).toHaveLength(2)
// promise1 should be consistent
expect(request1Ids[0]).toBe(request1Ids[1])
// promise2 should be consistent
expect(request2Ids[0]).toBe(request2Ids[1])
// Different requests should have different requestIds
expect(request1Ids[0]).not.toBe(request2Ids[0])
})
it('should not leak context between sequential requests', async () => {
let firstRequestId: string | undefined
let secondRequestId: string | undefined
// First request
await run(
async () => {
firstRequestId = getRequestId()
},
{ userId: 'first-user' }
)
// Second request (should have new context)
await run(
async () => {
secondRequestId = getRequestId()
},
{ userId: 'second-user' }
)
expect(firstRequestId).toBeDefined()
expect(secondRequestId).toBeDefined()
expect(firstRequestId).not.toBe(secondRequestId)
// Outside any context, should be undefined
expect(getRequestId()).toBeUndefined()
})
})
describe('Nested Contexts', () => {
it('should support nesting without affecting outer context', async () => {
const requestIds: { outer: string | undefined; inner: string | undefined } = {
outer: undefined,
inner: undefined
}
await run(
async () => {
requestIds.outer = getRequestId()
await run(async () => {
requestIds.inner = getRequestId()
})
// Outer context should be unchanged after nesting
expect(getRequestId()).toBe(requestIds.outer)
},
{ operation: 'outer' }
)
// Both should be defined but different
expect(requestIds.outer).toBeDefined()
expect(requestIds.inner).toBeDefined()
expect(requestIds.outer).not.toBe(requestIds.inner)
})
it('should restore outer context after nested context exits', async () => {
const capturedIds: (string | undefined)[] = []
await run(async () => {
capturedIds.push(getRequestId())
await run(async () => {
capturedIds.push(getRequestId())
})
capturedIds.push(getRequestId())
})
expect(capturedIds).toHaveLength(3)
expect(capturedIds[0]).toBe(capturedIds[2]) // Before and after should match
expect(capturedIds[0]).not.toBe(capturedIds[1]) // Inner should be different
})
})
describe('Non-Request Scenarios', () => {
it('should return undefined requestId outside context', async () => {
const requestId = getRequestId()
expect(requestId).toBeUndefined()
})
it('should return undefined context outside context', async () => {
const context = getContext()
expect(context).toBeUndefined()
})
it('should work normally without RequestContext wrapper', async () => {
// This simulates existing code that doesn't use request context
expect(getRequestId()).toBeUndefined()
const result = await Promise.resolve('test')
expect(result).toBe('test')
// Still undefined after regular async operation
expect(getRequestId()).toBeUndefined()
})
})
describe('withContext()', () => {
it('should override operation in nested context', async () => {
const operations: (string | undefined)[] = []
await run(
async () => {
operations.push(getContext()?.operation)
await withContext(
async () => {
operations.push(getContext()?.operation)
},
{ operation: 'inner-operation' }
)
operations.push(getContext()?.operation)
},
{ operation: 'outer-operation' }
)
expect(operations).toHaveLength(3)
expect(operations[0]).toBe('outer-operation')
expect(operations[1]).toBe('inner-operation')
expect(operations[2]).toBe('outer-operation')
})
it('should override userId in nested context', async () => {
const userIds: (string | undefined)[] = []
await run(
async () => {
userIds.push(getContext()?.userId)
await withContext(
async () => {
userIds.push(getContext()?.userId)
},
{ userId: 'inner-user' }
)
userIds.push(getContext()?.userId)
},
{ userId: 'outer-user' }
)
expect(userIds).toHaveLength(3)
expect(userIds[0]).toBe('outer-user')
expect(userIds[1]).toBe('inner-user')
expect(userIds[2]).toBe('outer-user')
})
it('should create new requestId if no outer context exists', async () => {
let requestIdInWith: string | undefined
await withContext(
async () => {
requestIdInWith = getRequestId()
},
{ operation: 'standalone' }
)
expect(requestIdInWith).toBeDefined()
expect(getContext()).toBeUndefined() // Back to undefined after exiting
})
it('should preserve requestId when overriding other fields', async () => {
const requestIds: (string | undefined)[] = []
await run(
async () => {
requestIds.push(getRequestId())
await withContext(
async () => {
requestIds.push(getRequestId())
},
{ operation: 'new-operation' }
)
requestIds.push(getRequestId())
},
{ userId: 'test-user' }
)
// requestId should remain the same across all scopes
expect(requestIds).toHaveLength(3)
expect(new Set(requestIds).size).toBe(1)
})
})
describe('Edge Cases', () => {
it('should handle errors within context gracefully', async () => {
let caughtRequestId: string | undefined
try {
await run(async () => {
throw new Error('Test error')
})
} catch (error) {
// Error caught, context should be cleaned up
caughtRequestId = getRequestId()
}
expect(caughtRequestId).toBeUndefined()
})
it('should handle context in try-catch-finally', async () => {
const tryId: string | undefined = undefined
const finallyId: string | undefined = undefined
await run(async () => {
try {
const id = getRequestId()
expect(id).toBeDefined()
throw new Error('Test')
} catch {
const id = getRequestId()
expect(id).toBeDefined()
} finally {
const id = getRequestId()
expect(id).toBeDefined()
}
})
})
it('should handle empty string userId and operation', async () => {
let context: LoggerContext | undefined
await run(
async () => {
context = getContext()
},
{ userId: '', operation: '' }
)
expect(context?.userId).toBe('')
expect(context?.operation).toBe('')
expect(context?.requestId).toBeDefined()
})
})
})

View File

@@ -0,0 +1,561 @@
/**
* Tests for Enhanced Error Logging Utilities
*
* Validates error serialization, formatting, and enhanced context support.
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import type { SerializedError } from '../../../../src/main/types/errors'
import {
isError,
serializeError,
sanitizeError,
extractErrorContext,
formatErrorForLogging,
logError,
enhancedLogError,
throwAfterLogging
} from '../../../../src/main/services/logger/error-utils'
import { run, getRequestId } from '../../../../src/main/services/logger/request-context'
// Mock logger for testing
function createMockLogger() {
return {
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
debug: vi.fn()
}
}
// Custom Error class for testing
class CustomError extends Error {
code: string
details?: Record<string, unknown>
constructor(message: string, code: string, details?: Record<string, unknown>) {
super(message)
this.name = 'CustomError'
this.code = code
this.details = details
}
}
describe('error-utils', () => {
describe('isError', () => {
it('should return true for Error instances', () => {
expect(isError(new Error('test'))).toBe(true)
expect(isError(new TypeError('test'))).toBe(true)
expect(isError(new CustomError('test', 'CODE'))).toBe(true)
})
it('should return true for Error-like objects', () => {
expect(isError({ name: 'Error', message: 'test' })).toBe(true)
expect(isError({ name: 'CustomError', message: 'test error' })).toBe(true)
})
it('should return false for non-error values', () => {
expect(isError('string')).toBe(false)
expect(isError(123)).toBe(false)
expect(isError(null)).toBe(false)
expect(isError(undefined)).toBe(false)
expect(isError({})).toBe(false)
expect(isError({ message: 'no name' })).toBe(false)
})
})
describe('serializeError', () => {
it('should serialize standard Error with all properties', () => {
const error = new Error('Test error message')
const serialized = serializeError(error)
expect(serialized).toEqual({
name: 'Error',
message: 'Test error message',
stack: expect.any(String),
cause: undefined
})
expect(serialized.stack).toContain('error-utils.test.ts')
})
it('should serialize custom Error with additional properties', () => {
const error = new CustomError('Custom error', 'CUSTOM_CODE', { userId: '123' })
const serialized = serializeError(error)
expect(serialized).toEqual({
name: 'CustomError',
message: 'Custom error',
stack: expect.any(String),
cause: undefined,
code: 'CUSTOM_CODE',
details: { userId: '123' }
})
})
it('should serialize error with cause', () => {
const cause = new Error('Root cause')
const error = new Error('Wrapped error')
;(error as any).cause = cause
const serialized = serializeError(error)
expect(serialized.cause).toEqual({
name: 'Error',
message: 'Root cause',
stack: expect.any(String)
})
})
it('should serialize Error-like objects', () => {
const errorLike = { name: 'APIError', message: 'API failed' }
const serialized = serializeError(errorLike)
expect(serialized).toEqual({
name: 'APIError',
message: 'API failed',
stack: undefined,
cause: undefined
})
})
it('should serialize non-error values', () => {
const serialized1 = serializeError('String error' as any)
expect(serialized1).toEqual({
name: 'UnknownError',
message: 'String error'
})
const serialized2 = serializeError({ code: 500 })
expect(serialized2).toEqual({
name: 'UnknownError',
message: '{"code":500}'
})
})
})
describe('sanitizeError', () => {
it('should sanitize sensitive fields in error message in production', () => {
const error: SerializedError = {
name: 'AuthError',
message: 'Invalid password provided',
stack: undefined
}
// Note: sanitizeError uses isProduction() from shared module
// In test environment, NODE_ENV='test' which is not production
// So this test verifies the message is kept in tests/non-prod
const sanitized = sanitizeError(error)
expect(sanitized.message).toBe('Invalid password provided')
expect(sanitized.name).toBe('AuthError')
})
it('should sanitize sensitive custom properties', () => {
const error: SerializedError = {
name: 'ConfigError',
message: 'Config failed',
apiKey: 'secret-key-123',
token: 'bearer-token'
}
const sanitized = sanitizeError(error)
// sanitizeError only sanitizes properties that contain sensitive key names
// It checks message content and property keys, but only for specific patterns
expect(sanitized.message).toBe('Config failed')
expect(sanitized.name).toBe('ConfigError')
// Note: apiKey and token are NOT sanitized by default - only 'password', 'secret', etc.
// The sanitization is based on key name matching, not automatic for all custom props
})
it('should sanitize custom properties by key name pattern', () => {
const error: SerializedError = {
name: 'ConfigError',
message: 'Config failed',
password: 'secret123',
secretKey: 'my-secret'
}
const sanitized = sanitizeError(error)
expect(sanitized.password).toBe('[REDACTED]')
expect(sanitized.secretKey).toBe('[REDACTED]')
})
it('should recursively sanitize cause', () => {
const error: SerializedError = {
name: 'ChainError',
message: 'Error chain',
cause: {
name: 'AuthError',
message: 'Invalid password',
password: 'secret123'
} as any
}
const sanitized = sanitizeError(error)
expect((sanitized.cause as any).password).toBe('[REDACTED]')
})
})
describe('extractErrorContext', () => {
it('should extract file, line, column from stack trace', () => {
const error = new Error('Test')
const serialized = serializeError(error)
const context = extractErrorContext(serialized)
expect(context.fileName).toBeDefined()
expect(context.lineNumber).toBeDefined()
expect(context.columnName).toBeDefined()
expect(context.fileName).toContain('error-utils.test.ts')
})
it('should return empty object when no stack trace', () => {
const serialized: SerializedError = {
name: 'Error',
message: 'Test',
stack: undefined
}
const context = extractErrorContext(serialized)
expect(context).toEqual({})
})
})
describe('formatErrorForLogging', () => {
it('should format error with basic metadata', () => {
const error = new Error('Basic error')
const { message, metadata } = formatErrorForLogging(error)
expect(message).toBe('[Error] Basic error')
expect(metadata.error).toBeDefined()
expect((metadata.error as SerializedError).name).toBe('Error')
})
it('should include context fields in metadata', () => {
const error = new Error('Context error')
const { metadata } = formatErrorForLogging(error, {
operation: 'extract',
userId: 'user123',
batchId: 'batch-001'
})
expect(metadata.operation).toBe('extract')
expect(metadata.userId).toBe('user123')
expect(metadata.batchId).toBe('batch-001')
})
it('should auto-inject requestId from async context', async () => {
await run(
async () => {
const requestId = getRequestId()
expect(requestId).toBeDefined()
const error = new Error('Contextual error')
const { metadata } = formatErrorForLogging(error, {
operation: 'validate'
})
expect(metadata.requestId).toBe(requestId)
},
{ operation: 'validate' }
)
})
it('should use explicit requestId if provided', async () => {
await run(
async () => {
const error = new Error('Test error')
const { metadata } = formatErrorForLogging(error, {
requestId: 'explicit-request-id',
operation: 'test'
})
expect(metadata.requestId).toBe('explicit-request-id')
},
{ operation: 'test' }
)
})
it('should include duration when provided', () => {
const error = new Error('Slow operation')
const { metadata } = formatErrorForLogging(error, {
operation: 'extract',
duration: 2500
})
expect(metadata.duration).toBe(2500)
})
it('should include orderNumbers and materialCodes when provided', () => {
const error = new Error('Processing error')
const { metadata } = formatErrorForLogging(error, {
operation: 'clean',
orderNumbers: ['ORD-001', 'ORD-002'],
materialCodes: ['MAT-100', 'MAT-101']
})
expect(metadata.orderNumbers).toEqual(['ORD-001', 'ORD-002'])
expect(metadata.materialCodes).toEqual(['MAT-100', 'MAT-101'])
})
it('should handle environment-specific formatting', () => {
const error = new Error('Environment test')
const { metadata } = formatErrorForLogging(error)
if (process.env.NODE_ENV === 'production') {
expect(metadata.environment).toBeUndefined()
} else {
expect(metadata.environment).toEqual({
NODE_ENV: expect.any(String),
platform: expect.any(String),
nodeVersion: expect.any(String)
})
}
})
it('should remove undefined fields from metadata', () => {
const error = new Error('Test')
const { metadata } = formatErrorForLogging(error, {
operation: 'test',
batchId: undefined as any
})
expect(metadata.batchId).toBeUndefined()
expect(metadata.operation).toBe('test')
})
})
describe('logError', () => {
let logger: ReturnType<typeof createMockLogger>
beforeEach(() => {
logger = createMockLogger()
})
afterEach(() => {
vi.clearAllMocks()
})
it('should log error with message and metadata', () => {
const error = new Error('Log test')
logError(logger, error, {
message: 'Custom message',
operation: 'test'
})
expect(logger.error).toHaveBeenCalledWith(
'Custom message',
expect.objectContaining({
error: expect.any(Object),
operation: 'test'
})
)
})
it('should use error message if no custom message provided', () => {
const error = new Error('Auto message')
logError(logger, error, { operation: 'test' })
expect(logger.error).toHaveBeenCalledWith(
expect.stringContaining('[Error] Auto message'),
expect.any(Object)
)
})
it('should log with all enhanced context fields', () => {
const error = new Error('Full context error')
logError(logger, error, {
operation: 'extract',
userId: 'user789',
batchId: 'batch-999',
duration: 3500,
module: 'ExtractorService'
})
const callArgs = logger.error.mock.calls[0]
expect(callArgs[1]).toEqual(
expect.objectContaining({
operation: 'extract',
userId: 'user789',
batchId: 'batch-999',
duration: 3500,
module: 'ExtractorService'
})
)
})
it('should auto-inject requestId from context', async () => {
await run(
async () => {
const requestId = getRequestId()
expect(requestId).toBeDefined()
const error = new Error('Contextual log')
const { metadata } = formatErrorForLogging(error, { operation: 'test' })
// The requestId should be auto-injected from async context
expect(metadata.requestId).toBe(requestId)
},
{ operation: 'test' }
)
})
})
describe('enhancedLogError', () => {
let logger: ReturnType<typeof createMockLogger>
beforeEach(() => {
logger = createMockLogger()
})
afterEach(() => {
vi.clearAllMocks()
})
it('should log error with required operation field', () => {
const error = new Error('Enhanced error')
enhancedLogError(logger, error, {
operation: 'validate'
})
expect(logger.error).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
operation: 'validate'
})
)
})
it('should log with userId and batchId', () => {
const error = new Error('Batch error')
enhancedLogError(logger, error, {
operation: 'extract',
userId: 'user-enhanced',
batchId: 'batch-enhanced'
})
const callArgs = logger.error.mock.calls[0]
expect(callArgs[1]).toEqual(
expect.objectContaining({
operation: 'extract',
userId: 'user-enhanced',
batchId: 'batch-enhanced'
})
)
})
it('should log with duration (performance metric)', () => {
const error = new Error('Slow error')
enhancedLogError(logger, error, {
operation: 'clean',
duration: 5000
})
expect(logger.error).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
operation: 'clean',
duration: 5000
})
)
})
it('should log with orderNumbers and materialCodes', () => {
const error = new Error('Order error')
enhancedLogError(logger, error, {
operation: 'process',
orderNumbers: ['ORD-ENH-001'],
materialCodes: ['MAT-ENH-100']
})
const callArgs = logger.error.mock.calls[0]
expect(callArgs[1]).toEqual(
expect.objectContaining({
operation: 'process',
orderNumbers: ['ORD-ENH-001'],
materialCodes: ['MAT-ENH-100']
})
)
})
it('should accept custom message', () => {
const error = new Error('Original message')
enhancedLogError(
logger,
error,
{
operation: 'test'
},
'Custom enhanced message'
)
expect(logger.error).toHaveBeenCalledWith('Custom enhanced message', expect.any(Object))
})
it('should auto-inject requestId from async context', async () => {
await run(
async () => {
const requestId = getRequestId()
expect(requestId).toBeDefined()
const error = new Error('Auto-inject test')
const { metadata } = formatErrorForLogging(error, { operation: 'auto-test' })
expect(metadata.requestId).toBe(requestId)
},
{ operation: 'auto-test' }
)
})
})
describe('throwAfterLogging', () => {
it('should log error and re-throw', () => {
const logger = createMockLogger()
const error = new Error('Re-throw test')
expect(() => {
throwAfterLogging(logger, error, {
operation: 'throw-test'
})
}).toThrow('Re-throw test')
expect(logger.error).toHaveBeenCalled()
})
})
describe('Backward Compatibility', () => {
it('should work without context parameter', () => {
const error = new Error('No context')
const { message, metadata } = formatErrorForLogging(error)
expect(message).toBe('[Error] No context')
expect(metadata.error).toBeDefined()
})
it('should work with minimal context', () => {
const error = new Error('Minimal context')
const logger = createMockLogger()
logError(logger, error, { userId: 'minimal-user' })
expect(logger.error).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
userId: 'minimal-user'
})
)
})
it('should not break existing error logging patterns', () => {
const error = new Error('Old pattern')
const { message, metadata } = formatErrorForLogging(error, {
operation: 'legacy',
module: 'LegacyModule'
})
expect(message).toContain('[Error] Old pattern')
expect(metadata.operation).toBe('legacy')
expect(metadata.module).toBe('LegacyModule')
})
})
})