Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b9c806719
|
||
|
|
477077d9e3
|
||
|
|
cdb93926bf
|
||
|
|
0bb584fe10
|
||
|
|
331497aff4
|
||
|
|
a2234077df
|
||
|
|
39285a2ee7
|
||
|
|
4959173f39
|
||
|
|
ca7c494a06
|
||
|
|
e1911ef13b
|
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/vasyansk/imap-copier/internal/crypto"
|
"github.com/vasyansk/imap-copier/internal/crypto"
|
||||||
|
"github.com/vasyansk/imap-copier/internal/imapx"
|
||||||
"github.com/vasyansk/imap-copier/internal/store"
|
"github.com/vasyansk/imap-copier/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ type AccountView struct {
|
|||||||
Copied int64 `json:"copied"`
|
Copied int64 `json:"copied"`
|
||||||
Skipped int64 `json:"skipped"`
|
Skipped int64 `json:"skipped"`
|
||||||
Errors int64 `json:"errors"`
|
Errors int64 `json:"errors"`
|
||||||
|
LastError string `json:"last_error,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func accountDTO(a store.Account) AccountView {
|
func accountDTO(a store.Account) AccountView {
|
||||||
@@ -27,9 +29,80 @@ func accountDTO(a store.Account) AccountView {
|
|||||||
ID: a.ID, SrcLogin: a.SrcLogin, DstLogin: a.DstLogin,
|
ID: a.ID, SrcLogin: a.SrcLogin, DstLogin: a.DstLogin,
|
||||||
TestSrcStatus: a.TestSrcStatus, TestDstStatus: a.TestDstStatus,
|
TestSrcStatus: a.TestSrcStatus, TestDstStatus: a.TestDstStatus,
|
||||||
Status: a.Status, Copied: a.Copied, Skipped: a.Skipped, Errors: a.Errors,
|
Status: a.Status, Copied: a.Copied, Skipped: a.Skipped, Errors: a.Errors,
|
||||||
|
LastError: a.LastError,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handleProbeFolders tests both logins with the given credentials and returns
|
||||||
|
// the folder list on each side, so the UI can offer a source->destination
|
||||||
|
// folder mapping before the account is created. Credentials are not stored.
|
||||||
|
func (s *Server) handleProbeFolders(w http.ResponseWriter, r *http.Request) {
|
||||||
|
taskID, err := pathID(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "bad id", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
task, err := s.store.GetTask(r.Context(), taskID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
srcEP, err := s.store.GetEndpoint(r.Context(), task.SrcEndpointID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dstEP, err := s.store.GetEndpoint(r.Context(), task.DstEndpointID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var body struct {
|
||||||
|
SrcLogin string `json:"src_login"`
|
||||||
|
SrcPass string `json:"src_pass"`
|
||||||
|
DstLogin string `json:"dst_login"`
|
||||||
|
DstPass string `json:"dst_pass"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
http.Error(w, "bad json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
probe := func(ep store.Endpoint, login, pass string) map[string]any {
|
||||||
|
folders, err := imapx.TestLogin(r.Context(),
|
||||||
|
imapx.Endpoint{Host: ep.Host, Port: ep.Port, TLSMode: ep.TLSMode},
|
||||||
|
strings.TrimSpace(login), strings.TrimSpace(pass))
|
||||||
|
if err != nil {
|
||||||
|
return map[string]any{"ok": false, "error": err.Error(), "folders": []string{}}
|
||||||
|
}
|
||||||
|
return map[string]any{"ok": true, "folders": folders}
|
||||||
|
}
|
||||||
|
writeJSON(w, http.StatusOK, map[string]any{
|
||||||
|
"src": probe(srcEP, body.SrcLogin, body.SrcPass),
|
||||||
|
"dst": probe(dstEP, body.DstLogin, body.DstPass),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleSetFolderMapping replaces the task's src->dst folder mapping.
|
||||||
|
func (s *Server) handleSetFolderMapping(w http.ResponseWriter, r *http.Request) {
|
||||||
|
taskID, err := pathID(r, "id")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "bad id", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var body struct {
|
||||||
|
Mapping map[string]string `json:"mapping"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
http.Error(w, "bad json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := s.store.SetTaskFolderMapping(r.Context(), taskID, body.Mapping); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
func pathID(r *http.Request, name string) (int64, error) {
|
func pathID(r *http.Request, name string) (int64, error) {
|
||||||
return strconv.ParseInt(r.PathValue(name), 10, 64)
|
return strconv.ParseInt(r.PathValue(name), 10, 64)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ func (s *Server) Router() http.Handler {
|
|||||||
api.HandleFunc("GET /api/tasks/{id}", s.handleGetTask)
|
api.HandleFunc("GET /api/tasks/{id}", s.handleGetTask)
|
||||||
api.HandleFunc("DELETE /api/tasks/{id}", s.handleDeleteTask)
|
api.HandleFunc("DELETE /api/tasks/{id}", s.handleDeleteTask)
|
||||||
api.HandleFunc("POST /api/tasks/{id}/accounts", s.handleCreateAccount)
|
api.HandleFunc("POST /api/tasks/{id}/accounts", s.handleCreateAccount)
|
||||||
|
api.HandleFunc("POST /api/tasks/{id}/probe", s.handleProbeFolders)
|
||||||
|
api.HandleFunc("PUT /api/tasks/{id}/folder-mapping", s.handleSetFolderMapping)
|
||||||
api.HandleFunc("DELETE /api/tasks/{id}/accounts/{accountId}", s.handleDeleteAccount)
|
api.HandleFunc("DELETE /api/tasks/{id}/accounts/{accountId}", s.handleDeleteAccount)
|
||||||
api.HandleFunc("POST /api/tasks/{id}/import", s.handleImportCSV)
|
api.HandleFunc("POST /api/tasks/{id}/import", s.handleImportCSV)
|
||||||
api.HandleFunc("POST /api/tasks/{id}/test", s.handleTestAccounts)
|
api.HandleFunc("POST /api/tasks/{id}/test", s.handleTestAccounts)
|
||||||
|
|||||||
@@ -3,9 +3,21 @@ package imapx
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/emersion/go-imap/v2"
|
||||||
"github.com/emersion/go-imap/v2/imapclient"
|
"github.com/emersion/go-imap/v2/imapclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// FolderMessageCount opens a folder read-only (EXAMINE) and returns how many
|
||||||
|
// messages it holds — used to plan an accurate overall progress total before
|
||||||
|
// copying begins. It does not fetch any message bodies.
|
||||||
|
func FolderMessageCount(c *imapclient.Client, folder string) (int64, error) {
|
||||||
|
sel, err := c.Select(folder, &imap.SelectOptions{ReadOnly: true}).Wait()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int64(sel.NumMessages), nil
|
||||||
|
}
|
||||||
|
|
||||||
// ListFolders returns the mailbox names visible on an already-connected, logged-in client.
|
// ListFolders returns the mailbox names visible on an already-connected, logged-in client.
|
||||||
func ListFolders(c *imapclient.Client) ([]string, error) {
|
func ListFolders(c *imapclient.Client) ([]string, error) {
|
||||||
mboxes, err := c.List("", "*", nil).Collect()
|
mboxes, err := c.List("", "*", nil).Collect()
|
||||||
|
|||||||
+52
-19
@@ -23,6 +23,10 @@ type CopyDeps struct {
|
|||||||
// (potentially long) envelope fetch, with the message count in the source
|
// (potentially long) envelope fetch, with the message count in the source
|
||||||
// folder — for progress visibility.
|
// folder — for progress visibility.
|
||||||
OnFolder func(srcFolder, dstFolder string, total int64)
|
OnFolder func(srcFolder, dstFolder string, total int64)
|
||||||
|
// OnScan is called during the streaming metadata pass with how many of the
|
||||||
|
// folder's messages have been examined so far — so the UI shows movement
|
||||||
|
// while dedup decisions are made, before bodies start copying.
|
||||||
|
OnScan func(scanned, total int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyResult summarizes the outcome of one CopyFolder run.
|
// CopyResult summarizes the outcome of one CopyFolder run.
|
||||||
@@ -46,48 +50,77 @@ func CopyFolder(ctx context.Context, src, dst *imapclient.Client, srcFolder, dst
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return res, fmt.Errorf("examine src %q: %w", srcFolder, err)
|
return res, fmt.Errorf("examine src %q: %w", srcFolder, err)
|
||||||
}
|
}
|
||||||
|
total := int64(sel.NumMessages)
|
||||||
if deps.OnFolder != nil {
|
if deps.OnFolder != nil {
|
||||||
deps.OnFolder(srcFolder, dstFolder, int64(sel.NumMessages))
|
deps.OnFolder(srcFolder, dstFolder, total)
|
||||||
}
|
}
|
||||||
if sel.NumMessages == 0 {
|
if total == 0 {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1) Collect envelope+uid+size for every message (cheap pass, no bodies).
|
|
||||||
metaSet := imap.SeqSet{imap.SeqRange{Start: 1, Stop: sel.NumMessages}}
|
|
||||||
metas, err := src.Fetch(metaSet, &imap.FetchOptions{
|
|
||||||
UID: true, Envelope: true, RFC822Size: true, Flags: true, InternalDate: true,
|
|
||||||
}).Collect()
|
|
||||||
if err != nil {
|
|
||||||
return res, fmt.Errorf("fetch meta: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// dst folder must exist (idempotent create; ignore "already exists").
|
// dst folder must exist (idempotent create; ignore "already exists").
|
||||||
_ = dst.Create(dstFolder, nil).Wait()
|
_ = dst.Create(dstFolder, nil).Wait()
|
||||||
|
|
||||||
for _, m := range metas {
|
// Pass 1: STREAM metadata (no bodies) via Next(), dedup as we go, and queue
|
||||||
|
// only the new messages. Streaming (not Collect) means progress shows during
|
||||||
|
// the scan and memory stays flat — we hold small meta for new messages only.
|
||||||
|
type queued struct {
|
||||||
|
uid imap.UID
|
||||||
|
key string
|
||||||
|
flags []imap.Flag
|
||||||
|
internalDate time.Time
|
||||||
|
}
|
||||||
|
var todo []queued
|
||||||
|
metaSet := imap.SeqSet{imap.SeqRange{Start: 1, Stop: sel.NumMessages}}
|
||||||
|
fc := src.Fetch(metaSet, &imap.FetchOptions{
|
||||||
|
UID: true, Envelope: true, RFC822Size: true, Flags: true, InternalDate: true,
|
||||||
|
})
|
||||||
|
var scanned int64
|
||||||
|
for {
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
|
_ = fc.Close()
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
msg := fc.Next()
|
||||||
key := MessageKey(m.Envelope, m.RFC822Size)
|
if msg == nil {
|
||||||
already, err := deps.IsMigrated(key)
|
break
|
||||||
|
}
|
||||||
|
buf, err := msg.Collect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.Errors++
|
res.Errors++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if already {
|
scanned++
|
||||||
|
key := MessageKey(buf.Envelope, buf.RFC822Size)
|
||||||
|
already, err := deps.IsMigrated(key)
|
||||||
|
if err != nil {
|
||||||
|
res.Errors++
|
||||||
|
} else if already {
|
||||||
res.Skipped++
|
res.Skipped++
|
||||||
if deps.OnProgress != nil {
|
if deps.OnProgress != nil {
|
||||||
deps.OnProgress(res.Copied, res.Skipped)
|
deps.OnProgress(res.Copied, res.Skipped)
|
||||||
}
|
}
|
||||||
continue
|
} else {
|
||||||
|
todo = append(todo, queued{uid: buf.UID, key: key, flags: buf.Flags, internalDate: buf.InternalDate})
|
||||||
}
|
}
|
||||||
if err := streamOne(src, dst, dstFolder, m.UID, m.Flags, m.InternalDate); err != nil {
|
if deps.OnScan != nil {
|
||||||
|
deps.OnScan(scanned, total)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := fc.Close(); err != nil {
|
||||||
|
return res, fmt.Errorf("fetch meta %q: %w", srcFolder, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass 2: fetch bodies for the queued (new) messages, one at a time.
|
||||||
|
for _, q := range todo {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
if err := streamOne(src, dst, dstFolder, q.uid, q.flags, q.internalDate); err != nil {
|
||||||
res.Errors++
|
res.Errors++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := deps.MarkMigrated(dstFolder, key); err != nil {
|
if err := deps.MarkMigrated(dstFolder, q.key); err != nil {
|
||||||
res.Errors++
|
res.Errors++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/vasyansk/imap-copier/internal/crypto"
|
"github.com/vasyansk/imap-copier/internal/crypto"
|
||||||
"github.com/vasyansk/imap-copier/internal/imapx"
|
"github.com/vasyansk/imap-copier/internal/imapx"
|
||||||
@@ -216,6 +217,7 @@ func (o *Orchestrator) runAccount(ctx context.Context, task store.Task, runID in
|
|||||||
"dst_login": a.DstLogin, "dst_host": dstEP.Host, "dst_port": dstEP.Port,
|
"dst_login": a.DstLogin, "dst_host": dstEP.Host, "dst_port": dstEP.Port,
|
||||||
}})
|
}})
|
||||||
_ = o.store.SetAccountStatus(ctx, a.ID, "running")
|
_ = o.store.SetAccountStatus(ctx, a.ID, "running")
|
||||||
|
_ = o.store.SetAccountError(ctx, a.ID, "") // clear any error from a previous run
|
||||||
|
|
||||||
// Per-account cancellable context: IMAP work uses actx (so CancelAccount
|
// Per-account cancellable context: IMAP work uses actx (so CancelAccount
|
||||||
// stops it); DB writes keep the parent ctx so status/counters persist even
|
// stops it); DB writes keep the parent ctx so status/counters persist even
|
||||||
@@ -266,42 +268,105 @@ func (o *Orchestrator) runAccount(ctx context.Context, task store.Task, runID in
|
|||||||
return o.accountFailed(ctx, task.ID, a, srcEP, dstEP, "src", err)
|
return o.accountFailed(ctx, task.ID, a, srcEP, dstEP, "src", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Planning pass: EXAMINE every folder up front to learn the total message
|
||||||
|
// count, so the UI can show an accurate overall bar / ETA before copying.
|
||||||
|
type folderPlan struct {
|
||||||
|
src, dst string
|
||||||
|
total int64
|
||||||
|
}
|
||||||
|
plan := make([]folderPlan, 0, len(folders))
|
||||||
|
var grandTotal int64
|
||||||
|
for _, folder := range folders {
|
||||||
|
if actx.Err() != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
df := folder
|
||||||
|
if m, ok := task.FolderMapping[folder]; ok {
|
||||||
|
df = m
|
||||||
|
}
|
||||||
|
n, cerr := imapx.FolderMessageCount(src, folder)
|
||||||
|
if cerr != nil && actx.Err() == nil {
|
||||||
|
slog.Warn("count folder failed", "account", a.ID, "folder", folder, "err", cerr)
|
||||||
|
}
|
||||||
|
plan = append(plan, folderPlan{src: folder, dst: df, total: n})
|
||||||
|
grandTotal += n
|
||||||
|
}
|
||||||
|
o.hub.Publish(wshub.Event{Type: "plan", TaskID: task.ID, Data: map[string]any{
|
||||||
|
"account_id": a.ID, "src_login": a.SrcLogin, "folders": len(plan), "total": grandTotal,
|
||||||
|
}})
|
||||||
|
|
||||||
var copied, skipped, errs int64
|
var copied, skipped, errs int64
|
||||||
|
// Account-level live progress state (all callbacks run on this goroutine,
|
||||||
|
// so plain vars are race-free). base* = totals from completed folders;
|
||||||
|
// c/s inside OnProgress are cumulative within the current folder.
|
||||||
|
var baseCopied, baseSkipped int64
|
||||||
|
var curFolder string
|
||||||
|
var curTotal int64
|
||||||
|
var lastEmit, lastScanEmit time.Time
|
||||||
deps := imapx.CopyDeps{
|
deps := imapx.CopyDeps{
|
||||||
IsMigrated: func(k string) (bool, error) { return o.store.IsMigrated(ctx, a.ID, k) },
|
IsMigrated: func(k string) (bool, error) { return o.store.IsMigrated(ctx, a.ID, k) },
|
||||||
MarkMigrated: func(folder, k string) error { return o.store.MarkMigrated(ctx, a.ID, folder, k) },
|
MarkMigrated: func(folder, k string) error { return o.store.MarkMigrated(ctx, a.ID, folder, k) },
|
||||||
OnProgress: func(c, s int) {
|
OnProgress: func(c, s int) {
|
||||||
o.hub.Publish(wshub.Event{Type: "progress", TaskID: task.ID,
|
now := time.Now()
|
||||||
Data: map[string]any{"account_id": a.ID, "copied": c, "skipped": s}})
|
done := c + s
|
||||||
|
// throttle to ~3/sec per account, but always emit folder completion
|
||||||
|
if now.Sub(lastEmit) < 350*time.Millisecond && int64(done) < curTotal {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lastEmit = now
|
||||||
|
o.hub.Publish(wshub.Event{Type: "progress", TaskID: task.ID, Data: map[string]any{
|
||||||
|
"account_id": a.ID,
|
||||||
|
"copied": baseCopied + int64(c),
|
||||||
|
"skipped": baseSkipped + int64(s),
|
||||||
|
"folder": curFolder,
|
||||||
|
"folder_done": done,
|
||||||
|
"folder_total": curTotal,
|
||||||
|
"account_total": grandTotal,
|
||||||
|
}})
|
||||||
},
|
},
|
||||||
// Fires after EXAMINE (before the long fetch) with the folder's message count.
|
// Fires after EXAMINE (before the long fetch) with the folder's message count.
|
||||||
OnFolder: func(srcFolder, dstFolder string, total int64) {
|
OnFolder: func(srcFolder, dstFolder string, total int64) {
|
||||||
|
curFolder, curTotal = srcFolder, total
|
||||||
o.hub.Publish(wshub.Event{Type: "folder", TaskID: task.ID, Data: map[string]any{
|
o.hub.Publish(wshub.Event{Type: "folder", TaskID: task.ID, Data: map[string]any{
|
||||||
"account_id": a.ID, "src_login": a.SrcLogin,
|
"account_id": a.ID, "src_login": a.SrcLogin,
|
||||||
"folder": srcFolder, "dst_folder": dstFolder, "messages": total,
|
"folder": srcFolder, "dst_folder": dstFolder, "messages": total,
|
||||||
}})
|
}})
|
||||||
},
|
},
|
||||||
|
// Fires while streaming metadata (dedup scan) so the UI shows movement
|
||||||
|
// before bodies start copying. Throttled to ~4/sec, always emit the last.
|
||||||
|
OnScan: func(scanned, total int64) {
|
||||||
|
now := time.Now()
|
||||||
|
if now.Sub(lastScanEmit) < 250*time.Millisecond && scanned < total {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lastScanEmit = now
|
||||||
|
o.hub.Publish(wshub.Event{Type: "scan", TaskID: task.ID, Data: map[string]any{
|
||||||
|
"account_id": a.ID, "folder": curFolder, "scanned": scanned, "folder_total": total,
|
||||||
|
}})
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, folder := range folders {
|
for _, fp := range plan {
|
||||||
if actx.Err() != nil {
|
if actx.Err() != nil {
|
||||||
break // cancelled — stop scheduling more folders
|
break // cancelled — stop scheduling more folders
|
||||||
}
|
}
|
||||||
dstFolder := folder
|
res, err := imapx.CopyFolder(actx, src, dst, fp.src, fp.dst, deps)
|
||||||
if m, ok := task.FolderMapping[folder]; ok {
|
folderErr := int64(0)
|
||||||
dstFolder = m
|
|
||||||
}
|
|
||||||
res, err := imapx.CopyFolder(actx, src, dst, folder, dstFolder, deps)
|
|
||||||
if err != nil && actx.Err() == nil {
|
if err != nil && actx.Err() == nil {
|
||||||
slog.Warn("folder copy error", "account", a.ID, "src_login", a.SrcLogin, "folder", folder, "err", err)
|
slog.Warn("folder copy error", "account", a.ID, "src_login", a.SrcLogin, "folder", fp.src, "err", err)
|
||||||
errs++
|
folderErr = 1
|
||||||
|
_ = o.store.SetAccountError(ctx, a.ID, "folder \""+fp.src+"\": "+err.Error())
|
||||||
o.hub.Publish(wshub.Event{Type: "error", TaskID: task.ID, Data: map[string]any{
|
o.hub.Publish(wshub.Event{Type: "error", TaskID: task.ID, Data: map[string]any{
|
||||||
"account_id": a.ID, "src_login": a.SrcLogin, "folder": folder, "error": err.Error(),
|
"account_id": a.ID, "src_login": a.SrcLogin, "folder": fp.src, "error": err.Error(),
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
copied += int64(res.Copied)
|
copied += int64(res.Copied)
|
||||||
skipped += int64(res.Skipped)
|
skipped += int64(res.Skipped)
|
||||||
errs += int64(res.Errors)
|
errs += int64(res.Errors) + folderErr
|
||||||
_ = o.store.IncAccountCounters(ctx, a.ID, int64(res.Copied), int64(res.Skipped), int64(res.Errors))
|
baseCopied += int64(res.Copied)
|
||||||
|
baseSkipped += int64(res.Skipped)
|
||||||
|
// Persist message-level AND folder-level errors so the account row's
|
||||||
|
// error count matches the task status (done_with_errors).
|
||||||
|
_ = o.store.IncAccountCounters(ctx, a.ID, int64(res.Copied), int64(res.Skipped), int64(res.Errors)+folderErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if actx.Err() != nil {
|
if actx.Err() != nil {
|
||||||
@@ -313,7 +378,11 @@ func (o *Orchestrator) runAccount(ctx context.Context, task store.Task, runID in
|
|||||||
return copied, skipped, errs
|
return copied, skipped, errs
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = o.store.SetAccountStatus(ctx, a.ID, "done")
|
acctStatus := "done"
|
||||||
|
if errs > 0 {
|
||||||
|
acctStatus = "done_with_errors"
|
||||||
|
}
|
||||||
|
_ = o.store.SetAccountStatus(ctx, a.ID, acctStatus)
|
||||||
o.hub.Publish(wshub.Event{Type: "account_done", TaskID: task.ID,
|
o.hub.Publish(wshub.Event{Type: "account_done", TaskID: task.ID,
|
||||||
Data: map[string]any{"account_id": a.ID, "src_login": a.SrcLogin, "dst_login": a.DstLogin,
|
Data: map[string]any{"account_id": a.ID, "src_login": a.SrcLogin, "dst_login": a.DstLogin,
|
||||||
"copied": copied, "skipped": skipped, "errors": errs}})
|
"copied": copied, "skipped": skipped, "errors": errs}})
|
||||||
@@ -335,6 +404,7 @@ func (o *Orchestrator) accountFailed(ctx context.Context, taskID int64, a store.
|
|||||||
}
|
}
|
||||||
slog.Error("account failed", "account", a.ID, "side", side, "login", login, "host", host, "port", port, "err", err)
|
slog.Error("account failed", "account", a.ID, "side", side, "login", login, "host", host, "port", port, "err", err)
|
||||||
_ = o.store.SetAccountStatus(ctx, a.ID, "error")
|
_ = o.store.SetAccountStatus(ctx, a.ID, "error")
|
||||||
|
_ = o.store.SetAccountError(ctx, a.ID, side+" "+login+"@"+host+": "+err.Error())
|
||||||
o.hub.Publish(wshub.Event{Type: "error", TaskID: taskID,
|
o.hub.Publish(wshub.Event{Type: "error", TaskID: taskID,
|
||||||
Data: map[string]any{"account_id": a.ID, "side": side, "login": login, "host": host, "port": port, "error": err.Error()}})
|
Data: map[string]any{"account_id": a.ID, "side": side, "login": login, "host": host, "port": port, "error": err.Error()}})
|
||||||
return 0, 0, 1
|
return 0, 0, 1
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type Account struct {
|
|||||||
Copied int64
|
Copied int64
|
||||||
Skipped int64
|
Skipped int64
|
||||||
Errors int64
|
Errors int64
|
||||||
|
LastError string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) CreateAccount(ctx context.Context, a Account) (int64, error) {
|
func (s *Store) CreateAccount(ctx context.Context, a Account) (int64, error) {
|
||||||
@@ -38,7 +39,7 @@ func (s *Store) DeleteAccount(ctx context.Context, id int64) error {
|
|||||||
func (s *Store) ListAccountsByTask(ctx context.Context, taskID int64) ([]Account, error) {
|
func (s *Store) ListAccountsByTask(ctx context.Context, taskID int64) ([]Account, error) {
|
||||||
rows, err := s.Pool.Query(ctx,
|
rows, err := s.Pool.Query(ctx,
|
||||||
`SELECT id, task_id, src_login, src_pass_enc, dst_login, dst_pass_enc,
|
`SELECT id, task_id, src_login, src_pass_enc, dst_login, dst_pass_enc,
|
||||||
test_src_status, test_dst_status, status, copied_count, skipped_count, error_count
|
test_src_status, test_dst_status, status, copied_count, skipped_count, error_count, last_error
|
||||||
FROM accounts WHERE task_id=$1 ORDER BY id`, taskID)
|
FROM accounts WHERE task_id=$1 ORDER BY id`, taskID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -48,7 +49,7 @@ func (s *Store) ListAccountsByTask(ctx context.Context, taskID int64) ([]Account
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var a Account
|
var a Account
|
||||||
if err := rows.Scan(&a.ID, &a.TaskID, &a.SrcLogin, &a.SrcPassEnc, &a.DstLogin, &a.DstPassEnc,
|
if err := rows.Scan(&a.ID, &a.TaskID, &a.SrcLogin, &a.SrcPassEnc, &a.DstLogin, &a.DstPassEnc,
|
||||||
&a.TestSrcStatus, &a.TestDstStatus, &a.Status, &a.Copied, &a.Skipped, &a.Errors); err != nil {
|
&a.TestSrcStatus, &a.TestDstStatus, &a.Status, &a.Copied, &a.Skipped, &a.Errors, &a.LastError); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
out = append(out, a)
|
out = append(out, a)
|
||||||
@@ -56,6 +57,13 @@ func (s *Store) ListAccountsByTask(ctx context.Context, taskID int64) ([]Account
|
|||||||
return out, rows.Err()
|
return out, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetAccountError stores (or clears, with "") the last error message shown for
|
||||||
|
// an account, so it survives a page reload after the run's live log is gone.
|
||||||
|
func (s *Store) SetAccountError(ctx context.Context, id int64, msg string) error {
|
||||||
|
_, err := s.Pool.Exec(ctx, `UPDATE accounts SET last_error=$2 WHERE id=$1`, id, msg)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// side = "src" | "dst"
|
// side = "src" | "dst"
|
||||||
func (s *Store) SetAccountTestStatus(ctx context.Context, id int64, side, status string) error {
|
func (s *Store) SetAccountTestStatus(ctx context.Context, id int64, side, status string) error {
|
||||||
col := "test_src_status"
|
col := "test_src_status"
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetTaskFolderMappingRoundTrip(t *testing.T) {
|
||||||
|
s := testStore(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
e1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"})
|
||||||
|
e2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"})
|
||||||
|
taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: e1, DstEndpointID: e2})
|
||||||
|
|
||||||
|
m := map[string]string{"Спам": "Spam", "Отправленные": "Sent"}
|
||||||
|
if err := s.SetTaskFolderMapping(ctx, taskID, m); err != nil {
|
||||||
|
t.Fatalf("set mapping: %v", err)
|
||||||
|
}
|
||||||
|
got, err := s.GetTask(ctx, taskID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("get: %v", err)
|
||||||
|
}
|
||||||
|
if got.FolderMapping["Спам"] != "Spam" || got.FolderMapping["Отправленные"] != "Sent" {
|
||||||
|
t.Fatalf("mapping round-trip failed: %+v", got.FolderMapping)
|
||||||
|
}
|
||||||
|
// nil clears to empty object, not null
|
||||||
|
if err := s.SetTaskFolderMapping(ctx, taskID, nil); err != nil {
|
||||||
|
t.Fatalf("clear: %v", err)
|
||||||
|
}
|
||||||
|
got, _ = s.GetTask(ctx, taskID)
|
||||||
|
if got.FolderMapping == nil {
|
||||||
|
t.Fatal("mapping should be non-nil empty map after clear")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -57,6 +57,14 @@ func (s *Store) ListTasks(ctx context.Context) ([]Task, error) {
|
|||||||
return out, rows.Err()
|
return out, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Store) SetTaskFolderMapping(ctx context.Context, id int64, mapping map[string]string) error {
|
||||||
|
if mapping == nil {
|
||||||
|
mapping = map[string]string{}
|
||||||
|
}
|
||||||
|
_, err := s.Pool.Exec(ctx, `UPDATE tasks SET folder_mapping=$2 WHERE id=$1`, id, mapping)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Store) SetTaskStatus(ctx context.Context, id int64, status string) error {
|
func (s *Store) SetTaskStatus(ctx context.Context, id int64, status string) error {
|
||||||
_, err := s.Pool.Exec(ctx, `UPDATE tasks SET status=$2 WHERE id=$1`, id, status)
|
_, err := s.Pool.Exec(ctx, `UPDATE tasks SET status=$2 WHERE id=$1`, id, status)
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE accounts DROP COLUMN last_error;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE accounts ADD COLUMN last_error TEXT NOT NULL DEFAULT '';
|
||||||
@@ -32,6 +32,7 @@ export interface Account {
|
|||||||
copied: number
|
copied: number
|
||||||
skipped: number
|
skipped: number
|
||||||
errors: number
|
errors: number
|
||||||
|
last_error?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TaskDetail {
|
export interface TaskDetail {
|
||||||
@@ -84,6 +85,25 @@ export const deleteAccount = (taskId: number, accountId: number) =>
|
|||||||
export const cancelAccount = (taskId: number, accountId: number) =>
|
export const cancelAccount = (taskId: number, accountId: number) =>
|
||||||
api(`/api/tasks/${taskId}/accounts/${accountId}/cancel`, { method: 'POST' })
|
api(`/api/tasks/${taskId}/accounts/${accountId}/cancel`, { method: 'POST' })
|
||||||
|
|
||||||
|
export interface ProbeSide {
|
||||||
|
ok: boolean
|
||||||
|
folders?: string[]
|
||||||
|
error?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProbeResult {
|
||||||
|
src: ProbeSide
|
||||||
|
dst: ProbeSide
|
||||||
|
}
|
||||||
|
|
||||||
|
export const probeFolders = (
|
||||||
|
taskId: number,
|
||||||
|
creds: { src_login: string; src_pass: string; dst_login: string; dst_pass: string },
|
||||||
|
) => api<ProbeResult>(`/api/tasks/${taskId}/probe`, jsonBody(creds))
|
||||||
|
|
||||||
|
export const setFolderMapping = (taskId: number, mapping: Record<string, string>) =>
|
||||||
|
api(`/api/tasks/${taskId}/folder-mapping`, { ...jsonBody({ mapping }), method: 'PUT' })
|
||||||
|
|
||||||
export const listTasks = () => api<Task[]>('/api/tasks')
|
export const listTasks = () => api<Task[]>('/api/tasks')
|
||||||
|
|
||||||
export const getTask = (id: number) => api<TaskDetail>(`/api/tasks/${id}`)
|
export const getTask = (id: number) => api<TaskDetail>(`/api/tasks/${id}`)
|
||||||
|
|||||||
+104
-1
@@ -254,6 +254,49 @@
|
|||||||
cursor: not-allowed;
|
cursor: not-allowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* per-account live progress */
|
||||||
|
.acct-progress {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
min-width: 160px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pbar {
|
||||||
|
height: 4px;
|
||||||
|
background: var(--bg-inset);
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pbar-fill {
|
||||||
|
display: block;
|
||||||
|
height: 100%;
|
||||||
|
background: var(--accent);
|
||||||
|
transition: width 0.3s ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pmeta {
|
||||||
|
font-size: 10px;
|
||||||
|
color: var(--fg-dim);
|
||||||
|
letter-spacing: 0.04em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pscan {
|
||||||
|
color: var(--info);
|
||||||
|
}
|
||||||
|
|
||||||
|
.acct-error {
|
||||||
|
margin-top: 3px;
|
||||||
|
max-width: 260px;
|
||||||
|
font-size: 10px;
|
||||||
|
line-height: 1.35;
|
||||||
|
color: var(--fail);
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
/* clear-log button: mirrors the .panel-label tab on the right edge */
|
/* clear-log button: mirrors the .panel-label tab on the right edge */
|
||||||
.log-clear {
|
.log-clear {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -282,7 +325,6 @@
|
|||||||
|
|
||||||
.modal-dialog {
|
.modal-dialog {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 440px;
|
|
||||||
background: var(--bg-panel-raised);
|
background: var(--bg-panel-raised);
|
||||||
border: 1px solid var(--border-bright);
|
border: 1px solid var(--border-bright);
|
||||||
box-shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
|
box-shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
|
||||||
@@ -291,6 +333,67 @@
|
|||||||
animation: modal-rise 0.14s ease-out;
|
animation: modal-rise 0.14s ease-out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.modal-md {
|
||||||
|
max-width: 440px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-lg {
|
||||||
|
max-width: 680px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* folder mapping */
|
||||||
|
.map-hint {
|
||||||
|
margin: 0 0 16px;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.5;
|
||||||
|
color: var(--fg-dim);
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-hint code {
|
||||||
|
color: var(--accent-strong);
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-grid {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
max-height: 52vh;
|
||||||
|
overflow-y: auto;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr auto 1fr auto;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-src {
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--fg);
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-arrow {
|
||||||
|
color: var(--accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-select {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-new {
|
||||||
|
font-size: 10px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.08em;
|
||||||
|
color: var(--info);
|
||||||
|
}
|
||||||
|
|
||||||
.modal-title {
|
.modal-title {
|
||||||
font-family: var(--font-mono);
|
font-family: var(--font-mono);
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
import { useMemo, useState } from 'react'
|
||||||
|
import { Modal } from './Modal'
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
open: boolean
|
||||||
|
srcFolders: string[]
|
||||||
|
dstFolders: string[]
|
||||||
|
initialMapping: Record<string, string>
|
||||||
|
onConfirm: (mapping: Record<string, string>) => void
|
||||||
|
onCancel: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pick a sensible default destination for a source folder: an explicit prior
|
||||||
|
// mapping, else an exact same-name match on the destination, else keep the name.
|
||||||
|
function defaultDst(src: string, dstFolders: string[], initial: Record<string, string>): string {
|
||||||
|
if (initial[src]) return initial[src]
|
||||||
|
if (dstFolders.includes(src)) return src
|
||||||
|
return src
|
||||||
|
}
|
||||||
|
|
||||||
|
export function FolderMappingModal({ open, srcFolders, dstFolders, initialMapping, onConfirm, onCancel }: Props) {
|
||||||
|
const [choice, setChoice] = useState<Record<string, string>>({})
|
||||||
|
|
||||||
|
// Options per select: all destination folders, plus the source name itself
|
||||||
|
// (marked "create") when it does not already exist on the destination.
|
||||||
|
const options = useMemo(() => {
|
||||||
|
const set = new Set(dstFolders)
|
||||||
|
return (src: string) => {
|
||||||
|
const opts = [...dstFolders]
|
||||||
|
if (!set.has(src)) opts.unshift(src)
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
}, [dstFolders])
|
||||||
|
|
||||||
|
const valueFor = (src: string) => choice[src] ?? defaultDst(src, dstFolders, initialMapping)
|
||||||
|
|
||||||
|
function confirm() {
|
||||||
|
const mapping: Record<string, string> = { ...initialMapping }
|
||||||
|
for (const src of srcFolders) {
|
||||||
|
const dst = valueFor(src)
|
||||||
|
if (dst === src) delete mapping[src]
|
||||||
|
else mapping[src] = dst
|
||||||
|
}
|
||||||
|
onConfirm(mapping)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal open={open} title="Map folders (source → destination)" onClose={onCancel} size="lg">
|
||||||
|
<div className="map-body">
|
||||||
|
<p className="map-hint">
|
||||||
|
Route each source folder to an existing destination folder. Leaving a folder mapped to its own name
|
||||||
|
creates it on the destination if missing (e.g. map <code>Спам</code> → <code>Spam</code> to avoid duplicates).
|
||||||
|
</p>
|
||||||
|
<div className="map-grid">
|
||||||
|
{srcFolders.map((src) => {
|
||||||
|
const val = valueFor(src)
|
||||||
|
const creates = !dstFolders.includes(val)
|
||||||
|
return (
|
||||||
|
<div className="map-row" key={src}>
|
||||||
|
<span className="map-src" title={src}>
|
||||||
|
{src}
|
||||||
|
</span>
|
||||||
|
<span className="map-arrow">→</span>
|
||||||
|
<select
|
||||||
|
className="map-select"
|
||||||
|
value={val}
|
||||||
|
onChange={(e) => setChoice((c) => ({ ...c, [src]: e.target.value }))}
|
||||||
|
>
|
||||||
|
{options(src).map((f) => (
|
||||||
|
<option key={f} value={f}>
|
||||||
|
{f}
|
||||||
|
{f === src && !dstFolders.includes(src) ? ' (create)' : ''}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
{creates && <span className="map-new">new</span>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
<div className="modal-actions">
|
||||||
|
<button type="button" className="btn" onClick={onCancel}>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button type="button" className="btn btn-primary" data-modal-autofocus onClick={confirm}>
|
||||||
|
Save mapping & add
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ type ModalProps = {
|
|||||||
title?: string
|
title?: string
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
children: ReactNode
|
children: ReactNode
|
||||||
|
size?: 'md' | 'lg'
|
||||||
}
|
}
|
||||||
|
|
||||||
function focusable(root: HTMLElement | null): HTMLElement[] {
|
function focusable(root: HTMLElement | null): HTMLElement[] {
|
||||||
@@ -19,7 +20,7 @@ function focusable(root: HTMLElement | null): HTMLElement[] {
|
|||||||
// Reusable modal shell: portal to <body>, ESC to close, Tab focus-trap,
|
// Reusable modal shell: portal to <body>, ESC to close, Tab focus-trap,
|
||||||
// focus-on-open (prefers [data-modal-autofocus]), focus restore on close,
|
// focus-on-open (prefers [data-modal-autofocus]), focus restore on close,
|
||||||
// click-on-overlay to close, and body scroll lock while open.
|
// click-on-overlay to close, and body scroll lock while open.
|
||||||
export function Modal({ open, title, onClose, children }: ModalProps) {
|
export function Modal({ open, title, onClose, children, size = 'md' }: ModalProps) {
|
||||||
const dialogRef = useRef<HTMLDivElement>(null)
|
const dialogRef = useRef<HTMLDivElement>(null)
|
||||||
const prevFocus = useRef<HTMLElement | null>(null)
|
const prevFocus = useRef<HTMLElement | null>(null)
|
||||||
const onCloseRef = useRef(onClose)
|
const onCloseRef = useRef(onClose)
|
||||||
@@ -78,7 +79,7 @@ export function Modal({ open, title, onClose, children }: ModalProps) {
|
|||||||
if (e.target === e.currentTarget) onClose()
|
if (e.target === e.currentTarget) onClose()
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="modal-dialog" role="dialog" aria-modal="true" aria-label={title} ref={dialogRef} tabIndex={-1}>
|
<div className={`modal-dialog modal-${size}`} role="dialog" aria-modal="true" aria-label={title} ref={dialogRef} tabIndex={-1}>
|
||||||
{title && <div className="modal-title">{title}</div>}
|
{title && <div className="modal-title">{title}</div>}
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+180
-13
@@ -1,11 +1,33 @@
|
|||||||
import { useEffect, useRef, useState, type ChangeEvent, type FormEvent } from 'react'
|
import { useEffect, useRef, useState, type ChangeEvent, type FormEvent } from 'react'
|
||||||
import { cancelAccount, createAccount, deleteAccount, getTask, importCSV, runTask, testAccounts, type TaskDetail as TaskDetailData } from '../api'
|
import { cancelAccount, createAccount, deleteAccount, getTask, importCSV, probeFolders, runTask, setFolderMapping, testAccounts, type TaskDetail as TaskDetailData } from '../api'
|
||||||
import { connectTaskWS, type TaskEvent } from '../ws'
|
import { connectTaskWS, type TaskEvent } from '../ws'
|
||||||
import { StatusBadge } from '../components/StatusBadge'
|
import { StatusBadge } from '../components/StatusBadge'
|
||||||
import { useConfirm } from '../components/ConfirmProvider'
|
import { useConfirm } from '../components/ConfirmProvider'
|
||||||
|
import { FolderMappingModal } from '../components/FolderMappingModal'
|
||||||
|
|
||||||
const emptyAccount = { src_login: '', src_pass: '', dst_login: '', dst_pass: '' }
|
const emptyAccount = { src_login: '', src_pass: '', dst_login: '', dst_pass: '' }
|
||||||
|
|
||||||
|
// Live per-account progress derived from throttled `progress` WS events.
|
||||||
|
type LiveProgress = {
|
||||||
|
copied: number
|
||||||
|
skipped: number
|
||||||
|
total: number // account-wide message total from the planning pass (0 if unknown)
|
||||||
|
folder?: string
|
||||||
|
startTs: number
|
||||||
|
startCount: number
|
||||||
|
speed: number // messages/sec, averaged since the account's run started
|
||||||
|
scanFolder?: string
|
||||||
|
scanned?: number
|
||||||
|
scanTotal?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
function fmtDuration(sec: number): string {
|
||||||
|
if (!isFinite(sec) || sec < 0) return '—'
|
||||||
|
const m = Math.floor(sec / 60)
|
||||||
|
const s = Math.floor(sec % 60)
|
||||||
|
return `${m}:${String(s).padStart(2, '0')}`
|
||||||
|
}
|
||||||
|
|
||||||
// Human-readable one-line description of a task event for the log panel.
|
// Human-readable one-line description of a task event for the log panel.
|
||||||
function describeEvent(ev: TaskEvent): string {
|
function describeEvent(ev: TaskEvent): string {
|
||||||
const d = (ev.data ?? {}) as Record<string, unknown>
|
const d = (ev.data ?? {}) as Record<string, unknown>
|
||||||
@@ -18,10 +40,15 @@ function describeEvent(ev: TaskEvent): string {
|
|||||||
}
|
}
|
||||||
case 'account_started':
|
case 'account_started':
|
||||||
return `START #${d.account_id}: ${d.src_login}@${d.src_host}:${d.src_port} → ${d.dst_login}@${d.dst_host}:${d.dst_port}`
|
return `START #${d.account_id}: ${d.src_login}@${d.src_host}:${d.src_port} → ${d.dst_login}@${d.dst_host}:${d.dst_port}`
|
||||||
|
case 'plan':
|
||||||
|
return `PLAN #${d.account_id} (${d.src_login}): ${d.folders} folders, ${d.total} messages total`
|
||||||
case 'account_done':
|
case 'account_done':
|
||||||
return `DONE #${d.account_id} (${d.src_login} → ${d.dst_login}): copied ${d.copied}, skipped ${d.skipped}, errors ${d.errors}`
|
return `DONE #${d.account_id} (${d.src_login} → ${d.dst_login}): copied ${d.copied}, skipped ${d.skipped}, errors ${d.errors}`
|
||||||
case 'progress':
|
case 'progress': {
|
||||||
return `progress #${d.account_id}: copied ${d.copied}, skipped ${d.skipped}`
|
const pct = d.folder_total ? Math.floor((Number(d.folder_done) / Number(d.folder_total)) * 100) : 0
|
||||||
|
const loc = d.folder ? `"${d.folder}" ${d.folder_done}/${d.folder_total} (${pct}%) · ` : ''
|
||||||
|
return `progress #${d.account_id}: ${loc}copied ${d.copied}, skipped ${d.skipped}`
|
||||||
|
}
|
||||||
case 'folder': {
|
case 'folder': {
|
||||||
const route = d.dst_folder && d.dst_folder !== d.folder ? ` → "${d.dst_folder}"` : ''
|
const route = d.dst_folder && d.dst_folder !== d.folder ? ` → "${d.dst_folder}"` : ''
|
||||||
return `folder "${d.folder}"${route}: ${d.messages ?? 0} messages — fetching (#${d.account_id})`
|
return `folder "${d.folder}"${route}: ${d.messages ?? 0} messages — fetching (#${d.account_id})`
|
||||||
@@ -46,9 +73,11 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
const [notFound, setNotFound] = useState(false)
|
const [notFound, setNotFound] = useState(false)
|
||||||
const [log, setLog] = useState<{ type: string; text: string }[]>([])
|
const [log, setLog] = useState<{ type: string; text: string }[]>([])
|
||||||
const [form, setForm] = useState(emptyAccount)
|
const [form, setForm] = useState(emptyAccount)
|
||||||
const [busy, setBusy] = useState<'test' | 'run' | 'add' | 'import' | 'delete' | null>(null)
|
const [busy, setBusy] = useState<'test' | 'run' | 'add' | 'import' | 'delete' | 'probe' | null>(null)
|
||||||
|
const [mapState, setMapState] = useState<{ src: string[]; dst: string[]; creds: typeof emptyAccount } | null>(null)
|
||||||
const confirm = useConfirm()
|
const confirm = useConfirm()
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
|
const [live, setLive] = useState<Record<number, LiveProgress>>({})
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||||
|
|
||||||
function reload() {
|
function reload() {
|
||||||
@@ -65,8 +94,70 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
useEffect(
|
useEffect(
|
||||||
() =>
|
() =>
|
||||||
connectTaskWS(id, (ev: TaskEvent) => {
|
connectTaskWS(id, (ev: TaskEvent) => {
|
||||||
setLog((l) => [{ type: ev.type, text: describeEvent(ev) }, ...l].slice(0, 300))
|
// `scan` is high-frequency and shown in the progress cell, not the log.
|
||||||
if (['account_started', 'account_test', 'account_done', 'progress', 'run_started', 'run_done', 'error', 'folder', 'cancelled'].includes(ev.type)) {
|
if (ev.type !== 'scan') {
|
||||||
|
setLog((l) => [{ type: ev.type, text: describeEvent(ev) }, ...l].slice(0, 300))
|
||||||
|
}
|
||||||
|
const d = (ev.data ?? {}) as Record<string, number | string | undefined>
|
||||||
|
const accId = typeof d.account_id === 'number' ? d.account_id : undefined
|
||||||
|
|
||||||
|
if (ev.type === 'scan' && accId != null) {
|
||||||
|
setLive((prev) => {
|
||||||
|
const cur = prev[accId]
|
||||||
|
const base: LiveProgress = cur ?? { copied: 0, skipped: 0, total: 0, startTs: Date.now(), startCount: 0, speed: 0 }
|
||||||
|
return { ...prev, [accId]: { ...base, scanFolder: d.folder as string | undefined, scanned: Number(d.scanned ?? 0), scanTotal: Number(d.folder_total ?? 0) } }
|
||||||
|
})
|
||||||
|
} else if (ev.type === 'plan' && accId != null) {
|
||||||
|
const total = Number(d.total ?? 0)
|
||||||
|
const now = Date.now()
|
||||||
|
setLive((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[accId]: {
|
||||||
|
copied: prev[accId]?.copied ?? 0,
|
||||||
|
skipped: prev[accId]?.skipped ?? 0,
|
||||||
|
total,
|
||||||
|
folder: prev[accId]?.folder,
|
||||||
|
startTs: prev[accId]?.startTs ?? now,
|
||||||
|
startCount: prev[accId]?.startCount ?? 0,
|
||||||
|
speed: prev[accId]?.speed ?? 0,
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
} else if (ev.type === 'progress' && accId != null) {
|
||||||
|
const now = Date.now()
|
||||||
|
const copied = Number(d.copied ?? 0)
|
||||||
|
const skipped = Number(d.skipped ?? 0)
|
||||||
|
const processed = copied + skipped
|
||||||
|
setLive((prev) => {
|
||||||
|
const cur = prev[accId]
|
||||||
|
const startTs = cur?.startTs ?? now
|
||||||
|
const startCount = cur?.startCount ?? processed
|
||||||
|
const dt = (now - startTs) / 1000
|
||||||
|
const speed = dt > 0.5 ? (processed - startCount) / dt : (cur?.speed ?? 0)
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
[accId]: {
|
||||||
|
copied,
|
||||||
|
skipped,
|
||||||
|
total: Number(d.account_total ?? cur?.total ?? 0),
|
||||||
|
folder: d.folder as string | undefined,
|
||||||
|
startTs,
|
||||||
|
startCount,
|
||||||
|
speed,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else if (accId != null && (ev.type === 'account_started' || ev.type === 'account_done' || ev.type === 'cancelled' || (ev.type === 'error' && d.folder == null))) {
|
||||||
|
// terminal/reset for this account — drop live overlay, fall back to DB
|
||||||
|
setLive((prev) => {
|
||||||
|
if (!(accId in prev)) return prev
|
||||||
|
const next = { ...prev }
|
||||||
|
delete next[accId]
|
||||||
|
return next
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Structural events refresh the persisted view; `progress` is covered by live state.
|
||||||
|
if (['account_started', 'account_test', 'account_done', 'run_started', 'run_done', 'error', 'folder', 'cancelled', 'plan'].includes(ev.type)) {
|
||||||
reload()
|
reload()
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
@@ -75,11 +166,35 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
|
|
||||||
async function submitAccount(e: FormEvent) {
|
async function submitAccount(e: FormEvent) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
setBusy('probe')
|
||||||
|
setError(null)
|
||||||
|
try {
|
||||||
|
const creds = { ...form }
|
||||||
|
const res = await probeFolders(id, creds)
|
||||||
|
if (!res.src.ok || !res.dst.ok) {
|
||||||
|
const parts: string[] = []
|
||||||
|
if (!res.src.ok) parts.push(`source: ${res.src.error ?? 'login failed'}`)
|
||||||
|
if (!res.dst.ok) parts.push(`destination: ${res.dst.error ?? 'login failed'}`)
|
||||||
|
setError(`Connection test failed — ${parts.join('; ')}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setMapState({ src: res.src.folders ?? [], dst: res.dst.folders ?? [], creds })
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'Failed to test connections')
|
||||||
|
} finally {
|
||||||
|
setBusy(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function confirmMapping(mapping: Record<string, string>) {
|
||||||
|
if (!mapState) return
|
||||||
setBusy('add')
|
setBusy('add')
|
||||||
setError(null)
|
setError(null)
|
||||||
try {
|
try {
|
||||||
await createAccount(id, form)
|
await createAccount(id, mapState.creds)
|
||||||
|
await setFolderMapping(id, mapping)
|
||||||
setForm(emptyAccount)
|
setForm(emptyAccount)
|
||||||
|
setMapState(null)
|
||||||
reload()
|
reload()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(err instanceof Error ? err.message : 'Failed to add account')
|
setError(err instanceof Error ? err.message : 'Failed to add account')
|
||||||
@@ -188,8 +303,14 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
|
|
||||||
const { task, accounts } = data
|
const { task, accounts } = data
|
||||||
const allTested = accounts.length > 0 && accounts.every((a) => a.test_src_status === 'ok' && a.test_dst_status === 'ok')
|
const allTested = accounts.length > 0 && accounts.every((a) => a.test_src_status === 'ok' && a.test_dst_status === 'ok')
|
||||||
|
// Prefer live (WS) copied/skipped over the DB values, which only advance per
|
||||||
|
// folder — so the summary moves in real time during a large folder.
|
||||||
const totals = accounts.reduce(
|
const totals = accounts.reduce(
|
||||||
(acc, a) => ({ copied: acc.copied + a.copied, skipped: acc.skipped + a.skipped, errors: acc.errors + a.errors }),
|
(acc, a) => ({
|
||||||
|
copied: acc.copied + (live[a.id]?.copied ?? a.copied),
|
||||||
|
skipped: acc.skipped + (live[a.id]?.skipped ?? a.skipped),
|
||||||
|
errors: acc.errors + a.errors,
|
||||||
|
}),
|
||||||
{ copied: 0, skipped: 0, errors: 0 },
|
{ copied: 0, skipped: 0, errors: 0 },
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -287,7 +408,7 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
</div>
|
</div>
|
||||||
<div className="btn-row">
|
<div className="btn-row">
|
||||||
<button className="btn btn-primary" disabled={busy !== null}>
|
<button className="btn btn-primary" disabled={busy !== null}>
|
||||||
{busy === 'add' ? 'Adding…' : 'Add account'}
|
{busy === 'probe' ? 'Testing…' : busy === 'add' ? 'Adding…' : 'Add account'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
@@ -337,6 +458,7 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
<th>Src test</th>
|
<th>Src test</th>
|
||||||
<th>Dst test</th>
|
<th>Dst test</th>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
|
<th>Progress</th>
|
||||||
<th>Copied</th>
|
<th>Copied</th>
|
||||||
<th>Skipped</th>
|
<th>Skipped</th>
|
||||||
<th>Errors</th>
|
<th>Errors</th>
|
||||||
@@ -346,12 +468,19 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
<tbody>
|
<tbody>
|
||||||
{accounts.length === 0 ? (
|
{accounts.length === 0 ? (
|
||||||
<tr className="empty-row">
|
<tr className="empty-row">
|
||||||
<td colSpan={9}>no accounts yet — add one or import a CSV above</td>
|
<td colSpan={10}>no accounts yet — add one or import a CSV above</td>
|
||||||
</tr>
|
</tr>
|
||||||
) : (
|
) : (
|
||||||
accounts.map((a) => (
|
accounts.map((a) => (
|
||||||
<tr key={a.id}>
|
<tr key={a.id}>
|
||||||
<td>{a.src_login}</td>
|
<td>
|
||||||
|
{a.src_login}
|
||||||
|
{a.last_error && (
|
||||||
|
<div className="acct-error" title={a.last_error}>
|
||||||
|
{a.last_error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
<td>{a.dst_login}</td>
|
<td>{a.dst_login}</td>
|
||||||
<td>
|
<td>
|
||||||
<StatusBadge status={a.test_src_status} />
|
<StatusBadge status={a.test_src_status} />
|
||||||
@@ -362,8 +491,34 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
<td>
|
<td>
|
||||||
<StatusBadge status={a.status} />
|
<StatusBadge status={a.status} />
|
||||||
</td>
|
</td>
|
||||||
<td className="num-cell">{a.copied}</td>
|
<td className="progress-cell">
|
||||||
<td className="num-cell">{a.skipped}</td>
|
{(() => {
|
||||||
|
const lv = live[a.id]
|
||||||
|
if (!lv || !lv.total) return <span className="muted-note">—</span>
|
||||||
|
const done = lv.copied + lv.skipped
|
||||||
|
const pct = Math.min(100, Math.floor((done / lv.total) * 100))
|
||||||
|
const eta = lv.speed > 0 ? (lv.total - done) / lv.speed : Infinity
|
||||||
|
const scanning = lv.scanned != null && lv.scanTotal != null && lv.scanned < lv.scanTotal
|
||||||
|
return (
|
||||||
|
<div className="acct-progress">
|
||||||
|
<div className="pbar">
|
||||||
|
<span className="pbar-fill" style={{ width: `${pct}%` }} />
|
||||||
|
</div>
|
||||||
|
<span className="pmeta mono-num">
|
||||||
|
{done}/{lv.total} ({pct}%) · {lv.speed >= 1 ? Math.round(lv.speed) : lv.speed.toFixed(1)}/s · ETA {fmtDuration(eta)}
|
||||||
|
{lv.folder ? ` · ${lv.folder}` : ''}
|
||||||
|
</span>
|
||||||
|
{scanning && (
|
||||||
|
<span className="pmeta pscan mono-num">
|
||||||
|
scanning {lv.scanFolder}: {lv.scanned}/{lv.scanTotal}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})()}
|
||||||
|
</td>
|
||||||
|
<td className="num-cell">{live[a.id]?.copied ?? a.copied}</td>
|
||||||
|
<td className="num-cell">{live[a.id]?.skipped ?? a.skipped}</td>
|
||||||
<td className="num-cell">{a.errors}</td>
|
<td className="num-cell">{a.errors}</td>
|
||||||
<td className="num-cell">
|
<td className="num-cell">
|
||||||
{a.status === 'running' ? (
|
{a.status === 'running' ? (
|
||||||
@@ -388,6 +543,18 @@ export function TaskDetail({ id }: { id: number }) {
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{mapState && (
|
||||||
|
<FolderMappingModal
|
||||||
|
key={`${mapState.creds.src_login}|${mapState.creds.dst_login}`}
|
||||||
|
open
|
||||||
|
srcFolders={mapState.src}
|
||||||
|
dstFolders={mapState.dst}
|
||||||
|
initialMapping={task.folder_mapping ?? {}}
|
||||||
|
onCancel={() => setMapState(null)}
|
||||||
|
onConfirm={confirmMapping}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user