Add multi-disk copy workflow

This commit is contained in:
2026-04-23 22:24:32 +03:00
parent 5b3cb9e393
commit 31bac2b5d8
10 changed files with 468 additions and 204 deletions

View File

@@ -34,59 +34,74 @@ func main() {
taskStore := task.NewStore() taskStore := task.NewStore()
cp := copier.New(taskStore) cp := copier.New(taskStore)
var activeDB *db.DB activeDBs := make(map[string]*db.DB)
var activeDiskID string mountToDiskID := make(map[string]string)
openDiskDB := func(info disk.DiskInfo) { openDiskDB := func(info disk.DiskInfo) {
if activeDiskID == info.DiskID { if info.DiskID == "" {
return // already open for this disk return
} }
if activeDB != nil {
activeDB.Close() if prevDiskID, ok := mountToDiskID[info.MountPath]; ok && prevDiskID != info.DiskID {
activeDB = nil if prevDB := activeDBs[prevDiskID]; prevDB != nil {
activeDiskID = "" prevDB.Close()
delete(activeDBs, prevDiskID)
cp.SetDB(prevDiskID, nil)
}
} }
mountToDiskID[info.MountPath] = info.DiskID
if _, ok := activeDBs[info.DiskID]; ok {
return
}
d, err := db.Open(disk.DBPath(info.MountPath)) d, err := db.Open(disk.DBPath(info.MountPath))
if err != nil { if err != nil {
log.Printf("open disk DB: %v", err) log.Printf("open disk DB: %v", err)
return return
} }
activeDB = d activeDBs[info.DiskID] = d
activeDiskID = info.DiskID cp.SetDB(info.DiskID, d)
cp.SetDB(d)
log.Printf("disk DB opened for %s", info.DiskID) log.Printf("disk DB opened for %s", info.DiskID)
} }
closeDiskDB := func() { closeDiskDB := func(info disk.DiskInfo) {
if activeDB != nil { diskID := info.DiskID
activeDB.Close() if diskID == "" {
activeDB = nil diskID = mountToDiskID[info.MountPath]
activeDiskID = ""
cp.SetDB(nil)
log.Println("disk DB closed")
} }
if diskID == "" {
return
}
cp.Cancel(diskID)
cp.SetDB(diskID, nil)
if d := activeDBs[diskID]; d != nil {
d.Close()
delete(activeDBs, diskID)
log.Printf("disk DB closed for %s", diskID)
}
delete(mountToDiskID, info.MountPath)
} }
watcherReady := false
w := watcher.New(*mountPath, 5*time.Second, func(ev watcher.DiskEvent) { w := watcher.New(*mountPath, 5*time.Second, func(ev watcher.DiskEvent) {
log.Printf("disk: %s -> %s", ev.Prev, ev.Info.State) log.Printf("disk: %s %s -> %s", ev.Info.MountPath, ev.Prev.State, ev.Info.State)
switch ev.Info.State { switch ev.Info.State {
case disk.DiskKnown: case disk.DiskKnown:
openDiskDB(ev.Info) openDiskDB(ev.Info)
if ev.Prev != disk.DiskKnown && cfg.AutoCopy { if watcherReady && ev.Prev.State != disk.DiskKnown && cfg.AutoCopy {
triggerAutoCopy(cp, cfg, ev.Info, *mediaPath) triggerAutoCopy(cp, cfg, ev.Info, *mediaPath)
} }
case disk.DiskForeign:
closeDiskDB(ev.Prev)
case disk.DiskAbsent: case disk.DiskAbsent:
closeDiskDB() closeDiskDB(ev.Prev)
} }
}) })
w.ProbeNow()
// Open DB immediately if disk already connected at startup watcherReady = true
{
info, _ := disk.Probe(*mountPath)
if info.State == disk.DiskKnown {
openDiskDB(info)
}
}
srv, err := api.New(api.Deps{ srv, err := api.New(api.Deps{
Config: cfg, Config: cfg,
@@ -96,6 +111,13 @@ func main() {
Tasks: taskStore, Tasks: taskStore,
MediaPath: *mediaPath, MediaPath: *mediaPath,
MountPath: *mountPath, MountPath: *mountPath,
OnDiskInit: func(mountPath, diskID string) {
openDiskDB(disk.DiskInfo{
State: disk.DiskKnown,
DiskID: diskID,
MountPath: mountPath,
})
},
}) })
if err != nil { if err != nil {
log.Fatalf("init server: %v", err) log.Fatalf("init server: %v", err)
@@ -119,7 +141,9 @@ func main() {
shutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) shutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
httpSrv.Shutdown(shutCtx) httpSrv.Shutdown(shutCtx)
closeDiskDB() for _, info := range w.ListDisks() {
closeDiskDB(info)
}
} }
func triggerAutoCopy(cp *copier.Copier, cfg *config.Config, info disk.DiskInfo, mediaPath string) { func triggerAutoCopy(cp *copier.Copier, cfg *config.Config, info disk.DiskInfo, mediaPath string) {

View File

@@ -9,8 +9,9 @@ import (
) )
func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) { func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) {
diskInfo := s.deps.Watcher.CurrentDisk() diskID := r.PathValue("diskID")
if diskInfo.State != disk.DiskKnown { diskInfo, ok := s.deps.Watcher.DiskByID(diskID)
if !ok || diskInfo.State != disk.DiskKnown {
jsonErr(w, http.StatusUnprocessableEntity, "no known disk connected") jsonErr(w, http.StatusUnprocessableEntity, "no known disk connected")
return return
} }
@@ -54,7 +55,8 @@ func (s *Server) handleCopyStart(w http.ResponseWriter, r *http.Request) {
} }
func (s *Server) handleCopyCancel(w http.ResponseWriter, r *http.Request) { func (s *Server) handleCopyCancel(w http.ResponseWriter, r *http.Request) {
s.deps.Copier.Cancel() diskID := r.PathValue("diskID")
s.deps.Copier.Cancel(diskID)
jsonOK(w, map[string]bool{"ok": true}) jsonOK(w, map[string]bool{"ok": true})
} }

View File

@@ -1,14 +1,13 @@
package api package api
import ( import (
"encoding/json"
"net/http" "net/http"
"jukebox_maker/internal/disk" "jukebox_maker/internal/disk"
) )
func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) { func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
info := s.deps.Watcher.CurrentDisk()
type response struct { type response struct {
State disk.DiskState `json:"state"` State disk.DiskState `json:"state"`
DiskID string `json:"disk_id"` DiskID string `json:"disk_id"`
@@ -18,17 +17,57 @@ func (s *Server) handleDiskStatus(w http.ResponseWriter, r *http.Request) {
ActiveTaskID string `json:"active_task_id,omitempty"` ActiveTaskID string `json:"active_task_id,omitempty"`
} }
resp := response{ disks := s.deps.Watcher.ListDisks()
State: info.State, resp := make([]response, 0, len(disks))
DiskID: info.DiskID, for _, info := range disks {
TotalBytes: info.TotalBytes, item := response{
FreeBytes: info.FreeBytes, State: info.State,
MountPath: info.MountPath, DiskID: info.DiskID,
TotalBytes: info.TotalBytes,
FreeBytes: info.FreeBytes,
MountPath: info.MountPath,
}
if info.DiskID != "" {
if t, ok := s.deps.Tasks.ActiveTaskByDisk(info.DiskID); ok {
item.ActiveTaskID = t.ID
}
}
resp = append(resp, item)
} }
if t, ok := s.deps.Tasks.ActiveTask(); ok { jsonOK(w, map[string]any{"items": resp})
resp.ActiveTaskID = t.ID }
}
func (s *Server) handleDiskInit(w http.ResponseWriter, r *http.Request) {
jsonOK(w, resp) var req struct {
MountPath string `json:"mount_path"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonErr(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
return
}
info, ok := s.deps.Watcher.DiskByMountPath(req.MountPath)
if !ok {
jsonErr(w, http.StatusNotFound, "disk not found")
return
}
if info.State == disk.DiskAbsent {
jsonErr(w, http.StatusUnprocessableEntity, "no disk connected")
return
}
if info.State == disk.DiskKnown {
jsonErr(w, http.StatusConflict, "disk already initialized")
return
}
diskID, err := disk.InitDisk(info.MountPath)
if err != nil {
jsonErr(w, http.StatusInternalServerError, "init disk: "+err.Error())
return
}
s.deps.OnDiskInit(info.MountPath, diskID)
s.deps.Watcher.ProbeNow()
jsonOK(w, map[string]string{"disk_id": diskID})
} }

View File

@@ -21,6 +21,8 @@ type Deps struct {
Tasks *task.Store Tasks *task.Store
MediaPath string MediaPath string
MountPath string MountPath string
// OnDiskInit вызывается при ручной инициализации диска через UI.
OnDiskInit func(mountPath, diskID string)
} }
type Server struct { type Server struct {
@@ -56,12 +58,13 @@ func (s *Server) routes() {
s.mux.HandleFunc("GET /settings", s.handleSettings) s.mux.HandleFunc("GET /settings", s.handleSettings)
s.mux.HandleFunc("GET /health", s.handleHealth) s.mux.HandleFunc("GET /health", s.handleHealth)
s.mux.HandleFunc("GET /api/disk", s.handleDiskStatus) s.mux.HandleFunc("GET /api/disks", s.handleDiskStatus)
s.mux.HandleFunc("POST /api/disks/init", s.handleDiskInit)
s.mux.HandleFunc("GET /api/sources", s.handleSources) s.mux.HandleFunc("GET /api/sources", s.handleSources)
s.mux.HandleFunc("GET /api/config", s.handleGetConfig) s.mux.HandleFunc("GET /api/config", s.handleGetConfig)
s.mux.HandleFunc("PUT /api/config", s.handlePutConfig) s.mux.HandleFunc("PUT /api/config", s.handlePutConfig)
s.mux.HandleFunc("POST /api/copy/start", s.handleCopyStart) s.mux.HandleFunc("POST /api/disks/{diskID}/copy/start", s.handleCopyStart)
s.mux.HandleFunc("POST /api/copy/cancel", s.handleCopyCancel) s.mux.HandleFunc("POST /api/disks/{diskID}/copy/cancel", s.handleCopyCancel)
s.mux.HandleFunc("GET /api/tasks/{id}", s.handleTaskGet) s.mux.HandleFunc("GET /api/tasks/{id}", s.handleTaskGet)
} }

View File

@@ -31,38 +31,46 @@ type Options struct {
type Copier struct { type Copier struct {
tasks *task.Store tasks *task.Store
mu sync.Mutex mu sync.Mutex
cancel context.CancelFunc cancels map[string]context.CancelFunc
dbMu sync.RWMutex dbMu sync.RWMutex
db *db.DB dbs map[string]*db.DB
} }
func New(tasks *task.Store) *Copier { func New(tasks *task.Store) *Copier {
return &Copier{tasks: tasks} return &Copier{
tasks: tasks,
cancels: make(map[string]context.CancelFunc),
dbs: make(map[string]*db.DB),
}
} }
func (c *Copier) SetDB(d *db.DB) { func (c *Copier) SetDB(diskID string, d *db.DB) {
c.dbMu.Lock() c.dbMu.Lock()
c.db = d if d == nil {
delete(c.dbs, diskID)
} else {
c.dbs[diskID] = d
}
c.dbMu.Unlock() c.dbMu.Unlock()
} }
func (c *Copier) getDB() *db.DB { func (c *Copier) getDB(diskID string) *db.DB {
c.dbMu.RLock() c.dbMu.RLock()
defer c.dbMu.RUnlock() defer c.dbMu.RUnlock()
return c.db return c.dbs[diskID]
} }
func (c *Copier) Start(ctx context.Context, opts Options) (string, error) { func (c *Copier) Start(ctx context.Context, opts Options) (string, error) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if _, active := c.tasks.ActiveTask(); active { if _, active := c.cancels[opts.DiskID]; active {
return "", errors.New("copy already running") return "", errors.New("copy already running")
} }
database := c.getDB() database := c.getDB(opts.DiskID)
if database == nil { if database == nil {
return "", errors.New("no disk database available") return "", errors.New("no disk database available")
} }
@@ -71,23 +79,29 @@ func (c *Copier) Start(ctx context.Context, opts Options) (string, error) {
opts.DestFolder = "media" opts.DestFolder = "media"
} }
t := c.tasks.Create("copy") t := c.tasks.Create("copy", opts.DiskID)
copyCtx, cancel := context.WithCancel(ctx) copyCtx, cancel := context.WithCancel(ctx)
c.cancel = cancel c.cancels[opts.DiskID] = cancel
go c.run(copyCtx, t.ID, opts, database) go c.run(copyCtx, t.ID, opts, database)
return t.ID, nil return t.ID, nil
} }
func (c *Copier) Cancel() { func (c *Copier) Cancel(diskID string) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
if c.cancel != nil { if cancel, ok := c.cancels[diskID]; ok {
c.cancel() cancel()
} }
} }
func (c *Copier) run(ctx context.Context, taskID string, opts Options, database *db.DB) { func (c *Copier) run(ctx context.Context, taskID string, opts Options, database *db.DB) {
defer func() {
c.mu.Lock()
delete(c.cancels, opts.DiskID)
c.mu.Unlock()
}()
setStatus := func(s task.Status, msg string, prog int) { setStatus := func(s task.Status, msg string, prog int) {
c.tasks.Update(taskID, func(t *task.Task) { c.tasks.Update(taskID, func(t *task.Task) {
t.Status = s t.Status = s

View File

@@ -19,6 +19,7 @@ const (
type Task struct { type Task struct {
ID string `json:"id"` ID string `json:"id"`
DiskID string `json:"disk_id"`
Type string `json:"type"` Type string `json:"type"`
Status Status `json:"status"` Status Status `json:"status"`
Progress int `json:"progress"` Progress int `json:"progress"`
@@ -43,9 +44,10 @@ func NewStore() *Store {
return &Store{tasks: make(map[string]*Task)} return &Store{tasks: make(map[string]*Task)}
} }
func (s *Store) Create(taskType string) *Task { func (s *Store) Create(taskType, diskID string) *Task {
t := &Task{ t := &Task{
ID: uuid.New().String(), ID: uuid.New().String(),
DiskID: diskID,
Type: taskType, Type: taskType,
Status: StatusQueued, Status: StatusQueued,
CreatedAt: time.Now().UTC(), CreatedAt: time.Now().UTC(),
@@ -77,11 +79,11 @@ func (s *Store) Update(id string, fn func(*Task)) {
} }
} }
func (s *Store) ActiveTask() (*Task, bool) { func (s *Store) ActiveTaskByDisk(diskID string) (*Task, bool) {
s.mu.RLock() s.mu.RLock()
defer s.mu.RUnlock() defer s.mu.RUnlock()
for _, t := range s.tasks { for _, t := range s.tasks {
if t.Status == StatusQueued || t.Status == StatusRunning { if t.DiskID == diskID && (t.Status == StatusQueued || t.Status == StatusRunning) {
copy := *t copy := *t
return &copy, true return &copy, true
} }

View File

@@ -2,6 +2,8 @@ package watcher
import ( import (
"context" "context"
"path/filepath"
"sort"
"sync" "sync"
"time" "time"
@@ -10,7 +12,7 @@ import (
type DiskEvent struct { type DiskEvent struct {
Info disk.DiskInfo Info disk.DiskInfo
Prev disk.DiskState Prev disk.DiskInfo
} }
type Handler func(event DiskEvent) type Handler func(event DiskEvent)
@@ -20,8 +22,8 @@ type Watcher struct {
interval time.Duration interval time.Duration
handler Handler handler Handler
mu sync.RWMutex mu sync.RWMutex
current disk.DiskInfo disks map[string]disk.DiskInfo
} }
func New(mountPath string, interval time.Duration, handler Handler) *Watcher { func New(mountPath string, interval time.Duration, handler Handler) *Watcher {
@@ -29,13 +31,42 @@ func New(mountPath string, interval time.Duration, handler Handler) *Watcher {
mountPath: mountPath, mountPath: mountPath,
interval: interval, interval: interval,
handler: handler, handler: handler,
disks: make(map[string]disk.DiskInfo),
} }
} }
func (w *Watcher) CurrentDisk() disk.DiskInfo { func (w *Watcher) ListDisks() []disk.DiskInfo {
w.mu.RLock() w.mu.RLock()
defer w.mu.RUnlock() defer w.mu.RUnlock()
return w.current
items := make([]disk.DiskInfo, 0, len(w.disks))
for _, info := range w.disks {
items = append(items, info)
}
sort.Slice(items, func(i, j int) bool { return items[i].MountPath < items[j].MountPath })
return items
}
func (w *Watcher) DiskByMountPath(mountPath string) (disk.DiskInfo, bool) {
w.mu.RLock()
defer w.mu.RUnlock()
info, ok := w.disks[mountPath]
return info, ok
}
func (w *Watcher) DiskByID(diskID string) (disk.DiskInfo, bool) {
w.mu.RLock()
defer w.mu.RUnlock()
for _, info := range w.disks {
if info.DiskID == diskID {
return info, true
}
}
return disk.DiskInfo{}, false
}
func (w *Watcher) ProbeNow() {
w.probe()
} }
func (w *Watcher) Run(ctx context.Context) { func (w *Watcher) Run(ctx context.Context) {
@@ -56,15 +87,61 @@ func (w *Watcher) Run(ctx context.Context) {
} }
func (w *Watcher) probe() { func (w *Watcher) probe() {
info, _ := disk.Probe(w.mountPath) next := discoverDisks(w.mountPath)
w.mu.Lock() w.mu.Lock()
prev := w.current.State prev := w.disks
changed := prev != info.State w.disks = next
w.current = info
w.mu.Unlock() w.mu.Unlock()
if changed && w.handler != nil { if w.handler == nil {
w.handler(DiskEvent{Info: info, Prev: prev}) return
}
seen := make(map[string]struct{}, len(prev)+len(next))
for mountPath, info := range next {
seen[mountPath] = struct{}{}
prevInfo := prev[mountPath]
if prevInfo.State != info.State || prevInfo.DiskID != info.DiskID {
w.handler(DiskEvent{Info: info, Prev: prevInfo})
}
}
for mountPath, prevInfo := range prev {
if _, ok := seen[mountPath]; ok {
continue
}
w.handler(DiskEvent{
Info: disk.DiskInfo{
State: disk.DiskAbsent,
MountPath: mountPath,
},
Prev: prevInfo,
})
} }
} }
func discoverDisks(root string) map[string]disk.DiskInfo {
candidates := []string{root}
if entries, err := filepath.Glob(filepath.Join(root, "*")); err == nil {
for _, path := range entries {
candidates = append(candidates, path)
}
}
disks := make(map[string]disk.DiskInfo)
seen := make(map[string]struct{}, len(candidates))
for _, mountPath := range candidates {
if _, ok := seen[mountPath]; ok {
continue
}
seen[mountPath] = struct{}{}
info, _ := disk.Probe(mountPath)
if info.State == disk.DiskAbsent {
continue
}
disks[mountPath] = info
}
return disks
}

View File

@@ -21,6 +21,7 @@ ROOT_DIR=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
die() { echo "error: $*" >&2; exit 1; } die() { echo "error: $*" >&2; exit 1; }
command -v docker >/dev/null 2>&1 || die "docker not found in PATH" command -v docker >/dev/null 2>&1 || die "docker not found in PATH"
command -v go >/dev/null 2>&1 || die "go not found in PATH"
DEFAULT_TAG=$(git -C "${ROOT_DIR}" rev-parse --short HEAD 2>/dev/null || echo dev) DEFAULT_TAG=$(git -C "${ROOT_DIR}" rev-parse --short HEAD 2>/dev/null || echo dev)
@@ -46,6 +47,12 @@ else
ask IMAGE "Image" "" ask IMAGE "Image" ""
fi fi
echo "checking Go build"
(
cd "${ROOT_DIR}"
go build ./...
)
if [ -n "${IMAGE}" ]; then if [ -n "${IMAGE}" ]; then
# multi-arch build + push # multi-arch build + push
docker buildx version >/dev/null 2>&1 || die "docker buildx not available" docker buildx version >/dev/null 2>&1 || die "docker buildx not available"

View File

@@ -89,6 +89,24 @@ a:hover { text-decoration: underline; }
color: var(--ink); color: var(--ink);
} }
.panel-body {
padding: 14px 16px;
}
.disk-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
gap: 20px;
}
.disk-card {
margin-bottom: 0;
}
.progress-wrap {
border-top: 1px solid var(--border-lite);
}
/* KV table */ /* KV table */
.kv-table { .kv-table {
width: 100%; width: 100%;
@@ -325,5 +343,7 @@ a:hover { text-decoration: underline; }
@media (max-width: 720px) { @media (max-width: 720px) {
.page-header { flex-wrap: wrap; padding: 12px 16px; } .page-header { flex-wrap: wrap; padding: 12px 16px; }
.page-main { width: calc(100vw - 24px); margin-top: 20px; } .page-main { width: calc(100vw - 24px); margin-top: 20px; }
.disk-grid { grid-template-columns: 1fr; }
.kv-table th { width: 130px; } .kv-table th { width: 130px; }
.btn-row { flex-wrap: wrap; }
} }

View File

@@ -1,161 +1,237 @@
{{define "content"}} {{define "content"}}
<section class="panel"> <section class="panel">
<h2>Накопитель</h2> <h2>Накопители</h2>
<table class="kv-table"> <div class="panel-body">
<tbody> <div id="diskSummary" class="text-muted">Загрузка списка накопителей…</div>
<tr>
<th>Статус</th>
<td id="diskState"><span class="badge badge-unknown">Не подключён</span></td>
</tr>
<tr id="rowDiskID" class="hidden">
<th>ID диска</th>
<td><span class="mono" id="valDiskID"></span></td>
</tr>
<tr id="rowTotal" class="hidden">
<th>Всего на диске</th>
<td id="valTotal"></td>
</tr>
<tr id="rowFree" class="hidden">
<th>Свободно</th>
<td id="valFree"></td>
</tr>
</tbody>
</table>
</section>
<section class="panel hidden" id="progressPanel">
<h2>Копирование</h2>
<div style="padding:14px 16px">
<div class="progress-bar-bg">
<div class="progress-bar-fill" id="progressFill" style="width:0%"></div>
</div>
<div style="display:flex;justify-content:space-between;align-items:baseline;margin-top:6px">
<div class="progress-label" id="progressMsg">Подготовка…</div>
<div class="progress-label" id="progressMeta" style="text-align:right"></div>
</div>
</div> </div>
</section> </section>
<div class="btn-row" style="background:transparent;border:none;padding:0;margin-bottom:24px"> <div class="disk-grid" id="diskGrid"></div>
<button class="button-primary" id="btnStart" onclick="startCopy()" disabled>▶ Запустить копирование</button>
<button class="button-danger hidden" id="btnCancel" onclick="cancelCopy()">✕ Отменить</button>
</div>
<script> <script>
let pollInterval = null; let disks = [];
let activeTaskId = null; const taskState = new Map();
const taskPollers = new Map();
async function refreshDisk() { function escapeHTML(value) {
try { return String(value || '').replace(/[&<>"']/g, (char) => ({
const r = await fetch('/api/disk'); '&': '&amp;',
if (!r.ok) return; '<': '&lt;',
const d = await r.json(); '>': '&gt;',
'"': '&quot;',
const labels = { absent: 'Не подключён', foreign: 'Незнакомый диск', known: 'Диск подключён' }; "'": '&#39;'
const cls = { absent: 'badge-unknown', foreign: 'badge-warn', known: 'badge-ok' }; }[char]));
document.getElementById('diskState').innerHTML =
`<span class="badge ${cls[d.state]||'badge-unknown'}">${labels[d.state]||'—'}</span>`;
const known = d.state === 'known';
['rowDiskID','rowTotal','rowFree'].forEach(id =>
document.getElementById(id).classList.toggle('hidden', !known));
if (known) {
document.getElementById('valDiskID').textContent = d.disk_id;
document.getElementById('valTotal').textContent = fmtBytes(d.total_bytes);
document.getElementById('valFree').textContent = fmtBytes(d.free_bytes);
}
const hasTask = !!d.active_task_id;
document.getElementById('btnStart').disabled = !known || hasTask;
document.getElementById('btnStart').classList.toggle('hidden', hasTask);
document.getElementById('btnCancel').classList.toggle('hidden', !hasTask);
document.getElementById('progressPanel').classList.toggle('hidden', !hasTask);
if (d.active_task_id && d.active_task_id !== activeTaskId) {
activeTaskId = d.active_task_id;
startTaskPoll(activeTaskId);
}
if (!d.active_task_id && activeTaskId) {
activeTaskId = null; stopTaskPoll();
document.getElementById('progressPanel').classList.add('hidden');
}
} catch(e) {}
} }
function startTaskPoll(id) { stopTaskPoll(); pollInterval = setInterval(() => pollTask(id), 1500); } function diskKey(disk) {
function stopTaskPoll() { if (pollInterval) { clearInterval(pollInterval); pollInterval = null; } } return disk.disk_id || disk.mount_path;
}
function badgeClass(state) {
return ({ absent: 'badge-unknown', foreign: 'badge-warn', known: 'badge-ok' })[state] || 'badge-unknown';
}
function badgeLabel(state) {
return ({ absent: 'Не подключён', foreign: 'Незнакомый диск', known: 'Диск подключён' })[state] || '—';
}
function fmtSpeed(bps) { function fmtSpeed(bps) {
if (!bps) return ''; if (!bps) return '';
if (bps >= 1e9) return (bps/1e9).toFixed(1) + ' ГБ/с'; if (bps >= 1e9) return (bps / 1e9).toFixed(1) + ' ГБ/с';
if (bps >= 1e6) return (bps/1e6).toFixed(1) + ' МБ/с'; if (bps >= 1e6) return (bps / 1e6).toFixed(1) + ' МБ/с';
if (bps >= 1e3) return (bps/1e3).toFixed(0) + ' КБ/с'; if (bps >= 1e3) return (bps / 1e3).toFixed(0) + ' КБ/с';
return bps + ' Б/с'; return bps + ' Б/с';
} }
function fmtETA(sec) { function fmtETA(sec) {
if (!sec || sec <= 0) return ''; if (!sec || sec <= 0) return '';
if (sec >= 3600) return Math.floor(sec/3600) + ' ч ' + Math.floor((sec%3600)/60) + ' мин'; if (sec >= 3600) return Math.floor(sec / 3600) + ' ч ' + Math.floor((sec % 3600) / 60) + ' мин';
if (sec >= 60) return Math.floor(sec/60) + ' мин'; if (sec >= 60) return Math.floor(sec / 60) + ' мин';
return sec + ' с'; return sec + ' с';
} }
async function pollTask(id) { function taskMeta(task) {
try { if (!task) return '';
const r = await fetch('/api/tasks/' + id); return [fmtSpeed(task.speed_bps), task.eta_sec ? 'ETA: ' + fmtETA(task.eta_sec) : ''].filter(Boolean).join(' · ');
if (!r.ok) return;
const t = await r.json();
document.getElementById('progressFill').style.width = t.progress + '%';
document.getElementById('progressMsg').textContent = t.message || '…';
const speed = fmtSpeed(t.speed_bps);
const eta = fmtETA(t.eta_sec);
const meta = [speed, eta ? 'ETA: ' + eta : ''].filter(Boolean).join(' · ');
document.getElementById('progressMeta').textContent = meta;
if (['success','failed','canceled'].includes(t.status)) {
stopTaskPoll(); activeTaskId = null;
document.getElementById('btnStart').disabled = false;
document.getElementById('btnStart').classList.remove('hidden');
document.getElementById('btnCancel').classList.add('hidden');
document.getElementById('progressPanel').classList.add('hidden');
document.getElementById('progressMeta').textContent = '';
if (t.status === 'success') toast(t.message || 'Готово', 'ok');
if (t.status === 'failed') toast('Ошибка: ' + t.error, 'error');
if (t.status === 'canceled') toast('Копирование отменено', 'error');
refreshDisk();
}
} catch(e) {}
} }
async function startCopy() { function renderDisks() {
document.getElementById('btnStart').disabled = true; const grid = document.getElementById('diskGrid');
const summary = document.getElementById('diskSummary');
if (!disks.length) {
summary.textContent = 'Подключённые накопители не найдены.';
grid.innerHTML = '';
return;
}
const knownCount = disks.filter((disk) => disk.state === 'known').length;
summary.textContent = `Найдено накопителей: ${disks.length}. Готово к копированию: ${knownCount}.`;
grid.innerHTML = disks.map((disk) => {
const activeTask = disk.active_task_id ? taskState.get(disk.active_task_id) : null;
const progress = activeTask ? activeTask.progress : 0;
const message = activeTask ? (activeTask.message || 'Подготовка…') : '';
const meta = activeTask ? taskMeta(activeTask) : '';
const isKnown = disk.state === 'known';
const isForeign = disk.state === 'foreign';
return `
<section class="panel disk-card">
<h2>${escapeHTML(disk.mount_path)}</h2>
<table class="kv-table">
<tbody>
<tr>
<th>Статус</th>
<td><span class="badge ${badgeClass(disk.state)}">${badgeLabel(disk.state)}</span></td>
</tr>
<tr>
<th>ID диска</th>
<td>${disk.disk_id ? `<span class="mono">${escapeHTML(disk.disk_id)}</span>` : '<span class="text-muted">ещё не инициализирован</span>'}</td>
</tr>
<tr>
<th>Всего на диске</th>
<td>${isKnown ? fmtBytes(disk.total_bytes) : '—'}</td>
</tr>
<tr>
<th>Свободно</th>
<td>${isKnown ? fmtBytes(disk.free_bytes) : '—'}</td>
</tr>
</tbody>
</table>
${activeTask ? `
<div class="panel-body progress-wrap">
<div class="progress-bar-bg">
<div class="progress-bar-fill" style="width:${progress}%"></div>
</div>
<div class="progress-label">${escapeHTML(message)}</div>
<div class="progress-label">${escapeHTML(meta)}</div>
</div>
` : ''}
<div class="btn-row">
${isKnown ? `
<button class="button-primary" data-action="start-copy" data-disk-id="${escapeHTML(disk.disk_id)}" ${activeTask ? 'disabled' : ''}>▶ Копировать</button>
<button class="button-danger ${activeTask ? '' : 'hidden'}" data-action="cancel-copy" data-disk-id="${escapeHTML(disk.disk_id)}">✕ Отменить</button>
` : ''}
${isForeign ? `
<button class="button-secondary" data-action="init-disk" data-mount-path="${escapeHTML(disk.mount_path)}">Инициализировать диск</button>
` : ''}
</div>
</section>
`;
}).join('');
}
function stopTaskPoll(taskID) {
if (!taskPollers.has(taskID)) return;
clearInterval(taskPollers.get(taskID));
taskPollers.delete(taskID);
}
function startTaskPoll(taskID) {
if (!taskID || taskPollers.has(taskID)) return;
taskPollers.set(taskID, setInterval(() => pollTask(taskID), 1500));
pollTask(taskID);
}
async function refreshDisks() {
try { try {
const r = await fetch('/api/copy/start', { method: 'POST' }); const response = await fetch('/api/disks');
const d = await r.json(); if (!response.ok) return;
if (!r.ok) { const payload = await response.json();
toast(d.error || 'Ошибка запуска', 'error'); disks = payload.items || [];
document.getElementById('btnStart').disabled = false; renderDisks();
const activeTasks = new Set();
for (const disk of disks) {
if (disk.active_task_id) {
activeTasks.add(disk.active_task_id);
startTaskPoll(disk.active_task_id);
}
}
for (const taskID of Array.from(taskPollers.keys())) {
if (!activeTasks.has(taskID)) {
stopTaskPoll(taskID);
taskState.delete(taskID);
}
}
} catch (error) {}
}
async function pollTask(taskID) {
try {
const response = await fetch('/api/tasks/' + taskID);
if (!response.ok) return;
const task = await response.json();
taskState.set(taskID, task);
renderDisks();
if (['success', 'failed', 'canceled'].includes(task.status)) {
stopTaskPoll(taskID);
taskState.delete(taskID);
if (task.status === 'success') toast(task.message || 'Готово', 'ok');
if (task.status === 'failed') toast('Ошибка: ' + task.error, 'error');
if (task.status === 'canceled') toast('Копирование отменено', 'error');
refreshDisks();
}
} catch (error) {}
}
async function startCopy(diskID) {
try {
const response = await fetch('/api/disks/' + encodeURIComponent(diskID) + '/copy/start', { method: 'POST' });
const payload = await response.json();
if (!response.ok) {
toast(payload.error || 'Ошибка запуска', 'error');
return; return;
} }
activeTaskId = d.task_id; startTaskPoll(payload.task_id);
document.getElementById('btnStart').classList.add('hidden'); refreshDisks();
document.getElementById('btnCancel').classList.remove('hidden'); } catch (error) {
document.getElementById('progressPanel').classList.remove('hidden');
document.getElementById('progressFill').style.width = '0%';
document.getElementById('progressMsg').textContent = 'Подготовка…';
startTaskPoll(activeTaskId);
} catch(e) {
toast('Ошибка связи', 'error'); toast('Ошибка связи', 'error');
document.getElementById('btnStart').disabled = false;
} }
} }
async function cancelCopy() { async function cancelCopy(diskID) {
try { await fetch('/api/copy/cancel', { method: 'POST' }); toast('Отмена…', 'ok'); } catch(e) {} try {
await fetch('/api/disks/' + encodeURIComponent(diskID) + '/copy/cancel', { method: 'POST' });
toast('Отмена…', 'ok');
} catch (error) {
toast('Ошибка связи', 'error');
}
} }
refreshDisk(); async function initDisk(mountPath) {
setInterval(refreshDisk, 5000); try {
const response = await fetch('/api/disks/init', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ mount_path: mountPath })
});
const payload = await response.json();
if (!response.ok) {
toast(payload.error || 'Ошибка инициализации', 'error');
return;
}
toast('Диск инициализирован', 'ok');
refreshDisks();
} catch (error) {
toast('Ошибка связи', 'error');
}
}
document.getElementById('diskGrid').addEventListener('click', (event) => {
const button = event.target.closest('button[data-action]');
if (!button) return;
const action = button.dataset.action;
if (action === 'start-copy') startCopy(button.dataset.diskId);
if (action === 'cancel-copy') cancelCopy(button.dataset.diskId);
if (action === 'init-disk') initDisk(button.dataset.mountPath);
});
refreshDisks();
setInterval(refreshDisks, 5000);
</script> </script>
{{end}} {{end}}