feat: 持久化采集记录去重 + Goods系统批量导入
- collected.txt 跨文件/跨次记录已查询条码,避免重复采集 - Web UI 显示历史已采集条码计数 - Goods API 新增 POST /api/import/bypos 批量导入端点(含upsert) - 采集器 Web UI 新增一键导入按钮(登录+上传JSONL) - Windows exe 重新编译 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
@@ -182,19 +183,22 @@ func (c *Collector) lookup(ctx context.Context, barcode string) (*Product, error
|
||||
|
||||
// ---- job / collector state ----
|
||||
|
||||
const collectedFile = "collected.txt"
|
||||
|
||||
type Stats struct {
|
||||
Running bool `json:"running"`
|
||||
Total int64 `json:"total"`
|
||||
Done int64 `json:"done"`
|
||||
Hits int64 `json:"hits"`
|
||||
Miss int64 `json:"miss"`
|
||||
Invalid int64 `json:"invalid"`
|
||||
Errors int64 `json:"errors"`
|
||||
Skipped int64 `json:"skipped"`
|
||||
Current string `json:"current"`
|
||||
OutFile string `json:"out_file"`
|
||||
StartedAt string `json:"started_at"`
|
||||
Message string `json:"message"`
|
||||
Running bool `json:"running"`
|
||||
Total int64 `json:"total"`
|
||||
Done int64 `json:"done"`
|
||||
Hits int64 `json:"hits"`
|
||||
Miss int64 `json:"miss"`
|
||||
Invalid int64 `json:"invalid"`
|
||||
Errors int64 `json:"errors"`
|
||||
Skipped int64 `json:"skipped"`
|
||||
Current string `json:"current"`
|
||||
OutFile string `json:"out_file"`
|
||||
StartedAt string `json:"started_at"`
|
||||
Message string `json:"message"`
|
||||
HistoryCount int `json:"history_count"`
|
||||
}
|
||||
|
||||
type Collector struct {
|
||||
@@ -215,8 +219,9 @@ type Collector struct {
|
||||
startedAt string
|
||||
message string
|
||||
|
||||
seen map[string]struct{} // barcodes already in output (dedupe / resume)
|
||||
recent []Product // ring of last results for UI
|
||||
seen map[string]struct{} // barcodes already collected (dedupe)
|
||||
collectedFp *os.File // persistent history file handle
|
||||
recent []Product // ring of last results for UI
|
||||
}
|
||||
|
||||
func NewCollector(sdogID string) *Collector {
|
||||
@@ -227,6 +232,7 @@ func NewCollector(sdogID string) *Collector {
|
||||
seen: map[string]struct{}{},
|
||||
}
|
||||
c.current.Store("")
|
||||
c.loadCollected()
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -238,20 +244,22 @@ func (c *Collector) snapshot() Stats {
|
||||
msg := c.message
|
||||
out := c.outPath
|
||||
started := c.startedAt
|
||||
hist := len(c.seen)
|
||||
c.mu.Unlock()
|
||||
return Stats{
|
||||
Running: c.isRunning(),
|
||||
Total: atomic.LoadInt64(&c.total),
|
||||
Done: atomic.LoadInt64(&c.done),
|
||||
Hits: atomic.LoadInt64(&c.hits),
|
||||
Miss: atomic.LoadInt64(&c.miss),
|
||||
Invalid: atomic.LoadInt64(&c.invalid),
|
||||
Errors: atomic.LoadInt64(&c.errors),
|
||||
Skipped: atomic.LoadInt64(&c.skipped),
|
||||
Current: cur,
|
||||
OutFile: out,
|
||||
StartedAt: started,
|
||||
Message: msg,
|
||||
Running: c.isRunning(),
|
||||
Total: atomic.LoadInt64(&c.total),
|
||||
Done: atomic.LoadInt64(&c.done),
|
||||
Hits: atomic.LoadInt64(&c.hits),
|
||||
Miss: atomic.LoadInt64(&c.miss),
|
||||
Invalid: atomic.LoadInt64(&c.invalid),
|
||||
Errors: atomic.LoadInt64(&c.errors),
|
||||
Skipped: atomic.LoadInt64(&c.skipped),
|
||||
Current: cur,
|
||||
OutFile: out,
|
||||
StartedAt: started,
|
||||
Message: msg,
|
||||
HistoryCount: hist,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,9 +280,24 @@ func (c *Collector) pushRecent(p Product) {
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
// loadSeen reads an existing output file to build the dedupe set (for resume).
|
||||
// loadCollected reads the persistent history file (one barcode per line).
|
||||
func (c *Collector) loadCollected() {
|
||||
f, err := os.Open(collectedFile)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
sc := bufio.NewScanner(f)
|
||||
for sc.Scan() {
|
||||
bc := strings.TrimSpace(sc.Text())
|
||||
if bc != "" {
|
||||
c.seen[bc] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loadSeen reads an existing output file and adds to the dedupe set (for resume).
|
||||
func (c *Collector) loadSeen(path string) error {
|
||||
c.seen = map[string]struct{}{}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
@@ -387,6 +410,7 @@ func (c *Collector) Start(req JobReq) error {
|
||||
}
|
||||
c.outFile = f
|
||||
c.outPath = abs
|
||||
c.collectedFp, _ = os.OpenFile(collectedFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
|
||||
// reset counters
|
||||
atomic.StoreInt64(&c.total, int64(len(barcodes)))
|
||||
@@ -424,6 +448,11 @@ func (c *Collector) run(ctx context.Context, barcodes []string, req JobReq) {
|
||||
c.outFile.Close()
|
||||
c.outFile = nil
|
||||
}
|
||||
if c.collectedFp != nil {
|
||||
c.collectedFp.Sync()
|
||||
c.collectedFp.Close()
|
||||
c.collectedFp = nil
|
||||
}
|
||||
c.current.Store("")
|
||||
}()
|
||||
|
||||
@@ -472,14 +501,17 @@ func (c *Collector) run(ctx context.Context, barcodes []string, req JobReq) {
|
||||
}
|
||||
atomic.AddInt64(&c.done, 1)
|
||||
c.pushRecent(*p)
|
||||
writeMu.Lock()
|
||||
if p.Status == "hit" || req.LogMiss {
|
||||
line, _ := json.Marshal(p)
|
||||
writeMu.Lock()
|
||||
c.outFile.Write(line)
|
||||
c.outFile.Write([]byte("\n"))
|
||||
c.seen[bc] = struct{}{}
|
||||
writeMu.Unlock()
|
||||
}
|
||||
c.seen[bc] = struct{}{}
|
||||
if c.collectedFp != nil {
|
||||
c.collectedFp.WriteString(bc + "\n")
|
||||
}
|
||||
writeMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user