Files
membank/auto-check/pkg/report/device.go
12600k-rog-d4 2aaaa16ae2 0813
2026-08-13 22:49:50 +08:00

81 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package report
import (
"fmt"
"path/filepath"
"strings"
"time"
"auto-check/pkg/config"
"auto-check/pkg/model"
"auto-check/pkg/stress"
)
// BuildDeviceReport 由压测结果构建设备报告MAC 唯一标识)
func BuildDeviceReport(dev *model.Device, rpt *stress.Report) *Report {
b := NewBuilder(ConfigSummary{})
b.StartPhase("压力测试")
b.AddHost(HostResult{MAC: dev.MAC, IP: dev.IP})
for _, res := range rpt.Results {
b.AddStressResult(dev.MAC, StressResult{
Type: string(res.Type),
Status: res.Status,
Duration: res.Duration.String(),
Score: res.Score,
ScoreUnit: stress.ScoreUnit(res.Type),
Metrics: res.OrderedMetrics(),
Error: res.Error,
Output: res.Output,
})
}
b.EndPhase(rpt.Summary())
return b.Build()
}
// SaveDeviceReport 保存设备报告(文件名含 MAC/IP/时间戳)
func SaveDeviceReport(dev *model.Device, rpt *stress.Report, dir string) error {
if dir == "" {
dir = "reports"
}
ts := time.Now().Format("20060102-150405")
name := strings.ReplaceAll(dev.MAC, ":", "")
path := filepath.Join(dir, fmt.Sprintf("report-%s-%s-%s.txt", name, dev.IP, ts))
return BuildDeviceReport(dev, rpt).SaveFile(path, "text")
}
// PrintDeviceReport 渲染 80mm 模板并送打印机
func PrintDeviceReport(dev *model.Device, rpt *stress.Report, cfg config.PrintConfig) error {
width := cfg.Width
if width <= 0 {
width = 42
}
text, err := BuildDeviceReport(dev, rpt).Render80mmWithConfig(TemplateConfig{Width: width})
if err != nil {
return err
}
printer, err := NewPrinter(config.PrintConfig{Enabled: true, Backend: cfg.Backend, Device: cfg.Device, Width: width})
if err != nil {
return err
}
defer printer.Close()
return printer.Print(text)
}
// SaveAndPrintDeviceReport 保存报告并(启用时)打印,含错误输出(一站式业务入口)
func SaveAndPrintDeviceReport(dev *model.Device, rpt *stress.Report, dir string, printCfg config.PrintConfig) {
if err := SaveDeviceReport(dev, rpt, dir); err != nil {
fmt.Printf(" [报告] %s 保存失败: %v\n", dev.IP, err)
}
if printCfg.Enabled {
if err := PrintDeviceReport(dev, rpt, printCfg); err != nil {
fmt.Printf(" [打印] %s 失败: %v\n", dev.IP, err)
} else {
fmt.Printf(" [打印] %s 已送出 (%s)\n", dev.IP, printCfg.Backend)
}
}
}