Compare commits
46 Commits
v1.6.0
...
6e431bc37e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e431bc37e | ||
|
|
fe02e37848 | ||
|
|
528a8157ff | ||
|
|
a5c4639392 | ||
|
|
4f3af2e9c3 | ||
|
|
7f38150d0a | ||
|
|
8be5a2763d | ||
|
|
3d5adb74d8 | ||
|
|
1e0bb1de24 | ||
|
|
fce8dbc37f | ||
|
|
c8783a2cef | ||
|
|
219d8ab752 | ||
|
|
12a17eccb7 | ||
|
|
018d524fe8 | ||
|
|
42b76c4de5 | ||
|
|
cfb80376ce | ||
|
|
78a3066904 | ||
|
|
24d9bfebaf | ||
|
|
ba436cf374 | ||
|
|
6413eef5b8 | ||
|
|
6a9d144bbc | ||
|
|
a2e3681c8f | ||
|
|
21359b31c6 | ||
|
|
0a1181fecd | ||
|
|
883f98065a | ||
|
|
020bbcdccc | ||
|
|
63a292c5f9 | ||
|
|
51f8e0a6e7 | ||
|
|
c8ab58d390 | ||
|
|
811361a1a3 | ||
|
|
ffbda4c618 | ||
|
|
348b02600d | ||
|
|
d004f8e9f8 | ||
|
|
3cbe9eef12 | ||
|
|
5b310d944b | ||
|
|
6e04f21b10 | ||
|
|
17fbd7d251 | ||
|
|
c6eb60ada7 | ||
|
|
571ec2325f | ||
|
|
557ed174c3 | ||
|
|
dd2cf1c576 | ||
|
|
b7e9e5e472 | ||
|
|
491f2afe3f | ||
|
|
6b2a3b088f | ||
|
|
82a6e24132 | ||
|
|
4a1a78ee14 |
@@ -40,6 +40,7 @@ extraction:
|
||||
autoConvert: true
|
||||
mergeBatches: true
|
||||
enableDbPersistence: true
|
||||
headless: true # 浏览器无头模式,true=后台运行,false=显示浏览器窗口(调试用)
|
||||
|
||||
validation:
|
||||
dataSource: database_full
|
||||
@@ -62,6 +63,16 @@ logging:
|
||||
auditRetention: 30
|
||||
appRetention: 14
|
||||
|
||||
# Seq 日志聚合服务配置(可选,用于集中管理日志)
|
||||
seq:
|
||||
enabled: false # 设置为 true 启用 Seq 日志发送
|
||||
serverUrl: 'http://localhost:5341' # Seq 服务器地址
|
||||
apiKey: '' # 可选:API key 用于认证
|
||||
batchPostingLimit: 50 # 每批次最大日志条目数
|
||||
period: 2000 # 发送间隔 (毫秒)
|
||||
queueLimit: 10000 # 本地队列最大容量
|
||||
maxRetries: 3 # 失败重试次数
|
||||
|
||||
# RustFS 对象存储配置(用于持久化报告)
|
||||
rustfs:
|
||||
enabled: false # 设置为 true 启用 RustFS 上传
|
||||
|
||||
547
docs/LOGGING_GUIDE.md
Normal file
547
docs/LOGGING_GUIDE.md
Normal 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_
|
||||
752
docs/LOGGING_IMPLEMENTATION.md
Normal file
752
docs/LOGGING_IMPLEMENTATION.md
Normal 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_
|
||||
654
docs/TEST_REVIEW_REPORT.md
Normal file
654
docs/TEST_REVIEW_REPORT.md
Normal file
@@ -0,0 +1,654 @@
|
||||
# ERPAuto 测试实现审查报告
|
||||
|
||||
**审查日期**: 2026 年 4 月 4 日
|
||||
**审查范围**: 单元测试、集成测试、E2E 测试
|
||||
**审查人**: Sisyphus AI Agent
|
||||
|
||||
---
|
||||
|
||||
## 📊 执行摘要
|
||||
|
||||
### 测试架构概览
|
||||
|
||||
| 维度 | 详情 |
|
||||
| ---------------- | ---------------------------------------------- |
|
||||
| **测试框架** | Vitest 4.0.18 + Playwright Test 1.58.2 |
|
||||
| **测试文件总数** | 44 个 (31 单元 + 7 集成 + 3 E2E + 3 调试/手动) |
|
||||
| **测试用例总数** | ~300 个 |
|
||||
| **当前通过率** | ~67% (约 200 通过 / 48 失败) |
|
||||
| **测试覆盖率** | 未配置阈值 |
|
||||
|
||||
### 测试结果摘要
|
||||
|
||||
```
|
||||
✅ 通过测试:~200 个
|
||||
❌ 失败套件:20 个
|
||||
❌ 失败用例:28 个
|
||||
⚠️ 空测试文件:17 个
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📁 测试文件组织
|
||||
|
||||
```
|
||||
tests/
|
||||
├── setup.ts # 全局 Setup (Electron Mock)
|
||||
├── fixtures/
|
||||
│ ├── create-fixtures.ts # Excel 测试数据生成器
|
||||
│ ├── test-export.xlsx # 生成的测试数据
|
||||
│ └── test-empty-orders.xlsx # 空数据夹具
|
||||
├── unit/ # 31 个单元测试文件
|
||||
│ ├── services/
|
||||
│ │ ├── erp/ # ERP 服务测试
|
||||
│ │ │ ├── page-diagnostics.test.ts
|
||||
│ │ │ └── erp-error-context.test.ts
|
||||
│ │ └── logger/
|
||||
│ │ └── error-utils.test.ts # ✅ 优秀测试示例
|
||||
│ ├── errors.test.ts # ✅ 错误类型测试
|
||||
│ ├── request-context.test.ts # ✅ 请求上下文测试 (432 行)
|
||||
│ ├── schemas.test.ts # ✅ Zod Schema 验证
|
||||
│ ├── repositories.test.ts # ❌ 数据库 Repository 测试 (失败)
|
||||
│ ├── mysql.test.ts # ❌ MySQL 单元测试 (失败)
|
||||
│ ├── sql-server.test.ts # ❌ SQL Server 测试 (失败)
|
||||
│ ├── extractor.test.ts # ❌ 提取器测试 (失败)
|
||||
│ ├── cleaner*.test.ts # ❌ 清理器测试 (3 个文件,失败)
|
||||
│ ├── update-*.test.ts # ❌ 更新服务测试 (5 个文件,部分失败)
|
||||
│ ├── logger*.test.ts # ❌ Logger 测试 (3 个文件,部分失败)
|
||||
│ ├── auth-handler.test.ts # ✅ IPC Handler 测试
|
||||
│ ├── excel-parser.test.ts # ❌ Excel 解析测试 (失败)
|
||||
│ ├── use-*.test.ts # ✅ React Hooks 测试 (2 个文件)
|
||||
│ └── ... # 其他服务测试
|
||||
├── integration/ # 7 个集成测试文件
|
||||
│ ├── cleaner.test.ts # ❌ 真实 ERP 集成 (0 测试)
|
||||
│ ├── extractor.test.ts # ❌ 提取器集成 (0 测试)
|
||||
│ ├── erp-auth.test.ts # ❌ 认证集成 (0 测试)
|
||||
│ ├── mysql.test.ts # ❌ MySQL 集成 (0 测试)
|
||||
│ ├── sql-server.test.ts # ❌ SQL Server 集成 (0 测试)
|
||||
│ ├── ipc-logging.test.ts # ❌ IPC 日志集成 (0 测试)
|
||||
│ └── logger-performance.test.ts # ✅ 日志性能测试 (24 测试)
|
||||
├── e2e/ # 3 个 E2E 测试文件
|
||||
│ ├── auth-flow.test.ts # 登录/登出流程
|
||||
│ ├── dialog-focus.test.ts # 对话框焦点管理
|
||||
│ └── extractor-workflow.test.ts # 完整提取工作流
|
||||
├── debug/ # 调试测试
|
||||
│ └── env.test.ts # 环境变量测试 (1 失败)
|
||||
└── manual/ # 手动测试脚本
|
||||
├── excel-parser-test.ts # Excel 解析手动测试
|
||||
└── ... # 临时调试脚本
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🐛 关键问题诊断
|
||||
|
||||
### P0 - 严重问题 (导致 20 个套件失败)
|
||||
|
||||
#### 问题 1: Electron Mock 不完整
|
||||
|
||||
**文件**: `tests/setup.ts`
|
||||
|
||||
**当前 Mock**:
|
||||
|
||||
```typescript
|
||||
vi.mock('electron', () => ({
|
||||
app: {
|
||||
isPackaged: false,
|
||||
isReady: vi.fn().mockReturnValue(false),
|
||||
getPath: vi.fn().mockReturnValue(path.join(process.cwd(), 'logs')),
|
||||
on: vi.fn()
|
||||
}
|
||||
}))
|
||||
```
|
||||
|
||||
**缺失方法**:
|
||||
|
||||
- `getVersion()` - 导致 20 个套件失败
|
||||
- `getName()`
|
||||
- `getAppPath()`
|
||||
- `getVersion()` 在以下位置被调用:
|
||||
- `src/main/services/logger/index.ts:220`
|
||||
- `src/main/services/erp/cleaner.ts`
|
||||
- `src/main/services/erp/extractor.ts`
|
||||
- `src/main/services/erp/erp-auth.ts`
|
||||
- `src/main/services/database/mysql.ts`
|
||||
- `src/main/services/database/sql-server.ts`
|
||||
- `src/main/ipc/file-handler.ts`
|
||||
- `src/main/ipc/logger-handler.ts`
|
||||
- `src/main/services/excel/excel-parser.ts`
|
||||
- `src/main/services/config/config-manager.ts`
|
||||
- `src/main/services/update/*.ts`
|
||||
|
||||
**影响范围**: 所有导入 logger 或依赖 Electron app API 的模块
|
||||
|
||||
**修复方案**:
|
||||
|
||||
```typescript
|
||||
vi.mock('electron', () => ({
|
||||
app: {
|
||||
isPackaged: false,
|
||||
isReady: vi.fn().mockReturnValue(false),
|
||||
getPath: vi.fn().mockImplementation((name) => {
|
||||
switch (name) {
|
||||
case 'userData':
|
||||
return 'D:/test-user-data'
|
||||
case 'logs':
|
||||
return path.join(process.cwd(), 'test-logs')
|
||||
default:
|
||||
return '/tmp'
|
||||
}
|
||||
}),
|
||||
getVersion: vi.fn(() => '1.9.0-test'),
|
||||
getName: vi.fn(() => 'ERPAuto'),
|
||||
getAppPath: vi.fn(() => '/tmp/erpauto'),
|
||||
on: vi.fn(),
|
||||
isDefaultProtocolClient: vi.fn(() => true)
|
||||
},
|
||||
ipcMain: {
|
||||
handle: vi.fn(),
|
||||
on: vi.fn(),
|
||||
removeHandler: vi.fn(),
|
||||
removeListener: vi.fn()
|
||||
},
|
||||
dialog: {
|
||||
showErrorBox: vi.fn(),
|
||||
showMessageBox: vi.fn()
|
||||
},
|
||||
BrowserWindow: {
|
||||
getAllWindows: vi.fn(() => []),
|
||||
fromWebContents: vi.fn(() => null)
|
||||
}
|
||||
}))
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### 问题 2: Winston Logger Mock 不完整
|
||||
|
||||
**文件**: `tests/unit/logger.test.ts`
|
||||
|
||||
**问题代码**:
|
||||
|
||||
```typescript
|
||||
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)
|
||||
```
|
||||
|
||||
**问题**: `format().combine().timestamp().printf()` 链式调用失败
|
||||
|
||||
**修复方案**:
|
||||
|
||||
```typescript
|
||||
const createFormatFn = () => {
|
||||
const formatFn = vi.fn((fn) => fn) as any
|
||||
formatFn.combine = vi.fn((...args) => createFormatFn())
|
||||
formatFn.timestamp = vi.fn(() => createFormatFn())
|
||||
formatFn.colorize = vi.fn(() => createFormatFn())
|
||||
formatFn.printf = vi.fn((fn) => fn)
|
||||
formatFn.json = vi.fn(() => createFormatFn())
|
||||
formatFn.errors = vi.fn(() => createFormatFn())
|
||||
return formatFn
|
||||
}
|
||||
|
||||
const format = createFormatFn()
|
||||
|
||||
vi.mock('winston', () => ({
|
||||
default: {
|
||||
format,
|
||||
createLogger: vi.fn(() => createLoggerInstance),
|
||||
transports: {
|
||||
Console: vi.fn(),
|
||||
DailyRotateFile: vi.fn()
|
||||
}
|
||||
}
|
||||
}))
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### P1 - 高优先级问题
|
||||
|
||||
#### 问题 3: 环境变量测试失败
|
||||
|
||||
**文件**: `tests/debug/env.test.ts`
|
||||
|
||||
**失败原因**: `.env` 文件缺少 ERP 凭据配置
|
||||
|
||||
**当前状态**:
|
||||
|
||||
```
|
||||
process.cwd(): D:\FileLib\Projects\CodeMigration\ERPAuto
|
||||
ERP_URL: (NOT SET)
|
||||
ERP_USERNAME: (NOT SET)
|
||||
ERP_PASSWORD: (NOT SET)
|
||||
Has Credentials: false
|
||||
```
|
||||
|
||||
**修复方案**: 创建 `tests/.env.test` 文件
|
||||
|
||||
```env
|
||||
# Test Environment Configuration
|
||||
ERP_URL=https://erp-test.example.com
|
||||
ERP_USERNAME=test_user
|
||||
ERP_PASSWORD=test_password
|
||||
|
||||
# Database Test Configuration
|
||||
MYSQL_HOST=localhost
|
||||
MYSQL_PORT=3306
|
||||
MYSQL_DATABASE=erpauto_test
|
||||
MYSQL_USERNAME=test
|
||||
MYSQL_PASSWORD=test
|
||||
|
||||
SQLSERVER_SERVER=localhost
|
||||
SQLSERVER_PORT=1433
|
||||
SQLSERVER_DATABASE=erpauto_test
|
||||
SQLSERVER_USERNAME=test
|
||||
SQLSERVER_PASSWORD=test
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### 问题 4: 空测试文件 (17 个)
|
||||
|
||||
**单元测试 (8 个)**:
|
||||
|
||||
- `tests/unit/cleaner.test.ts`
|
||||
- `tests/unit/extractor.test.ts`
|
||||
- `tests/unit/excel-parser.test.ts`
|
||||
- `tests/unit/mysql.test.ts`
|
||||
- `tests/unit/sql-server.test.ts`
|
||||
- `tests/unit/data-importer.test.ts`
|
||||
- `tests/unit/ipc-index.test.ts`
|
||||
- `tests/unit/file-ipc-paths.test.ts`
|
||||
|
||||
**集成测试 (6 个)**:
|
||||
|
||||
- `tests/integration/cleaner.test.ts`
|
||||
- `tests/integration/extractor.test.ts`
|
||||
- `tests/integration/erp-auth.test.ts`
|
||||
- `tests/integration/mysql.test.ts`
|
||||
- `tests/integration/sql-server.test.ts`
|
||||
- `tests/integration/ipc-logging.test.ts`
|
||||
|
||||
**其他 (3 个)**:
|
||||
|
||||
- `tests/unit/update-catalog-service.test.ts`
|
||||
- `tests/unit/update-installer.test.ts`
|
||||
- `tests/unit/production-input-service.test.ts`
|
||||
|
||||
**影响**: 测试覆盖率为 0%,这些模块无自动化测试保护
|
||||
|
||||
---
|
||||
|
||||
#### 问题 5: E2E 测试覆盖不足
|
||||
|
||||
**当前状态**: 仅 3 个 E2E 测试文件
|
||||
|
||||
- `auth-flow.test.ts` - 登录流程
|
||||
- `dialog-focus.test.ts` - 对话框焦点
|
||||
- `extractor-workflow.test.ts` - 提取工作流
|
||||
|
||||
**缺失覆盖**:
|
||||
|
||||
- 物料清理工作流
|
||||
- 配置管理
|
||||
- 用户管理
|
||||
- 错误处理流程
|
||||
- 更新功能
|
||||
|
||||
---
|
||||
|
||||
### P2 - 中等优先级问题
|
||||
|
||||
#### 问题 6: 错误处理函数行为变更
|
||||
|
||||
**文件**: `tests/unit/errors.test.ts`
|
||||
|
||||
**失败测试**:
|
||||
|
||||
```typescript
|
||||
it('getErrorMessage should handle unknown types', () => {
|
||||
expect(getErrorMessage('string error')).toBe('string error')
|
||||
// 失败:实际返回 'An unknown error occurred'
|
||||
})
|
||||
```
|
||||
|
||||
**根因**: `getErrorMessage` 实现逻辑变更,测试未同步更新
|
||||
|
||||
---
|
||||
|
||||
#### 问题 7: 缺少测试数据工厂
|
||||
|
||||
**当前状态**: 测试数据分散在各测试文件中
|
||||
|
||||
- 无中央测试数据工厂
|
||||
- 重复的测试数据创建逻辑
|
||||
- 测试数据一致性难以保证
|
||||
|
||||
**建议**: 创建 `tests/fixtures/factories.ts`
|
||||
|
||||
```typescript
|
||||
export function createMockUser(overrides = {}) {
|
||||
return {
|
||||
id: 'user-' + Math.random().toString(36).substr(2, 9),
|
||||
username: 'test_user',
|
||||
role: 'User',
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
export function createMockOrder(overrides = {}) {
|
||||
return {
|
||||
orderNumber: 'ORD-' + Date.now(),
|
||||
materialCodes: ['MAT-001', 'MAT-002'],
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ✅ 优秀测试实践
|
||||
|
||||
### 1. Request Context 测试 (request-context.test.ts)
|
||||
|
||||
**特点**:
|
||||
|
||||
- 432 行完整的 AsyncLocalStorage 测试
|
||||
- 覆盖所有边界情况
|
||||
- 良好的测试分组和命名
|
||||
- 包含并发请求隔离测试
|
||||
|
||||
**值得学习**:
|
||||
|
||||
```typescript
|
||||
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])
|
||||
|
||||
// 验证隔离性
|
||||
expect(request1Ids[0]).not.toBe(request2Ids[0])
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. Error Utils 测试 (error-utils.test.ts)
|
||||
|
||||
**特点**:
|
||||
|
||||
- 561 行完整的错误处理测试
|
||||
- 覆盖序列化、清理、格式化
|
||||
- 包含 requestId 自动注入测试
|
||||
- 良好的 backward compatibility 测试
|
||||
|
||||
**值得学习**:
|
||||
|
||||
```typescript
|
||||
describe('sanitizeError', () => {
|
||||
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]')
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 集成测试可用性检查模式
|
||||
|
||||
**特点**: 优雅处理外部依赖缺失
|
||||
|
||||
```typescript
|
||||
const hasCredentials = !!(config.url && config.username && config.password)
|
||||
|
||||
beforeAll(() => {
|
||||
if (!hasCredentials) {
|
||||
console.warn('Skipping ERP auth tests: credentials not configured')
|
||||
return
|
||||
}
|
||||
authService = new ErpAuthService(config)
|
||||
})
|
||||
|
||||
it('should login successfully', async () => {
|
||||
if (!hasCredentials) {
|
||||
console.warn('Skipping test: ERP credentials not configured')
|
||||
return
|
||||
}
|
||||
const session = await authService.login()
|
||||
expect(session.isLoggedIn).toBe(true)
|
||||
}, 30000)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📈 测试质量评估
|
||||
|
||||
### 测试覆盖率分析
|
||||
|
||||
| 模块类型 | 文件数 | 有测试 | 测试质量 | 覆盖率估计 |
|
||||
| --------------- | ------ | ------ | -------- | ---------- |
|
||||
| **服务层** | ~15 | 8 | 中 | ~40% |
|
||||
| **数据库** | 4 | 0 | 无 | 0% |
|
||||
| **IPC** | ~10 | 2 | 中 | ~20% |
|
||||
| **工具类** | ~8 | 6 | 高 | ~80% |
|
||||
| **React Hooks** | ~5 | 2 | 中 | ~40% |
|
||||
| **E2E 场景** | N/A | 3 | 中 | ~15% |
|
||||
|
||||
### 测试健康状况
|
||||
|
||||
| 指标 | 状态 | 目标 |
|
||||
| ----------- | ------ | ---- |
|
||||
| 套件通过率 | 55% | 100% |
|
||||
| 用例通过率 | 67% | 95%+ |
|
||||
| 空测试文件 | 17 个 | 0 个 |
|
||||
| Mock 完整性 | 中 | 高 |
|
||||
| E2E 覆盖 | 低 | 中 |
|
||||
| 覆盖率阈值 | 无配置 | 70%+ |
|
||||
|
||||
---
|
||||
|
||||
## 🎯 改进计划
|
||||
|
||||
改进计划详情请参阅:[docs/test-improvement-plan.md](./test-improvement-plan.md)
|
||||
|
||||
### 阶段 1: 立即修复 (第 1-2 周) - P0
|
||||
|
||||
| 任务 | 描述 | 预计工时 | 成功标准 |
|
||||
| ---- | ------------------ | -------- | ------------------- |
|
||||
| 1.1 | 完成 Electron Mock | 2h | 20 个套件全部通过 |
|
||||
| 1.2 | 修复 Winston Mock | 2h | Logger 测试全部通过 |
|
||||
| 1.3 | 创建测试环境配置 | 1h | 环境测试通过 |
|
||||
|
||||
**预期结果**: 消除全部 48 个失败,通过率提升至 100%
|
||||
|
||||
---
|
||||
|
||||
### 阶段 2: 短期改进 (第 3-6 周) - P1
|
||||
|
||||
| 任务 | 描述 | 预计工时 | 成功标准 |
|
||||
| ---- | ----------------------- | -------- | ----------------- |
|
||||
| 2.1 | 填充单元测试 (8 个文件) | 16h | 新增 50+ 测试用例 |
|
||||
| 2.2 | 完成集成测试 (6 个文件) | 12h | 新增 30+ 测试用例 |
|
||||
| 2.3 | 修复 28 个现有失败用例 | 8h | 用例通过率 100% |
|
||||
|
||||
**预期结果**: 测试用例总数达 380+,关键模块覆盖率达 80%
|
||||
|
||||
---
|
||||
|
||||
### 阶段 3: 中期目标 (第 2-3 月) - P2
|
||||
|
||||
| 任务 | 描述 | 预计工时 | 成功标准 |
|
||||
| ---- | ------------------------ | -------- | ------------------ |
|
||||
| 3.1 | E2E 覆盖扩展至 12 个文件 | 20h | 50+ E2E 测试用例 |
|
||||
| 3.2 | 创建测试数据工厂 | 8h | 统一测试数据创建 |
|
||||
| 3.3 | 测试覆盖率阈值配置 | 4h | 70% 全局,80% 关键 |
|
||||
|
||||
**预期结果**: E2E 覆盖关键用户旅程,覆盖率达标
|
||||
|
||||
---
|
||||
|
||||
### 阶段 4: 长期战略 (第 4-6 月) - P3
|
||||
|
||||
| 任务 | 描述 | 预计工时 | 成功标准 |
|
||||
| ---- | ---------------------- | -------- | --------------- |
|
||||
| 4.1 | GitHub Actions CI 集成 | 8h | PR 自动运行测试 |
|
||||
| 4.2 | 测试健康监控仪表板 | 12h | 实时覆盖率追踪 |
|
||||
| 4.3 | 变异测试试点 | 16h | 测试质量提升 |
|
||||
|
||||
**预期结果**: 完整的 CI/CD 测试流水线,自动化测试文化
|
||||
|
||||
---
|
||||
|
||||
## 📋 行动项清单
|
||||
|
||||
### 立即执行 (本周)
|
||||
|
||||
- [ ] 更新 `tests/setup.ts` 添加完整 Electron Mock
|
||||
- [ ] 修复 `tests/unit/logger.test.ts` Winston Mock
|
||||
- [ ] 创建 `tests/.env.test` 测试环境配置
|
||||
- [ ] 运行 `npm run test:run` 验证修复效果
|
||||
|
||||
### 短期执行 (本月)
|
||||
|
||||
- [ ] 为 8 个空单元测试文件添加测试
|
||||
- [ ] 为 6 个空集成测试文件添加测试
|
||||
- [ ] 创建 `tests/fixtures/factories.ts` 测试数据工厂
|
||||
- [ ] 修复所有失败的测试用例
|
||||
|
||||
### 中期执行 (本季度)
|
||||
|
||||
- [ ] 扩展 E2E 测试至 12 个文件
|
||||
- [ ] 配置 vitest 覆盖率阈值
|
||||
- [ ] 建立测试审查流程
|
||||
- [ ] 编写测试最佳实践文档
|
||||
|
||||
---
|
||||
|
||||
## 📚 附录
|
||||
|
||||
### A. 测试运行命令
|
||||
|
||||
```bash
|
||||
# 全量测试
|
||||
npm run test:run
|
||||
|
||||
# 带覆盖率测试
|
||||
npm run test:coverage
|
||||
|
||||
# 单次运行特定文件
|
||||
npx vitest run tests/unit/request-context.test.ts
|
||||
|
||||
# 监听模式
|
||||
npm run test
|
||||
|
||||
# E2E 测试
|
||||
npm run test:e2e
|
||||
|
||||
# E2E 报告
|
||||
npm run test:e2e:report
|
||||
```
|
||||
|
||||
### B. 关键文件参考
|
||||
|
||||
| 文件 | 用途 |
|
||||
| ----------------------------------- | ---------------- |
|
||||
| `vitest.config.ts` | Vitest 配置 |
|
||||
| `playwright.config.ts` | Playwright 配置 |
|
||||
| `tests/setup.ts` | 全局 Setup/Mocks |
|
||||
| `tests/fixtures/create-fixtures.ts` | 测试数据生成 |
|
||||
|
||||
### C. 测试模式参考
|
||||
|
||||
**单元测试模板**:
|
||||
|
||||
```typescript
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
|
||||
describe('ServiceName', () => {
|
||||
let service: ServiceClass
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
service = new ServiceClass(config)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('methodName', () => {
|
||||
it('should do something', async () => {
|
||||
const result = await service.methodName()
|
||||
expect(result).toBeDefined()
|
||||
})
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
**集成测试模板**:
|
||||
|
||||
```typescript
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
||||
|
||||
const hasCredentials = !!process.env.TEST_DB_HOST
|
||||
|
||||
describe('DatabaseService Integration', () => {
|
||||
let service: DatabaseService
|
||||
|
||||
beforeAll(async () => {
|
||||
if (!hasCredentials) {
|
||||
console.warn('Skipping: DB credentials not configured')
|
||||
return
|
||||
}
|
||||
service = new DatabaseService(testConfig)
|
||||
await service.connect()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
if (service) await service.disconnect()
|
||||
})
|
||||
|
||||
it.skipIf(!hasCredentials)('should connect to database', async () => {
|
||||
expect(service.isConnected()).toBe(true)
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**审查结论**: 项目测试基础良好,但存在关键 Mock 不完整和覆盖率缺口问题。建议优先修复 P0/P1 问题,然后系统性扩展测试覆盖。
|
||||
@@ -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` | 最近几轮重要重构和架构决策是什么 |
|
||||
|
||||
## 按问题选择阅读路径
|
||||
|
||||
|
||||
@@ -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` | 构建、发布、更新产物与上传流程 |
|
||||
|
||||
## 与其他文档目录的关系
|
||||
|
||||
|
||||
@@ -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 凭据加载与保存、当前用户配置管理 |
|
||||
|
||||
## 模块入口地图
|
||||
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
# ReportAnalysisDialog 组件重构分析
|
||||
|
||||
## 📊 当前状态分析
|
||||
|
||||
### 基本指标
|
||||
|
||||
- **总行数**: 948 行
|
||||
- **函数/声明**: 9 个
|
||||
- **React Hooks**: 20 个使用
|
||||
- **职责数量**: 5+ 个主要职责
|
||||
|
||||
### 组件职责分析
|
||||
|
||||
#### 1. 数据获取与解析 (~150 行)
|
||||
|
||||
- `loadAndAnalyzeReports` - 数据加载逻辑
|
||||
- `extractReportValues` - 报告内容解析
|
||||
- `parseDurationToSeconds` - 时间解析
|
||||
|
||||
#### 2. 数据聚合与转换 (~200 行)
|
||||
|
||||
- `chartData` useMemo - 按日期聚合
|
||||
- `comparisonData` useMemo - 按用户聚合
|
||||
- `comparisonChartData` useMemo - 图表数据格式化
|
||||
- `allUsers` useMemo - 用户列表提取
|
||||
|
||||
#### 3. 状态管理 (~100 行)
|
||||
|
||||
- 6 个 useState hooks
|
||||
- 5 个 useCallback handlers
|
||||
- 复杂的状态交互逻辑
|
||||
|
||||
#### 4. UI 控制与交互 (~200 行)
|
||||
|
||||
- 指标选择按钮
|
||||
- 视图模式切换
|
||||
- 用户筛选器
|
||||
- 加载/错误状态显示
|
||||
|
||||
#### 5. 图表渲染 (~300 行)
|
||||
|
||||
- Recharts 图表配置
|
||||
- 两个不同的视图模式
|
||||
- 自定义 Tooltip 组件
|
||||
- 图表样式和布局
|
||||
|
||||
## 🎯 重构目标
|
||||
|
||||
### 主要问题
|
||||
|
||||
1. **单一文件过大**: 难以维护和理解
|
||||
2. **职责混乱**: 数据获取、处理、UI 混在一起
|
||||
3. **复用性差**: 逻辑和 UI 紧耦合
|
||||
4. **测试困难**: 难以单独测试各个部分
|
||||
|
||||
### 重构原则
|
||||
|
||||
1. **单一职责**: 每个模块只负责一件事
|
||||
2. **可复用性**: 提取通用逻辑到 hooks
|
||||
3. **可测试性**: 分离逻辑和 UI
|
||||
4. **可维护性**: 清晰的文件结构
|
||||
|
||||
## 📦 建议的文件结构
|
||||
|
||||
```
|
||||
src/renderer/src/components/report-analysis/
|
||||
├── index.tsx # 主组件入口 (~150 行)
|
||||
├── hooks/
|
||||
│ ├── useReportData.ts # 数据获取和解析 (~100 行)
|
||||
│ ├── useChartData.ts # 数据聚合和转换 (~150 行)
|
||||
│ └── useReportFilters.ts # 筛选状态管理 (~80 行)
|
||||
├── components/
|
||||
│ ├── ReportChart.tsx # 图表组件 (~200 行)
|
||||
│ ├── MetricSelector.tsx # 指标选择器 (~80 行)
|
||||
│ ├── ViewModeToggle.tsx # 视图模式切换 (~50 行)
|
||||
│ ├── UserFilter.tsx # 用户筛选器 (~100 行)
|
||||
│ ├── CustomTooltip.tsx # 自定义 tooltip (~100 行)
|
||||
│ ├── ComparisonTooltip.tsx # 对比 tooltip (~80 行)
|
||||
│ └── LoadingState.tsx # 加载状态组件 (~60 行)
|
||||
├── utils/
|
||||
│ ├── parser.ts # 报告解析工具 (~100 行)
|
||||
│ ├── aggregators.ts # 数据聚合函数 (~120 行)
|
||||
│ └── formatters.ts # 格式化工具 (~60 行)
|
||||
└── types.ts # 类型定义 (~80 行)
|
||||
```
|
||||
|
||||
## 🔧 重构方案
|
||||
|
||||
### 方案 A: 完全重构 (推荐)
|
||||
|
||||
**优点**: 最大程度的解耦和可维护性
|
||||
**缺点**: 需要更多时间,可能引入新问题
|
||||
**时间估计**: 2-3 小时
|
||||
|
||||
### 方案 B: 渐进式重构
|
||||
|
||||
**优点**: 风险较低,可以逐步验证
|
||||
**缺点**: 过渡期代码可能不够优雅
|
||||
**时间估计**: 1-2 小时
|
||||
|
||||
### 方案 C: 最小化重构
|
||||
|
||||
**优点**: 改动最小,风险最低
|
||||
**缺点**: 解决根本问题有限
|
||||
**时间估计**: 30-45 分钟
|
||||
|
||||
## 📝 详细重构步骤
|
||||
|
||||
### Phase 1: 提取类型和工具函数 (低风险)
|
||||
|
||||
1. 创建 `types.ts` - 集中管理所有类型定义
|
||||
2. 创建 `utils/parser.ts` - 提取报告解析逻辑
|
||||
3. 创建 `utils/aggregators.ts` - 提取数据聚合逻辑
|
||||
|
||||
### Phase 2: 提取自定义 Hooks (中风险)
|
||||
|
||||
1. 创建 `hooks/useReportData.ts` - 数据获取和解析
|
||||
2. 创建 `hooks/useChartData.ts` - 数据聚合和转换
|
||||
3. 创建 `hooks/useReportFilters.ts` - 筛选状态管理
|
||||
|
||||
### Phase 3: 提取 UI 组件 (中风险)
|
||||
|
||||
1. 创建 `components/MetricSelector.tsx`
|
||||
2. 创建 `components/ViewModeToggle.tsx`
|
||||
3. 创建 `components/UserFilter.tsx`
|
||||
4. 创建 `components/ReportChart.tsx`
|
||||
|
||||
### Phase 4: 重构主组件 (高风险)
|
||||
|
||||
1. 简化 `index.tsx` 只保留组合逻辑
|
||||
2. 添加错误边界
|
||||
3. 优化加载状态
|
||||
|
||||
## 🎯 重构后的预期效果
|
||||
|
||||
### 代码行数分布
|
||||
|
||||
- 主组件: ~150 行 (减少 84%)
|
||||
- 每个 hook: ~80-150 行
|
||||
- 每个 UI 组件: ~50-200 行
|
||||
- 工具函数: ~60-120 行
|
||||
|
||||
### 可维护性提升
|
||||
|
||||
- ✅ 单个文件更小,更易理解
|
||||
- ✅ 职责清晰,修改影响范围小
|
||||
- ✅ 更容易进行单元测试
|
||||
- ✅ 可以独立优化各个部分
|
||||
|
||||
### 性能影响
|
||||
|
||||
- ➡️ 性能基本不变或略有提升
|
||||
- ➡️ 代码分割优化可能略微改善首次加载
|
||||
- ➡️ 更好的 memoization 机会
|
||||
|
||||
## 🚨 风险评估
|
||||
|
||||
### 高风险区域
|
||||
|
||||
- 图表配置逻辑(Recharts 配置复杂)
|
||||
- 数据转换和聚合(业务逻辑密集)
|
||||
- 状态同步(多个状态之间的交互)
|
||||
|
||||
### 缓解措施
|
||||
|
||||
- 保持现有测试通过
|
||||
- 逐步重构,每步验证
|
||||
- 添加 TypeScript 严格检查
|
||||
- 保留原有功能注释
|
||||
|
||||
## 📋 验证清单
|
||||
|
||||
重构完成后需要验证:
|
||||
|
||||
- [ ] 所有现有功能正常工作
|
||||
- [ ] 单元测试通过
|
||||
- [ ] E2E 测试通过
|
||||
- [ ] 类型检查无错误
|
||||
- [ ] 性能无明显下降
|
||||
- [ ] 代码风格符合规范
|
||||
|
||||
## 🤔 建议的实施顺序
|
||||
|
||||
### 推荐方案: 渐进式重构 (方案 B)
|
||||
|
||||
**第1步**: 提取类型和工具函数 (15分钟)
|
||||
|
||||
- 创建类型定义文件
|
||||
- 提取解析工具函数
|
||||
- 验证编译和测试
|
||||
|
||||
**第2步**: 提取自定义 Hooks (30分钟)
|
||||
|
||||
- 提取数据获取逻辑
|
||||
- 提取数据聚合逻辑
|
||||
- 提取筛选状态管理
|
||||
- 验证功能正常
|
||||
|
||||
**第3步**: 提取 UI 组件 (30分钟)
|
||||
|
||||
- 提取控制面板组件
|
||||
- 提取图表组件
|
||||
- 提取状态显示组件
|
||||
- 验证交互正常
|
||||
|
||||
**第4步**: 简化主组件 (15分钟)
|
||||
|
||||
- 重构为组合式组件
|
||||
- 清理代码和注释
|
||||
- 最终验证
|
||||
|
||||
**总计**: 约 90 分钟,分4个阶段,每个阶段都可以独立验证
|
||||
42
docs/releases/1.6.1.md
Normal file
42
docs/releases/1.6.1.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# 1.6.1
|
||||
|
||||
## 核心改进
|
||||
|
||||
- **重大重构**:将报告分析组件从 948 行单体组件重构为模块化架构,拆分为 11 个专注的模块文件。
|
||||
- **代码质量提升**:主组件代码量减少 79%(948 → 200 行),显著提升可维护性和可读性。
|
||||
- **架构优化**:分离数据获取、状态管理和 UI 渲染逻辑,遵循单一职责原则。
|
||||
|
||||
## 体验优化
|
||||
|
||||
- **修复 tooltip 显示问题**:解决执行时间在提示框中重复显示的问题,现在只显示一次格式化后的时间值。
|
||||
- **统一时间格式**:所有时间数值统一保留 1 位小数,提升数据显示的一致性和专业度。
|
||||
- **优化界面布局**:精简 tooltip 底部信息,避免冗余内容干扰用户视线。
|
||||
|
||||
## 性能优化
|
||||
|
||||
- **组件渲染优化**:将 tooltip 组件移出父组件并使用 React.memo,减少不必要的重新渲染。
|
||||
- **正则表达式优化**:预编译正则表达式模式,避免在循环中重复创建,提升数据处理效率。
|
||||
- **状态更新优化**:使用函数式 setState 更新,避免闭包陷阱和过期的状态读取。
|
||||
- **回调函数优化**:使用 useCallback 稳定回调函数引用,减少子组件的不必要更新。
|
||||
|
||||
## 开发体验
|
||||
|
||||
- **模块化设计**:将复杂组件拆分为可复用的 hooks 和 UI 组件,便于单独测试和维护。
|
||||
- **类型安全**:完整的 TypeScript 类型定义,提升开发时的类型检查和 IDE 支持。
|
||||
- **代码组织**:清晰的文件结构(types、hooks、components、utils),便于团队协作和代码导航。
|
||||
- **向后兼容**:保持原有 API 接口不变,现有使用方式无需修改。
|
||||
|
||||
## 技术细节
|
||||
|
||||
- 应用 Vercel React 最佳实践,包括:
|
||||
- 避免内联组件定义(rerender-no-inline-components)
|
||||
- 提升正则表达式创建位置(js-hoist-regexp)
|
||||
- 使用函数式状态更新(rerender-functional-setState)
|
||||
- 最小化回调依赖项(rerender-dependencies)
|
||||
- 新增自定义 hooks:useReportData、useChartData、useReportFilters
|
||||
- 新增 UI 组件:MetricSelector、ViewModeToggle、UserFilter、ReportChart
|
||||
- 新增工具函数:数据解析器和聚合器
|
||||
|
||||
## 破坏性变更
|
||||
|
||||
无破坏性变更,所有现有功能保持完全兼容。
|
||||
6
docs/releases/1.6.2.md
Normal file
6
docs/releases/1.6.2.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# 1.6.2
|
||||
|
||||
## 系统优化
|
||||
|
||||
- 简化用户角色体系,移除未使用的 Guest 角色。
|
||||
- 优化类型安全性,加强用户认证流程健壮性。
|
||||
18
docs/releases/1.7.0.md
Normal file
18
docs/releases/1.7.0.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# 1.7.0
|
||||
|
||||
## 核心功能
|
||||
|
||||
- 新增提取操作历史记录功能,每次执行提取后自动保存订单号和总排号。
|
||||
- 支持查看历史批次详情,包含操作时间、订单数、记录数、成功/失败统计。
|
||||
- 批次记录可展开查看,显示总排号与订单号的对应关系。
|
||||
|
||||
## 界面与交互
|
||||
|
||||
- 提取页面新增"操作历史"按钮,点击打开历史记录对话框。
|
||||
- 管理员可查看所有用户的历史记录,普通用户仅查看自己的记录。
|
||||
- 支持删除历史批次,管理员可删除任意批次,普通用户仅可删除自己的记录。
|
||||
|
||||
## 数据存储
|
||||
|
||||
- 新增数据库表 `ExtractorOperationHistory`,支持 SQL Server 和 MySQL。
|
||||
- 需执行数据库脚本创建表结构(详见项目文档)。
|
||||
6
docs/releases/1.7.1.md
Normal file
6
docs/releases/1.7.1.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# 1.7.1
|
||||
|
||||
## 问题修复
|
||||
|
||||
- 修复 MySQL 数据库下操作历史查询报错问题。
|
||||
- 优化历史记录数据结构,支持按订单统计记录数量。
|
||||
6
docs/releases/1.7.2.md
Normal file
6
docs/releases/1.7.2.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# 1.7.2
|
||||
|
||||
## 问题修复
|
||||
|
||||
- 修复操作历史时间显示错误(时区转换导致时间快8小时)。
|
||||
- 操作历史支持一键复制总排号和订单号。
|
||||
12
docs/releases/1.8.0.md
Normal file
12
docs/releases/1.8.0.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# 1.8.0
|
||||
|
||||
## 权限控制
|
||||
|
||||
- 操作历史删除按钮仅对管理员可见,普通用户无法删除历史记录。
|
||||
- 修复用户状态传递问题,确保权限判断正确生效。
|
||||
|
||||
## 界面与交互
|
||||
|
||||
- 管理员可使用多选标签(Chip)按用户筛选操作历史。
|
||||
- 支持同时选择多个用户查看记录,点击标签即可切换选中状态。
|
||||
- 添加"清空筛选"按钮,一键恢复显示所有用户记录。
|
||||
18
docs/releases/1.9.0.md
Normal file
18
docs/releases/1.9.0.md
Normal file
@@ -0,0 +1,18 @@
|
||||
# 1.9.0
|
||||
|
||||
## 核心功能
|
||||
|
||||
- **物料清理日志大幅增强**: CleanerService 新增 400+ 行详细日志,问题排查更精准。
|
||||
- **全链路耗时追踪**:导航、查询、订单处理、重试各阶段均记录耗时,慢操作自动标记。
|
||||
- **重试机制可视化**:每次重试尝试的详细步骤、成功率、平均耗时完整记录。
|
||||
|
||||
## 改进
|
||||
|
||||
- **导航过程透明化**:5 个导航步骤逐一记录,帧加载状态、错误上下文完整捕获。
|
||||
- **物料决策可追溯**:每个物料的删除/跳过决定均记录详细原因(行号保护、待发数量等)。
|
||||
- **批次处理性能监控**:批次开始/结束统计、订单处理效率一目了然。
|
||||
|
||||
## 开发者工具
|
||||
|
||||
- **统一日志格式**:所有日志采用 `[阶段] 操作描述` 格式,支持按标签快速过滤。
|
||||
- **错误诊断增强**:关键错误自动捕获页面快照和浏览器上下文信息。
|
||||
1484
docs/test-improvement-plan.md
Normal file
1484
docs/test-improvement-plan.md
Normal file
File diff suppressed because it is too large
Load Diff
128
package-lock.json
generated
128
package-lock.json
generated
@@ -1,15 +1,16 @@
|
||||
{
|
||||
"name": "erpauto",
|
||||
"version": "1.6.0",
|
||||
"version": "1.9.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "erpauto",
|
||||
"version": "1.6.0",
|
||||
"version": "1.9.0",
|
||||
"hasInstallScript": true,
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.929.0",
|
||||
"@datalust/winston-seq": "^3.0.1",
|
||||
"@electron-toolkit/preload": "^3.0.2",
|
||||
"@electron-toolkit/utils": "^4.0.0",
|
||||
"@headlessui/react": "^2.2.9",
|
||||
@@ -970,6 +971,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@azure/core-client/-/core-client-1.10.1.tgz",
|
||||
"integrity": "sha512-Nh5PhEOeY6PrnxNPsEHRr9eimxLwgLlpmguQaHKBinFYA/RU9+kOYVOQqOrTsCL+KSxrLLl1gD8Dk5BFW/7l/w==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@azure/abort-controller": "^2.1.2",
|
||||
"@azure/core-auth": "^1.10.0",
|
||||
@@ -1031,6 +1033,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@azure/core-rest-pipeline/-/core-rest-pipeline-1.22.2.tgz",
|
||||
"integrity": "sha512-MzHym+wOi8CLUlKCQu12de0nwcq9k9Kuv43j4Wa++CsCpJwps2eeBQwD2Bu8snkxTtDKDx4GwjuR9E8yC8LNrg==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@azure/abort-controller": "^2.1.2",
|
||||
"@azure/core-auth": "^1.10.0",
|
||||
@@ -1222,6 +1225,7 @@
|
||||
"integrity": "sha512-CGOfOJqWjg2qW/Mb6zNsDm+u5vFQ8DxXfbM09z69p5Z6+mE1ikP2jUXw+j42Pf1XTYED2Rni5f95npYeuwMDQA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@babel/code-frame": "^7.29.0",
|
||||
"@babel/generator": "^7.29.0",
|
||||
@@ -1528,6 +1532,19 @@
|
||||
"kuler": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@datalust/winston-seq": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@datalust/winston-seq/-/winston-seq-3.0.1.tgz",
|
||||
"integrity": "sha512-jWJd5PKcj/nM5f1T65KJgKaxPJRADWe+GEWtj1yEji1H0ub4RWhBEDLYzIFdwUy365lxtc5njsakenp4Evmv+g==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"seq-logging": "^3.0.0",
|
||||
"winston-transport": "^4.9.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"winston": "^3.17.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@develar/schema-utils": {
|
||||
"version": "2.6.5",
|
||||
"resolved": "https://registry.npmjs.org/@develar/schema-utils/-/schema-utils-2.6.5.tgz",
|
||||
@@ -1999,7 +2016,6 @@
|
||||
"dev": true,
|
||||
"license": "BSD-2-Clause",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"cross-dirname": "^0.1.0",
|
||||
"debug": "^4.3.4",
|
||||
@@ -2021,7 +2037,6 @@
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"graceful-fs": "^4.2.0",
|
||||
"jsonfile": "^6.0.1",
|
||||
@@ -2038,7 +2053,6 @@
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"universalify": "^2.0.0"
|
||||
},
|
||||
@@ -2053,7 +2067,6 @@
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"engines": {
|
||||
"node": ">= 10.0.0"
|
||||
}
|
||||
@@ -5002,6 +5015,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.13.tgz",
|
||||
"integrity": "sha512-akNQMv0wW5uyRpD2v2IEyRSZiR+BeGuoB6L310EgGObO44HSMNT8z1xzio28V8qOrgYaopIDNA18YgdXd+qTiw==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"undici-types": "~6.21.0"
|
||||
}
|
||||
@@ -5023,6 +5037,7 @@
|
||||
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.2.14.tgz",
|
||||
"integrity": "sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"csstype": "^3.2.2"
|
||||
}
|
||||
@@ -5143,6 +5158,7 @@
|
||||
"integrity": "sha512-klQbnPAAiGYFyI02+znpBRLyjL4/BrBd0nyWkdC0s/6xFLkXYQ8OoRrSkqacS1ddVxf/LDyODIKbQ5TgKAf/Fg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@typescript-eslint/scope-manager": "8.56.1",
|
||||
"@typescript-eslint/types": "8.56.1",
|
||||
@@ -5582,6 +5598,7 @@
|
||||
"integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"acorn": "bin/acorn"
|
||||
},
|
||||
@@ -5614,6 +5631,7 @@
|
||||
"integrity": "sha512-IWrosm/yrn43eiKqkfkHis7QioDleaXQHdDVPKg0FSwwd/DuvyX79TZnFOnYpB7dcsFAMmtFztZuXPDvSePkFw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
"fast-json-stable-stringify": "^2.0.0",
|
||||
@@ -6376,6 +6394,7 @@
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"baseline-browser-mapping": "^2.9.0",
|
||||
"caniuse-lite": "^1.0.30001759",
|
||||
@@ -7171,8 +7190,7 @@
|
||||
"integrity": "sha512-+R08/oI0nl3vfPcqftZRpytksBXDzOUveBq/NBVx0sUp1axwzPQrKinNx5yd5sxPu8j1wIy8AfnVQ+5eFdha6Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true
|
||||
"optional": true
|
||||
},
|
||||
"node_modules/cross-spawn": {
|
||||
"version": "7.0.6",
|
||||
@@ -7705,6 +7723,7 @@
|
||||
"integrity": "sha512-glMJgnTreo8CFINujtAhCgN96QAqApDMZ8Vl1r8f0QT8QprvC1UCltV4CcWj20YoIyLZx6IUskaJZ0NV8fokcg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"app-builder-lib": "26.8.1",
|
||||
"builder-util": "26.8.1",
|
||||
@@ -7919,6 +7938,7 @@
|
||||
"integrity": "sha512-Rz5QvP1pTqoU1DPRrG3EeX2oWBtS3uRmd6Z/wzZsb2e/iIUsrT+XcBaAhFr4FW48gDc8uP2wYVyY5Aamha/5Zg==",
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@electron/get": "^2.0.0",
|
||||
"@types/node": "^22.7.7",
|
||||
@@ -8107,7 +8127,6 @@
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@electron/asar": "^3.2.1",
|
||||
"debug": "^4.1.1",
|
||||
@@ -8128,7 +8147,6 @@
|
||||
"integrity": "sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"graceful-fs": "^4.1.2",
|
||||
"jsonfile": "^4.0.0",
|
||||
@@ -8150,17 +8168,6 @@
|
||||
"integrity": "sha512-AKrN98kuwOzMIdAizXGI86UFBoo26CL21UM763y1h/GMSJ4/OHU9k2YlsmBpyScFo/wbLzWQJBMCW4+IO3/+OQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/encoding": {
|
||||
"version": "0.1.13",
|
||||
"resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.13.tgz",
|
||||
"integrity": "sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"dependencies": {
|
||||
"iconv-lite": "^0.6.2"
|
||||
}
|
||||
},
|
||||
"node_modules/end-of-stream": {
|
||||
"version": "1.4.5",
|
||||
"resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz",
|
||||
@@ -8467,6 +8474,7 @@
|
||||
"integrity": "sha512-VmQ+sifHUbI/IcSopBCF/HO3YiHQx/AVd3UVyYL6weuwW+HvON9VYn5l6Zl1WZzPWXPNZrSQpxwkkZ/VuvJZzg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@eslint-community/eslint-utils": "^4.8.0",
|
||||
"@eslint-community/regexpp": "^4.12.1",
|
||||
@@ -8527,6 +8535,7 @@
|
||||
"integrity": "sha512-82GZUjRS0p/jganf6q1rEO25VSoHH0hKPCTrgillPjdI/3bgBhAE1QzHrHTizjpRvy6pGAvKjDJtk2pF9NDq8w==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"eslint-config-prettier": "bin/cli.js"
|
||||
},
|
||||
@@ -9955,7 +9964,7 @@
|
||||
"version": "0.6.3",
|
||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
|
||||
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
|
||||
"dev": true,
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"safer-buffer": ">= 2.1.2 < 3.0.0"
|
||||
@@ -12978,6 +12987,26 @@
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/node-fetch": {
|
||||
"version": "2.7.0",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
|
||||
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"whatwg-url": "^5.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "4.x || >=6.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"encoding": "^0.1.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"encoding": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/node-gyp": {
|
||||
"version": "11.5.0",
|
||||
"resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-11.5.0.tgz",
|
||||
@@ -13514,6 +13543,7 @@
|
||||
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.3.tgz",
|
||||
"integrity": "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
@@ -13594,6 +13624,7 @@
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"nanoid": "^3.3.11",
|
||||
"picocolors": "^1.1.1",
|
||||
@@ -13617,7 +13648,6 @@
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"commander": "^9.4.0"
|
||||
},
|
||||
@@ -13635,7 +13665,6 @@
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"optional": true,
|
||||
"peer": true,
|
||||
"engines": {
|
||||
"node": "^12.20.0 || >=14"
|
||||
}
|
||||
@@ -13656,6 +13685,7 @@
|
||||
"integrity": "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"prettier": "bin/prettier.cjs"
|
||||
},
|
||||
@@ -13797,6 +13827,7 @@
|
||||
"resolved": "https://registry.npmjs.org/react/-/react-19.2.4.tgz",
|
||||
"integrity": "sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
@@ -13818,6 +13849,7 @@
|
||||
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.4.tgz",
|
||||
"integrity": "sha512-AXJdLo8kgMbimY95O2aKQqsz2iWi9jMgKJhRBAxECE4IFxfcazB2LmzloIoibJI3C12IlY20+KFaLv+71bUJeQ==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"scheduler": "^0.27.0"
|
||||
},
|
||||
@@ -13852,7 +13884,8 @@
|
||||
"version": "16.13.1",
|
||||
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
|
||||
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/react-markdown": {
|
||||
"version": "10.1.0",
|
||||
@@ -13886,6 +13919,7 @@
|
||||
"resolved": "https://registry.npmjs.org/react-redux/-/react-redux-9.2.0.tgz",
|
||||
"integrity": "sha512-ROY9fvHhwOD9ySfrF0wmvu//bKCQ6AeZZq1nJNtbDC+kk5DuSuNX/n6YWYF/SYy7bSba4D4FSz8DJeKY/S/r+g==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@types/use-sync-external-store": "^0.0.6",
|
||||
"use-sync-external-store": "^1.4.0"
|
||||
@@ -14011,7 +14045,8 @@
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/redux/-/redux-5.0.1.tgz",
|
||||
"integrity": "sha512-M9/ELqF6fy8FwmkpnF0S3YKOqMyoWJ4+CS5Efg2ct3oY9daQvd/Pc71FpGZsVsbl3Cpb+IIcjBDUnnyBdQbq4w==",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/redux-thunk": {
|
||||
"version": "3.1.0",
|
||||
@@ -14540,6 +14575,19 @@
|
||||
"license": "MIT",
|
||||
"optional": true
|
||||
},
|
||||
"node_modules/seq-logging": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/seq-logging/-/seq-logging-3.0.0.tgz",
|
||||
"integrity": "sha512-ys5QV0745vxBCWuZBPSkgoobuLoUMxTSz1g7ZclHqX1tXXKFLyRIIn8V89EPgDnfRiWfoSo4KSxy/E0MtOYYyw==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"abort-controller": "^3.0.0",
|
||||
"node-fetch": "^2.7.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.18"
|
||||
}
|
||||
},
|
||||
"node_modules/serialize-error": {
|
||||
"version": "7.0.1",
|
||||
"resolved": "https://registry.npmjs.org/serialize-error/-/serialize-error-7.0.1.tgz",
|
||||
@@ -15405,7 +15453,6 @@
|
||||
"integrity": "sha512-yYrrsWnrXMcdsnu/7YMYAofM1ktpL5By7vZhf15CrXijWWrEYZks5AXBudalfSWJLlnen/QUJUB5aoB0kqZUGA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"mkdirp": "^0.5.1",
|
||||
"rimraf": "~2.6.2"
|
||||
@@ -15571,6 +15618,12 @@
|
||||
"node": ">= 0.4"
|
||||
}
|
||||
},
|
||||
"node_modules/tr46": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
|
||||
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/traverse": {
|
||||
"version": "0.3.9",
|
||||
"resolved": "https://registry.npmjs.org/traverse/-/traverse-0.3.9.tgz",
|
||||
@@ -16441,6 +16494,7 @@
|
||||
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"peer": true,
|
||||
"bin": {
|
||||
"tsc": "bin/tsc",
|
||||
"tsserver": "bin/tsserver"
|
||||
@@ -16878,6 +16932,7 @@
|
||||
"resolved": "https://registry.npmjs.org/vite/-/vite-7.3.1.tgz",
|
||||
"integrity": "sha512-w+N7Hifpc3gRjZ63vYBXA56dvvRlNWRczTdmCBBa+CotUzAPf5b7YMdMR/8CQoeYE5LX3W4wj6RYTgonm1b9DA==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"esbuild": "^0.27.0",
|
||||
"fdir": "^6.5.0",
|
||||
@@ -17424,6 +17479,7 @@
|
||||
"integrity": "sha512-hOQuK7h0FGKgBAas7v0mSAsnvrIgAvWmRFjmzpJ7SwFHH3g1k2u37JtYwOwmEKhK6ZO3v9ggDBBm0La1LCK4uQ==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@vitest/expect": "4.0.18",
|
||||
"@vitest/mocker": "4.0.18",
|
||||
@@ -17506,6 +17562,22 @@
|
||||
"defaults": "^1.0.3"
|
||||
}
|
||||
},
|
||||
"node_modules/webidl-conversions": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
|
||||
"license": "BSD-2-Clause"
|
||||
},
|
||||
"node_modules/whatwg-url": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
|
||||
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"tr46": "~0.0.3",
|
||||
"webidl-conversions": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/which": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/which/-/which-5.0.0.tgz",
|
||||
@@ -17632,6 +17704,7 @@
|
||||
"resolved": "https://registry.npmjs.org/winston/-/winston-3.19.0.tgz",
|
||||
"integrity": "sha512-LZNJgPzfKR+/J3cHkxcpHKpKKvGfDZVPS4hfJCc4cCG0CgYzvlD6yE/S3CIL/Yt91ak327YCpiF/0MyeZHEHKA==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"dependencies": {
|
||||
"@colors/colors": "^1.6.0",
|
||||
"@dabh/diagnostics": "^2.0.8",
|
||||
@@ -17869,6 +17942,7 @@
|
||||
"resolved": "https://registry.npmjs.org/zod/-/zod-4.3.6.tgz",
|
||||
"integrity": "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg==",
|
||||
"license": "MIT",
|
||||
"peer": true,
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/colinhacks"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "erpauto",
|
||||
"version": "1.6.0",
|
||||
"version": "1.9.0",
|
||||
"description": "An Electron application with React and TypeScript",
|
||||
"main": "./out/main/index.js",
|
||||
"author": "example.com",
|
||||
@@ -34,6 +34,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.929.0",
|
||||
"@datalust/winston-seq": "^3.0.1",
|
||||
"@electron-toolkit/preload": "^3.0.2",
|
||||
"@electron-toolkit/utils": "^4.0.0",
|
||||
"@headlessui/react": "^2.2.9",
|
||||
|
||||
@@ -1,40 +1,50 @@
|
||||
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'
|
||||
import { serializeError } from '../services/logger/error-utils'
|
||||
|
||||
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) => {
|
||||
logger.error('Unhandled Rejection', { reason: String(reason) })
|
||||
await logAudit('SYSTEM_ERROR', 'system', {
|
||||
process.on('unhandledRejection', (reason) => {
|
||||
const errorMeta =
|
||||
reason instanceof Error
|
||||
? { error: serializeError(reason) }
|
||||
: { reason: String(reason) }
|
||||
logger.error('Unhandled Rejection', errorMeta)
|
||||
logAudit('SYSTEM_ERROR', 'system', {
|
||||
username: 'system',
|
||||
computerName: process.env.COMPUTERNAME || 'unknown',
|
||||
resource: 'main-process',
|
||||
status: 'failure',
|
||||
metadata: { reason: String(reason) }
|
||||
metadata: errorMeta
|
||||
})
|
||||
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()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ import fs from 'fs'
|
||||
import { join } from 'path'
|
||||
import { ConfigManager } from '../services/config/config-manager'
|
||||
import { UpdateService } from '../services/update/update-service'
|
||||
import { createLogger } from '../services/logger'
|
||||
|
||||
const log = createLogger('Bootstrap')
|
||||
|
||||
export function configurePlaywrightBrowsersPath(): string {
|
||||
const browsersPath = join(app.getPath('userData'), 'ms-playwright')
|
||||
@@ -39,7 +42,7 @@ export function ensurePlaywrightRuntime(browsersPath: string): boolean {
|
||||
try {
|
||||
fs.mkdirSync(browsersPath, { recursive: true })
|
||||
} catch (error) {
|
||||
console.error('Failed to create browsers directory:', error)
|
||||
log.error('Failed to create browsers directory', { error })
|
||||
}
|
||||
|
||||
const newChromiumPath = join(browsersPath, 'chromium-1208', 'chrome-win64', 'chrome.exe')
|
||||
@@ -57,7 +60,7 @@ export function ensurePlaywrightRuntime(browsersPath: string): boolean {
|
||||
if (entry.startsWith('chromium-') && !entry.includes('headless')) {
|
||||
const revisionPath = join(browsersPath, entry, 'chrome-win64', 'chrome.exe')
|
||||
if (fs.existsSync(revisionPath)) {
|
||||
console.log('Found Chromium revision:', entry)
|
||||
log.info('Found Chromium revision', { revision: entry })
|
||||
foundRevision = true
|
||||
break
|
||||
}
|
||||
@@ -71,10 +74,10 @@ export function ensurePlaywrightRuntime(browsersPath: string): boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
console.warn(
|
||||
'Playwright browser not found. Available:',
|
||||
fs.existsSync(browsersPath) ? fs.readdirSync(browsersPath) : 'none'
|
||||
)
|
||||
log.warn('Playwright browser not found', {
|
||||
available: fs.existsSync(browsersPath) ? fs.readdirSync(browsersPath) : 'none',
|
||||
browsersPath
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -84,7 +87,7 @@ export async function initializeMainProcessServices(): Promise<void> {
|
||||
await configManager.initialize()
|
||||
UpdateService.getInstance().initialize()
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize ConfigManager:', error)
|
||||
log.error('Failed to initialize ConfigManager', { error })
|
||||
}
|
||||
|
||||
const { registerIpcHandlers } = await import('../ipc')
|
||||
|
||||
@@ -7,17 +7,20 @@ import {
|
||||
setupElectronRuntime
|
||||
} from './bootstrap/runtime'
|
||||
import { setupProcessGuards } from './bootstrap/process-guards'
|
||||
import { createLogger } from './services/logger'
|
||||
|
||||
const log = createLogger('App')
|
||||
|
||||
app.whenReady().then(async () => {
|
||||
setupProcessGuards()
|
||||
registerMainWindowLifecycle()
|
||||
const playwrightBrowsersPath = configurePlaywrightBrowsersPath()
|
||||
const browsersExist = ensurePlaywrightRuntime(playwrightBrowsersPath)
|
||||
console.log('Playwright browsers exist:', browsersExist)
|
||||
log.info('Playwright browsers check', { browsersExist })
|
||||
await initializeMainProcessServices()
|
||||
setupElectronRuntime()
|
||||
|
||||
ipcMain.on('ping', () => console.log('pong'))
|
||||
ipcMain.on('ping', () => log.debug('pong'))
|
||||
|
||||
createMainWindow()
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import { ErpAuthService } from '../services/erp/erp-auth'
|
||||
import { ExtractorService } from '../services/erp/extractor'
|
||||
import { OrderNumberResolver } from '../services/erp/order-resolver'
|
||||
import { create, type IDatabaseService } from '../services/database'
|
||||
import { ExtractorOperationHistoryDAO } from '../services/database/extractor-operation-history-dao'
|
||||
import { createLogger } from '../services/logger'
|
||||
import { logAudit } from '../services/logger/audit-logger'
|
||||
import { SessionManager } from '../services/user/session-manager'
|
||||
@@ -12,6 +13,7 @@ import type { ExtractorInput, ExtractorResult, ExtractionProgress } from '../typ
|
||||
import { UserErpConfigService } from '../services/user/user-erp-config-service'
|
||||
import { ConfigManager } from '../services/config/config-manager'
|
||||
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||
import { randomUUID } from 'crypto'
|
||||
|
||||
const log = createLogger('ExtractorHandler')
|
||||
|
||||
@@ -88,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'
|
||||
@@ -141,6 +147,29 @@ export function registerExtractorHandlers(): void {
|
||||
|
||||
log.info('Resolved order numbers', { count: validOrderNumbers.length })
|
||||
|
||||
// Initialize operation history recording
|
||||
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||
const historyDao = new ExtractorOperationHistoryDAO()
|
||||
const batchId = randomUUID()
|
||||
|
||||
// Save order records to history (preserve productionId -> orderNumber mapping)
|
||||
if (currentUser) {
|
||||
const orderRecords = mappings.map((m) => ({
|
||||
productionId: m.productionId || null,
|
||||
orderNumber: m.orderNumber || m.input
|
||||
}))
|
||||
await historyDao.insertBatchRecords(
|
||||
batchId,
|
||||
currentUser.id,
|
||||
currentUser.username,
|
||||
orderRecords
|
||||
)
|
||||
log.info('Operation history batch created', {
|
||||
batchId,
|
||||
recordCount: orderRecords.length
|
||||
})
|
||||
}
|
||||
|
||||
// Log deduplication summary
|
||||
sendLog(sender, 'info', dedupReport.summary)
|
||||
|
||||
@@ -163,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, {
|
||||
@@ -223,11 +252,35 @@ export function registerExtractorHandlers(): void {
|
||||
})
|
||||
}
|
||||
|
||||
// Update operation history batch status
|
||||
if (currentUser) {
|
||||
const status: 'success' | 'failed' | 'partial' =
|
||||
result.errors.length > 0 && result.recordCount > 0
|
||||
? 'partial'
|
||||
: result.errors.length > 0
|
||||
? 'failed'
|
||||
: 'success'
|
||||
|
||||
// Write per-order record counts
|
||||
for (const { orderNumber, recordCount } of result.orderRecordCounts) {
|
||||
await historyDao.updateRecordStatus(
|
||||
batchId,
|
||||
orderNumber,
|
||||
status,
|
||||
undefined,
|
||||
recordCount
|
||||
)
|
||||
}
|
||||
|
||||
// Update batch status without recordCount (per-order counts are set individually)
|
||||
await historyDao.updateBatchStatus(batchId, status)
|
||||
log.info('Operation history batch status updated', { batchId, status })
|
||||
}
|
||||
|
||||
// Audit log: EXTRACT (non-blocking)
|
||||
const os = await import('os')
|
||||
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||
if (currentUser) {
|
||||
const status: 'success' | 'failure' | 'partial' =
|
||||
const auditStatus: 'success' | 'failure' | 'partial' =
|
||||
result.errors.length > 0 && result.recordCount > 0
|
||||
? 'partial'
|
||||
: result.errors.length > 0
|
||||
@@ -237,13 +290,13 @@ export function registerExtractorHandlers(): void {
|
||||
username: currentUser.username,
|
||||
computerName: os.hostname(),
|
||||
resource: 'MATERIAL_PLAN',
|
||||
status,
|
||||
status: auditStatus,
|
||||
metadata: {
|
||||
orderCount: validOrderNumbers.length,
|
||||
recordCount: result.recordCount,
|
||||
errorCount: result.errors.length
|
||||
}
|
||||
}).catch((err) => log.warn('Failed to write audit log', { err }))
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
@@ -17,6 +17,7 @@ import { registerLoggerHandlers } from './logger-handler'
|
||||
import { registerReportHandlers } from './report-handler'
|
||||
import { registerUpdateHandlers } from './update-handler'
|
||||
import { registerPlaywrightBrowserHandlers } from './playwright-browser'
|
||||
import { registerOperationHistoryHandlers } from './operation-history-handler'
|
||||
import { createLogger, logError } from '../services/logger'
|
||||
import { serializeError, sanitizeError } from '../services/logger/error-utils'
|
||||
import { getErrorMessage, getErrorCode, isBaseError } from '../types/errors'
|
||||
@@ -67,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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -107,5 +114,6 @@ export function registerIpcHandlers(): void {
|
||||
registerReportHandlers()
|
||||
registerUpdateHandlers()
|
||||
registerPlaywrightBrowserHandlers()
|
||||
registerOperationHistoryHandlers()
|
||||
log.info('All IPC handlers registered')
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
124
src/main/ipc/operation-history-handler.ts
Normal file
124
src/main/ipc/operation-history-handler.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* IPC Handler for Extractor Operation History
|
||||
*
|
||||
* Handles IPC requests for operation history management:
|
||||
* - Get batch list (filtered by user for non-admin users)
|
||||
* - Get batch details
|
||||
* - Delete batches
|
||||
*/
|
||||
|
||||
import { ipcMain } from 'electron'
|
||||
import { ExtractorOperationHistoryDAO } from '../services/database/extractor-operation-history-dao'
|
||||
import { SessionManager } from '../services/user/session-manager'
|
||||
import { withErrorHandling, type IpcResult } from './index'
|
||||
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||
import { createLogger } from '../services/logger'
|
||||
import type {
|
||||
BatchStats,
|
||||
OperationHistoryRecord,
|
||||
GetBatchesOptions
|
||||
} from '../types/operation-history.types'
|
||||
|
||||
const log = createLogger('OperationHistoryHandler')
|
||||
|
||||
/**
|
||||
* Register IPC handlers for operation history
|
||||
*/
|
||||
export function registerOperationHistoryHandlers(): void {
|
||||
const dao = new ExtractorOperationHistoryDAO()
|
||||
|
||||
/**
|
||||
* Get batches list
|
||||
* Admin users get all batches, regular users get only their own
|
||||
*/
|
||||
ipcMain.handle(
|
||||
IPC_CHANNELS.OPERATION_HISTORY_GET_BATCHES,
|
||||
async (event, options?: GetBatchesOptions): Promise<IpcResult<BatchStats[]>> => {
|
||||
return withErrorHandling(async () => {
|
||||
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||
|
||||
if (!currentUser) {
|
||||
throw new Error('用户未登录')
|
||||
}
|
||||
|
||||
// Admin gets all batches, User gets only their own
|
||||
const userId = currentUser.userType === 'Admin' ? undefined : currentUser.id
|
||||
|
||||
log.info('Getting operation history batches', {
|
||||
userId: currentUser.id,
|
||||
userType: currentUser.userType,
|
||||
filtered: userId !== undefined
|
||||
})
|
||||
|
||||
const batches = await dao.getBatches(userId, options)
|
||||
return batches
|
||||
}, 'operationHistory:getBatches')
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
* Get batch details
|
||||
* Users can only view their own batch details, admins can view all
|
||||
*/
|
||||
ipcMain.handle(
|
||||
IPC_CHANNELS.OPERATION_HISTORY_GET_BATCH_DETAILS,
|
||||
async (event, batchId: string): Promise<IpcResult<OperationHistoryRecord[]>> => {
|
||||
return withErrorHandling(async () => {
|
||||
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||
|
||||
if (!currentUser) {
|
||||
throw new Error('用户未登录')
|
||||
}
|
||||
|
||||
log.info('Getting batch details', { batchId, userId: currentUser.id })
|
||||
|
||||
const details = await dao.getBatchDetails(batchId)
|
||||
|
||||
// For non-admin users, verify they own this batch
|
||||
if (currentUser.userType !== 'Admin' && details.length > 0) {
|
||||
const batchOwnerId = details[0].userId
|
||||
if (batchOwnerId !== currentUser.id) {
|
||||
throw new Error('没有权限查看此批次详情')
|
||||
}
|
||||
}
|
||||
|
||||
return details
|
||||
}, 'operationHistory:getBatchDetails')
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
* Delete a batch
|
||||
* Users can only delete their own batches, admins can delete any
|
||||
*/
|
||||
ipcMain.handle(
|
||||
IPC_CHANNELS.OPERATION_HISTORY_DELETE_BATCH,
|
||||
async (event, batchId: string): Promise<IpcResult<{ deleted: boolean }>> => {
|
||||
return withErrorHandling(async () => {
|
||||
const currentUser = SessionManager.getInstance().getUserInfo()
|
||||
|
||||
if (!currentUser) {
|
||||
throw new Error('用户未登录')
|
||||
}
|
||||
|
||||
const isAdmin = currentUser.userType === 'Admin'
|
||||
|
||||
log.info('Deleting batch', {
|
||||
batchId,
|
||||
userId: currentUser.id,
|
||||
isAdmin
|
||||
})
|
||||
|
||||
const result = await dao.deleteBatch(batchId, currentUser.id, isAdmin)
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error || '删除批次失败')
|
||||
}
|
||||
|
||||
return { deleted: true }
|
||||
}, 'operationHistory:deleteBatch')
|
||||
}
|
||||
)
|
||||
|
||||
log.info('Operation history IPC handlers registered')
|
||||
}
|
||||
@@ -27,10 +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 () => (sessionManager.getUserType() as UserType) || 'Guest',
|
||||
'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(
|
||||
@@ -71,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 }
|
||||
|
||||
@@ -20,7 +20,7 @@ export type LoginRequestZod = z.infer<typeof LoginRequestSchema>
|
||||
export const UserInfoSchema = z.object({
|
||||
id: z.number().int().positive(),
|
||||
username: z.string().min(1),
|
||||
userType: z.enum(['Admin', 'User', 'Guest']),
|
||||
userType: z.enum(['Admin', 'User']),
|
||||
computerName: z.string().optional()
|
||||
})
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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',
|
||||
@@ -100,6 +102,15 @@ const DEFAULT_CONFIG: FullConfig = {
|
||||
auditRetention: 30,
|
||||
appRetention: 14
|
||||
},
|
||||
seq: {
|
||||
enabled: false,
|
||||
serverUrl: '',
|
||||
apiKey: '',
|
||||
batchPostingLimit: 50,
|
||||
period: 2000,
|
||||
queueLimit: 10000,
|
||||
maxRetries: 3
|
||||
},
|
||||
rustfs: {
|
||||
enabled: false,
|
||||
endpoint: '',
|
||||
@@ -139,14 +150,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 +185,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 +216,29 @@ export class ConfigManager {
|
||||
this.config = validated
|
||||
|
||||
// Apply logging configuration
|
||||
setLogLevel(validated.logging.level)
|
||||
applyLoggingConfig(validated.logging, validated.seq)
|
||||
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 +251,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 +266,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 +339,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 +355,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 : '未知错误' }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
730
src/main/services/database/extractor-operation-history-dao.ts
Normal file
730
src/main/services/database/extractor-operation-history-dao.ts
Normal file
@@ -0,0 +1,730 @@
|
||||
/**
|
||||
* Data Access Object for ExtractorOperationHistory table
|
||||
*
|
||||
* Handles database operations for tracking extraction operation history:
|
||||
* - Batch record insertion
|
||||
* - Batch status updates
|
||||
* - Querying batches (with user filtering for non-admin users)
|
||||
* - Getting batch details
|
||||
* - Deleting batches
|
||||
*/
|
||||
|
||||
import { create, type IDatabaseService } from './index'
|
||||
import { createLogger, run, getRequestId, trackDuration } from '../logger'
|
||||
import type {
|
||||
OperationHistoryRecord,
|
||||
BatchStats,
|
||||
InsertBatchRecordInput,
|
||||
UpdateBatchStatusResult,
|
||||
GetBatchesOptions
|
||||
} from '../../types/operation-history.types'
|
||||
|
||||
const log = createLogger('ExtractorOperationHistoryDAO')
|
||||
|
||||
/**
|
||||
* Format datetime value from database to ISO string
|
||||
* mssql driver returns Date objects in UTC format
|
||||
*/
|
||||
function formatDateTime(value: unknown): string {
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString()
|
||||
}
|
||||
return value ? String(value) : new Date().toISOString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for ExtractorOperationHistory table
|
||||
*/
|
||||
export const EXTRACTOR_OPERATION_HISTORY_CONFIG = {
|
||||
TABLE_NAME_SQLSERVER: '[dbo].[ExtractorOperationHistory]',
|
||||
TABLE_NAME_MYSQL: 'dbo_ExtractorOperationHistory',
|
||||
COLUMNS: {
|
||||
ID: 'ID',
|
||||
BATCH_ID: 'BatchId',
|
||||
USER_ID: 'UserId',
|
||||
USERNAME: 'Username',
|
||||
PRODUCTION_ID: 'ProductionId',
|
||||
ORDER_NUMBER: 'OrderNumber',
|
||||
OPERATION_TIME: 'OperationTime',
|
||||
STATUS: 'Status',
|
||||
RECORD_COUNT: 'RecordCount',
|
||||
ERROR_MESSAGE: 'ErrorMessage'
|
||||
}
|
||||
} as const
|
||||
|
||||
/**
|
||||
* ExtractorOperationHistory DAO Class
|
||||
*/
|
||||
export class ExtractorOperationHistoryDAO {
|
||||
private dbService: IDatabaseService | null = null
|
||||
|
||||
/**
|
||||
* Get the appropriate table name based on database type
|
||||
*/
|
||||
private getTableName(): string {
|
||||
const isSqlServer = this.dbService?.type === 'sqlserver'
|
||||
return isSqlServer
|
||||
? EXTRACTOR_OPERATION_HISTORY_CONFIG.TABLE_NAME_SQLSERVER
|
||||
: EXTRACTOR_OPERATION_HISTORY_CONFIG.TABLE_NAME_MYSQL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get database service instance using DatabaseFactory
|
||||
*/
|
||||
private async getDatabaseService(): Promise<IDatabaseService> {
|
||||
if (this.dbService && this.dbService.isConnected()) {
|
||||
return this.dbService
|
||||
}
|
||||
|
||||
this.dbService = await create()
|
||||
return this.dbService
|
||||
}
|
||||
|
||||
/**
|
||||
* Build placeholders for IN clause based on database type
|
||||
*/
|
||||
private buildPlaceholders(count: number, isSqlServer: boolean): string {
|
||||
return isSqlServer
|
||||
? Array.from({ length: count }, (_, idx) => `@p${idx}`).join(',')
|
||||
: Array.from({ length: count }, () => '?').join(',')
|
||||
}
|
||||
|
||||
// ==================== INSERT ====================
|
||||
|
||||
/**
|
||||
* Insert batch records for a single extraction operation
|
||||
* @param batchId - Unique batch identifier
|
||||
* @param userId - User ID performing the operation
|
||||
* @param username - Username performing the operation
|
||||
* @param records - Array of order records to insert
|
||||
* @returns True if successful
|
||||
*/
|
||||
async insertBatchRecords(
|
||||
batchId: string,
|
||||
userId: number,
|
||||
username: string,
|
||||
records: InsertBatchRecordInput[]
|
||||
): Promise<boolean> {
|
||||
if (!records || records.length === 0) {
|
||||
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) {
|
||||
const sqlString = `
|
||||
INSERT INTO ${tableName}
|
||||
(BatchId, UserId, Username, ProductionId, OrderNumber, OperationTime, Status)
|
||||
VALUES
|
||||
(@p0, @p1, @p2, @p3, @p4, GETDATE(), 'pending')
|
||||
`
|
||||
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}
|
||||
(BatchId, UserId, Username, ProductionId, OrderNumber, OperationTime, Status)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, NOW(), 'pending')
|
||||
`
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== UPDATE ====================
|
||||
|
||||
/**
|
||||
* Update the status of all records in a batch
|
||||
* @param batchId - Batch identifier
|
||||
* @param status - New status (success, failed, partial)
|
||||
* @returns Update result
|
||||
*/
|
||||
async updateBatchStatus(batchId: string, status: string): Promise<UpdateBatchStatusResult> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
const sqlString = `
|
||||
UPDATE ${tableName}
|
||||
SET Status = ${isSqlServer ? '@p0' : '?'}
|
||||
WHERE BatchId = ${isSqlServer ? '@p1' : '?'}
|
||||
`
|
||||
const params = [status, batchId]
|
||||
|
||||
const result = await trackDuration(async () => await dbService.query(sqlString, params), {
|
||||
operationName: 'ExtractorOperationHistoryDAO.updateBatchStatus',
|
||||
context: { tableName, operationType: 'UPDATE', batchId }
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
return { success: false, updatedCount: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single record's status, error message, and optional record count
|
||||
* @param batchId - Batch identifier
|
||||
* @param orderNumber - Order number
|
||||
* @param status - New status
|
||||
* @param errorMessage - Optional error message
|
||||
* @param recordCount - Optional per-order record count
|
||||
* @returns True if successful
|
||||
*/
|
||||
async updateRecordStatus(
|
||||
batchId: string,
|
||||
orderNumber: string,
|
||||
status: string,
|
||||
errorMessage?: string,
|
||||
recordCount?: number
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
let sqlString: string
|
||||
let params: (string | number | null)[]
|
||||
|
||||
if (recordCount !== undefined) {
|
||||
sqlString = `
|
||||
UPDATE ${tableName}
|
||||
SET Status = ${isSqlServer ? '@p0' : '?'},
|
||||
ErrorMessage = ${isSqlServer ? '@p1' : '?'},
|
||||
RecordCount = ${isSqlServer ? '@p2' : '?'}
|
||||
WHERE BatchId = ${isSqlServer ? '@p3' : '?'}
|
||||
AND OrderNumber = ${isSqlServer ? '@p4' : '?'}
|
||||
`
|
||||
params = [status, errorMessage || null, recordCount, batchId, orderNumber]
|
||||
} else {
|
||||
sqlString = `
|
||||
UPDATE ${tableName}
|
||||
SET Status = ${isSqlServer ? '@p0' : '?'},
|
||||
ErrorMessage = ${isSqlServer ? '@p1' : '?'}
|
||||
WHERE BatchId = ${isSqlServer ? '@p2' : '?'}
|
||||
AND OrderNumber = ${isSqlServer ? '@p3' : '?'}
|
||||
`
|
||||
params = [status, errorMessage || null, batchId, orderNumber]
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== READ ====================
|
||||
|
||||
/**
|
||||
* 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, usernames)
|
||||
* @returns Array of batch statistics
|
||||
*/
|
||||
async getBatches(userId?: number, options?: GetBatchesOptions): Promise<BatchStats[]> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
let sqlString = `
|
||||
SELECT
|
||||
BatchId,
|
||||
UserId,
|
||||
Username,
|
||||
MIN(OperationTime) as OperationTime,
|
||||
MAX(Status) as Status,
|
||||
COUNT(*) as TotalOrders,
|
||||
SUM(COALESCE(RecordCount, 0)) as TotalRecords,
|
||||
SUM(CASE WHEN Status = 'success' THEN 1 ELSE 0 END) as SuccessCount,
|
||||
SUM(CASE WHEN Status = 'failed' THEN 1 ELSE 0 END) as FailedCount
|
||||
FROM ${tableName}
|
||||
`
|
||||
|
||||
const params: (number | string)[] = []
|
||||
|
||||
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 += `
|
||||
GROUP BY BatchId, UserId, Username
|
||||
ORDER BY OperationTime DESC
|
||||
`
|
||||
|
||||
if (options?.limit) {
|
||||
const safeLimit = Math.floor(options.limit)
|
||||
const safeOffset = options.offset !== undefined ? Math.floor(options.offset) : undefined
|
||||
|
||||
if (isSqlServer) {
|
||||
const offsetIndex = params.length
|
||||
if (safeOffset !== undefined) {
|
||||
params.push(safeOffset)
|
||||
}
|
||||
params.push(safeLimit)
|
||||
|
||||
if (safeOffset !== undefined) {
|
||||
sqlString += ` OFFSET @p${offsetIndex} ROWS FETCH NEXT @p${offsetIndex + 1} ROWS ONLY`
|
||||
} else {
|
||||
sqlString += ` OFFSET 0 ROWS FETCH NEXT @p${offsetIndex} ROWS ONLY`
|
||||
}
|
||||
} else {
|
||||
if (safeOffset !== undefined) {
|
||||
sqlString += ` LIMIT ${safeLimit} OFFSET ${safeOffset}`
|
||||
} else {
|
||||
sqlString += ` LIMIT ${safeLimit}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await trackDuration(async () => await dbService.query(sqlString, params), {
|
||||
operationName: 'ExtractorOperationHistoryDAO.getBatches',
|
||||
context: { tableName, operationType: 'SELECT', userId }
|
||||
})
|
||||
|
||||
return result.result.rows.map((row) => ({
|
||||
batchId: row.BatchId as string,
|
||||
userId: row.UserId as number,
|
||||
username: row.Username as string,
|
||||
operationTime: formatDateTime(row.OperationTime),
|
||||
status: row.Status as string,
|
||||
totalOrders: row.TotalOrders as number,
|
||||
totalRecords: (row.TotalRecords as number) || 0,
|
||||
successCount: (row.SuccessCount as number) || 0,
|
||||
failedCount: (row.FailedCount as number) || 0
|
||||
}))
|
||||
} catch (error) {
|
||||
log.error('Get batches error', {
|
||||
tableName: this.getTableName(),
|
||||
operationType: 'SELECT',
|
||||
requestId: getRequestId(),
|
||||
userId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get detailed records for a specific batch
|
||||
* @param batchId - Batch identifier
|
||||
* @returns Array of operation records
|
||||
*/
|
||||
async getBatchDetails(batchId: string): Promise<OperationHistoryRecord[]> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
const placeholder = isSqlServer ? '@p0' : '?'
|
||||
const sqlString = `
|
||||
SELECT
|
||||
ID,
|
||||
BatchId,
|
||||
UserId,
|
||||
Username,
|
||||
ProductionId,
|
||||
OrderNumber,
|
||||
OperationTime,
|
||||
Status,
|
||||
RecordCount,
|
||||
ErrorMessage
|
||||
FROM ${tableName}
|
||||
WHERE BatchId = ${placeholder}
|
||||
ORDER BY ID
|
||||
`
|
||||
|
||||
const result = await trackDuration(async () => await dbService.query(sqlString, [batchId]), {
|
||||
operationName: 'ExtractorOperationHistoryDAO.getBatchDetails',
|
||||
context: { tableName, operationType: 'SELECT', batchId }
|
||||
})
|
||||
|
||||
return result.result.rows.map((row) => ({
|
||||
id: row.ID as number,
|
||||
batchId: row.BatchId as string,
|
||||
userId: row.UserId as number,
|
||||
username: row.Username as string,
|
||||
productionId: row.ProductionId as string | null,
|
||||
orderNumber: row.OrderNumber as string,
|
||||
operationTime: new Date(row.OperationTime as string),
|
||||
status: row.Status as string,
|
||||
recordCount: row.RecordCount as number | null,
|
||||
errorMessage: row.ErrorMessage as string | null
|
||||
}))
|
||||
} catch (error) {
|
||||
log.error('Get batch details error', {
|
||||
tableName: this.getTableName(),
|
||||
operationType: 'SELECT',
|
||||
requestId: getRequestId(),
|
||||
batchId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single batch's statistics
|
||||
* @param batchId - Batch identifier
|
||||
* @returns Batch statistics or null
|
||||
*/
|
||||
async getBatchStats(batchId: string): Promise<BatchStats | null> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
const placeholder = isSqlServer ? '@p0' : '?'
|
||||
const sqlString = `
|
||||
SELECT
|
||||
BatchId,
|
||||
UserId,
|
||||
Username,
|
||||
MIN(OperationTime) as OperationTime,
|
||||
MAX(Status) as Status,
|
||||
COUNT(*) as TotalOrders,
|
||||
SUM(COALESCE(RecordCount, 0)) as TotalRecords,
|
||||
SUM(CASE WHEN Status = 'success' THEN 1 ELSE 0 END) as SuccessCount,
|
||||
SUM(CASE WHEN Status = 'failed' THEN 1 ELSE 0 END) as FailedCount
|
||||
FROM ${tableName}
|
||||
WHERE BatchId = ${placeholder}
|
||||
GROUP BY BatchId, UserId, Username
|
||||
`
|
||||
|
||||
const result = await trackDuration(async () => await dbService.query(sqlString, [batchId]), {
|
||||
operationName: 'ExtractorOperationHistoryDAO.getBatchStats',
|
||||
context: { tableName, operationType: 'SELECT', batchId }
|
||||
})
|
||||
|
||||
if (result.result.rows.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
const row = result.result.rows[0]
|
||||
return {
|
||||
batchId: row.BatchId as string,
|
||||
userId: row.UserId as number,
|
||||
username: row.Username as string,
|
||||
operationTime: formatDateTime(row.OperationTime),
|
||||
status: row.Status as string,
|
||||
totalOrders: row.TotalOrders as number,
|
||||
totalRecords: (row.TotalRecords as number) || 0,
|
||||
successCount: (row.SuccessCount as number) || 0,
|
||||
failedCount: (row.FailedCount as number) || 0
|
||||
}
|
||||
} catch (error) {
|
||||
log.error('Get batch stats error', {
|
||||
tableName: this.getTableName(),
|
||||
operationType: 'SELECT',
|
||||
requestId: getRequestId(),
|
||||
batchId,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== DELETE ====================
|
||||
|
||||
/**
|
||||
* Delete a batch with permission checking
|
||||
* @param batchId - Batch identifier
|
||||
* @param requestingUserId - User ID requesting the deletion
|
||||
* @param isAdmin - Whether the requesting user is an admin
|
||||
* @returns True if successful
|
||||
*/
|
||||
async deleteBatch(
|
||||
batchId: string,
|
||||
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()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
// First check if the batch exists and if the user has permission
|
||||
const batchStats = await this.getBatchStats(batchId)
|
||||
|
||||
if (!batchStats) {
|
||||
return { success: false, error: '批次不存在' }
|
||||
}
|
||||
|
||||
// Non-admin users can only delete their own batches
|
||||
if (!isAdmin && batchStats.userId !== requestingUserId) {
|
||||
return { success: false, error: '没有权限删除此批次' }
|
||||
}
|
||||
|
||||
// Delete the batch
|
||||
const placeholder = isSqlServer ? '@p0' : '?'
|
||||
const sqlString = `
|
||||
DELETE FROM ${tableName}
|
||||
WHERE BatchId = ${placeholder}
|
||||
`
|
||||
|
||||
const result = await trackDuration(async () => await dbService.query(sqlString, [batchId]), {
|
||||
operationName: 'ExtractorOperationHistoryDAO.deleteBatch',
|
||||
context: { tableName, operationType: 'DELETE', batchId, requestingUserId }
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all batches for a specific user
|
||||
* @param userId - User ID
|
||||
* @returns Number of batches deleted
|
||||
*/
|
||||
async deleteByUser(userId: number): Promise<number> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
const placeholder = isSqlServer ? '@p0' : '?'
|
||||
const sqlString = `
|
||||
DELETE FROM ${tableName}
|
||||
WHERE UserId = ${placeholder}
|
||||
`
|
||||
|
||||
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)
|
||||
})
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== UTILITIES ====================
|
||||
|
||||
/**
|
||||
* Check if a batch exists
|
||||
* @param batchId - Batch identifier
|
||||
* @returns True if batch exists
|
||||
*/
|
||||
async batchExists(batchId: string): Promise<boolean> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
const placeholder = isSqlServer ? '@p0' : '?'
|
||||
const sqlString = `
|
||||
SELECT COUNT(*) as count
|
||||
FROM ${tableName}
|
||||
WHERE BatchId = ${placeholder}
|
||||
`
|
||||
|
||||
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)
|
||||
})
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, usernames?: string[]): Promise<number> {
|
||||
try {
|
||||
const dbService = await this.getDatabaseService()
|
||||
const tableName = this.getTableName()
|
||||
const isSqlServer = dbService.type === 'sqlserver'
|
||||
|
||||
let sqlString = `
|
||||
SELECT COUNT(DISTINCT BatchId) as count
|
||||
FROM ${tableName}
|
||||
`
|
||||
|
||||
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 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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from database
|
||||
*/
|
||||
async disconnect(): Promise<void> {
|
||||
if (this.dbService) {
|
||||
await this.dbService.disconnect()
|
||||
this.dbService = null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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}`)
|
||||
}
|
||||
|
||||
@@ -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}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,6 +195,10 @@ export class ErpBrowserManager {
|
||||
async navigate(url: string, options?: { timeout?: number }): Promise<void> {
|
||||
const page = this.session?.page
|
||||
if (!page) {
|
||||
log.error('No page available for navigation', {
|
||||
url,
|
||||
hasSession: !!this.session
|
||||
})
|
||||
throw new Error('No page available. Call initialize() first.')
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,8 @@
|
||||
import { chromium } from 'playwright'
|
||||
import type { ErpConfig, ErpSession } from '../../types/erp.types'
|
||||
import { createLogger } from '../logger'
|
||||
import { capturePageContext } from './erp-error-context'
|
||||
import { attachPageDiagnostics, attachContextDiagnostics } from './page-diagnostics'
|
||||
|
||||
const log = createLogger('ErpAuthService')
|
||||
|
||||
@@ -29,6 +31,8 @@ export class ErpAuthService {
|
||||
return this.session
|
||||
}
|
||||
|
||||
log.info('开始ERP登录', { url: this.config.url })
|
||||
|
||||
// Launch browser with SSL certificate errors ignored
|
||||
const browser = await chromium.launch({
|
||||
headless: this.config.headless ?? false, // Use config or default to false
|
||||
@@ -41,6 +45,8 @@ export class ErpAuthService {
|
||||
]
|
||||
})
|
||||
|
||||
log.debug('浏览器已启动', { headless: this.config.headless ?? false })
|
||||
|
||||
const context = await browser.newContext({
|
||||
acceptDownloads: true,
|
||||
viewport: { width: 1920, height: 1080 },
|
||||
@@ -50,11 +56,15 @@ export class ErpAuthService {
|
||||
})
|
||||
|
||||
const page = await context.newPage()
|
||||
attachPageDiagnostics(page)
|
||||
attachContextDiagnostics(context)
|
||||
|
||||
// Navigate to login page (use actual login URL from Python code)
|
||||
const loginUrl = `${this.config.url}/yonbip/resources/uap/rbac/login/main/index.html`
|
||||
await page.goto(loginUrl)
|
||||
|
||||
log.debug('已导航到登录页面')
|
||||
|
||||
// Wait for page to load
|
||||
await page.waitForLoadState('domcontentloaded', { timeout: PAGE_LOAD_TIMEOUT })
|
||||
|
||||
@@ -68,8 +78,12 @@ export class ErpAuthService {
|
||||
// This is the main working frame for all subsequent operations
|
||||
const frameLocator = page.locator('#forwardFrame')
|
||||
const contentFrame = await frameLocator.contentFrame()
|
||||
log.debug('已获取 forwardFrame')
|
||||
|
||||
if (!contentFrame) {
|
||||
log.error('Failed to access forwardFrame content frame', {
|
||||
...(await capturePageContext(page))
|
||||
})
|
||||
throw new Error('Failed to access forwardFrame content frame')
|
||||
}
|
||||
|
||||
@@ -80,6 +94,10 @@ export class ErpAuthService {
|
||||
try {
|
||||
await contentFrame.getByRole('textbox', { name: '用户名' }).fill(this.config.username)
|
||||
} catch (e) {
|
||||
log.error('Failed to find username input', {
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
...(await capturePageContext(page, undefined, 'login.username'))
|
||||
})
|
||||
throw new Error(`Failed to find username input: ${e}`)
|
||||
}
|
||||
|
||||
@@ -87,6 +105,10 @@ export class ErpAuthService {
|
||||
try {
|
||||
await contentFrame.getByRole('textbox', { name: '密码' }).fill(this.config.password)
|
||||
} catch (e) {
|
||||
log.error('Failed to find password input', {
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
...(await capturePageContext(page, undefined, 'login.password'))
|
||||
})
|
||||
throw new Error(`Failed to find password input: ${e}`)
|
||||
}
|
||||
|
||||
@@ -94,6 +116,10 @@ export class ErpAuthService {
|
||||
try {
|
||||
await contentFrame.getByRole('button', { name: '登录' }).click()
|
||||
} catch (e) {
|
||||
log.error('Failed to click login button', {
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
...(await capturePageContext(page, undefined, 'login.button'))
|
||||
})
|
||||
throw new Error(`Failed to click login button: ${e}`)
|
||||
}
|
||||
|
||||
@@ -112,6 +138,8 @@ export class ErpAuthService {
|
||||
isLoggedIn: true
|
||||
}
|
||||
|
||||
log.info('ERP会话已建立')
|
||||
|
||||
return this.session
|
||||
}
|
||||
|
||||
@@ -138,6 +166,7 @@ export class ErpAuthService {
|
||||
|
||||
const hasError = await errorLocator.isVisible()
|
||||
if (hasError) {
|
||||
log.error('ERP login failed: incorrect username or password')
|
||||
throw new Error('ERP 登录失败:名称或密码错误')
|
||||
}
|
||||
|
||||
@@ -149,6 +178,7 @@ export class ErpAuthService {
|
||||
|
||||
const hasError = await errorLocator.isVisible().catch(() => false)
|
||||
if (hasError) {
|
||||
log.error('ERP login failed: incorrect username or password (retry check)')
|
||||
throw new Error('ERP 登录失败:名称或密码错误')
|
||||
}
|
||||
|
||||
@@ -161,6 +191,7 @@ export class ErpAuthService {
|
||||
*/
|
||||
async close(): Promise<void> {
|
||||
if (this.session) {
|
||||
log.info('正在关闭ERP会话')
|
||||
await this.session.context.close()
|
||||
await this.session.browser.close()
|
||||
this.session = null
|
||||
@@ -172,6 +203,7 @@ export class ErpAuthService {
|
||||
*/
|
||||
getSession(): ErpSession {
|
||||
if (!this.session?.isLoggedIn) {
|
||||
log.error('getSession called without active session')
|
||||
throw new Error('Not logged in. Call login() first.')
|
||||
}
|
||||
return this.session
|
||||
|
||||
104
src/main/services/erp/erp-error-context.ts
Normal file
104
src/main/services/erp/erp-error-context.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* ERP Error Context Capture
|
||||
*
|
||||
* Lightweight helper to capture Playwright page state when ERP operations fail.
|
||||
* All capture calls are defensive — failures do not propagate to the caller.
|
||||
*/
|
||||
|
||||
import type { Page } from 'playwright'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { getLogDir } from '../logger/shared'
|
||||
|
||||
export interface ErpErrorContext {
|
||||
pageUrl?: string
|
||||
frameHierarchy?: Array<{ name: string; url: string }>
|
||||
targetSelector?: string
|
||||
step?: string
|
||||
screenshotPath?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a step name for use as a filename component.
|
||||
* Replaces non-alphanumeric characters with underscores and truncates.
|
||||
*/
|
||||
function sanitizeForFilename(step: string | undefined): string {
|
||||
if (!step) return 'unknown'
|
||||
return step.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 40)
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture a screenshot of the page for error diagnostics.
|
||||
* Stored as PNG under <logDir>/screenshots/.
|
||||
* Defensive: never throws.
|
||||
*/
|
||||
async function captureScreenshot(page: Page, step?: string): Promise<string | undefined> {
|
||||
try {
|
||||
if (page.isClosed()) return undefined
|
||||
|
||||
const screenshotDir = path.join(getLogDir(), 'screenshots')
|
||||
fs.mkdirSync(screenshotDir, { recursive: true })
|
||||
|
||||
const now = new Date()
|
||||
const timestamp = [
|
||||
now.getFullYear(),
|
||||
String(now.getMonth() + 1).padStart(2, '0'),
|
||||
String(now.getDate()).padStart(2, '0'),
|
||||
'_',
|
||||
String(now.getHours()).padStart(2, '0'),
|
||||
String(now.getMinutes()).padStart(2, '0'),
|
||||
String(now.getSeconds()).padStart(2, '0')
|
||||
].join('')
|
||||
|
||||
const filename = `err_${timestamp}_${sanitizeForFilename(step)}.png`
|
||||
const filePath = path.join(screenshotDir, filename)
|
||||
|
||||
const buffer = await page.screenshot({ type: 'png', timeout: 5000 })
|
||||
fs.writeFileSync(filePath, buffer)
|
||||
|
||||
return filePath
|
||||
} catch {
|
||||
// screenshot failure must not propagate
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture the current state of a Playwright page for error logging.
|
||||
* Returns a plain object safe for structured logging.
|
||||
*
|
||||
* @param page - The Playwright page to inspect
|
||||
* @param targetSelector - Optional selector that was being targeted
|
||||
*/
|
||||
export async function capturePageContext(
|
||||
page: Page,
|
||||
targetSelector?: string,
|
||||
step?: string
|
||||
): Promise<ErpErrorContext> {
|
||||
const ctx: ErpErrorContext = {}
|
||||
|
||||
try {
|
||||
ctx.pageUrl = page.url()
|
||||
} catch {
|
||||
// page may be closed or inaccessible
|
||||
}
|
||||
|
||||
try {
|
||||
const frames = page.frames()
|
||||
ctx.frameHierarchy = frames.map((f) => ({ name: f.name(), url: f.url() }))
|
||||
} catch {
|
||||
// frame enumeration may fail on detached pages
|
||||
}
|
||||
|
||||
if (targetSelector) {
|
||||
ctx.targetSelector = targetSelector
|
||||
}
|
||||
|
||||
if (step) {
|
||||
ctx.step = step
|
||||
}
|
||||
|
||||
ctx.screenshotPath = await captureScreenshot(page, step)
|
||||
|
||||
return ctx
|
||||
}
|
||||
@@ -6,6 +6,10 @@ import type {
|
||||
ExtractorCoreResult,
|
||||
ExtractionProgress
|
||||
} from '../../types/extractor.types'
|
||||
import { createLogger } from '../logger'
|
||||
import { capturePageContext } from './erp-error-context'
|
||||
|
||||
const log = createLogger('ExtractorCore')
|
||||
|
||||
/**
|
||||
* ExtractorCore - Handles all web page operations for data extraction
|
||||
@@ -27,6 +31,11 @@ export class ExtractorCore {
|
||||
}
|
||||
|
||||
const totalBatches = this.createBatches(input.orderNumbers, input.batchSize).length
|
||||
log.info('开始下载所有批次', {
|
||||
totalOrders: input.orderNumbers.length,
|
||||
totalBatches,
|
||||
batchSize: input.batchSize
|
||||
})
|
||||
const totalPoints = 1 + totalBatches + 2
|
||||
const progressPerPoint = 100 / totalPoints
|
||||
|
||||
@@ -59,10 +68,16 @@ export class ExtractorCore {
|
||||
result.downloadedFiles.push(filePath)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Unknown error'
|
||||
log.error('批次下载失败', { batchIndex: i, totalBatches, error: message })
|
||||
result.errors.push(`Batch ${i + 1}: ${message}`)
|
||||
}
|
||||
}
|
||||
|
||||
log.info('所有批次下载完成', {
|
||||
downloadedCount: result.downloadedFiles.length,
|
||||
errorCount: result.errors.length
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -85,32 +100,44 @@ export class ExtractorCore {
|
||||
// Step 1: Click menu icon (Python line 266)
|
||||
// main_frame is #forwardFrame.content_frame returned from login
|
||||
await mainFrame.locator('i').first().click()
|
||||
log.debug('导航: 已点击菜单图标')
|
||||
|
||||
// Step 2: Click discrete material plan menu item and expect popup (Python lines 267-271)
|
||||
const popupPromise = page.waitForEvent('popup')
|
||||
await mainFrame.getByTitle('离散备料计划维护', { exact: true }).first().click()
|
||||
const popupPage = await popupPromise
|
||||
log.debug('导航: 弹出窗口已打开')
|
||||
|
||||
// Step 3 & 4: Get nested frame structure in popup window (Python lines 273-276)
|
||||
// popup page contains #forwardFrame, which contains #mainiframe
|
||||
const forwardFrameLocator = popupPage.locator('#forwardFrame')
|
||||
const fFrame = await forwardFrameLocator.contentFrame()
|
||||
log.debug('导航: 已获取 forwardFrame')
|
||||
|
||||
if (!fFrame) {
|
||||
log.error('Failed to access popup forward frame', {
|
||||
...(await capturePageContext(popupPage, undefined, 'navigate.forwardFrame'))
|
||||
})
|
||||
throw new Error('Failed to access popup forward frame')
|
||||
}
|
||||
|
||||
const innerFrameLocator = fFrame.locator('#mainiframe')
|
||||
await innerFrameLocator.waitFor({ state: 'visible', timeout: 15000 })
|
||||
const workFrame = await innerFrameLocator.contentFrame()
|
||||
log.debug('导航: 已获取内部工作框架')
|
||||
|
||||
if (!workFrame) {
|
||||
log.error('Failed to access inner work frame', {
|
||||
...(await capturePageContext(popupPage, undefined, 'navigate.innerFrame'))
|
||||
})
|
||||
throw new Error('Failed to access inner work frame')
|
||||
}
|
||||
|
||||
// Step 5: Setup query interface (Python line 278)
|
||||
await this.setupQueryInterface(workFrame)
|
||||
|
||||
log.info('提取器页面导航完成')
|
||||
|
||||
return { popupPage, workFrame }
|
||||
}
|
||||
|
||||
@@ -121,17 +148,21 @@ export class ExtractorCore {
|
||||
private async setupQueryInterface(innerFrame: any): Promise<void> {
|
||||
// Click search icon (Python line 233)
|
||||
await innerFrame.locator('.search-name-wrapper > .iconfont').click()
|
||||
log.debug('查询界面: 已点击搜索图标')
|
||||
|
||||
// Click "订单号查询" menu item (Python line 234)
|
||||
await innerFrame.getByText('订单号查询').click()
|
||||
log.debug('查询界面: 已点击订单号查询')
|
||||
|
||||
// Click "全部" tab (Python line 235)
|
||||
await innerFrame.getByRole('tab', { name: '全部' }).click()
|
||||
log.debug('查询界面: 已切换到全部标签页')
|
||||
|
||||
// Set limit to 5000 (Python lines 237-239)
|
||||
const inputBox = innerFrame.locator('#rc_select_0')
|
||||
await inputBox.fill('5000')
|
||||
await inputBox.press('Enter')
|
||||
log.debug('查询界面: 已设置查询限制为5000')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,16 +178,21 @@ export class ExtractorCore {
|
||||
_totalBatches: number,
|
||||
downloadDir: string
|
||||
): Promise<string> {
|
||||
log.info('开始下载批次', { batchIndex: batchIndex + 1, orderCount: orderNumbers.length })
|
||||
|
||||
// Fill order numbers (Python lines 143-145)
|
||||
const textbox = workFrame.getByRole('textbox', { name: '来源生产订单号' })
|
||||
await textbox.fill('')
|
||||
await textbox.fill(orderNumbers.join(','))
|
||||
log.debug('已填入订单号', { orderCount: orderNumbers.length })
|
||||
|
||||
// Click search button (Python line 147)
|
||||
await workFrame.locator('.search-component-searchBtn').click()
|
||||
log.debug('已点击搜索按钮')
|
||||
|
||||
// Wait for loading (Python lines 148-153)
|
||||
await this.waitForLoading(workFrame)
|
||||
log.debug('查询加载完成')
|
||||
|
||||
// Click first row checkbox (Python line 155)
|
||||
await workFrame.getByRole('row', { name: '序号' }).getByLabel('').click()
|
||||
@@ -181,6 +217,8 @@ export class ExtractorCore {
|
||||
const download = await downloadPromise
|
||||
await download.saveAs(downloadPath)
|
||||
|
||||
log.info('批次下载完成', { batchIndex: batchIndex + 1, downloadPath })
|
||||
|
||||
return downloadPath
|
||||
}
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -46,72 +47,109 @@ export class ExtractorService {
|
||||
downloadedFiles: [],
|
||||
mergedFile: null,
|
||||
recordCount: 0,
|
||||
errors: []
|
||||
errors: [],
|
||||
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
|
||||
|
||||
// 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' }
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,73 +157,117 @@ 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[]
|
||||
): Promise<{ mergedFile: string | null; recordCount: number; error?: string }> {
|
||||
filePaths: string[],
|
||||
orderNumbers: string[]
|
||||
): Promise<{
|
||||
mergedFile: string | null
|
||||
recordCount: number
|
||||
error?: string
|
||||
orderRecordCounts: Array<{ orderNumber: string; recordCount: number }>
|
||||
}> {
|
||||
if (filePaths.length === 0) {
|
||||
return { mergedFile: null, recordCount: 0 }
|
||||
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
|
||||
for (const order of allOrders) {
|
||||
recordCount += order.materials.length
|
||||
}
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
// 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 }
|
||||
} 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, error: `保存合并文件失败:${errorMsg}` }
|
||||
}
|
||||
return trackedResult.result
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -292,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
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -317,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
|
||||
}
|
||||
}
|
||||
|
||||
50
src/main/services/erp/page-diagnostics.ts
Normal file
50
src/main/services/erp/page-diagnostics.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Page Diagnostics
|
||||
*
|
||||
* Attaches browser console and error listeners to Playwright pages
|
||||
* so that ERP-side JS errors are visible in the application log.
|
||||
*
|
||||
* Only warning and error level console messages are captured —
|
||||
* ERP (YonBIP) outputs large volumes of info-level messages that
|
||||
* would drown the log.
|
||||
*/
|
||||
|
||||
import type { Page, BrowserContext } from 'playwright'
|
||||
import { createLogger } from '../logger'
|
||||
|
||||
const log = createLogger('BrowserDiagnostics')
|
||||
|
||||
/**
|
||||
* Attach console and error listeners to a single page.
|
||||
*/
|
||||
export function attachPageDiagnostics(page: Page): void {
|
||||
page.on('console', (msg) => {
|
||||
const type = msg.type()
|
||||
if (type !== 'warning' && type !== 'error') return
|
||||
|
||||
const location = msg.location()
|
||||
log.error(`[Browser ${type}] ${msg.text()}`, {
|
||||
pageUrl: page.url(),
|
||||
consoleType: type,
|
||||
location: location ? `${location.url}:${location.lineNumber}` : undefined
|
||||
})
|
||||
})
|
||||
|
||||
page.on('pageerror', (error) => {
|
||||
log.error(`[Browser pageerror] ${error.message}`, {
|
||||
pageUrl: page.url(),
|
||||
error: error.message,
|
||||
stack: error.stack
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach diagnostics to all current and future pages in a browser context.
|
||||
* Covers popups and pages opened by ERP automation.
|
||||
*/
|
||||
export function attachContextDiagnostics(context: BrowserContext): void {
|
||||
context.on('page', (page) => {
|
||||
attachPageDiagnostics(page)
|
||||
})
|
||||
}
|
||||
@@ -7,7 +7,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
|
||||
@@ -24,6 +24,8 @@ export interface AuditEntry {
|
||||
username: string
|
||||
/** Computer name from which the action was performed */
|
||||
computerName: string
|
||||
/** Application version when the action was performed */
|
||||
appVersion: string
|
||||
/** The resource that was affected (e.g., table name, file path) */
|
||||
resource: string
|
||||
/** Status of the action: 'success' | 'failure' | 'partial' */
|
||||
@@ -32,22 +34,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 +45,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 +89,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,13 +100,14 @@ export async function logAudit(
|
||||
status: 'success' | 'failure' | 'partial'
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
): Promise<void> {
|
||||
): void {
|
||||
const entry: AuditEntry = {
|
||||
timestamp: new Date().toISOString(),
|
||||
action,
|
||||
userId,
|
||||
username: details.username,
|
||||
computerName: details.computerName,
|
||||
appVersion: app.getVersion(),
|
||||
resource: details.resource,
|
||||
status: details.status,
|
||||
metadata: details.metadata || {}
|
||||
@@ -118,8 +121,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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,66 +11,184 @@
|
||||
import winston from 'winston'
|
||||
import DailyRotateFile from 'winston-daily-rotate-file'
|
||||
import path from 'path'
|
||||
import { app } from 'electron'
|
||||
import fs from 'fs'
|
||||
import os from 'os'
|
||||
import { app, BrowserWindow } from 'electron'
|
||||
import { serializeError, sanitizeError } from './error-utils'
|
||||
import { getLogDir, isProduction, cleanupOldScreenshots } from './shared'
|
||||
import { IPC_CHANNELS } from '../../../shared/ipc-channels'
|
||||
import { getContext, run } from './request-context'
|
||||
import { createSeqTransportSync } from './seq-transport'
|
||||
import type { SeqConfig } from '../../types/config.schema'
|
||||
|
||||
// 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
|
||||
// Custom log levels matching project semantics:
|
||||
// verbose (most detailed) → error (most severe)
|
||||
// Winston rule: logs with level value <= threshold are emitted.
|
||||
const PROJECT_LEVELS = {
|
||||
error: 0,
|
||||
warn: 1,
|
||||
info: 2,
|
||||
debug: 3,
|
||||
verbose: 4
|
||||
} as const
|
||||
|
||||
// Cache isProduction() at module load — app.isPackaged never changes at runtime
|
||||
const IS_PROD = isProduction()
|
||||
|
||||
/**
|
||||
* Check if an IPv4 address is an RFC 1918 private address.
|
||||
* Private ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
|
||||
*/
|
||||
function isPrivateIpv4(ip: string): boolean {
|
||||
const parts = ip.split('.').map(Number)
|
||||
if (parts.length !== 4) return false
|
||||
// 10.0.0.0/8
|
||||
if (parts[0] === 10) return true
|
||||
// 172.16.0.0/12
|
||||
if (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) return true
|
||||
// 192.168.0.0/16
|
||||
if (parts[0] === 192 && parts[1] === 168) return true
|
||||
return false
|
||||
}
|
||||
|
||||
// Check if running in production
|
||||
const isProduction = app?.isPackaged ?? process.env.NODE_ENV === 'production'
|
||||
/**
|
||||
* Get the primary local IPv4 address (RFC 1918 private address).
|
||||
*
|
||||
* Strategy:
|
||||
* 1. Collect all non-internal, non-APIPA IPv4 addresses from physical adapters
|
||||
* 2. Return the first private (LAN) address found
|
||||
* 3. Fallback to any remaining non-internal address
|
||||
* 4. Returns 'N/A' if none found
|
||||
*/
|
||||
function getLocalIpAddress(): string {
|
||||
const interfaces = os.networkInterfaces()
|
||||
const candidates: string[] = []
|
||||
|
||||
for (const [, addrs] of Object.entries(interfaces)) {
|
||||
if (!addrs) continue
|
||||
for (const iface of addrs) {
|
||||
if (iface.family === 'IPv4' && !iface.internal && !iface.address.startsWith('169.254.')) {
|
||||
candidates.push(iface.address)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Prefer private (LAN) addresses
|
||||
const privateIp = candidates.find(isPrivateIpv4)
|
||||
if (privateIp) return privateIp
|
||||
|
||||
// Fallback: any non-internal address
|
||||
return candidates[0] || 'N/A'
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
)
|
||||
}
|
||||
|
||||
// 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,53 +198,135 @@ 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({
|
||||
levels: PROJECT_LEVELS,
|
||||
level: 'info', // Default level, can be updated via setLogLevel()
|
||||
defaultMeta: { service: 'erpauto' },
|
||||
defaultMeta: {
|
||||
service: 'erpauto',
|
||||
appVersion: app.getVersion(),
|
||||
computerName: os.hostname(),
|
||||
ipAddress: getLocalIpAddress()
|
||||
},
|
||||
transports: [
|
||||
// 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
|
||||
* Also configures Seq transport if enabled
|
||||
*
|
||||
* @param config - Logging configuration from config.yaml
|
||||
* @param seqConfig - Optional Seq configuration from config.yaml
|
||||
*/
|
||||
export function applyLoggingConfig(
|
||||
config: { level: string; appRetention: number },
|
||||
seqConfig?: SeqConfig
|
||||
): 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
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
// Add Seq transport if configured and enabled
|
||||
// Note: Uses sync wrapper which initializes asynchronously
|
||||
if (seqConfig && seqConfig.enabled && seqConfig.serverUrl) {
|
||||
try {
|
||||
const seqTransport = createSeqTransportSync(seqConfig)
|
||||
if (seqTransport) {
|
||||
logger.add(seqTransport)
|
||||
logger.info('Seq transport added successfully', {
|
||||
serverUrl: seqConfig.serverUrl,
|
||||
batchPostingLimit: seqConfig.batchPostingLimit,
|
||||
period: seqConfig.period,
|
||||
context: 'seq-transport'
|
||||
})
|
||||
} else {
|
||||
// Transport not ready yet, will be initialized on next call
|
||||
logger.debug('Seq transport initialization in progress', {
|
||||
serverUrl: seqConfig.serverUrl,
|
||||
context: 'seq-transport'
|
||||
})
|
||||
// Initialize and add when ready
|
||||
import('./seq-transport').then(({ createSeqTransport }) => {
|
||||
createSeqTransport(seqConfig).then((transport) => {
|
||||
if (transport) {
|
||||
logger.add(transport)
|
||||
logger.info('Seq transport added (async init complete)', {
|
||||
serverUrl: seqConfig.serverUrl,
|
||||
context: 'seq-transport'
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Failed to add Seq transport', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
context: 'seq-transport'
|
||||
})
|
||||
// Don't throw - allow app to continue without Seq
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up old screenshot files beyond the retention window
|
||||
cleanupOldScreenshots(config.appRetention)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,25 +339,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'
|
||||
|
||||
340
src/main/services/logger/performance-monitor.ts
Normal file
340
src/main/services/logger/performance-monitor.ts
Normal 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)
|
||||
}
|
||||
152
src/main/services/logger/request-context.ts
Normal file
152
src/main/services/logger/request-context.ts
Normal 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)
|
||||
}
|
||||
131
src/main/services/logger/seq-transport.ts
Normal file
131
src/main/services/logger/seq-transport.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* Seq logging transport using @datalust/winston-seq
|
||||
*
|
||||
* Provides asynchronous batched logging to Seq server
|
||||
* with configurable parameters for production use.
|
||||
*
|
||||
* Features:
|
||||
* - Non-blocking async batch sending (via dynamic import)
|
||||
* - Configurable batch size, period, queue limits
|
||||
* - Automatic retry on failure
|
||||
* - Graceful shutdown with flush
|
||||
* - Error suppression (Seq failures don't crash app)
|
||||
*
|
||||
* Note: Uses dynamic import because @datalust/winston-seq is an ESM module
|
||||
*/
|
||||
|
||||
import type { SeqConfig } from '../../types/config.schema'
|
||||
import logger from './index'
|
||||
|
||||
/**
|
||||
* Seq transport constructor type (from dynamic import)
|
||||
*/
|
||||
type SeqTransportClass = new (options: {
|
||||
serverUrl: string
|
||||
apiKey?: string
|
||||
batchSizeLimit?: number
|
||||
maxBatchingTime?: number
|
||||
maxRetries?: number
|
||||
onError?: (error: Error) => void
|
||||
}) => any
|
||||
|
||||
/**
|
||||
* Create and configure Seq transport dynamically
|
||||
*
|
||||
* This function handles the ESM module loading and returns
|
||||
* a configured transport instance compatible with Winston.
|
||||
*
|
||||
* @param config - Seq configuration from config.schema
|
||||
* @returns Promise resolving to transport instance or null if disabled/failed
|
||||
*/
|
||||
export async function createSeqTransport(config: SeqConfig): Promise<any> {
|
||||
// Don't create transport if disabled
|
||||
if (!config.enabled || !config.serverUrl) {
|
||||
logger.info('Seq logging is disabled or server URL not provided', {
|
||||
enabled: config.enabled,
|
||||
hasServerUrl: !!config.serverUrl,
|
||||
context: 'seq-transport'
|
||||
})
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
// Dynamically import the ESM module
|
||||
const { SeqTransport } = await import('@datalust/winston-seq')
|
||||
const SeqTransportClass = SeqTransport as SeqTransportClass
|
||||
|
||||
// Create transport instance with mapped config
|
||||
const transport = new SeqTransportClass({
|
||||
serverUrl: config.serverUrl,
|
||||
apiKey: config.apiKey || undefined,
|
||||
batchSizeLimit: config.batchPostingLimit,
|
||||
maxBatchingTime: config.period,
|
||||
maxRetries: config.maxRetries,
|
||||
onError: (error: Error) => {
|
||||
// Log errors but don't throw - Seq failures should not crash app
|
||||
logger.error('Seq transport error', {
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
context: 'seq-transport'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
logger.info('Seq transport created successfully', {
|
||||
serverUrl: config.serverUrl,
|
||||
batchPostingLimit: config.batchPostingLimit,
|
||||
period: config.period,
|
||||
maxRetries: config.maxRetries,
|
||||
context: 'seq-transport'
|
||||
})
|
||||
|
||||
return transport
|
||||
} catch (error) {
|
||||
logger.error('Failed to create Seq transport', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
context: 'seq-transport'
|
||||
})
|
||||
// Return null to allow app to continue without Seq
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync factory wrapper for backward compatibility
|
||||
*
|
||||
* Note: This creates the transport asynchronously internally.
|
||||
* The transport is cached and reused.
|
||||
*/
|
||||
let cachedTransport: any = null
|
||||
let isInitializing = false
|
||||
|
||||
export function createSeqTransportSync(config: SeqConfig): any {
|
||||
if (!config.enabled || !config.serverUrl) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (cachedTransport) {
|
||||
return cachedTransport
|
||||
}
|
||||
|
||||
if (!isInitializing) {
|
||||
isInitializing = true
|
||||
createSeqTransport(config)
|
||||
.then((transport) => {
|
||||
cachedTransport = transport
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error('Seq transport initialization failed', {
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
})
|
||||
.finally(() => {
|
||||
isInitializing = false
|
||||
})
|
||||
}
|
||||
|
||||
// Return null on first call, transport will be available on next call
|
||||
return null
|
||||
}
|
||||
|
||||
export default createSeqTransportSync
|
||||
92
src/main/services/logger/shared.ts
Normal file
92
src/main/services/logger/shared.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete screenshot files older than the given retention period.
|
||||
* Scans <logDir>/screenshots/ and removes .png files whose mtime exceeds
|
||||
* the retention window. Defensive: never throws.
|
||||
*
|
||||
* @param retentionDays - Number of days to keep screenshots
|
||||
*/
|
||||
export function cleanupOldScreenshots(retentionDays: number): void {
|
||||
try {
|
||||
const screenshotDir = path.join(getLogDir(), 'screenshots')
|
||||
if (!fs.existsSync(screenshotDir)) return
|
||||
|
||||
const files = fs.readdirSync(screenshotDir)
|
||||
const cutoff = Date.now() - retentionDays * 24 * 60 * 60 * 1000
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.endsWith('.png')) continue
|
||||
const filePath = path.join(screenshotDir, file)
|
||||
try {
|
||||
const stat = fs.statSync(filePath)
|
||||
if (stat.mtimeMs < cutoff) {
|
||||
fs.unlinkSync(filePath)
|
||||
}
|
||||
} catch {
|
||||
// individual file deletion failure should not stop the loop
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// entire cleanup is best-effort
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
type GetObjectCommandInput,
|
||||
type DeleteObjectCommandInput
|
||||
} from '@aws-sdk/client-s3'
|
||||
import { createLogger } from '../logger'
|
||||
import { createLogger, run, trackDuration, PerformanceTracker } 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 {
|
||||
|
||||
@@ -29,7 +29,7 @@ export class UpdateCatalogService {
|
||||
|
||||
public getDialogCatalog(status: UpdateStatus, catalog: UpdateCatalog): UpdateDialogCatalog {
|
||||
const currentUserType = status.currentUserType
|
||||
if (!status.enabled || !currentUserType || currentUserType === 'Guest') {
|
||||
if (!status.enabled || !currentUserType) {
|
||||
return { mode: 'disabled' }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as fs from 'fs'
|
||||
import { ConfigManager } from '../config/config-manager'
|
||||
import { createLogger } from '../logger'
|
||||
import { createLogger, run, trackDuration, PerformanceTracker } 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
|
||||
}
|
||||
|
||||
@@ -106,7 +127,11 @@ export class UpdateService {
|
||||
this.ensureInitialized()
|
||||
this.status.currentUserType = userType
|
||||
|
||||
if (!this.status.enabled || !userType || userType === 'Guest') {
|
||||
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
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
@@ -139,7 +139,7 @@ export class BIPUsersDAO {
|
||||
return {
|
||||
id: row.ID as number,
|
||||
username: row.UserName as string,
|
||||
userType: row.UserType as 'Admin' | 'User' | 'Guest'
|
||||
userType: row.UserType as 'Admin' | 'User'
|
||||
}
|
||||
}
|
||||
return null
|
||||
@@ -157,16 +157,16 @@ export class BIPUsersDAO {
|
||||
return {
|
||||
id: row.ID as number,
|
||||
username: row.UserName as string,
|
||||
userType: row.UserType as 'Admin' | 'User' | 'Guest'
|
||||
userType: row.UserType as 'Admin' | 'User'
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -198,7 +198,7 @@ export class BIPUsersDAO {
|
||||
return {
|
||||
id: row.ID as number,
|
||||
username: row.UserName as string,
|
||||
userType: row.UserType as 'Admin' | 'User' | 'Guest'
|
||||
userType: row.UserType as 'Admin' | 'User'
|
||||
}
|
||||
}
|
||||
return null
|
||||
@@ -216,16 +216,16 @@ export class BIPUsersDAO {
|
||||
return {
|
||||
id: row.ID as number,
|
||||
username: row.UserName as string,
|
||||
userType: row.UserType as 'Admin' | 'User' | 'Guest'
|
||||
userType: row.UserType as 'Admin' | 'User'
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -254,13 +254,14 @@ export class BIPUsersDAO {
|
||||
return result.rows.map((row) => ({
|
||||
id: row.ID as number,
|
||||
username: row.UserName as string,
|
||||
userType: row.UserType as 'Admin' | 'User' | 'Guest',
|
||||
userType: row.UserType as 'Admin' | 'User',
|
||||
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 []
|
||||
}
|
||||
@@ -270,7 +271,7 @@ export class BIPUsersDAO {
|
||||
* Create a new user
|
||||
* @param username - The username (must be unique)
|
||||
* @param password - The password
|
||||
* @param userType - User type ('Admin', 'User', or 'Guest')
|
||||
* @param userType - User type ('Admin' or 'User')
|
||||
* @param computerName - Optional computer name for silent login
|
||||
* @returns True if successful
|
||||
*/
|
||||
@@ -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 []
|
||||
}
|
||||
|
||||
@@ -17,6 +17,9 @@ import { dirname } from 'path'
|
||||
import { ConfigManager } from '../../config/config-manager'
|
||||
import { MySqlService } from '../../database/mysql'
|
||||
import { SqlServerService } from '../../database/sql-server'
|
||||
import { createLogger } from '../../logger'
|
||||
|
||||
const log = createLogger('Migration')
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = dirname(__filename)
|
||||
@@ -136,7 +139,9 @@ async function runMySQLMigration(configManager: ConfigManager): Promise<void> {
|
||||
|
||||
await mysqlService.disconnect()
|
||||
} catch (error) {
|
||||
console.error('✗ MySQL Migration failed:', error instanceof Error ? error.message : error)
|
||||
log.error('MySQL Migration failed', {
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
if (mysqlService.isConnected()) {
|
||||
await mysqlService.disconnect()
|
||||
}
|
||||
@@ -192,7 +197,9 @@ async function runSqlServerMigration(configManager: ConfigManager): Promise<void
|
||||
|
||||
await sqlServerService.disconnect()
|
||||
} catch (error) {
|
||||
console.error('✗ SQL Server Migration failed:', error instanceof Error ? error.message : error)
|
||||
log.error('SQL Server Migration failed', {
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
if (sqlServerService.isConnected()) {
|
||||
await sqlServerService.disconnect()
|
||||
}
|
||||
@@ -223,7 +230,7 @@ async function main(): Promise<void> {
|
||||
|
||||
console.log('\n✅ Migration completed successfully!\n')
|
||||
} catch (error) {
|
||||
console.error('\n❌ Migration failed:', error instanceof Error ? error.message : error)
|
||||
log.error('Migration failed', { error: error instanceof Error ? error.message : String(error) })
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@ import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import yaml from 'js-yaml'
|
||||
import { z } from 'zod'
|
||||
import { createLogger } from '../../logger'
|
||||
|
||||
const log = createLogger('MigrationRunner')
|
||||
|
||||
/**
|
||||
* MySQL configuration schema
|
||||
@@ -105,8 +108,10 @@ async function runMigration(): Promise<void> {
|
||||
try {
|
||||
dbConfig = loadConfig(configPath)
|
||||
} catch (error) {
|
||||
console.error('Failed to load config.yaml:', error instanceof Error ? error.message : error)
|
||||
console.error('Please ensure config.yaml exists and contains valid MySQL configuration.')
|
||||
log.error('Failed to load config', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
configPath
|
||||
})
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
@@ -171,8 +176,7 @@ async function runMigration(): Promise<void> {
|
||||
console.log(` ERP_Password = 'your_password'`)
|
||||
console.log(` WHERE ERP_URL IS NULL;\n`)
|
||||
} catch (error) {
|
||||
console.error('\n❌ Migration failed with error:')
|
||||
console.error(error)
|
||||
log.error('Migration failed', { error })
|
||||
console.error('\nTroubleshooting:')
|
||||
console.error('1. Check if MySQL server is running')
|
||||
console.error('2. Verify database credentials in config.yaml file')
|
||||
@@ -194,6 +198,6 @@ async function runMigration(): Promise<void> {
|
||||
|
||||
// Run migration
|
||||
runMigration().catch((error) => {
|
||||
console.error('Unexpected error:', error)
|
||||
log.error('Unexpected error', { error })
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
*/
|
||||
|
||||
import type { UserInfo } from '../../types/user.types'
|
||||
import { createLogger } from '../logger'
|
||||
|
||||
const log = createLogger('SessionManager')
|
||||
|
||||
/**
|
||||
* Session Manager Class
|
||||
@@ -59,12 +62,12 @@ export class SessionManager {
|
||||
}
|
||||
return false
|
||||
} catch (error) {
|
||||
console.error('[SessionManager] Login error:', error)
|
||||
log.error('Login error', { error })
|
||||
return false
|
||||
} finally {
|
||||
if (dao) {
|
||||
await dao.disconnect().catch((error) => {
|
||||
console.error('[SessionManager] Login disconnect error:', error)
|
||||
log.error('Login disconnect error', { error })
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -93,12 +96,12 @@ export class SessionManager {
|
||||
}
|
||||
return false
|
||||
} catch (error) {
|
||||
console.error('[SessionManager] Silent login error:', error)
|
||||
log.error('Silent login error', { error })
|
||||
return false
|
||||
} finally {
|
||||
if (dao) {
|
||||
await dao.disconnect().catch((error) => {
|
||||
console.error('[SessionManager] Silent login disconnect error:', error)
|
||||
log.error('Silent login disconnect error', { error })
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -126,13 +129,6 @@ export class SessionManager {
|
||||
return this.currentUser?.userType === 'Admin'
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user is a guest
|
||||
*/
|
||||
public isGuest(): boolean {
|
||||
return this.currentUser?.userType === 'Guest'
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current username
|
||||
*/
|
||||
@@ -194,12 +190,12 @@ export class SessionManager {
|
||||
dao = new BIPUsersDAO()
|
||||
return await dao.getAllUsers()
|
||||
} catch (error) {
|
||||
console.error('[SessionManager] Get all users error:', error)
|
||||
log.error('Get all users error', { error })
|
||||
return []
|
||||
} finally {
|
||||
if (dao) {
|
||||
await dao.disconnect().catch((error) => {
|
||||
console.error('[SessionManager] Get all users disconnect error:', error)
|
||||
log.error('Get all users disconnect error', { error })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
27
src/main/tools/debug-env.ts
Normal file
27
src/main/tools/debug-env.ts
Normal 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()
|
||||
@@ -35,6 +35,8 @@ export interface AuditEntry {
|
||||
username: string
|
||||
/** Computer name where action was performed */
|
||||
computerName: string
|
||||
/** Application version when action was performed */
|
||||
appVersion: string
|
||||
/** Resource affected by the action */
|
||||
resource?: string
|
||||
/** Status of the action */
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
/**
|
||||
@@ -131,6 +132,21 @@ export const loggingConfigSchema = z.object({
|
||||
appRetention: z.number().int().min(1).max(365).default(14)
|
||||
})
|
||||
|
||||
/**
|
||||
* Seq 日志聚合服务配置 Schema
|
||||
*/
|
||||
export const seqConfigSchema = z.object({
|
||||
enabled: z.boolean().default(false),
|
||||
serverUrl: z.string().url('Seq server URL must be a valid URL').optional().default(''),
|
||||
apiKey: z.string().optional().default(''),
|
||||
batchPostingLimit: z.number().int().min(1).max(1000).default(50),
|
||||
period: z.number().int().min(1000).max(30000).default(2000), // milliseconds
|
||||
queueLimit: z.number().int().min(1).max(10000).default(10000),
|
||||
maxRetries: z.number().int().min(0).max(10).default(3)
|
||||
})
|
||||
|
||||
export type SeqConfig = z.infer<typeof seqConfigSchema>
|
||||
|
||||
/**
|
||||
* RustFS 对象存储配置 Schema
|
||||
*/
|
||||
@@ -171,6 +187,7 @@ export const fullConfigSchema = z.object({
|
||||
cleaner: cleanerConfigSchema,
|
||||
orderResolution: orderResolutionSchema,
|
||||
logging: loggingConfigSchema,
|
||||
seq: seqConfigSchema.optional(),
|
||||
rustfs: rustfsConfigSchema.optional(),
|
||||
update: updateConfigSchema.optional()
|
||||
})
|
||||
|
||||
@@ -43,6 +43,8 @@ export interface ExtractorResult {
|
||||
errors: string[]
|
||||
/** Database import result (only populated if mergedFile was created) */
|
||||
importResult?: ImportResult
|
||||
/** Per-order material row counts */
|
||||
orderRecordCounts: Array<{ orderNumber: string; recordCount: number }>
|
||||
}
|
||||
|
||||
export interface OrderInfo {
|
||||
|
||||
244
src/main/types/logger.types.ts
Normal file
244
src/main/types/logger.types.ts
Normal 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
|
||||
}
|
||||
}
|
||||
88
src/main/types/operation-history.types.ts
Normal file
88
src/main/types/operation-history.types.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Operation History Type Definitions
|
||||
*
|
||||
* Type definitions for the Extractor Operation History feature.
|
||||
* Tracks extraction operations with batch and individual order record details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Individual operation history record
|
||||
*/
|
||||
export interface OperationHistoryRecord {
|
||||
/** Auto-increment ID */
|
||||
id?: number
|
||||
/** Batch ID - shared among all orders in a single extraction operation */
|
||||
batchId: string
|
||||
/** User ID who performed the operation */
|
||||
userId: number
|
||||
/** Username who performed the operation */
|
||||
username: string
|
||||
/** Original input production ID (e.g., "22A1"), null if input was already an order number */
|
||||
productionId: string | null
|
||||
/** Resolved order number (e.g., "SC70202602120085") */
|
||||
orderNumber: string
|
||||
/** When the operation was performed */
|
||||
operationTime: Date
|
||||
/** Operation status: pending, success, failed, partial */
|
||||
status: string
|
||||
/** Number of records extracted for this order */
|
||||
recordCount: number | null
|
||||
/** Error message if operation failed */
|
||||
errorMessage: string | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch statistics - aggregated view of a batch operation
|
||||
*/
|
||||
export interface BatchStats {
|
||||
/** Unique batch identifier */
|
||||
batchId: string
|
||||
/** User ID who performed the operation */
|
||||
userId: number
|
||||
/** Username who performed the operation */
|
||||
username: string
|
||||
/** When the operation started */
|
||||
operationTime: string
|
||||
/** Overall batch status: pending, success, failed, partial */
|
||||
status: string
|
||||
/** Total number of orders in the batch */
|
||||
totalOrders: number
|
||||
/** Total records extracted across all orders */
|
||||
totalRecords: number
|
||||
/** Number of orders that succeeded */
|
||||
successCount: number
|
||||
/** Number of orders that failed */
|
||||
failedCount: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Input for inserting batch records
|
||||
*/
|
||||
export interface InsertBatchRecordInput {
|
||||
/** Original input production ID (e.g., "22A1") */
|
||||
productionId: string | null
|
||||
/** Resolved order number (e.g., "SC70202602120085") */
|
||||
orderNumber: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Result for batch status update
|
||||
*/
|
||||
export interface UpdateBatchStatusResult {
|
||||
/** Whether the update was successful */
|
||||
success: boolean
|
||||
/** Number of records updated */
|
||||
updatedCount: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for querying batches
|
||||
*/
|
||||
export interface GetBatchesOptions {
|
||||
/** Maximum number of batches to return */
|
||||
limit?: number
|
||||
/** Number of batches to skip (for pagination) */
|
||||
offset?: number
|
||||
/** Optional username filter for Admin users (supports multiple) */
|
||||
usernames?: string[]
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
/**
|
||||
* User type for settings permission control
|
||||
*/
|
||||
export type UserType = 'Admin' | 'User' | 'Guest'
|
||||
export type UserType = 'Admin' | 'User'
|
||||
|
||||
/**
|
||||
* Database type selection
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
/**
|
||||
* User type enumeration
|
||||
*/
|
||||
export type UserType = 'Admin' | 'User' | 'Guest'
|
||||
export type UserType = 'Admin' | 'User'
|
||||
|
||||
/**
|
||||
* User information interface
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
} from './materials'
|
||||
import { loggerApi } from './logger'
|
||||
import { playwrightBrowserApi } from './browser-download'
|
||||
import { operationHistoryApi } from './operation-history'
|
||||
|
||||
export const api = {
|
||||
process: processApi,
|
||||
@@ -35,7 +36,8 @@ export const api = {
|
||||
logger: loggerApi,
|
||||
report: reportApi,
|
||||
update: updateApi,
|
||||
playwrightBrowser: playwrightBrowserApi
|
||||
playwrightBrowser: playwrightBrowserApi,
|
||||
operationHistory: operationHistoryApi
|
||||
} as const
|
||||
|
||||
export type ElectronApi = typeof api
|
||||
|
||||
@@ -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
|
||||
|
||||
30
src/preload/api/operation-history.ts
Normal file
30
src/preload/api/operation-history.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { IPC_CHANNELS } from '../../shared/ipc-channels'
|
||||
import { invokeIpc } from '../lib/ipc'
|
||||
import type {
|
||||
BatchStats,
|
||||
OperationHistoryRecord,
|
||||
GetBatchesOptions
|
||||
} from '../../main/types/operation-history.types'
|
||||
import type { IpcResult } from '../../main/types/ipc.types'
|
||||
|
||||
export const operationHistoryApi = {
|
||||
/**
|
||||
* Get list of operation batches
|
||||
* Admin users receive all batches, regular users only their own
|
||||
*/
|
||||
getBatches: (options?: GetBatchesOptions): Promise<IpcResult<BatchStats[]>> =>
|
||||
invokeIpc(IPC_CHANNELS.OPERATION_HISTORY_GET_BATCHES, options),
|
||||
|
||||
/**
|
||||
* Get detailed records for a specific batch
|
||||
*/
|
||||
getBatchDetails: (batchId: string): Promise<IpcResult<OperationHistoryRecord[]>> =>
|
||||
invokeIpc(IPC_CHANNELS.OPERATION_HISTORY_GET_BATCH_DETAILS, batchId),
|
||||
|
||||
/**
|
||||
* Delete a batch
|
||||
* Admin users can delete any batch, regular users only their own
|
||||
*/
|
||||
deleteBatch: (batchId: string): Promise<IpcResult<{ deleted: boolean }>> =>
|
||||
invokeIpc(IPC_CHANNELS.OPERATION_HISTORY_DELETE_BATCH, batchId)
|
||||
} as const
|
||||
34
src/preload/index.d.ts
vendored
34
src/preload/index.d.ts
vendored
@@ -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 {
|
||||
@@ -157,6 +159,37 @@ export interface PlaywrightBrowserAPI {
|
||||
onProgress: (callback: (data: DownloadProgress) => void) => () => void
|
||||
}
|
||||
|
||||
export interface OperationHistoryAPI {
|
||||
getBatches: (options?: { limit?: number; offset?: number }) => Promise<IpcResult<BatchStats[]>>
|
||||
getBatchDetails: (batchId: string) => Promise<IpcResult<OperationHistoryRecord[]>>
|
||||
deleteBatch: (batchId: string) => Promise<IpcResult<{ deleted: boolean }>>
|
||||
}
|
||||
|
||||
export interface BatchStats {
|
||||
batchId: string
|
||||
userId: number
|
||||
username: string
|
||||
operationTime: string
|
||||
status: string
|
||||
totalOrders: number
|
||||
totalRecords: number
|
||||
successCount: number
|
||||
failedCount: number
|
||||
}
|
||||
|
||||
export interface OperationHistoryRecord {
|
||||
id?: number
|
||||
batchId: string
|
||||
userId: number
|
||||
username: string
|
||||
productionId: string | null
|
||||
orderNumber: string
|
||||
operationTime: Date
|
||||
status: string
|
||||
recordCount: number | null
|
||||
errorMessage: string | null
|
||||
}
|
||||
|
||||
export interface ProcessAPI {
|
||||
versions: {
|
||||
electron: string
|
||||
@@ -185,6 +218,7 @@ declare global {
|
||||
report: ReportAPI
|
||||
update: UpdateAPI
|
||||
playwrightBrowser: PlaywrightBrowserAPI
|
||||
operationHistory: OperationHistoryAPI
|
||||
}
|
||||
api: unknown
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React from 'react'
|
||||
import { AuthenticatedAppShell } from './components/app/AuthenticatedAppShell'
|
||||
import { UnauthenticatedApp } from './components/app/UnauthenticatedApp'
|
||||
import { ErrorBoundary } from './components/ErrorBoundary'
|
||||
import PlaywrightDownloadDialog from './components/PlaywrightDownloadDialog'
|
||||
import { useAppBootstrap } from './hooks/useAppBootstrap'
|
||||
|
||||
@@ -49,51 +50,57 @@ function App(): React.JSX.Element {
|
||||
// Show Playwright download dialog first (before authentication check)
|
||||
if (showPlaywrightDownload) {
|
||||
return (
|
||||
<PlaywrightDownloadDialog
|
||||
isOpen={showPlaywrightDownload}
|
||||
onClose={() => {}}
|
||||
onDownloadComplete={handlePlaywrightDownloadComplete}
|
||||
/>
|
||||
<ErrorBoundary scope="PlaywrightDownload">
|
||||
<PlaywrightDownloadDialog
|
||||
isOpen={showPlaywrightDownload}
|
||||
onClose={() => {}}
|
||||
onDownloadComplete={handlePlaywrightDownloadComplete}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return (
|
||||
<UnauthenticatedApp
|
||||
isAuthenticating={isAuthenticating}
|
||||
showLoginDialog={showLoginDialog}
|
||||
showUserSelection={showUserSelection}
|
||||
computerName={computerName}
|
||||
currentUser={currentUser}
|
||||
allUsers={allUsers}
|
||||
errorMessage={errorMessage}
|
||||
onLogin={handleLogin}
|
||||
onLoginCancel={handleLoginCancel}
|
||||
onSelectUser={handleUserSelect}
|
||||
onUserSelectionCancel={handleUserSelectionCancel}
|
||||
onError={showError}
|
||||
logoutButtonRef={logoutButtonRef}
|
||||
/>
|
||||
<ErrorBoundary scope="UnauthenticatedApp">
|
||||
<UnauthenticatedApp
|
||||
isAuthenticating={isAuthenticating}
|
||||
showLoginDialog={showLoginDialog}
|
||||
showUserSelection={showUserSelection}
|
||||
computerName={computerName}
|
||||
currentUser={currentUser}
|
||||
allUsers={allUsers}
|
||||
errorMessage={errorMessage}
|
||||
onLogin={handleLogin}
|
||||
onLoginCancel={handleLoginCancel}
|
||||
onSelectUser={handleUserSelect}
|
||||
onUserSelectionCancel={handleUserSelectionCancel}
|
||||
onError={showError}
|
||||
logoutButtonRef={logoutButtonRef}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthenticatedAppShell
|
||||
currentUser={currentUser}
|
||||
currentPage={currentPage}
|
||||
onNavigate={setCurrentPage}
|
||||
updateStatus={updateStatus}
|
||||
updateCatalog={updateCatalog}
|
||||
showUpdateDialog={showUpdateDialog}
|
||||
onOpenUpdateDialog={openUpdateDialog}
|
||||
onCloseUpdateDialog={() => setShowUpdateDialog(false)}
|
||||
onInstallUserRelease={handleInstallUserRelease}
|
||||
onDownloadAndInstallAdminRelease={handleAdminDownloadAndInstall}
|
||||
onRefreshCatalog={refreshUpdateDialogState}
|
||||
shouldShowLogout={shouldShowLogout}
|
||||
onLogout={handleLogout}
|
||||
logoutButtonRef={logoutButtonRef}
|
||||
/>
|
||||
<ErrorBoundary scope="AuthenticatedApp">
|
||||
<AuthenticatedAppShell
|
||||
currentUser={currentUser}
|
||||
currentPage={currentPage}
|
||||
onNavigate={setCurrentPage}
|
||||
updateStatus={updateStatus}
|
||||
updateCatalog={updateCatalog}
|
||||
showUpdateDialog={showUpdateDialog}
|
||||
onOpenUpdateDialog={openUpdateDialog}
|
||||
onCloseUpdateDialog={() => setShowUpdateDialog(false)}
|
||||
onInstallUserRelease={handleInstallUserRelease}
|
||||
onDownloadAndInstallAdminRelease={handleAdminDownloadAndInstall}
|
||||
onRefreshCatalog={refreshUpdateDialogState}
|
||||
shouldShowLogout={shouldShowLogout}
|
||||
onLogout={handleLogout}
|
||||
logoutButtonRef={logoutButtonRef}
|
||||
/>
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
96
src/renderer/src/components/ErrorBoundary.tsx
Normal file
96
src/renderer/src/components/ErrorBoundary.tsx
Normal file
@@ -0,0 +1,96 @@
|
||||
import React from 'react'
|
||||
import { AlertTriangle, RotateCcw } from 'lucide-react'
|
||||
|
||||
interface ErrorBoundaryProps {
|
||||
children: React.ReactNode
|
||||
/** Optional label identifying the boundary scope (e.g. "App", "AuthenticatedShell") */
|
||||
scope?: string
|
||||
}
|
||||
|
||||
interface ErrorBoundaryState {
|
||||
hasError: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
/**
|
||||
* React Error Boundary that catches rendering errors in child components,
|
||||
* logs the full error + component stack to the main process logger,
|
||||
* and displays a fallback UI.
|
||||
*
|
||||
* Cannot use hooks (React constraint), so calls window.electron.logger directly.
|
||||
*/
|
||||
export class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
||||
constructor(props: ErrorBoundaryProps) {
|
||||
super(props)
|
||||
this.state = { hasError: false, error: null }
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
|
||||
return { hasError: true, error }
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
|
||||
const scope = this.props.scope || 'Unknown'
|
||||
|
||||
// Log to main process via IPC logger
|
||||
try {
|
||||
if (typeof window !== 'undefined' && window.electron?.logger?.log) {
|
||||
window.electron.logger.log('error', `Render error in <${scope}>`, {
|
||||
error: {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack
|
||||
},
|
||||
componentStack: errorInfo.componentStack,
|
||||
boundaryScope: scope
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
// Logging failed — don't make things worse
|
||||
}
|
||||
|
||||
// Also print to console in development for immediate visibility
|
||||
if (import.meta.env.DEV) {
|
||||
console.error(`[ErrorBoundary:${scope}]`, error, errorInfo.componentStack)
|
||||
}
|
||||
}
|
||||
|
||||
handleReload = (): void => {
|
||||
this.setState({ hasError: false, error: null })
|
||||
}
|
||||
|
||||
render(): React.ReactNode {
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-slate-50 p-8">
|
||||
<div className="w-full max-w-md rounded-xl border border-slate-200 bg-white p-8 text-center shadow-lg">
|
||||
<div className="mx-auto mb-4 flex h-14 w-14 items-center justify-center rounded-full bg-rose-100">
|
||||
<AlertTriangle size={28} className="text-rose-600" />
|
||||
</div>
|
||||
<h2 className="mb-2 text-xl font-bold text-slate-800">页面出现错误</h2>
|
||||
<p className="mb-4 text-sm text-slate-500">
|
||||
应用发生了未预期的错误,请尝试刷新页面。如果问题持续存在,请联系管理员。
|
||||
</p>
|
||||
<details className="mb-6 text-left">
|
||||
<summary className="cursor-pointer text-xs text-slate-400 hover:text-slate-600">
|
||||
错误详情
|
||||
</summary>
|
||||
<pre className="mt-2 max-h-40 overflow-auto rounded-lg bg-slate-100 p-3 text-xs text-slate-700">
|
||||
{this.state.error?.message}
|
||||
</pre>
|
||||
</details>
|
||||
<button
|
||||
onClick={this.handleReload}
|
||||
className="inline-flex items-center gap-2 rounded-lg bg-blue-600 px-5 py-2.5 text-sm font-medium text-white transition hover:bg-blue-700"
|
||||
>
|
||||
<RotateCcw size={16} />
|
||||
刷新页面
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return this.props.children
|
||||
}
|
||||
}
|
||||
510
src/renderer/src/components/ExtractorOperationHistoryModal.tsx
Normal file
510
src/renderer/src/components/ExtractorOperationHistoryModal.tsx
Normal file
@@ -0,0 +1,510 @@
|
||||
/**
|
||||
* Extractor Operation History Modal
|
||||
*
|
||||
* Displays extraction operation history with batch statistics and details.
|
||||
* Admin users see all users' records, regular users see only their own.
|
||||
*/
|
||||
|
||||
import React, { useState, useEffect, useCallback } from 'react'
|
||||
import { Modal } from './ui/Modal'
|
||||
import { useLogger } from '../hooks/useLogger'
|
||||
import {
|
||||
RefreshCw,
|
||||
Trash2,
|
||||
ChevronDown,
|
||||
ChevronRight,
|
||||
CheckCircle,
|
||||
XCircle,
|
||||
Clock,
|
||||
Copy
|
||||
} from 'lucide-react'
|
||||
import type { UserInfo } from './UserSelectionDialog'
|
||||
import type {
|
||||
BatchStats,
|
||||
OperationHistoryRecord
|
||||
} from '../../../main/types/operation-history.types'
|
||||
import { showSuccess, showError, showWarning } from '../stores/useAppStore'
|
||||
|
||||
interface ExtractorOperationHistoryModalProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
user?: UserInfo | null
|
||||
}
|
||||
|
||||
const statusStyles: Record<string, string> = {
|
||||
success: 'bg-green-100 text-green-700',
|
||||
partial: 'bg-amber-100 text-amber-700',
|
||||
failed: 'bg-red-100 text-red-700',
|
||||
pending: 'bg-gray-100 text-gray-700'
|
||||
}
|
||||
|
||||
const statusLabels: Record<string, string> = {
|
||||
success: '成功',
|
||||
partial: '部分成功',
|
||||
failed: '失败',
|
||||
pending: '进行中'
|
||||
}
|
||||
|
||||
const statusIcons: Record<string, React.ReactNode> = {
|
||||
success: <CheckCircle size={16} className="text-green-600" />,
|
||||
partial: <Clock size={16} className="text-amber-600" />,
|
||||
failed: <XCircle size={16} className="text-red-600" />,
|
||||
pending: <Clock size={16} className="text-gray-500" />
|
||||
}
|
||||
|
||||
const formatDateTime = (dateStr: string) => {
|
||||
const date = new Date(dateStr)
|
||||
|
||||
// Check if the date is valid
|
||||
if (isNaN(date.getTime())) {
|
||||
return dateStr // Return original if invalid
|
||||
}
|
||||
|
||||
// Use UTC methods to display the time as stored in database (without timezone conversion)
|
||||
const year = date.getUTCFullYear()
|
||||
const month = String(date.getUTCMonth() + 1).padStart(2, '0')
|
||||
const day = String(date.getUTCDate()).padStart(2, '0')
|
||||
const hours = String(date.getUTCHours()).padStart(2, '0')
|
||||
const minutes = String(date.getUTCMinutes()).padStart(2, '0')
|
||||
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}`
|
||||
}
|
||||
|
||||
export const ExtractorOperationHistoryModal: React.FC<ExtractorOperationHistoryModalProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
user
|
||||
}) => {
|
||||
const [batches, setBatches] = useState<BatchStats[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
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 logger = useLogger('OperationHistory')
|
||||
|
||||
const isAdmin = user?.userType === 'Admin'
|
||||
|
||||
const fetchBatches = useCallback(async () => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
// 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 {
|
||||
setError(result.error || '获取历史记录失败')
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : '获取历史记录失败')
|
||||
} 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) {
|
||||
logger.error('Failed to fetch users list', {
|
||||
error: err instanceof Error ? err.message : String(err)
|
||||
})
|
||||
}
|
||||
}, [logger])
|
||||
|
||||
const fetchBatchDetails = useCallback(
|
||||
async (batchId: string) => {
|
||||
// If already loaded, don't fetch again
|
||||
if (batchDetails.has(batchId)) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await window.electron.operationHistory.getBatchDetails(batchId)
|
||||
if (result.success && result.data) {
|
||||
setBatchDetails((prev) => new Map(prev).set(batchId, result.data!))
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error('Failed to fetch batch details', {
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
batchId
|
||||
})
|
||||
}
|
||||
},
|
||||
[batchDetails, logger]
|
||||
)
|
||||
|
||||
// Fetch batches when modal opens
|
||||
useEffect(() => {
|
||||
if (isOpen) {
|
||||
void fetchBatches()
|
||||
if (isAdmin) {
|
||||
void fetchAllUsers()
|
||||
}
|
||||
}
|
||||
}, [isOpen, fetchBatches, fetchAllUsers, isAdmin])
|
||||
|
||||
const toggleBatchExpansion = (batchId: string) => {
|
||||
setExpandedBatches((prev) => {
|
||||
const newSet = new Set(prev)
|
||||
if (newSet.has(batchId)) {
|
||||
newSet.delete(batchId)
|
||||
} else {
|
||||
newSet.add(batchId)
|
||||
void fetchBatchDetails(batchId)
|
||||
}
|
||||
return newSet
|
||||
})
|
||||
}
|
||||
|
||||
const handleDeleteBatch = async (batchId: string) => {
|
||||
if (deleting.has(batchId)) return
|
||||
|
||||
const confirmed = confirm('确定要删除此批次记录吗?此操作不可撤销。')
|
||||
if (!confirmed) return
|
||||
|
||||
setDeleting((prev) => new Set(prev).add(batchId))
|
||||
|
||||
try {
|
||||
const result = await window.electron.operationHistory.deleteBatch(batchId)
|
||||
if (result.success) {
|
||||
// Remove from local state
|
||||
setBatches((prev) => prev.filter((b) => b.batchId !== batchId))
|
||||
setBatchDetails((prev) => {
|
||||
const newMap = new Map(prev)
|
||||
newMap.delete(batchId)
|
||||
return newMap
|
||||
})
|
||||
setExpandedBatches((prev) => {
|
||||
const newSet = new Set(prev)
|
||||
newSet.delete(batchId)
|
||||
return newSet
|
||||
})
|
||||
} else {
|
||||
alert(result.error || '删除失败')
|
||||
}
|
||||
} catch (err) {
|
||||
alert(err instanceof Error ? err.message : '删除失败')
|
||||
} finally {
|
||||
setDeleting((prev) => {
|
||||
const newSet = new Set(prev)
|
||||
newSet.delete(batchId)
|
||||
return newSet
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const handleCopyColumn = async (field: 'productionId' | 'orderNumber', batchId: string) => {
|
||||
const details = batchDetails.get(batchId) || []
|
||||
const values = details
|
||||
.map((d) => (field === 'productionId' ? d.productionId : d.orderNumber))
|
||||
.filter(Boolean) // 移除空值
|
||||
.join('\n') // 使用换行符分隔
|
||||
|
||||
if (!values) {
|
||||
showWarning('没有可复制的数据')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(values)
|
||||
showSuccess(`已复制 ${values.split('\n').length} 条数据`)
|
||||
} catch {
|
||||
showError('复制失败,请手动复制')
|
||||
}
|
||||
}
|
||||
|
||||
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-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 flex-shrink-0"
|
||||
onClick={() => void fetchBatches()}
|
||||
disabled={loading}
|
||||
title="刷新"
|
||||
>
|
||||
<RefreshCw size={18} className={loading ? 'animate-spin' : ''} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Error message */}
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Batch list */}
|
||||
<div className="flex-1 overflow-y-auto">
|
||||
{loading && batches.length === 0 ? (
|
||||
<div className="flex items-center justify-center h-32 text-gray-500">加载中...</div>
|
||||
) : batches.length === 0 ? (
|
||||
<div className="flex items-center justify-center h-32 text-gray-500">暂无操作记录</div>
|
||||
) : (
|
||||
<div className="flex flex-col gap-3">
|
||||
{batches.map((batch) => {
|
||||
const isExpanded = expandedBatches.has(batch.batchId)
|
||||
const details = batchDetails.get(batch.batchId) || []
|
||||
const isDeleting = deleting.has(batch.batchId)
|
||||
|
||||
return (
|
||||
<div
|
||||
key={batch.batchId}
|
||||
className="border border-gray-200 rounded-lg overflow-hidden"
|
||||
>
|
||||
{/* Batch summary */}
|
||||
<div
|
||||
className={`flex items-center justify-between p-4 cursor-pointer transition-colors ${
|
||||
isExpanded ? 'bg-gray-50' : 'hover:bg-gray-50'
|
||||
}`}
|
||||
onClick={() => toggleBatchExpansion(batch.batchId)}
|
||||
>
|
||||
<div className="flex items-center gap-4 flex-1">
|
||||
<button className="p-1 hover:bg-gray-200 rounded">
|
||||
{isExpanded ? <ChevronDown size={18} /> : <ChevronRight size={18} />}
|
||||
</button>
|
||||
|
||||
<div className="flex-1 grid grid-cols-6 gap-4 text-sm">
|
||||
<div>
|
||||
<div className="text-gray-500 text-xs">操作时间</div>
|
||||
<div className="font-medium text-gray-900">
|
||||
{formatDateTime(batch.operationTime)}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500 text-xs">操作用户</div>
|
||||
<div className="font-medium text-gray-900">{batch.username}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500 text-xs">状态</div>
|
||||
<div className="flex items-center gap-1">
|
||||
{statusIcons[batch.status] || statusIcons.pending}
|
||||
<span
|
||||
className={`px-2 py-0.5 rounded text-xs font-medium ${
|
||||
statusStyles[batch.status] || statusStyles.pending
|
||||
}`}
|
||||
>
|
||||
{statusLabels[batch.status] || batch.status}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500 text-xs">订单数</div>
|
||||
<div className="font-medium text-gray-900">{batch.totalOrders}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500 text-xs">记录数</div>
|
||||
<div className="font-medium text-gray-900">{batch.totalRecords}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-gray-500 text-xs">成功/失败</div>
|
||||
<div className="font-medium text-gray-900">
|
||||
<span className="text-green-600">{batch.successCount}</span>
|
||||
{batch.failedCount > 0 && (
|
||||
<>
|
||||
{' / '}
|
||||
<span className="text-red-600">{batch.failedCount}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{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 */}
|
||||
{isExpanded && details.length > 0 && (
|
||||
<div className="border-t border-gray-200 bg-white">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||
<div className="flex items-center gap-2">
|
||||
总排号
|
||||
<button
|
||||
className="p-1 hover:bg-gray-200 rounded transition-colors"
|
||||
onClick={() =>
|
||||
void handleCopyColumn('productionId', batch.batchId)
|
||||
}
|
||||
title="复制所有总排号"
|
||||
>
|
||||
<Copy
|
||||
size={14}
|
||||
className="text-gray-500 hover:text-gray-700"
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
</th>
|
||||
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||
<div className="flex items-center gap-2">
|
||||
订单号
|
||||
<button
|
||||
className="p-1 hover:bg-gray-200 rounded transition-colors"
|
||||
onClick={() =>
|
||||
void handleCopyColumn('orderNumber', batch.batchId)
|
||||
}
|
||||
title="复制所有订单号"
|
||||
>
|
||||
<Copy
|
||||
size={14}
|
||||
className="text-gray-500 hover:text-gray-700"
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
</th>
|
||||
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||
状态
|
||||
</th>
|
||||
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||
记录数
|
||||
</th>
|
||||
<th className="px-4 py-2 text-left font-medium text-gray-600">
|
||||
错误信息
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{details.map((detail) => (
|
||||
<tr key={detail.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-2 text-gray-900">
|
||||
{detail.productionId || '-'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-gray-900 font-mono text-xs">
|
||||
{detail.orderNumber}
|
||||
</td>
|
||||
<td className="px-4 py-2">
|
||||
<span
|
||||
className={`inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-medium ${
|
||||
statusStyles[detail.status] || statusStyles.pending
|
||||
}`}
|
||||
>
|
||||
{statusIcons[detail.status]}
|
||||
{statusLabels[detail.status] || detail.status}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-4 py-2 text-gray-900">
|
||||
{detail.recordCount ?? '-'}
|
||||
</td>
|
||||
<td className="px-4 py-2 text-red-600 text-xs max-w-xs truncate">
|
||||
{detail.errorMessage || '-'}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
<div className="pt-4 border-t border-gray-200 flex justify-end">
|
||||
<button
|
||||
className="px-6 py-2 rounded-md bg-gray-100 hover:bg-gray-200 text-gray-700 font-medium transition-colors"
|
||||
onClick={onClose}
|
||||
>
|
||||
关闭
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
export default ExtractorOperationHistoryModal
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
import React, { useState, useRef } from 'react'
|
||||
import { Modal } from './ui/Modal'
|
||||
import { useLogger } from '../hooks/useLogger'
|
||||
|
||||
interface LoginDialogProps {
|
||||
isOpen: boolean
|
||||
@@ -31,6 +32,7 @@ export const LoginDialog: React.FC<LoginDialogProps> = ({
|
||||
const [errorMessage, setErrorMessage] = useState('')
|
||||
const usernameInputRef = useRef<HTMLInputElement>(null)
|
||||
const errorRef = useRef<HTMLDivElement>(null)
|
||||
const logger = useLogger('LoginDialog')
|
||||
|
||||
// Display error message with aria-live
|
||||
const showError = (message: string): void => {
|
||||
@@ -42,12 +44,14 @@ export const LoginDialog: React.FC<LoginDialogProps> = ({
|
||||
setErrorMessage('')
|
||||
|
||||
if (!username.trim()) {
|
||||
logger.warn('Login validation: empty username')
|
||||
showError('请输入用户名')
|
||||
usernameInputRef.current?.focus()
|
||||
return
|
||||
}
|
||||
|
||||
if (!password.trim()) {
|
||||
logger.warn('Login validation: empty password')
|
||||
showError('请输入密码')
|
||||
return
|
||||
}
|
||||
@@ -57,8 +61,8 @@ export const LoginDialog: React.FC<LoginDialogProps> = ({
|
||||
setIsLoggingIn(false)
|
||||
|
||||
if (!success) {
|
||||
logger.error('Login failed: invalid credentials', { username: username.trim(), computerName })
|
||||
showError('用户名或密码错误')
|
||||
setPassword('')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Modal } from './ui/Modal'
|
||||
import { showSuccess, showError, showInfo } from '../stores/useAppStore'
|
||||
import { ConfirmDialog } from './ui/ConfirmDialog'
|
||||
import { useConfirmDialog } from './ui/useConfirmDialog'
|
||||
import { useLogger } from '../hooks/useLogger'
|
||||
|
||||
interface MaterialTypeRecord {
|
||||
id?: number
|
||||
@@ -48,6 +49,7 @@ export const MaterialTypeManagementDialog: React.FC<MaterialTypeManagementDialog
|
||||
const [editingCell, setEditingCell] = useState<{ rowIndex: number; field: string } | null>(null)
|
||||
const [editValue, setEditValue] = useState('')
|
||||
const [selectedRowIndex, setSelectedRowIndex] = useState<number | null>(null)
|
||||
const logger = useLogger('MaterialType')
|
||||
|
||||
const tableRef = useRef<HTMLTableElement>(null)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
@@ -96,11 +98,15 @@ export const MaterialTypeManagementDialog: React.FC<MaterialTypeManagementDialog
|
||||
)
|
||||
setSelectedRowIndex(null)
|
||||
} catch (error) {
|
||||
console.error('Failed to load material types:', error)
|
||||
logger.error('Failed to load material types', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
isAdmin,
|
||||
currentUsername
|
||||
})
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [currentUsername, isAdmin])
|
||||
}, [currentUsername, isAdmin, logger])
|
||||
|
||||
// Load data when dialog opens
|
||||
useEffect(() => {
|
||||
@@ -286,6 +292,12 @@ export const MaterialTypeManagementDialog: React.FC<MaterialTypeManagementDialog
|
||||
}
|
||||
} catch (error) {
|
||||
showError(`保存失败:${error instanceof Error ? error.message : '未知错误'}`)
|
||||
logger.error('Failed to save material types', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
inserts: toInsert.length,
|
||||
updates: toUpdate.length,
|
||||
deletes: toDelete.length
|
||||
})
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import React, { useEffect, useState, useCallback } from 'react'
|
||||
import { DownloadCloud, LoaderCircle, X } from 'lucide-react'
|
||||
import Modal from './ui/Modal'
|
||||
import { useLogger } from '../hooks/useLogger'
|
||||
interface DownloadProgress {
|
||||
percent: number // 0-100
|
||||
downloadedBytes: number
|
||||
@@ -25,6 +26,7 @@ export default function PlaywrightDownloadDialog({
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [isDownloading, setIsDownloading] = useState(false)
|
||||
const [showCancelConfirm, setShowCancelConfirm] = useState(false)
|
||||
const logger = useLogger('PlaywrightDownload')
|
||||
|
||||
// Format bytes to human-readable string
|
||||
const formatBytes = useCallback((bytes: number): string => {
|
||||
@@ -99,13 +101,15 @@ export default function PlaywrightDownloadDialog({
|
||||
try {
|
||||
await window.electron.playwrightBrowser.cancel()
|
||||
} catch (err) {
|
||||
console.error('Failed to cancel download:', err)
|
||||
logger.error('Failed to cancel download', {
|
||||
error: err instanceof Error ? err.message : String(err)
|
||||
})
|
||||
} finally {
|
||||
setShowCancelConfirm(false)
|
||||
setIsDownloading(false)
|
||||
onClose()
|
||||
}
|
||||
}, [onClose])
|
||||
}, [onClose, logger])
|
||||
|
||||
const handleConfirmCancel = useCallback(() => {
|
||||
void handleCancel()
|
||||
|
||||
@@ -1,823 +1,35 @@
|
||||
import React, { useCallback, useEffect, useState, useMemo } from 'react'
|
||||
import { X, BarChart3, Loader2, AlertCircle } from 'lucide-react'
|
||||
import {
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
Line,
|
||||
ComposedChart
|
||||
} from 'recharts'
|
||||
|
||||
interface ReportAnalysisDialogProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
isAdmin: boolean
|
||||
}
|
||||
|
||||
// Extracted metrics from a single report
|
||||
interface ReportMetrics {
|
||||
date: string
|
||||
user: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeSecs: number
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
// Aggregated daily metrics
|
||||
interface DailyMetrics {
|
||||
date: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeSecs: number
|
||||
avgExecutionTimeSecs: number
|
||||
users: string[] // Unique users who ran reports on this day
|
||||
reportCount: number
|
||||
}
|
||||
|
||||
// User-specific daily metrics for comparison view
|
||||
interface UserDailyMetrics {
|
||||
date: string
|
||||
user: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeSecs: number
|
||||
reportCount: number
|
||||
}
|
||||
|
||||
type MetricKey = keyof Omit<DailyMetrics, 'date' | 'users' | 'reportCount' | 'avgExecutionTimeSecs'>
|
||||
|
||||
const METRIC_LABELS: Record<MetricKey, string> = {
|
||||
processedOrders: '处理订单数',
|
||||
deletedMaterials: '删除物料数',
|
||||
skippedMaterials: '跳过物料数',
|
||||
errors: '错误数量',
|
||||
retriedOrders: '重试订单数',
|
||||
successfulRetries: '成功重试数',
|
||||
executionTimeSecs: '每订单平均耗时(秒)'
|
||||
}
|
||||
|
||||
const METRIC_COLORS: Record<MetricKey, string> = {
|
||||
processedOrders: '#3b82f6', // blue-500
|
||||
deletedMaterials: '#ef4444', // red-500
|
||||
skippedMaterials: '#eab308', // yellow-500
|
||||
errors: '#000000', // black
|
||||
retriedOrders: '#8b5cf6', // violet-500
|
||||
successfulRetries: '#10b981', // emerald-500
|
||||
executionTimeSecs: '#f97316' // orange-500
|
||||
}
|
||||
|
||||
// User colors for comparison view
|
||||
const USER_COLORS = [
|
||||
'#3b82f6', // blue-500
|
||||
'#10b981', // emerald-500
|
||||
'#f59e0b', // amber-500
|
||||
'#ef4444', // red-500
|
||||
'#8b5cf6', // violet-500
|
||||
'#ec4899', // pink-500
|
||||
'#06b6d4', // cyan-500
|
||||
'#84cc16' // lime-500
|
||||
]
|
||||
|
||||
const getUserColor = (user: string, users: string[]): string => {
|
||||
const index = users.indexOf(user)
|
||||
return USER_COLORS[index % USER_COLORS.length]
|
||||
}
|
||||
|
||||
export const ReportAnalysisDialog: React.FC<ReportAnalysisDialogProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
isAdmin
|
||||
}) => {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [reportData, setReportData] = useState<ReportMetrics[]>([])
|
||||
|
||||
// Selected metrics for the chart
|
||||
const [selectedMetrics, setSelectedMetrics] = useState<Set<MetricKey>>(
|
||||
new Set(['processedOrders', 'deletedMaterials', 'errors'])
|
||||
)
|
||||
|
||||
// View mode: aggregated (by date) or comparison (by user)
|
||||
const [viewMode, setViewMode] = useState<'aggregated' | 'comparison'>('aggregated')
|
||||
|
||||
// Selected users for comparison view
|
||||
const [selectedUsers, setSelectedUsers] = useState<Set<string>>(new Set())
|
||||
|
||||
const parseDurationToSeconds = (durationStr: string): number => {
|
||||
// Handle empty or zero case
|
||||
if (!durationStr || durationStr === '0秒' || durationStr === '0分0秒') {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Remove any remaining backticks
|
||||
const cleanStr = durationStr.replace(/\`/g, '').trim()
|
||||
|
||||
let totalSeconds = 0
|
||||
const minutesMatch = cleanStr.match(/(\d+)分/)
|
||||
if (minutesMatch) {
|
||||
totalSeconds += parseInt(minutesMatch[1], 10) * 60
|
||||
}
|
||||
const secondsMatch = cleanStr.match(/(\d+)秒/)
|
||||
if (secondsMatch) {
|
||||
totalSeconds += parseInt(secondsMatch[1], 10)
|
||||
}
|
||||
|
||||
return totalSeconds
|
||||
}
|
||||
|
||||
const loadAndAnalyzeReports = useCallback(async () => {
|
||||
if (!isAdmin) return
|
||||
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
// 1. Fetch report list
|
||||
const listResult = await window.electron.report.listAll()
|
||||
if (!listResult.success || !listResult.data) {
|
||||
throw new Error(listResult.error || '获取报告列表失败')
|
||||
}
|
||||
|
||||
const reports = listResult.data
|
||||
const metricsList: ReportMetrics[] = []
|
||||
|
||||
// 2. Fetch content for each report (in chunks to avoid memory/network issues if there are many)
|
||||
const chunkSize = 10
|
||||
for (let i = 0; i < reports.length; i += chunkSize) {
|
||||
const chunk = reports.slice(i, i + chunkSize)
|
||||
const contentPromises = chunk.map(async (report) => {
|
||||
try {
|
||||
const contentResult = await window.electron.report.download(report.key)
|
||||
if (contentResult.success && contentResult.data) {
|
||||
return { report, content: contentResult.data }
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Failed to fetch content for report ${report.key}`, e)
|
||||
}
|
||||
return null
|
||||
})
|
||||
|
||||
const chunkContents = await Promise.all(contentPromises)
|
||||
|
||||
// 3. Parse each report's markdown content
|
||||
for (const item of chunkContents) {
|
||||
if (!item) continue
|
||||
|
||||
const { report, content } = item
|
||||
|
||||
// Regex to extract values from the markdown table
|
||||
const extractValue = (key: string): string | null => {
|
||||
// Try multiple patterns in order of specificity
|
||||
const patterns = [
|
||||
// Pattern 1: Standard format with backticks
|
||||
new RegExp(`\\|\\s*\\*\\*${key}\\*\\*\\s*\\|\\s*\`([^\`]+)\`\\s*\\|`),
|
||||
// Pattern 2: Without backticks (fallback for older formats)
|
||||
new RegExp(`\\|\\s*\\*\\*${key}\\*\\*\\s*\\|\\s*([^\\|\\s]+(?:\\s+[^\\|\\s]+)*)\\s*\\|`),
|
||||
// Pattern 3: More relaxed - any content between pipes
|
||||
new RegExp(`\\|\\s*\\*\\*${key}\\*\\*\\s*\\|\\s*(.+?)\\s*\\|`)
|
||||
]
|
||||
|
||||
for (const pattern of patterns) {
|
||||
const match = content.match(pattern)
|
||||
if (match && match[1]) {
|
||||
const value = match[1].trim()
|
||||
// Remove backticks if they're still present
|
||||
return value.replace(/\`/g, '')
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const execTimeStr = extractValue('执行时间')
|
||||
const user = extractValue('操作用户') || report.username || 'unknown'
|
||||
const processedOrders = parseInt(extractValue('处理订单数') || '0', 10)
|
||||
const deletedMaterials = parseInt(extractValue('删除物料数') || '0', 10)
|
||||
const skippedMaterials = parseInt(extractValue('跳过物料数') || '0', 10)
|
||||
const errors = parseInt(extractValue('错误数量') || '0', 10)
|
||||
const retriedOrders = parseInt(extractValue('重试订单数') || '0', 10)
|
||||
const successfulRetries = parseInt(extractValue('成功重试数') || '0', 10)
|
||||
const executionTimeStr = extractValue('执行耗时') || '0秒'
|
||||
if (executionTimeStr === '0秒') {
|
||||
console.warn('Failed to extract execution time from report:', report.key)
|
||||
}
|
||||
|
||||
const executionTimeSecs = parseDurationToSeconds(executionTimeStr)
|
||||
|
||||
// Try to parse the date
|
||||
let dateStr = '未知日期'
|
||||
let timestamp = report.lastModified ? new Date(report.lastModified).getTime() : 0
|
||||
|
||||
if (execTimeStr) {
|
||||
try {
|
||||
const parsedDate = new Date(execTimeStr)
|
||||
if (!isNaN(parsedDate.getTime())) {
|
||||
dateStr = parsedDate
|
||||
.toLocaleDateString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
})
|
||||
.replace(/\//g, '-')
|
||||
timestamp = parsedDate.getTime()
|
||||
}
|
||||
} catch {
|
||||
// Fallback to report lastModified
|
||||
}
|
||||
}
|
||||
|
||||
if (dateStr === '未知日期' && report.lastModified) {
|
||||
const d = new Date(report.lastModified)
|
||||
dateStr = d
|
||||
.toLocaleDateString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
})
|
||||
.replace(/\//g, '-')
|
||||
}
|
||||
|
||||
metricsList.push({
|
||||
date: dateStr,
|
||||
user,
|
||||
processedOrders,
|
||||
deletedMaterials,
|
||||
skippedMaterials,
|
||||
errors,
|
||||
retriedOrders,
|
||||
successfulRetries,
|
||||
executionTimeSecs,
|
||||
timestamp
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
setReportData(metricsList)
|
||||
} catch (err: any) {
|
||||
setError(err.message || '分析报告时发生错误')
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}, [isAdmin])
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen && isAdmin) {
|
||||
void loadAndAnalyzeReports()
|
||||
} else {
|
||||
setReportData([])
|
||||
setError(null)
|
||||
}
|
||||
}, [isOpen, isAdmin, loadAndAnalyzeReports])
|
||||
|
||||
// Aggregate data by date
|
||||
const chartData = useMemo(() => {
|
||||
if (!reportData.length) return []
|
||||
|
||||
const dailyMap = new Map<string, DailyMetrics>()
|
||||
|
||||
for (const data of reportData) {
|
||||
const { date } = data
|
||||
|
||||
if (!dailyMap.has(date)) {
|
||||
dailyMap.set(date, {
|
||||
date,
|
||||
processedOrders: 0,
|
||||
deletedMaterials: 0,
|
||||
skippedMaterials: 0,
|
||||
errors: 0,
|
||||
retriedOrders: 0,
|
||||
successfulRetries: 0,
|
||||
executionTimeSecs: 0,
|
||||
avgExecutionTimeSecs: 0,
|
||||
users: [],
|
||||
reportCount: 0
|
||||
})
|
||||
}
|
||||
|
||||
const day = dailyMap.get(date)!
|
||||
day.processedOrders += data.processedOrders
|
||||
day.deletedMaterials += data.deletedMaterials
|
||||
day.skippedMaterials += data.skippedMaterials
|
||||
day.errors += data.errors
|
||||
day.retriedOrders += data.retriedOrders
|
||||
day.successfulRetries += data.successfulRetries
|
||||
day.executionTimeSecs += data.executionTimeSecs
|
||||
day.reportCount += 1
|
||||
|
||||
if (!day.users.includes(data.user)) {
|
||||
day.users.push(data.user)
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate average execution time per order for each day
|
||||
// Formula: total execution time / total processed orders (efficiency metric)
|
||||
for (const day of dailyMap.values()) {
|
||||
day.avgExecutionTimeSecs = day.processedOrders > 0
|
||||
? day.executionTimeSecs / day.processedOrders
|
||||
: 0
|
||||
// Replace executionTimeSecs with avgExecutionTimeSecs for chart display
|
||||
day.executionTimeSecs = day.avgExecutionTimeSecs
|
||||
}
|
||||
|
||||
// Convert map to array and sort by date
|
||||
const sortedData = Array.from(dailyMap.values()).sort((a, b) => {
|
||||
// Basic string comparison works for YYYY-MM-DD
|
||||
return a.date.localeCompare(b.date)
|
||||
})
|
||||
|
||||
return sortedData
|
||||
}, [reportData])
|
||||
|
||||
// Extract all unique users from report data
|
||||
const allUsers = useMemo(() => {
|
||||
const userSet = new Set<string>()
|
||||
reportData.forEach(data => userSet.add(data.user))
|
||||
return Array.from(userSet).sort()
|
||||
}, [reportData])
|
||||
|
||||
// Aggregate data by date AND user for comparison view
|
||||
const comparisonData = useMemo(() => {
|
||||
if (!reportData.length) return []
|
||||
|
||||
// Filter by selected users if any
|
||||
const filteredData = selectedUsers.size > 0
|
||||
? reportData.filter(data => selectedUsers.has(data.user))
|
||||
: reportData
|
||||
|
||||
// Group by date + user
|
||||
const keyMap = new Map<string, UserDailyMetrics>()
|
||||
|
||||
for (const data of filteredData) {
|
||||
const key = `${data.date}|${data.user}`
|
||||
|
||||
if (!keyMap.has(key)) {
|
||||
keyMap.set(key, {
|
||||
date: data.date,
|
||||
user: data.user,
|
||||
processedOrders: 0,
|
||||
deletedMaterials: 0,
|
||||
skippedMaterials: 0,
|
||||
errors: 0,
|
||||
retriedOrders: 0,
|
||||
successfulRetries: 0,
|
||||
executionTimeSecs: 0,
|
||||
reportCount: 0
|
||||
})
|
||||
}
|
||||
|
||||
const entry = keyMap.get(key)!
|
||||
entry.processedOrders += data.processedOrders
|
||||
entry.deletedMaterials += data.deletedMaterials
|
||||
entry.skippedMaterials += data.skippedMaterials
|
||||
entry.errors += data.errors
|
||||
entry.retriedOrders += data.retriedOrders
|
||||
entry.successfulRetries += data.successfulRetries
|
||||
entry.executionTimeSecs += data.executionTimeSecs
|
||||
entry.reportCount += 1
|
||||
}
|
||||
|
||||
// Calculate average execution time per order for each entry
|
||||
// Formula: total execution time / total processed orders (efficiency metric)
|
||||
for (const entry of keyMap.values()) {
|
||||
entry.executionTimeSecs = entry.processedOrders > 0
|
||||
? entry.executionTimeSecs / entry.processedOrders
|
||||
: 0
|
||||
}
|
||||
|
||||
return Array.from(keyMap.values())
|
||||
.sort((a, b) => {
|
||||
const dateCompare = a.date.localeCompare(b.date)
|
||||
if (dateCompare !== 0) return dateCompare
|
||||
return a.user.localeCompare(b.user)
|
||||
})
|
||||
}, [reportData, selectedUsers])
|
||||
|
||||
// Format comparison data for chart rendering
|
||||
const comparisonChartData = useMemo(() => {
|
||||
if (!comparisonData.length) return []
|
||||
|
||||
const dates = [...new Set(comparisonData.map(d => d.date))].sort()
|
||||
const users = [...new Set(comparisonData.map(d => d.user))]
|
||||
.filter(user => selectedUsers.size === 0 || selectedUsers.has(user))
|
||||
.sort()
|
||||
|
||||
const lookup = new Map<string, UserDailyMetrics>()
|
||||
comparisonData.forEach(d => {
|
||||
lookup.set(`${d.date}|${d.user}`, d)
|
||||
})
|
||||
|
||||
return dates.map(date => {
|
||||
const point: any = { date }
|
||||
users.forEach(user => {
|
||||
const key = `${date}|${user}`
|
||||
const data = lookup.get(key)
|
||||
|
||||
Array.from(selectedMetrics).forEach(metric => {
|
||||
const userKey = `${user}_${metric}` as any
|
||||
point[userKey] = data ? (data as any)[metric] : 0
|
||||
})
|
||||
})
|
||||
return point
|
||||
})
|
||||
}, [comparisonData, selectedMetrics, selectedUsers])
|
||||
|
||||
const handleMetricToggle = (metric: MetricKey) => {
|
||||
// In comparison view, only allow single metric selection
|
||||
if (viewMode === 'comparison') {
|
||||
setSelectedMetrics(new Set([metric]))
|
||||
} else {
|
||||
// In aggregated view, allow multiple metric selection
|
||||
const next = new Set(selectedMetrics)
|
||||
if (next.has(metric)) {
|
||||
if (next.size > 1) {
|
||||
// Ensure at least one metric is selected
|
||||
next.delete(metric)
|
||||
}
|
||||
} else {
|
||||
next.add(metric)
|
||||
}
|
||||
setSelectedMetrics(next)
|
||||
}
|
||||
}
|
||||
|
||||
const handleViewModeChange = (newMode: 'aggregated' | 'comparison') => {
|
||||
setViewMode(newMode)
|
||||
// When switching to comparison view, keep only the first selected metric
|
||||
if (newMode === 'comparison' && selectedMetrics.size > 1) {
|
||||
const firstMetric = Array.from(selectedMetrics)[0]
|
||||
setSelectedMetrics(new Set([firstMetric]))
|
||||
}
|
||||
}
|
||||
|
||||
// Custom Tooltip formatter
|
||||
const CustomTooltip = ({ active, payload, label }: any) => {
|
||||
if (active && payload && payload.length) {
|
||||
// Find the original daily data
|
||||
const dailyData = chartData.find((d) => d.date === label)
|
||||
|
||||
return (
|
||||
<div className="bg-white p-4 border border-slate-200 shadow-lg rounded-lg max-w-sm">
|
||||
<p className="font-semibold text-slate-800 mb-2 border-b border-slate-100 pb-2">
|
||||
{label}
|
||||
</p>
|
||||
|
||||
<div className="space-y-1.5 text-sm">
|
||||
{payload.map((entry: any, index: number) => (
|
||||
<div key={index} className="flex justify-between items-center gap-4">
|
||||
<span className="flex items-center gap-1.5 text-slate-600">
|
||||
<span
|
||||
className="w-2.5 h-2.5 rounded-full"
|
||||
style={{ backgroundColor: entry.color }}
|
||||
></span>
|
||||
{entry.name}:
|
||||
</span>
|
||||
<span className="font-medium text-slate-900">
|
||||
{entry.value} {entry.dataKey === 'executionTimeSecs' ? '秒' : ''}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{dailyData && (
|
||||
<div className="mt-3 pt-2 border-t border-slate-100 text-xs text-slate-500">
|
||||
<p>操作用户: {dailyData.users.join(', ')}</p>
|
||||
<p className="mt-1">报告总数: {dailyData.reportCount}</p>
|
||||
<p className="mt-1">每订单平均耗时: {dailyData.avgExecutionTimeSecs.toFixed(1)} 秒</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Comparison Tooltip for user-specific data
|
||||
const ComparisonTooltip = ({ active, payload, label, users, selectedUsers }: any) => {
|
||||
if (active && payload && payload.length) {
|
||||
const displayUsers = selectedUsers.size === 0 ? users : Array.from(selectedUsers)
|
||||
const firstMetric = Array.from(selectedMetrics)[0]
|
||||
|
||||
return (
|
||||
<div className="bg-white p-4 border border-slate-200 shadow-lg rounded-lg max-w-sm">
|
||||
<p className="font-semibold text-slate-800 mb-2 border-b border-slate-100 pb-2">{label}</p>
|
||||
|
||||
<div className="space-y-1.5 text-sm">
|
||||
{displayUsers.map((user: string) => {
|
||||
const userEntry = payload.find((p: any) => p.name === (user || '未分配'))
|
||||
if (!userEntry) return null
|
||||
|
||||
return (
|
||||
<div key={user} className="flex justify-between items-center gap-4">
|
||||
<span className="flex items-center gap-1.5 text-slate-600">
|
||||
<span className="w-2.5 h-2.5 rounded-full" style={{ backgroundColor: userEntry.color }} />
|
||||
{user || '未分配'}:
|
||||
</span>
|
||||
<span className="font-medium text-slate-900">
|
||||
{userEntry.value} {firstMetric === 'executionTimeSecs' ? '秒' : ''}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
if (!isOpen) return null
|
||||
if (!isAdmin) return null
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-[110] flex items-center justify-center bg-slate-900/50 backdrop-blur-sm animate-in fade-in duration-200">
|
||||
<div className="bg-white rounded-2xl shadow-2xl w-[1000px] max-w-[95vw] h-[85vh] flex flex-col border border-slate-200 overflow-hidden animate-in zoom-in-95 duration-200">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between px-6 py-4 border-b border-slate-200 bg-slate-50 flex-shrink-0">
|
||||
<div className="flex items-center gap-2 text-slate-800">
|
||||
<BarChart3 size={20} className="text-blue-600" />
|
||||
<h2 className="text-lg font-semibold">执行报告分析</h2>
|
||||
</div>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="text-slate-400 hover:text-slate-600 hover:bg-slate-200/50 p-1.5 rounded-lg transition-colors"
|
||||
>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 overflow-hidden flex flex-col bg-white">
|
||||
{isLoading ? (
|
||||
<div className="flex-1 flex flex-col items-center justify-center text-slate-500">
|
||||
<Loader2 size={32} className="animate-spin text-blue-500 mb-4" />
|
||||
<p>正在分析报告数据,可能需要几秒钟...</p>
|
||||
</div>
|
||||
) : error ? (
|
||||
<div className="flex-1 flex flex-col items-center justify-center text-red-500 p-8 text-center">
|
||||
<AlertCircle size={48} className="mb-4 opacity-80" />
|
||||
<p className="text-lg font-medium mb-2">分析失败</p>
|
||||
<p className="text-sm opacity-80">{error}</p>
|
||||
<button
|
||||
onClick={loadAndAnalyzeReports}
|
||||
className="mt-6 px-4 py-2 bg-red-50 text-red-600 border border-red-200 rounded-lg hover:bg-red-100 transition-colors"
|
||||
>
|
||||
重试
|
||||
</button>
|
||||
</div>
|
||||
) : chartData.length === 0 ? (
|
||||
<div className="flex-1 flex flex-col items-center justify-center text-slate-400">
|
||||
<BarChart3 size={48} className="mb-4 opacity-50 text-slate-300" />
|
||||
<p>暂无报告数据可供分析</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex-1 flex flex-col p-6 overflow-y-auto">
|
||||
{/* Controls */}
|
||||
<div className="mb-8">
|
||||
<h3 className="text-sm font-medium text-slate-700 mb-3">
|
||||
选择呈现内容 ({viewMode === 'comparison' ? '单选' : '多选'})
|
||||
</h3>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{(Object.keys(METRIC_LABELS) as MetricKey[]).map((key) => {
|
||||
const isSelected = selectedMetrics.has(key)
|
||||
return (
|
||||
<button
|
||||
key={key}
|
||||
onClick={() => handleMetricToggle(key)}
|
||||
className={`
|
||||
px-3 py-1.5 rounded-full text-xs font-medium border transition-colors flex items-center gap-1.5
|
||||
${
|
||||
isSelected
|
||||
? 'bg-blue-50 border-blue-200 text-blue-700'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}
|
||||
`}
|
||||
>
|
||||
<div
|
||||
className="w-2 h-2 rounded-full"
|
||||
style={{ backgroundColor: isSelected ? METRIC_COLORS[key] : '#cbd5e1' }}
|
||||
/>
|
||||
{METRIC_LABELS[key]}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* View Mode Toggle */}
|
||||
<div className="mb-6">
|
||||
<h3 className="text-sm font-medium text-slate-700 mb-3">视图模式</h3>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={() => handleViewModeChange('aggregated')}
|
||||
className={`px-4 py-2 rounded-lg text-sm font-medium border transition-colors ${
|
||||
viewMode === 'aggregated'
|
||||
? 'bg-blue-50 border-blue-200 text-blue-700'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}`}
|
||||
>
|
||||
按日期聚合
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleViewModeChange('comparison')}
|
||||
className={`px-4 py-2 rounded-lg text-sm font-medium border transition-colors ${
|
||||
viewMode === 'comparison'
|
||||
? 'bg-blue-50 border-blue-200 text-blue-700'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}`}
|
||||
>
|
||||
用户对比
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* User Filter Chips - Only in comparison mode */}
|
||||
{viewMode === 'comparison' && (
|
||||
<div className="mb-6">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<h3 className="text-sm font-medium text-slate-700">筛选用户</h3>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={() => setSelectedUsers(new Set(allUsers))}
|
||||
className="text-xs text-blue-600 hover:underline"
|
||||
>
|
||||
全选
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setSelectedUsers(new Set())}
|
||||
className="text-xs text-slate-500 hover:underline"
|
||||
>
|
||||
清空
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{allUsers.map((user) => {
|
||||
const isSelected = selectedUsers.has(user)
|
||||
const color = getUserColor(user, allUsers)
|
||||
|
||||
return (
|
||||
<button
|
||||
key={user}
|
||||
onClick={() => {
|
||||
const next = new Set(selectedUsers)
|
||||
if (next.has(user)) {
|
||||
next.delete(user)
|
||||
} else {
|
||||
next.add(user)
|
||||
}
|
||||
setSelectedUsers(next)
|
||||
}}
|
||||
className={`px-3 py-1.5 rounded-full text-xs font-medium border transition-colors flex items-center gap-1.5 ${
|
||||
isSelected
|
||||
? 'bg-white border-current'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}`}
|
||||
style={isSelected ? { color, borderColor: color } : {}}
|
||||
>
|
||||
<div
|
||||
className="w-2 h-2 rounded-full"
|
||||
style={{ backgroundColor: isSelected ? color : '#cbd5e1' }}
|
||||
/>
|
||||
{user || '未分配'}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
{selectedUsers.size === 0 && (
|
||||
<p className="text-xs text-slate-500 mt-2">
|
||||
未选择用户时将显示所有用户数据
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Chart */}
|
||||
<div className="flex-1 min-h-[400px]">
|
||||
{viewMode === 'aggregated' ? (
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<ComposedChart
|
||||
data={chartData}
|
||||
margin={{ top: 20, right: 30, left: 20, bottom: 20 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#e2e8f0" />
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
tick={{ fill: '#64748b', fontSize: 12 }}
|
||||
dy={10}
|
||||
/>
|
||||
<YAxis
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
tick={{ fill: '#64748b', fontSize: 12 }}
|
||||
/>
|
||||
<Tooltip content={<CustomTooltip />} />
|
||||
<Legend wrapperStyle={{ paddingTop: '20px' }} iconType="circle" />
|
||||
|
||||
{Array.from(selectedMetrics).map((metric) => (
|
||||
<Line
|
||||
key={metric}
|
||||
type="monotone"
|
||||
dataKey={metric}
|
||||
name={METRIC_LABELS[metric]}
|
||||
stroke={METRIC_COLORS[metric]}
|
||||
strokeWidth={2}
|
||||
dot={{ r: 4, strokeWidth: 2 }}
|
||||
activeDot={{ r: 6, strokeWidth: 0 }}
|
||||
/>
|
||||
))}
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<ComposedChart
|
||||
data={comparisonChartData}
|
||||
margin={{ top: 20, right: 30, left: 20, bottom: 20 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#e2e8f0" />
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
tick={{ fill: '#64748b', fontSize: 12 }}
|
||||
dy={10}
|
||||
/>
|
||||
<YAxis
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
tick={{ fill: '#64748b', fontSize: 12 }}
|
||||
/>
|
||||
<Tooltip content={<ComparisonTooltip users={allUsers} selectedUsers={selectedUsers} />} />
|
||||
<Legend wrapperStyle={{ paddingTop: '20px' }} iconType="circle" />
|
||||
|
||||
{selectedUsers.size === 0 || selectedUsers.size > 1
|
||||
? // Multiple users: show first metric for each user
|
||||
allUsers.filter(user => selectedUsers.size === 0 || selectedUsers.has(user)).map((user) => (
|
||||
<Line
|
||||
key={user}
|
||||
type="monotone"
|
||||
dataKey={`${user}_${Array.from(selectedMetrics)[0]}`}
|
||||
name={user || '未分配'}
|
||||
stroke={getUserColor(user, allUsers)}
|
||||
strokeWidth={2}
|
||||
dot={{ r: 4, strokeWidth: 2 }}
|
||||
activeDot={{ r: 6, strokeWidth: 0 }}
|
||||
/>
|
||||
))
|
||||
: // Single user: show all metrics for that user
|
||||
Array.from(selectedMetrics).map((metric) => {
|
||||
const user = Array.from(selectedUsers)[0]
|
||||
return (
|
||||
<Line
|
||||
key={metric}
|
||||
type="monotone"
|
||||
dataKey={`${user}_${metric}`}
|
||||
name={METRIC_LABELS[metric]}
|
||||
stroke={METRIC_COLORS[metric]}
|
||||
strokeWidth={2}
|
||||
dot={{ r: 4, strokeWidth: 2 }}
|
||||
activeDot={{ r: 6, strokeWidth: 0 }}
|
||||
/>
|
||||
)
|
||||
})
|
||||
}
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mt-4 text-center text-xs text-slate-400">
|
||||
{viewMode === 'aggregated'
|
||||
? '数据以天为单位进行聚合统计。展示的是选定时间段内的总量。'
|
||||
: selectedUsers.size === 0
|
||||
? '展示所有用户的数据对比。未选择用户时显示全部。'
|
||||
: '展示选定用户的数据对比。'
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default ReportAnalysisDialog
|
||||
/**
|
||||
* ReportAnalysisDialog Component - Re-export
|
||||
*
|
||||
* This file now re-exports the refactored component from the report-analysis module.
|
||||
* All functionality has been preserved while improving code organization.
|
||||
*
|
||||
* The refactored version is located at: ./report-analysis/index.tsx
|
||||
*
|
||||
* Refactoring changes:
|
||||
* - Split into 11 focused files (was 948 lines, now ~150 lines per file)
|
||||
* - Extracted custom hooks for business logic
|
||||
* - Separated UI components for better reusability
|
||||
* - Centralized type definitions
|
||||
* - Isolated utility functions for easier testing
|
||||
*
|
||||
* @see ./report-analysis/ for the refactored implementation
|
||||
*/
|
||||
|
||||
// Re-export everything from the refactored module
|
||||
export { ReportAnalysisDialog as default, ReportAnalysisDialog } from './report-analysis'
|
||||
|
||||
// Re-export types for external use
|
||||
export type {
|
||||
ReportMetrics,
|
||||
DailyMetrics,
|
||||
UserDailyMetrics,
|
||||
MetricKey,
|
||||
ViewMode,
|
||||
ReportAnalysisDialogProps,
|
||||
CustomTooltipProps,
|
||||
ComparisonTooltipProps
|
||||
} from './report-analysis/types'
|
||||
|
||||
// Re-export constants
|
||||
export { METRIC_LABELS, METRIC_COLORS, USER_COLORS } from './report-analysis/types'
|
||||
|
||||
@@ -13,7 +13,7 @@ import { Modal } from './ui/Modal'
|
||||
export interface UserInfo {
|
||||
id: number
|
||||
username: string
|
||||
userType: 'Admin' | 'User' | 'Guest'
|
||||
userType: 'Admin' | 'User'
|
||||
createTime?: Date
|
||||
}
|
||||
|
||||
@@ -61,8 +61,7 @@ export const UserSelectionDialog: React.FC<UserSelectionDialogProps> = ({
|
||||
|
||||
const userTypeStyles: Record<string, string> = {
|
||||
Admin: 'bg-amber-50 text-amber-600',
|
||||
User: 'bg-blue-50 text-blue-600',
|
||||
Guest: 'bg-gray-100 text-gray-600'
|
||||
User: 'bg-blue-50 text-blue-600'
|
||||
}
|
||||
|
||||
if (!isOpen) return null
|
||||
|
||||
@@ -185,7 +185,7 @@ export function AuthenticatedAppShell({
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{currentPage === 'extractor' && <ExtractorPage />}
|
||||
{currentPage === 'extractor' && <ExtractorPage currentUser={currentUser} />}
|
||||
{currentPage === 'cleaner' && <CleanerPage />}
|
||||
{currentPage === 'settings' && <SettingsPage />}
|
||||
</main>
|
||||
|
||||
@@ -4,7 +4,7 @@ import UserSelectionDialog, { type UserInfo as SelectedUserInfo } from '../UserS
|
||||
|
||||
interface CurrentUser {
|
||||
username: string
|
||||
userType: 'Admin' | 'User' | 'Guest'
|
||||
userType: 'Admin' | 'User'
|
||||
}
|
||||
|
||||
interface UnauthenticatedAppProps {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* ComparisonTooltip Component
|
||||
* Custom tooltip for comparison chart view showing user-specific metrics
|
||||
*/
|
||||
|
||||
import React from 'react'
|
||||
import { MetricKey } from '../types'
|
||||
|
||||
interface ComparisonTooltipProps {
|
||||
active?: boolean
|
||||
payload?: any[]
|
||||
label?: string
|
||||
users: string[]
|
||||
selectedUsers: Set<string>
|
||||
selectedMetrics: Set<MetricKey>
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a detailed tooltip for the comparison chart view
|
||||
* Shows user-specific metric values for the selected date
|
||||
*/
|
||||
export const ComparisonTooltip = React.memo(
|
||||
({ active, payload, label, users, selectedUsers, selectedMetrics }: ComparisonTooltipProps) => {
|
||||
if (!active || !payload || !payload.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
const displayUsers = selectedUsers.size === 0 ? users : Array.from(selectedUsers)
|
||||
const firstMetric = Array.from(selectedMetrics)[0]
|
||||
|
||||
return (
|
||||
<div className="bg-white p-4 border border-slate-200 shadow-lg rounded-lg max-w-sm">
|
||||
<p className="font-semibold text-slate-800 mb-2 border-b border-slate-100 pb-2">{label}</p>
|
||||
|
||||
<div className="space-y-1.5 text-sm">
|
||||
{displayUsers.map((user: string) => {
|
||||
const userEntry = payload.find((p: any) => p.name === (user || '未分配'))
|
||||
if (!userEntry) return null
|
||||
|
||||
return (
|
||||
<div key={user} className="flex justify-between items-center gap-4">
|
||||
<span className="flex items-center gap-1.5 text-slate-600">
|
||||
<span
|
||||
className="w-2.5 h-2.5 rounded-full"
|
||||
style={{ backgroundColor: userEntry.color }}
|
||||
/>
|
||||
{user || '未分配'}:
|
||||
</span>
|
||||
<span className="font-medium text-slate-900">
|
||||
{firstMetric === 'executionTimeSecs'
|
||||
? Number(userEntry.value).toFixed(1)
|
||||
: userEntry.value}{' '}
|
||||
{firstMetric === 'executionTimeSecs' ? '秒' : ''}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
(prevProps, nextProps) => {
|
||||
// Custom comparison for memoization
|
||||
return (
|
||||
prevProps.label === nextProps.label &&
|
||||
prevProps.selectedUsers.size === nextProps.selectedUsers.size &&
|
||||
prevProps.selectedMetrics.size === nextProps.selectedMetrics.size &&
|
||||
prevProps.payload?.length === nextProps.payload?.length
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
ComparisonTooltip.displayName = 'ComparisonTooltip'
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* CustomTooltip Component
|
||||
* Custom tooltip for aggregated chart view showing daily metrics
|
||||
*/
|
||||
|
||||
import React from 'react'
|
||||
import { DailyMetrics } from '../types'
|
||||
|
||||
interface CustomTooltipProps {
|
||||
active?: boolean
|
||||
payload?: any[]
|
||||
label?: string
|
||||
chartData: DailyMetrics[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a detailed tooltip for the aggregated chart view
|
||||
* Shows metric values along with additional context like user count and report count
|
||||
*/
|
||||
export const CustomTooltip = React.memo(
|
||||
({ active, payload, label, chartData }: CustomTooltipProps) => {
|
||||
if (!active || !payload || !payload.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
const dailyData = chartData.find((d) => d.date === label)
|
||||
|
||||
return (
|
||||
<div className="bg-white p-4 border border-slate-200 shadow-lg rounded-lg max-w-sm">
|
||||
<p className="font-semibold text-slate-800 mb-2 border-b border-slate-100 pb-2">{label}</p>
|
||||
|
||||
<div className="space-y-1.5 text-sm">
|
||||
{payload.map((entry: any, index: number) => (
|
||||
<div key={`${entry.name}-${index}`} className="flex justify-between items-center gap-4">
|
||||
<span className="flex items-center gap-1.5 text-slate-600">
|
||||
<span
|
||||
className="w-2.5 h-2.5 rounded-full"
|
||||
style={{ backgroundColor: entry.color }}
|
||||
></span>
|
||||
{entry.name}:
|
||||
</span>
|
||||
<span className="font-medium text-slate-900">
|
||||
{entry.dataKey === 'executionTimeSecs'
|
||||
? Number(entry.value).toFixed(1)
|
||||
: entry.value}{' '}
|
||||
{entry.dataKey === 'executionTimeSecs' ? '秒' : ''}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{dailyData && (
|
||||
<div className="mt-3 pt-2 border-t border-slate-100 text-xs text-slate-500">
|
||||
<p>操作用户: {dailyData.users.join(', ')}</p>
|
||||
<p className="mt-1">报告总数: {dailyData.reportCount}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
},
|
||||
(prevProps, nextProps) => {
|
||||
// Custom comparison for memoization
|
||||
return (
|
||||
prevProps.label === nextProps.label &&
|
||||
prevProps.payload?.length === nextProps.payload?.length &&
|
||||
prevProps.chartData === nextProps.chartData
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
CustomTooltip.displayName = 'CustomTooltip'
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* MetricSelector Component
|
||||
* Allows users to select which metrics to display in the chart
|
||||
* Supports both single-select (comparison mode) and multi-select (aggregated mode)
|
||||
*/
|
||||
|
||||
import React from 'react'
|
||||
import { MetricKey, METRIC_LABELS, METRIC_COLORS } from '../types'
|
||||
|
||||
interface MetricSelectorProps {
|
||||
selectedMetrics: Set<MetricKey>
|
||||
viewMode: 'aggregated' | 'comparison'
|
||||
onMetricToggle: (metric: MetricKey) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a list of metric selection buttons
|
||||
* Shows multi-select hint in aggregated mode and single-select hint in comparison mode
|
||||
*/
|
||||
export const MetricSelector: React.FC<MetricSelectorProps> = ({
|
||||
selectedMetrics,
|
||||
viewMode,
|
||||
onMetricToggle
|
||||
}) => {
|
||||
return (
|
||||
<div className="mb-8">
|
||||
<h3 className="text-sm font-medium text-slate-700 mb-3">
|
||||
选择呈现内容 ({viewMode === 'comparison' ? '单选' : '多选'})
|
||||
</h3>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{(Object.keys(METRIC_LABELS) as MetricKey[]).map((key) => {
|
||||
const isSelected = selectedMetrics.has(key)
|
||||
return (
|
||||
<button
|
||||
key={key}
|
||||
onClick={() => onMetricToggle(key)}
|
||||
className={`px-3 py-1.5 rounded-full text-xs font-medium border transition-colors flex items-center gap-1.5 ${
|
||||
isSelected
|
||||
? 'bg-blue-50 border-blue-200 text-blue-700'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}`}
|
||||
>
|
||||
<div
|
||||
className="w-2 h-2 rounded-full"
|
||||
style={{ backgroundColor: isSelected ? METRIC_COLORS[key] : '#cbd5e1' }}
|
||||
/>
|
||||
{METRIC_LABELS[key]}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* ReportChart Component
|
||||
* Renders the main chart display with support for both aggregated and comparison views
|
||||
* Uses Recharts library for responsive, interactive charts
|
||||
*/
|
||||
|
||||
import React from 'react'
|
||||
import {
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
Line,
|
||||
ComposedChart
|
||||
} from 'recharts'
|
||||
import { DailyMetrics, MetricKey, METRIC_LABELS, METRIC_COLORS, USER_COLORS } from '../types'
|
||||
import { CustomTooltip } from './CustomTooltip'
|
||||
import { ComparisonTooltip } from './ComparisonTooltip'
|
||||
|
||||
interface ReportChartProps {
|
||||
viewMode: 'aggregated' | 'comparison'
|
||||
selectedMetrics: Set<MetricKey>
|
||||
selectedUsers: Set<string>
|
||||
allUsers: string[]
|
||||
chartData: DailyMetrics[]
|
||||
comparisonChartData: any[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get consistent color for a user
|
||||
*/
|
||||
const getUserColor = (user: string, users: string[]): string => {
|
||||
const index = users.indexOf(user)
|
||||
return USER_COLORS[index % USER_COLORS.length]
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the appropriate chart based on view mode
|
||||
* - Aggregated: Shows metrics grouped by date
|
||||
* - Comparison: Shows metrics grouped by user for comparison
|
||||
*/
|
||||
export const ReportChart: React.FC<ReportChartProps> = ({
|
||||
viewMode,
|
||||
selectedMetrics,
|
||||
selectedUsers,
|
||||
allUsers,
|
||||
chartData,
|
||||
comparisonChartData
|
||||
}) => {
|
||||
return (
|
||||
<div className="flex-1 min-h-[400px]">
|
||||
{viewMode === 'aggregated' ? (
|
||||
// Aggregated view: metrics by date
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<ComposedChart data={chartData} margin={{ top: 20, right: 30, left: 20, bottom: 20 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#e2e8f0" />
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
tick={{ fill: '#64748b', fontSize: 12 }}
|
||||
dy={10}
|
||||
/>
|
||||
<YAxis axisLine={false} tickLine={false} tick={{ fill: '#64748b', fontSize: 12 }} />
|
||||
<Tooltip content={<CustomTooltip chartData={chartData} />} />
|
||||
<Legend wrapperStyle={{ paddingTop: '20px' }} iconType="circle" />
|
||||
|
||||
{Array.from(selectedMetrics).map((metric) => (
|
||||
<Line
|
||||
key={metric}
|
||||
type="monotone"
|
||||
dataKey={metric}
|
||||
name={METRIC_LABELS[metric]}
|
||||
stroke={METRIC_COLORS[metric]}
|
||||
strokeWidth={2}
|
||||
dot={{ r: 4, strokeWidth: 2 }}
|
||||
activeDot={{ r: 6, strokeWidth: 0 }}
|
||||
/>
|
||||
))}
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
// Comparison view: metrics by user
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<ComposedChart
|
||||
data={comparisonChartData}
|
||||
margin={{ top: 20, right: 30, left: 20, bottom: 20 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#e2e8f0" />
|
||||
<XAxis
|
||||
dataKey="date"
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
tick={{ fill: '#64748b', fontSize: 12 }}
|
||||
dy={10}
|
||||
/>
|
||||
<YAxis axisLine={false} tickLine={false} tick={{ fill: '#64748b', fontSize: 12 }} />
|
||||
<Tooltip
|
||||
content={
|
||||
<ComparisonTooltip
|
||||
users={allUsers}
|
||||
selectedUsers={selectedUsers}
|
||||
selectedMetrics={selectedMetrics}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<Legend wrapperStyle={{ paddingTop: '20px' }} iconType="circle" />
|
||||
|
||||
{selectedUsers.size === 0 || selectedUsers.size > 1
|
||||
? // Multiple users: show first metric for each user
|
||||
allUsers
|
||||
.filter((user) => selectedUsers.size === 0 || selectedUsers.has(user))
|
||||
.map((user) => (
|
||||
<Line
|
||||
key={user}
|
||||
type="monotone"
|
||||
dataKey={`${user}_${Array.from(selectedMetrics)[0]}`}
|
||||
name={user || '未分配'}
|
||||
stroke={getUserColor(user, allUsers)}
|
||||
strokeWidth={2}
|
||||
dot={{ r: 4, strokeWidth: 2 }}
|
||||
activeDot={{ r: 6, strokeWidth: 0 }}
|
||||
/>
|
||||
))
|
||||
: // Single user: show all metrics for that user
|
||||
Array.from(selectedMetrics).map((metric) => {
|
||||
const user = Array.from(selectedUsers)[0]
|
||||
return (
|
||||
<Line
|
||||
key={metric}
|
||||
type="monotone"
|
||||
dataKey={`${user}_${metric}`}
|
||||
name={METRIC_LABELS[metric]}
|
||||
stroke={METRIC_COLORS[metric]}
|
||||
strokeWidth={2}
|
||||
dot={{ r: 4, strokeWidth: 2 }}
|
||||
activeDot={{ r: 6, strokeWidth: 0 }}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</ComposedChart>
|
||||
</ResponsiveContainer>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* UserFilter Component
|
||||
* Allows users to filter which users to display in comparison view
|
||||
* Shows all users as selectable chips with color coding
|
||||
*/
|
||||
|
||||
import React from 'react'
|
||||
import { USER_COLORS } from '../types'
|
||||
|
||||
interface UserFilterProps {
|
||||
allUsers: string[]
|
||||
selectedUsers: Set<string>
|
||||
onUserToggle: (user: string) => void
|
||||
onSelectAll: () => void
|
||||
onClearAll: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get consistent color for a user
|
||||
*/
|
||||
const getUserColor = (user: string, users: string[]): string => {
|
||||
const index = users.indexOf(user)
|
||||
return USER_COLORS[index % USER_COLORS.length]
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders user filter interface with selectable user chips
|
||||
* Only displayed in comparison view mode
|
||||
*/
|
||||
export const UserFilter: React.FC<UserFilterProps> = ({
|
||||
allUsers,
|
||||
selectedUsers,
|
||||
onUserToggle,
|
||||
onSelectAll,
|
||||
onClearAll
|
||||
}) => {
|
||||
return (
|
||||
<div className="mb-6">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<h3 className="text-sm font-medium text-slate-700">筛选用户</h3>
|
||||
<div className="flex gap-2">
|
||||
<button onClick={onSelectAll} className="text-xs text-blue-600 hover:underline">
|
||||
全选
|
||||
</button>
|
||||
<button onClick={onClearAll} className="text-xs text-slate-500 hover:underline">
|
||||
清空
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{allUsers.map((user) => {
|
||||
const isSelected = selectedUsers.has(user)
|
||||
const color = getUserColor(user, allUsers)
|
||||
|
||||
return (
|
||||
<button
|
||||
key={user}
|
||||
onClick={() => onUserToggle(user)}
|
||||
className={`px-3 py-1.5 rounded-full text-xs font-medium border transition-colors flex items-center gap-1.5 ${
|
||||
isSelected
|
||||
? 'bg-white border-current'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}`}
|
||||
style={isSelected ? { color, borderColor: color } : {}}
|
||||
>
|
||||
<div
|
||||
className="w-2 h-2 rounded-full"
|
||||
style={{ backgroundColor: isSelected ? color : '#cbd5e1' }}
|
||||
/>
|
||||
{user || '未分配'}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
{selectedUsers.size === 0 && (
|
||||
<p className="text-xs text-slate-500 mt-2">未选择用户时将显示所有用户数据</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* ViewModeToggle Component
|
||||
* Allows users to switch between aggregated and comparison view modes
|
||||
*/
|
||||
|
||||
import React from 'react'
|
||||
import { ViewMode } from '../types'
|
||||
|
||||
interface ViewModeToggleProps {
|
||||
viewMode: ViewMode
|
||||
onViewModeChange: (newMode: ViewMode) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders toggle buttons for switching between view modes
|
||||
* Aggregated: shows data grouped by date
|
||||
* Comparison: shows data grouped by user for comparison
|
||||
*/
|
||||
export const ViewModeToggle: React.FC<ViewModeToggleProps> = ({ viewMode, onViewModeChange }) => {
|
||||
return (
|
||||
<div className="mb-6">
|
||||
<h3 className="text-sm font-medium text-slate-700 mb-3">视图模式</h3>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={() => onViewModeChange('aggregated')}
|
||||
className={`px-4 py-2 rounded-lg text-sm font-medium border transition-colors ${
|
||||
viewMode === 'aggregated'
|
||||
? 'bg-blue-50 border-blue-200 text-blue-700'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}`}
|
||||
>
|
||||
按日期聚合
|
||||
</button>
|
||||
<button
|
||||
onClick={() => onViewModeChange('comparison')}
|
||||
className={`px-4 py-2 rounded-lg text-sm font-medium border transition-colors ${
|
||||
viewMode === 'comparison'
|
||||
? 'bg-blue-50 border-blue-200 text-blue-700'
|
||||
: 'bg-white border-slate-200 text-slate-600 hover:bg-slate-50'
|
||||
}`}
|
||||
>
|
||||
用户对比
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
50
src/renderer/src/components/report-analysis/export.ts
Normal file
50
src/renderer/src/components/report-analysis/export.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Report Analysis Feature Module
|
||||
* Centralized exports for the refactored report analysis components
|
||||
*/
|
||||
|
||||
// Main component
|
||||
export { ReportAnalysisDialog, default } from './index'
|
||||
|
||||
// Types
|
||||
export type {
|
||||
ReportMetrics,
|
||||
DailyMetrics,
|
||||
UserDailyMetrics,
|
||||
MetricKey,
|
||||
ViewMode,
|
||||
ReportAnalysisDialogProps,
|
||||
CustomTooltipProps,
|
||||
ComparisonTooltipProps
|
||||
} from './types'
|
||||
|
||||
export { METRIC_LABELS, METRIC_COLORS, USER_COLORS } from './types'
|
||||
|
||||
// Hooks
|
||||
export { useReportData } from './hooks/useReportData'
|
||||
export { useChartData } from './hooks/useChartData'
|
||||
export { useReportFilters } from './hooks/useReportFilters'
|
||||
|
||||
// Components
|
||||
export { MetricSelector } from './components/MetricSelector'
|
||||
export { ViewModeToggle } from './components/ViewModeToggle'
|
||||
export { UserFilter } from './components/UserFilter'
|
||||
export { ReportChart } from './components/ReportChart'
|
||||
export { CustomTooltip } from './components/CustomTooltip'
|
||||
export { ComparisonTooltip } from './components/ComparisonTooltip'
|
||||
|
||||
// Utilities
|
||||
export {
|
||||
extractReportValues,
|
||||
parseDurationToSeconds,
|
||||
formatDateToChinese,
|
||||
parseReportData
|
||||
} from './utils/parser'
|
||||
|
||||
export {
|
||||
aggregateByDate,
|
||||
extractAllUsers,
|
||||
aggregateByUserAndDate,
|
||||
formatComparisonChartData,
|
||||
getUserColor
|
||||
} from './utils/aggregators'
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Custom hook for transforming report data into chart-ready formats
|
||||
* Handles data aggregation for different view modes
|
||||
*/
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import { ReportMetrics, DailyMetrics, UserDailyMetrics, MetricKey } from '../types'
|
||||
import {
|
||||
aggregateByDate,
|
||||
extractAllUsers,
|
||||
aggregateByUserAndDate,
|
||||
formatComparisonChartData
|
||||
} from '../utils/aggregators'
|
||||
|
||||
interface UseChartDataResult {
|
||||
chartData: DailyMetrics[]
|
||||
allUsers: string[]
|
||||
comparisonData: UserDailyMetrics[]
|
||||
comparisonChartData: any[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for managing chart data transformations
|
||||
*
|
||||
* @param reportData - Raw report metrics data
|
||||
* @param selectedUsers - Set of selected users for filtering
|
||||
* @param selectedMetrics - Set of selected metrics to display
|
||||
* @returns Transformed data ready for chart rendering
|
||||
*/
|
||||
export const useChartData = (
|
||||
reportData: ReportMetrics[],
|
||||
selectedUsers: Set<string>,
|
||||
selectedMetrics: Set<MetricKey>
|
||||
): UseChartDataResult => {
|
||||
// Aggregate data by date
|
||||
const chartData = useMemo(() => aggregateByDate(reportData), [reportData])
|
||||
|
||||
// Extract all unique users from report data
|
||||
const allUsers = useMemo(() => extractAllUsers(reportData), [reportData])
|
||||
|
||||
// Aggregate data by date AND user for comparison view
|
||||
const comparisonData = useMemo(
|
||||
() => aggregateByUserAndDate(reportData, selectedUsers),
|
||||
[reportData, selectedUsers]
|
||||
)
|
||||
|
||||
// Format comparison data for chart rendering
|
||||
const comparisonChartData = useMemo(
|
||||
() => formatComparisonChartData(comparisonData, selectedUsers, selectedMetrics),
|
||||
[comparisonData, selectedUsers, selectedMetrics]
|
||||
)
|
||||
|
||||
return {
|
||||
chartData,
|
||||
allUsers,
|
||||
comparisonData,
|
||||
comparisonChartData
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Custom hook for fetching and managing report data
|
||||
* Handles data loading, parsing, and error states
|
||||
*/
|
||||
|
||||
import { useState, useCallback, useEffect } from 'react'
|
||||
import { ReportMetrics } from '../types'
|
||||
import { parseReportData } from '../utils/parser'
|
||||
import { useLogger } from '../../../hooks/useLogger'
|
||||
|
||||
interface UseReportDataResult {
|
||||
isLoading: boolean
|
||||
error: string | null
|
||||
reportData: ReportMetrics[]
|
||||
loadAndAnalyzeReports: () => Promise<void>
|
||||
clearData: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for managing report data fetching and parsing
|
||||
*
|
||||
* @param isAdmin - Whether the current user has admin privileges
|
||||
* @param isOpen - Whether the dialog is open
|
||||
* @returns Report data state and control functions
|
||||
*/
|
||||
export const useReportData = (isAdmin: boolean, isOpen: boolean): UseReportDataResult => {
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [reportData, setReportData] = useState<ReportMetrics[]>([])
|
||||
const logger = useLogger('ReportData')
|
||||
|
||||
const loadAndAnalyzeReports = useCallback(async () => {
|
||||
if (!isAdmin) return
|
||||
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
// 1. Fetch report list
|
||||
const listResult = await window.electron.report.listAll()
|
||||
if (!listResult.success || !listResult.data) {
|
||||
throw new Error(listResult.error || '获取报告列表失败')
|
||||
}
|
||||
|
||||
const reports = listResult.data
|
||||
const metricsList: ReportMetrics[] = []
|
||||
|
||||
// 2. Fetch content for each report (in chunks to avoid memory/network issues)
|
||||
// Rule: async-parallel - Using Promise.all for parallel fetching
|
||||
const chunkSize = 10
|
||||
for (let i = 0; i < reports.length; i += chunkSize) {
|
||||
const chunk = reports.slice(i, i + chunkSize)
|
||||
const contentPromises = chunk.map(async (report) => {
|
||||
try {
|
||||
const contentResult = await window.electron.report.download(report.key)
|
||||
if (contentResult.success && contentResult.data) {
|
||||
return { report, content: contentResult.data }
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn('Failed to fetch content for report', {
|
||||
reportKey: report.key,
|
||||
error: e instanceof Error ? e.message : String(e)
|
||||
})
|
||||
}
|
||||
return null
|
||||
})
|
||||
|
||||
const chunkContents = await Promise.all(contentPromises)
|
||||
|
||||
// 3. Parse each report's markdown content
|
||||
// Rule: js-hoist-regexp - Regex patterns now in parseReportData function
|
||||
for (const item of chunkContents) {
|
||||
if (!item) continue
|
||||
|
||||
const { report, content } = item
|
||||
const parsedData = parseReportData(report, content)
|
||||
|
||||
metricsList.push(parsedData)
|
||||
}
|
||||
}
|
||||
|
||||
setReportData(metricsList)
|
||||
} catch (err: any) {
|
||||
setError(err.message || '分析报告时发生错误')
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}, [isAdmin, logger])
|
||||
|
||||
const clearData = useCallback(() => {
|
||||
setReportData([])
|
||||
setError(null)
|
||||
}, [])
|
||||
|
||||
// Auto-load data when dialog opens
|
||||
useEffect(() => {
|
||||
if (isOpen && isAdmin) {
|
||||
void loadAndAnalyzeReports()
|
||||
} else {
|
||||
clearData()
|
||||
}
|
||||
}, [isOpen, isAdmin, loadAndAnalyzeReports, clearData])
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
error,
|
||||
reportData,
|
||||
loadAndAnalyzeReports,
|
||||
clearData
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Custom hook for managing report analysis filters and view modes
|
||||
* Handles metric selection, view mode switching, and user filtering
|
||||
*/
|
||||
|
||||
import { useState, useCallback } from 'react'
|
||||
import { MetricKey, ViewMode } from '../types'
|
||||
|
||||
interface UseReportFiltersResult {
|
||||
selectedMetrics: Set<MetricKey>
|
||||
viewMode: ViewMode
|
||||
selectedUsers: Set<string>
|
||||
handleMetricToggle: (metric: MetricKey) => void
|
||||
handleViewModeChange: (newMode: ViewMode) => void
|
||||
handleUserToggle: (user: string) => void
|
||||
handleSelectAllUsers: (users: string[]) => void
|
||||
handleClearAllUsers: () => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for managing filter state and user interactions
|
||||
*
|
||||
* @returns Filter state and handler functions
|
||||
*/
|
||||
export const useReportFilters = (): UseReportFiltersResult => {
|
||||
const [selectedMetrics, setSelectedMetrics] = useState<Set<MetricKey>>(
|
||||
new Set(['processedOrders', 'deletedMaterials', 'errors'])
|
||||
)
|
||||
|
||||
const [viewMode, setViewMode] = useState<ViewMode>('aggregated')
|
||||
|
||||
const [selectedUsers, setSelectedUsers] = useState<Set<string>>(new Set())
|
||||
|
||||
/**
|
||||
* Handles metric selection with view mode awareness
|
||||
* In comparison view: single selection only
|
||||
* In aggregated view: multiple selection allowed
|
||||
*/
|
||||
const handleMetricToggle = useCallback(
|
||||
(metric: MetricKey) => {
|
||||
setSelectedMetrics((prev) => {
|
||||
const next = new Set(prev)
|
||||
|
||||
if (viewMode === 'comparison') {
|
||||
// Single selection mode for comparison view
|
||||
return new Set([metric])
|
||||
} else {
|
||||
// Multi-selection mode for aggregated view
|
||||
if (next.has(metric)) {
|
||||
// Ensure at least one metric is selected
|
||||
if (next.size > 1) {
|
||||
next.delete(metric)
|
||||
}
|
||||
} else {
|
||||
next.add(metric)
|
||||
}
|
||||
return next
|
||||
}
|
||||
})
|
||||
},
|
||||
[viewMode]
|
||||
)
|
||||
|
||||
/**
|
||||
* Handles view mode switching with automatic metric adjustment
|
||||
* When switching to comparison view, keeps only first selected metric
|
||||
*/
|
||||
const handleViewModeChange = useCallback((newMode: ViewMode) => {
|
||||
setViewMode(newMode)
|
||||
|
||||
// When switching to comparison view, keep only the first selected metric
|
||||
if (newMode === 'comparison') {
|
||||
setSelectedMetrics((prev) => {
|
||||
if (prev.size > 1) {
|
||||
const firstMetric = Array.from(prev)[0]
|
||||
return new Set([firstMetric])
|
||||
}
|
||||
return prev
|
||||
})
|
||||
}
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Handles user selection toggle
|
||||
*/
|
||||
const handleUserToggle = useCallback((user: string) => {
|
||||
setSelectedUsers((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (next.has(user)) {
|
||||
next.delete(user)
|
||||
} else {
|
||||
next.add(user)
|
||||
}
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Selects all provided users
|
||||
*/
|
||||
const handleSelectAllUsers = useCallback((users: string[]) => {
|
||||
setSelectedUsers(new Set(users))
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Clears all user selections
|
||||
*/
|
||||
const handleClearAllUsers = useCallback(() => {
|
||||
setSelectedUsers(new Set())
|
||||
}, [])
|
||||
|
||||
return {
|
||||
selectedMetrics,
|
||||
viewMode,
|
||||
selectedUsers,
|
||||
handleMetricToggle,
|
||||
handleViewModeChange,
|
||||
handleUserToggle,
|
||||
handleSelectAllUsers,
|
||||
handleClearAllUsers
|
||||
}
|
||||
}
|
||||
211
src/renderer/src/components/report-analysis/index.tsx
Normal file
211
src/renderer/src/components/report-analysis/index.tsx
Normal file
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* ReportAnalysisDialog Component - Refactored
|
||||
*
|
||||
* A comprehensive dashboard for analyzing ERP system execution reports.
|
||||
* Features include:
|
||||
* - Aggregated view: Daily metrics overview
|
||||
* - Comparison view: User performance comparison
|
||||
* - Interactive filtering and metric selection
|
||||
*
|
||||
* This refactored version separates concerns into:
|
||||
* - Custom hooks for business logic
|
||||
* - Reusable components for UI
|
||||
* - Utility functions for data processing
|
||||
*/
|
||||
|
||||
import React, { useCallback } from 'react'
|
||||
import { X, BarChart3, Loader2, AlertCircle } from 'lucide-react'
|
||||
import { ReportAnalysisDialogProps, MetricKey } from './types'
|
||||
import { useReportData } from './hooks/useReportData'
|
||||
import { useChartData } from './hooks/useChartData'
|
||||
import { useReportFilters } from './hooks/useReportFilters'
|
||||
import { MetricSelector } from './components/MetricSelector'
|
||||
import { ViewModeToggle } from './components/ViewModeToggle'
|
||||
import { UserFilter } from './components/UserFilter'
|
||||
import { ReportChart } from './components/ReportChart'
|
||||
|
||||
export const ReportAnalysisDialog: React.FC<ReportAnalysisDialogProps> = ({
|
||||
isOpen,
|
||||
onClose,
|
||||
isAdmin
|
||||
}) => {
|
||||
// Data management hook
|
||||
const { isLoading, error, reportData, loadAndAnalyzeReports } = useReportData(isAdmin, isOpen)
|
||||
|
||||
// Filter state management hook
|
||||
const {
|
||||
selectedMetrics,
|
||||
viewMode,
|
||||
selectedUsers,
|
||||
handleMetricToggle,
|
||||
handleViewModeChange,
|
||||
handleUserToggle,
|
||||
handleSelectAllUsers: handleSelectAllUsersWithParam,
|
||||
handleClearAllUsers
|
||||
} = useReportFilters()
|
||||
|
||||
// Chart data transformation hook (must be called before using allUsers)
|
||||
const { chartData, allUsers, comparisonChartData } = useChartData(
|
||||
reportData,
|
||||
selectedUsers,
|
||||
selectedMetrics
|
||||
)
|
||||
|
||||
// Adapt handleSelectAllUsers to match component interface
|
||||
const handleSelectAllUsers = useCallback(() => {
|
||||
handleSelectAllUsersWithParam(allUsers)
|
||||
}, [allUsers, handleSelectAllUsersWithParam])
|
||||
|
||||
// Early returns for conditional rendering
|
||||
if (!isOpen) return null
|
||||
if (!isAdmin) return null
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-[110] flex items-center justify-center bg-slate-900/50 backdrop-blur-sm animate-in fade-in duration-200">
|
||||
<div className="bg-white rounded-2xl shadow-2xl w-[1000px] max-w-[95vw] h-[85vh] flex flex-col border border-slate-200 overflow-hidden animate-in zoom-in-95 duration-200">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between px-6 py-4 border-b border-slate-200 bg-slate-50 flex-shrink-0">
|
||||
<div className="flex items-center gap-2 text-slate-800">
|
||||
<BarChart3 size={20} className="text-blue-600" />
|
||||
<h2 className="text-lg font-semibold">执行报告分析</h2>
|
||||
</div>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="text-slate-400 hover:text-slate-600 hover:bg-slate-200/50 p-1.5 rounded-lg transition-colors"
|
||||
>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 overflow-hidden flex flex-col bg-white">
|
||||
{isLoading ? (
|
||||
<LoadingState />
|
||||
) : error ? (
|
||||
<ErrorState error={error} onRetry={loadAndAnalyzeReports} />
|
||||
) : chartData.length === 0 ? (
|
||||
<EmptyState />
|
||||
) : (
|
||||
<MainContent
|
||||
viewMode={viewMode}
|
||||
selectedMetrics={selectedMetrics}
|
||||
selectedUsers={selectedUsers}
|
||||
allUsers={allUsers}
|
||||
chartData={chartData}
|
||||
comparisonChartData={comparisonChartData}
|
||||
handleMetricToggle={handleMetricToggle}
|
||||
handleViewModeChange={handleViewModeChange}
|
||||
handleUserToggle={handleUserToggle}
|
||||
handleSelectAllUsers={handleSelectAllUsers}
|
||||
handleClearAllUsers={handleClearAllUsers}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Sub-components for better organization
|
||||
// ============================================================================
|
||||
|
||||
const LoadingState: React.FC = () => (
|
||||
<div className="flex-1 flex flex-col items-center justify-center text-slate-500">
|
||||
<Loader2 size={32} className="animate-spin text-blue-500 mb-4" />
|
||||
<p>正在分析报告数据,可能需要几秒钟...</p>
|
||||
</div>
|
||||
)
|
||||
|
||||
const ErrorState: React.FC<{ error: string; onRetry: () => void }> = ({ error, onRetry }) => (
|
||||
<div className="flex-1 flex flex-col items-center justify-center text-red-500 p-8 text-center">
|
||||
<AlertCircle size={48} className="mb-4 opacity-80" />
|
||||
<p className="text-lg font-medium mb-2">分析失败</p>
|
||||
<p className="text-sm opacity-80">{error}</p>
|
||||
<button
|
||||
onClick={onRetry}
|
||||
className="mt-6 px-4 py-2 bg-red-50 text-red-600 border border-red-200 rounded-lg hover:bg-red-100 transition-colors"
|
||||
>
|
||||
重试
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
|
||||
const EmptyState: React.FC = () => (
|
||||
<div className="flex-1 flex flex-col items-center justify-center text-slate-400">
|
||||
<BarChart3 size={48} className="mb-4 opacity-50 text-slate-300" />
|
||||
<p>暂无报告数据可供分析</p>
|
||||
</div>
|
||||
)
|
||||
|
||||
interface MainContentProps {
|
||||
viewMode: 'aggregated' | 'comparison'
|
||||
selectedMetrics: Set<MetricKey>
|
||||
selectedUsers: Set<string>
|
||||
allUsers: string[]
|
||||
chartData: any[]
|
||||
comparisonChartData: any[]
|
||||
handleMetricToggle: (metric: MetricKey) => void
|
||||
handleViewModeChange: (newMode: 'aggregated' | 'comparison') => void
|
||||
handleUserToggle: (user: string) => void
|
||||
handleSelectAllUsers: () => void
|
||||
handleClearAllUsers: () => void
|
||||
}
|
||||
|
||||
const MainContent: React.FC<MainContentProps> = ({
|
||||
viewMode,
|
||||
selectedMetrics,
|
||||
selectedUsers,
|
||||
allUsers,
|
||||
chartData,
|
||||
comparisonChartData,
|
||||
handleMetricToggle,
|
||||
handleViewModeChange,
|
||||
handleUserToggle,
|
||||
handleSelectAllUsers,
|
||||
handleClearAllUsers
|
||||
}) => (
|
||||
<div className="flex-1 flex flex-col p-6 overflow-y-auto">
|
||||
{/* Metric Selector */}
|
||||
<MetricSelector
|
||||
selectedMetrics={selectedMetrics}
|
||||
viewMode={viewMode}
|
||||
onMetricToggle={handleMetricToggle}
|
||||
/>
|
||||
|
||||
{/* View Mode Toggle */}
|
||||
<ViewModeToggle viewMode={viewMode} onViewModeChange={handleViewModeChange} />
|
||||
|
||||
{/* User Filter - Only in comparison mode */}
|
||||
{viewMode === 'comparison' && (
|
||||
<UserFilter
|
||||
allUsers={allUsers}
|
||||
selectedUsers={selectedUsers}
|
||||
onUserToggle={handleUserToggle}
|
||||
onSelectAll={handleSelectAllUsers}
|
||||
onClearAll={handleClearAllUsers}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Chart */}
|
||||
<ReportChart
|
||||
viewMode={viewMode}
|
||||
selectedMetrics={selectedMetrics}
|
||||
selectedUsers={selectedUsers}
|
||||
allUsers={allUsers}
|
||||
chartData={chartData}
|
||||
comparisonChartData={comparisonChartData}
|
||||
/>
|
||||
|
||||
{/* Description */}
|
||||
<div className="mt-4 text-center text-xs text-slate-400">
|
||||
{viewMode === 'aggregated'
|
||||
? '数据以天为单位进行聚合统计。展示的是选定时间段内的总量。'
|
||||
: selectedUsers.size === 0
|
||||
? '展示所有用户的数据对比。未选择用户时显示全部。'
|
||||
: '展示选定用户的数据对比。'}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
export default ReportAnalysisDialog
|
||||
153
src/renderer/src/components/report-analysis/types.ts
Normal file
153
src/renderer/src/components/report-analysis/types.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* Type definitions for Report Analysis feature
|
||||
* Centralized type management for better maintainability
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Domain Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Extracted metrics from a single report
|
||||
*/
|
||||
export interface ReportMetrics {
|
||||
date: string
|
||||
user: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeSecs: number
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregated daily metrics
|
||||
*/
|
||||
export interface DailyMetrics {
|
||||
date: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeSecs: number
|
||||
avgExecutionTimeSecs: number
|
||||
users: string[] // Unique users who ran reports on this day
|
||||
reportCount: number
|
||||
}
|
||||
|
||||
/**
|
||||
* User-specific daily metrics for comparison view
|
||||
*/
|
||||
export interface UserDailyMetrics {
|
||||
date: string
|
||||
user: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeSecs: number
|
||||
reportCount: number
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// UI Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Available metric keys for chart display
|
||||
*/
|
||||
export type MetricKey = keyof Omit<
|
||||
DailyMetrics,
|
||||
'date' | 'users' | 'reportCount' | 'avgExecutionTimeSecs'
|
||||
>
|
||||
|
||||
/**
|
||||
* View mode for the analysis display
|
||||
*/
|
||||
export type ViewMode = 'aggregated' | 'comparison'
|
||||
|
||||
// ============================================================================
|
||||
// Component Props Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Props for the main ReportAnalysisDialog component
|
||||
*/
|
||||
export interface ReportAnalysisDialogProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
isAdmin: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Props for custom tooltip component
|
||||
*/
|
||||
export interface CustomTooltipProps {
|
||||
active?: boolean
|
||||
payload?: any[]
|
||||
label?: string
|
||||
chartData: DailyMetrics[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Props for comparison tooltip component
|
||||
*/
|
||||
export interface ComparisonTooltipProps {
|
||||
active?: boolean
|
||||
payload?: any[]
|
||||
label?: string
|
||||
users: string[]
|
||||
selectedUsers: Set<string>
|
||||
selectedMetrics: Set<MetricKey>
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Configuration Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Metric labels mapping
|
||||
*/
|
||||
export const METRIC_LABELS: Record<MetricKey, string> = {
|
||||
processedOrders: '处理订单数',
|
||||
deletedMaterials: '删除物料数',
|
||||
skippedMaterials: '跳过物料数',
|
||||
errors: '错误数量',
|
||||
retriedOrders: '重试订单数',
|
||||
successfulRetries: '成功重试数',
|
||||
executionTimeSecs: '每订单平均耗时(秒)'
|
||||
}
|
||||
|
||||
/**
|
||||
* Metric colors mapping
|
||||
*/
|
||||
export const METRIC_COLORS: Record<MetricKey, string> = {
|
||||
processedOrders: '#3b82f6', // blue-500
|
||||
deletedMaterials: '#ef4444', // red-500
|
||||
skippedMaterials: '#eab308', // yellow-500
|
||||
errors: '#000000', // black
|
||||
retriedOrders: '#8b5cf6', // violet-500
|
||||
successfulRetries: '#10b981', // emerald-500
|
||||
executionTimeSecs: '#f97316' // orange-500
|
||||
}
|
||||
|
||||
/**
|
||||
* User colors for comparison view
|
||||
*/
|
||||
export const USER_COLORS = [
|
||||
'#3b82f6', // blue-500
|
||||
'#10b981', // emerald-500
|
||||
'#f59e0b', // amber-500
|
||||
'#ef4444', // red-500
|
||||
'#8b5cf6', // violet-500
|
||||
'#ec4899', // pink-500
|
||||
'#06b6d4', // cyan-500
|
||||
'#84cc16' // lime-500
|
||||
]
|
||||
202
src/renderer/src/components/report-analysis/utils/aggregators.ts
Normal file
202
src/renderer/src/components/report-analysis/utils/aggregators.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
/**
|
||||
* Data aggregation utilities for transforming raw report data
|
||||
* Handles date-based and user-based aggregations
|
||||
*/
|
||||
|
||||
import { ReportMetrics, DailyMetrics, UserDailyMetrics, MetricKey } from '../types'
|
||||
|
||||
// ============================================================================
|
||||
// Aggregation Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Aggregates report data by date for the overview chart
|
||||
* Calculates totals and averages for each day
|
||||
*
|
||||
* @param reportData - Array of individual report metrics
|
||||
* @returns Array of daily aggregated metrics sorted by date
|
||||
*/
|
||||
export const aggregateByDate = (reportData: ReportMetrics[]): DailyMetrics[] => {
|
||||
if (!reportData.length) return []
|
||||
|
||||
const dailyMap = new Map<string, DailyMetrics>()
|
||||
|
||||
// First pass: aggregate by date
|
||||
for (const data of reportData) {
|
||||
const { date } = data
|
||||
|
||||
if (!dailyMap.has(date)) {
|
||||
dailyMap.set(date, {
|
||||
date,
|
||||
processedOrders: 0,
|
||||
deletedMaterials: 0,
|
||||
skippedMaterials: 0,
|
||||
errors: 0,
|
||||
retriedOrders: 0,
|
||||
successfulRetries: 0,
|
||||
executionTimeSecs: 0,
|
||||
avgExecutionTimeSecs: 0,
|
||||
users: [],
|
||||
reportCount: 0
|
||||
})
|
||||
}
|
||||
|
||||
const day = dailyMap.get(date)!
|
||||
day.processedOrders += data.processedOrders
|
||||
day.deletedMaterials += data.deletedMaterials
|
||||
day.skippedMaterials += data.skippedMaterials
|
||||
day.errors += data.errors
|
||||
day.retriedOrders += data.retriedOrders
|
||||
day.successfulRetries += data.successfulRetries
|
||||
day.executionTimeSecs += data.executionTimeSecs
|
||||
day.reportCount += 1
|
||||
|
||||
if (!day.users.includes(data.user)) {
|
||||
day.users.push(data.user)
|
||||
}
|
||||
}
|
||||
|
||||
// Second pass: calculate averages
|
||||
for (const day of dailyMap.values()) {
|
||||
day.avgExecutionTimeSecs =
|
||||
day.processedOrders > 0 ? day.executionTimeSecs / day.processedOrders : 0
|
||||
// Replace executionTimeSecs with avgExecutionTimeSecs for chart display
|
||||
day.executionTimeSecs = day.avgExecutionTimeSecs
|
||||
}
|
||||
|
||||
// Convert map to array and sort by date
|
||||
return Array.from(dailyMap.values()).sort((a, b) => a.date.localeCompare(b.date))
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts all unique users from report data
|
||||
*
|
||||
* @param reportData - Array of individual report metrics
|
||||
* @returns Sorted array of unique usernames
|
||||
*/
|
||||
export const extractAllUsers = (reportData: ReportMetrics[]): string[] => {
|
||||
const userSet = new Set<string>()
|
||||
reportData.forEach((data) => userSet.add(data.user))
|
||||
return Array.from(userSet).sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregates report data by date AND user for comparison view
|
||||
* Allows comparing multiple users across the same time periods
|
||||
*
|
||||
* @param reportData - Array of individual report metrics
|
||||
* @param selectedUsers - Set of selected users for filtering (empty = all)
|
||||
* @returns Array of user-daily aggregated metrics sorted by date and user
|
||||
*/
|
||||
export const aggregateByUserAndDate = (
|
||||
reportData: ReportMetrics[],
|
||||
selectedUsers: Set<string>
|
||||
): UserDailyMetrics[] => {
|
||||
if (!reportData.length) return []
|
||||
|
||||
// Filter by selected users if any
|
||||
const filteredData =
|
||||
selectedUsers.size > 0 ? reportData.filter((data) => selectedUsers.has(data.user)) : reportData
|
||||
|
||||
// Group by date + user
|
||||
const keyMap = new Map<string, UserDailyMetrics>()
|
||||
|
||||
for (const data of filteredData) {
|
||||
const key = `${data.date}|${data.user}`
|
||||
|
||||
if (!keyMap.has(key)) {
|
||||
keyMap.set(key, {
|
||||
date: data.date,
|
||||
user: data.user,
|
||||
processedOrders: 0,
|
||||
deletedMaterials: 0,
|
||||
skippedMaterials: 0,
|
||||
errors: 0,
|
||||
retriedOrders: 0,
|
||||
successfulRetries: 0,
|
||||
executionTimeSecs: 0,
|
||||
reportCount: 0
|
||||
})
|
||||
}
|
||||
|
||||
const entry = keyMap.get(key)!
|
||||
entry.processedOrders += data.processedOrders
|
||||
entry.deletedMaterials += data.deletedMaterials
|
||||
entry.skippedMaterials += data.skippedMaterials
|
||||
entry.errors += data.errors
|
||||
entry.retriedOrders += data.retriedOrders
|
||||
entry.successfulRetries += data.successfulRetries
|
||||
entry.executionTimeSecs += data.executionTimeSecs
|
||||
entry.reportCount += 1
|
||||
}
|
||||
|
||||
// Calculate average execution time per order for each entry
|
||||
for (const entry of keyMap.values()) {
|
||||
entry.executionTimeSecs =
|
||||
entry.processedOrders > 0 ? entry.executionTimeSecs / entry.processedOrders : 0
|
||||
}
|
||||
|
||||
return Array.from(keyMap.values()).sort((a, b) => {
|
||||
const dateCompare = a.date.localeCompare(b.date)
|
||||
if (dateCompare !== 0) return dateCompare
|
||||
return a.user.localeCompare(b.user)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats comparison data for chart rendering
|
||||
* Transforms user-date data into a format suitable for Recharts
|
||||
*
|
||||
* @param comparisonData - Array of user-daily aggregated metrics
|
||||
* @param selectedUsers - Set of selected users for filtering
|
||||
* @param selectedMetrics - Set of selected metrics to display
|
||||
* @returns Array of chart data points formatted for Recharts
|
||||
*/
|
||||
export const formatComparisonChartData = (
|
||||
comparisonData: UserDailyMetrics[],
|
||||
selectedUsers: Set<string>,
|
||||
selectedMetrics: Set<MetricKey>
|
||||
): any[] => {
|
||||
if (!comparisonData.length) return []
|
||||
|
||||
const dates = [...new Set(comparisonData.map((d) => d.date))].sort()
|
||||
const users = [...new Set(comparisonData.map((d) => d.user))]
|
||||
.filter((user) => selectedUsers.size === 0 || selectedUsers.has(user))
|
||||
.sort()
|
||||
|
||||
const lookup = new Map<string, UserDailyMetrics>()
|
||||
comparisonData.forEach((d) => {
|
||||
lookup.set(`${d.date}|${d.user}`, d)
|
||||
})
|
||||
|
||||
return dates.map((date) => {
|
||||
const point: any = { date }
|
||||
users.forEach((user) => {
|
||||
const key = `${date}|${user}`
|
||||
const data = lookup.get(key)
|
||||
|
||||
Array.from(selectedMetrics).forEach((metric) => {
|
||||
const userKey = `${user}_${metric}` as any
|
||||
point[userKey] = data ? (data as any)[metric] : 0
|
||||
})
|
||||
})
|
||||
return point
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Utility Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Gets a consistent color for a user based on their position in the list
|
||||
*
|
||||
* @param user - Username to get color for
|
||||
* @param users - Array of all users (for consistent indexing)
|
||||
* @param colors - Array of color values to cycle through
|
||||
* @returns Color hex string
|
||||
*/
|
||||
export const getUserColor = (user: string, users: string[], colors: string[]): string => {
|
||||
const index = users.indexOf(user)
|
||||
return colors[index % colors.length]
|
||||
}
|
||||
185
src/renderer/src/components/report-analysis/utils/parser.ts
Normal file
185
src/renderer/src/components/report-analysis/utils/parser.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
/**
|
||||
* Parser utilities for extracting report data from markdown content
|
||||
* Optimized for performance with pre-compiled regex patterns
|
||||
*/
|
||||
|
||||
// ============================================================================
|
||||
// Result Types
|
||||
// ============================================================================
|
||||
|
||||
interface ExtractValueResult {
|
||||
execTimeStr: string | null
|
||||
user: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeStr: string
|
||||
}
|
||||
|
||||
interface ParsedReportData {
|
||||
date: string
|
||||
user: string
|
||||
processedOrders: number
|
||||
deletedMaterials: number
|
||||
skippedMaterials: number
|
||||
errors: number
|
||||
retriedOrders: number
|
||||
successfulRetries: number
|
||||
executionTimeSecs: number
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Parser Functions
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Extracts values from markdown report content using pre-compiled regex patterns.
|
||||
* Patterns are created once and reused for better performance.
|
||||
*
|
||||
* @param content - The markdown content to parse
|
||||
* @returns Extracted metrics values
|
||||
*/
|
||||
export const extractReportValues = (content: string): ExtractValueResult => {
|
||||
// Pre-compile regex patterns for better performance (js-hoist-regexp)
|
||||
const createPattern = (key: string) => ({
|
||||
standard: new RegExp(`\\|\\s*\\*\\*${key}\\*\\*\\s*\\|\\s*\`([^\`]+)\`\\s*\\|`),
|
||||
noBackticks: new RegExp(
|
||||
`\\|\\s*\\*\\*${key}\\*\\*\\s*\\|\\s*([^\\|\\s]+(?:\\s+[^\\|\\s]+)*)\\s*\\|`
|
||||
),
|
||||
relaxed: new RegExp(`\\|\\s*\\*\\*${key}\\*\\*\\s*\\|\\s*(.+?)\\s*\\|`)
|
||||
})
|
||||
|
||||
const extractValue = (key: string): string | null => {
|
||||
const patterns = createPattern(key)
|
||||
|
||||
for (const pattern of Object.values(patterns)) {
|
||||
const match = content.match(pattern)
|
||||
if (match && match[1]) {
|
||||
const value = match[1].trim()
|
||||
return value.replace(/`/g, '')
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
execTimeStr: extractValue('执行时间'),
|
||||
user: extractValue('操作用户') || 'unknown',
|
||||
processedOrders: parseInt(extractValue('处理订单数') || '0', 10),
|
||||
deletedMaterials: parseInt(extractValue('删除物料数') || '0', 10),
|
||||
skippedMaterials: parseInt(extractValue('跳过物料数') || '0', 10),
|
||||
errors: parseInt(extractValue('错误数量') || '0', 10),
|
||||
retriedOrders: parseInt(extractValue('重试订单数') || '0', 10),
|
||||
successfulRetries: parseInt(extractValue('成功重试数') || '0', 10),
|
||||
executionTimeStr: extractValue('执行耗时') || '0秒'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses duration string (e.g., "5分30秒", "120秒") to total seconds
|
||||
*
|
||||
* @param durationStr - Duration string to parse
|
||||
* @returns Total seconds
|
||||
*/
|
||||
export const parseDurationToSeconds = (durationStr: string): number => {
|
||||
// Handle empty or zero case
|
||||
if (!durationStr || durationStr === '0秒' || durationStr === '0分0秒') {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Remove any remaining backticks
|
||||
const cleanStr = durationStr.replace(/`/g, '').trim()
|
||||
|
||||
let totalSeconds = 0
|
||||
const minutesMatch = cleanStr.match(/(\d+)分/)
|
||||
if (minutesMatch) {
|
||||
totalSeconds += parseInt(minutesMatch[1], 10) * 60
|
||||
}
|
||||
const secondsMatch = cleanStr.match(/(\d+)秒/)
|
||||
if (secondsMatch) {
|
||||
totalSeconds += parseInt(secondsMatch[1], 10)
|
||||
}
|
||||
|
||||
return totalSeconds
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a date object to Chinese date string format (YYYY-MM-DD)
|
||||
*
|
||||
* @param date - Date object to format
|
||||
* @returns Formatted date string
|
||||
*/
|
||||
export const formatDateToChinese = (date: Date): string => {
|
||||
return date
|
||||
.toLocaleDateString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit'
|
||||
})
|
||||
.replace(/\//g, '-')
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses report metadata and content into a structured ReportMetrics object
|
||||
*
|
||||
* @param report - Report metadata with key, username, lastModified
|
||||
* @param content - Markdown content of the report
|
||||
* @returns Parsed report metrics
|
||||
*/
|
||||
export const parseReportData = (
|
||||
report: { key: string; username?: string; lastModified?: string | number | Date },
|
||||
content: string
|
||||
): ParsedReportData => {
|
||||
const values = extractReportValues(content)
|
||||
const user = values.user || report.username || 'unknown'
|
||||
const executionTimeSecs = parseDurationToSeconds(values.executionTimeStr)
|
||||
|
||||
if (values.executionTimeStr === '0秒') {
|
||||
try {
|
||||
window.electron?.logger?.log?.('warn', 'Failed to extract execution time from report', {
|
||||
reportKey: report.key,
|
||||
context: 'ReportParser'
|
||||
})
|
||||
} catch {
|
||||
// Gracefully degrade if logger unavailable
|
||||
}
|
||||
}
|
||||
|
||||
// Try to parse the date
|
||||
let dateStr = '未知日期'
|
||||
let timestamp = report.lastModified ? new Date(report.lastModified).getTime() : 0
|
||||
|
||||
if (values.execTimeStr) {
|
||||
try {
|
||||
const parsedDate = new Date(values.execTimeStr)
|
||||
if (!isNaN(parsedDate.getTime())) {
|
||||
dateStr = formatDateToChinese(parsedDate)
|
||||
timestamp = parsedDate.getTime()
|
||||
}
|
||||
} catch {
|
||||
// Fallback to report lastModified
|
||||
}
|
||||
}
|
||||
|
||||
if (dateStr === '未知日期' && report.lastModified) {
|
||||
const d = new Date(report.lastModified)
|
||||
dateStr = formatDateToChinese(d)
|
||||
}
|
||||
|
||||
return {
|
||||
date: dateStr,
|
||||
user,
|
||||
processedOrders: values.processedOrders,
|
||||
deletedMaterials: values.deletedMaterials,
|
||||
skippedMaterials: values.skippedMaterials,
|
||||
errors: values.errors,
|
||||
retriedOrders: values.retriedOrders,
|
||||
successfulRetries: values.successfulRetries,
|
||||
executionTimeSecs,
|
||||
timestamp
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ export interface CurrentUser {
|
||||
export interface SelectedUserInfo {
|
||||
id: number
|
||||
username: string
|
||||
userType: 'Admin' | 'User' | 'Guest'
|
||||
userType: 'Admin' | 'User'
|
||||
computerName?: string
|
||||
}
|
||||
|
||||
@@ -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()])
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useState, useCallback } from 'react'
|
||||
interface UserInfo {
|
||||
id: number
|
||||
username: string
|
||||
userType: 'Admin' | 'User' | 'Guest'
|
||||
userType: 'Admin' | 'User'
|
||||
computerName?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useState, useEffect, useMemo, useRef, useCallback } from 'react'
|
||||
import { showSuccess, showError, showWarning, formatListMessage } from '../stores/useAppStore'
|
||||
import { useLogger } from './useLogger'
|
||||
import { ConfirmDialogProps } from '../components/ui/ConfirmDialog'
|
||||
import {
|
||||
buildDeletionPlan,
|
||||
@@ -20,6 +21,8 @@ import {
|
||||
import type { CleanerProgress, CleanerReportData, ValidationResult } from './cleaner/types'
|
||||
|
||||
export function useCleaner() {
|
||||
const logger = useLogger('Cleaner')
|
||||
|
||||
// Authentication & permissions
|
||||
const [isAdmin, setIsAdmin] = useState(false)
|
||||
const [currentUsername, setCurrentUsername] = useState<string>('')
|
||||
@@ -108,11 +111,13 @@ export function useCleaner() {
|
||||
setSelectedManagers(new Set([result.currentUsername]))
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Initialization failed:', err)
|
||||
logger.error('Cleaner page initialization failed', {
|
||||
error: err instanceof Error ? err.message : String(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
initializePage()
|
||||
}, [])
|
||||
}, [logger])
|
||||
|
||||
// Subscribe to cleaner progress events
|
||||
useEffect(() => {
|
||||
@@ -135,11 +140,13 @@ export function useCleaner() {
|
||||
setProcessConcurrency(result.processConcurrency)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to load cleaner config:', err)
|
||||
logger.error('Failed to load cleaner config', {
|
||||
error: err instanceof Error ? err.message : String(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
loadCleanerConfig()
|
||||
}, [])
|
||||
}, [logger])
|
||||
|
||||
useEffect(() => {
|
||||
sessionStorage.setItem('cleaner_dryRun', dryRun.toString())
|
||||
@@ -155,7 +162,10 @@ export function useCleaner() {
|
||||
try {
|
||||
await window.electron.config.updateCleaner({ processConcurrency: clamped })
|
||||
} catch (err) {
|
||||
console.error('Failed to update cleaner config:', err)
|
||||
logger.error('Failed to update process concurrency', {
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
value: clamped
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, RefObject } from 'react'
|
||||
import { useLogger } from './useLogger'
|
||||
|
||||
/**
|
||||
* Options for configuring dialog focus management
|
||||
@@ -83,6 +84,8 @@ export function useDialogFocus(options: UseDialogFocusOptions): UseDialogFocusRe
|
||||
shouldCloseOnEscape = true
|
||||
} = options
|
||||
|
||||
const logger = useLogger('DialogFocus')
|
||||
|
||||
// Handle Escape key press
|
||||
useEffect(() => {
|
||||
if (!isOpen) return
|
||||
@@ -175,10 +178,10 @@ export function useDialogFocus(options: UseDialogFocusOptions): UseDialogFocusRe
|
||||
return
|
||||
}
|
||||
// Fallback: element found but not visible, log warning and try default
|
||||
console.warn(`Focus element found but not visible: ${initialFocusSelector}`)
|
||||
logger.warn('Focus element found but not visible', { selector: initialFocusSelector })
|
||||
} else {
|
||||
// Fallback: element not found, log warning and try default
|
||||
console.warn(`Focus element not found for selector: ${initialFocusSelector}`)
|
||||
logger.warn('Focus element not found for selector', { selector: initialFocusSelector })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,7 +206,7 @@ export function useDialogFocus(options: UseDialogFocusOptions): UseDialogFocusRe
|
||||
|
||||
// Delay to ensure portal content is rendered
|
||||
requestAnimationFrame(setupFocus)
|
||||
}, [isOpen, dialogRef, initialFocusSelector])
|
||||
}, [isOpen, dialogRef, initialFocusSelector, logger])
|
||||
|
||||
// Restore focus to trigger element when dialog closes
|
||||
useEffect(() => {
|
||||
@@ -214,50 +217,36 @@ export function useDialogFocus(options: UseDialogFocusOptions): UseDialogFocusRe
|
||||
|
||||
// Check if element still exists in DOM
|
||||
if (!triggerElement || !document.contains(triggerElement)) {
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn('[useDialogFocus] Trigger element not found in DOM, cannot restore focus')
|
||||
}
|
||||
logger.warn('Trigger element not found in DOM, cannot restore focus')
|
||||
return
|
||||
}
|
||||
|
||||
// Check if element has a focus method
|
||||
if (typeof triggerElement.focus !== 'function') {
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn('[useDialogFocus] Trigger element does not have a focus method')
|
||||
}
|
||||
logger.warn('Trigger element does not have a focus method')
|
||||
return
|
||||
}
|
||||
|
||||
// Check if element is visible (not display: none)
|
||||
const style = window.getComputedStyle(triggerElement)
|
||||
if (style.display === 'none') {
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn('[useDialogFocus] Trigger element is display: none, cannot restore focus')
|
||||
}
|
||||
logger.warn('Trigger element is display: none, cannot restore focus')
|
||||
return
|
||||
}
|
||||
|
||||
if (style.visibility === 'hidden') {
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn(
|
||||
'[useDialogFocus] Trigger element is visibility: hidden, cannot restore focus'
|
||||
)
|
||||
}
|
||||
logger.warn('Trigger element is visibility: hidden, cannot restore focus')
|
||||
return
|
||||
}
|
||||
|
||||
// Check if element is disabled
|
||||
if (triggerElement instanceof HTMLButtonElement && triggerElement.disabled) {
|
||||
if (import.meta.env.DEV) {
|
||||
console.warn('[useDialogFocus] Trigger element is disabled, cannot restore focus')
|
||||
}
|
||||
logger.warn('Trigger element is disabled, cannot restore focus')
|
||||
// Try to find nearest enabled ancestor or fallback to body
|
||||
const focusableParent = findNearestFocusableElement(triggerElement)
|
||||
if (focusableParent) {
|
||||
focusableParent.focus({ preventScroll: true })
|
||||
if (import.meta.env.DEV) {
|
||||
console.info('[useDialogFocus] Restored focus to nearest focusable ancestor')
|
||||
}
|
||||
logger.debug('Restored focus to nearest focusable ancestor')
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -265,13 +254,11 @@ export function useDialogFocus(options: UseDialogFocusOptions): UseDialogFocusRe
|
||||
// All checks passed, restore focus
|
||||
try {
|
||||
triggerElement.focus({ preventScroll: true })
|
||||
if (import.meta.env.DEV) {
|
||||
console.info('[useDialogFocus] Successfully restored focus to trigger element')
|
||||
}
|
||||
logger.debug('Successfully restored focus to trigger element')
|
||||
} catch (error) {
|
||||
if (import.meta.env.DEV) {
|
||||
console.error('[useDialogFocus] Error restoring focus:', error)
|
||||
}
|
||||
logger.error('Error restoring focus', {
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,7 +295,7 @@ export function useDialogFocus(options: UseDialogFocusOptions): UseDialogFocusRe
|
||||
|
||||
// Use microtask queue to ensure this runs after DOM cleanup
|
||||
queueMicrotask(restoreFocus)
|
||||
}, [isOpen, triggerRef])
|
||||
}, [isOpen, triggerRef, logger])
|
||||
|
||||
// Return focus lock configuration
|
||||
return {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { useEffect } from 'react'
|
||||
import type { LogLevel } from '../stores/extractorStore'
|
||||
import { useExtractorStore } from '../stores/extractorStore'
|
||||
import { useLogger } from './useLogger'
|
||||
|
||||
function isLogLevel(value: string): value is LogLevel {
|
||||
return ['info', 'success', 'warning', 'error', 'system'].includes(value)
|
||||
}
|
||||
|
||||
export function useExtractor() {
|
||||
const logger = useLogger('Extractor')
|
||||
|
||||
const {
|
||||
isRunning,
|
||||
isComplete,
|
||||
@@ -73,6 +76,11 @@ export function useExtractor() {
|
||||
'success',
|
||||
`提取完成:下载 ${data.downloadedFiles.length} 个文件,共 ${data.recordCount} 条记录`
|
||||
)
|
||||
logger.info('Extraction completed', {
|
||||
downloadedFiles: data.downloadedFiles.length,
|
||||
recordCount: data.recordCount,
|
||||
errorCount: data.errors.length
|
||||
})
|
||||
if (data.errors.length > 0) {
|
||||
addLog('warning', `存在 ${data.errors.length} 个错误`)
|
||||
// Log each error detail for debugging
|
||||
@@ -83,11 +91,13 @@ export function useExtractor() {
|
||||
} else {
|
||||
setError(response.error || '提取失败')
|
||||
addLog('error', response.error || '提取失败')
|
||||
logger.error('Extraction failed', { error: response.error })
|
||||
}
|
||||
} catch (err) {
|
||||
const errMsg = err instanceof Error ? err.message : '发生未知错误'
|
||||
setError(errMsg)
|
||||
addLog('error', errMsg)
|
||||
logger.error('Extraction exception', { error: errMsg })
|
||||
} finally {
|
||||
setRunning(false)
|
||||
setProgress(null)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user