diff --git a/app.go b/app.go index 142df3d..8c77334 100644 --- a/app.go +++ b/app.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io/fs" "os" "github.com/estebangarcia21/subprocess" @@ -56,7 +57,7 @@ func startLocalServer(name, dir, cmd string) { s.ExecAsync() } -// StartServer - запускает сервер на выполение +// StartServer - запускает сервер на выполнение func (a *App) StartServer(s string) string { runtime.LogInfo(a.ctx, s) var server Server @@ -84,11 +85,38 @@ func (a *App) Weed() { runtime.LogInfo(a.ctx, fmt.Sprintf("команда выполнена успешно")) } -func (a *App) GetStatus() string { +func getCountPics(dir string) int { pics, err := os.ReadDir("d:\\pics") if err != nil { - runtime.LogError(a.ctx, err.Error()) - return "error: " + err.Error() + return -1 } - return fmt.Sprint(len(pics)) + return len(pics) +} + +// The `getCountReady` function is counting the number of files (excluding directories) in a +// specified directory. It uses the `fs.WalkDir` function to walk through the file tree starting +// from the given directory within a file system. For each file encountered during the walk, it +// increments a counter if the file is not a directory. Finally, it returns the total count of +// files found in the directory. +func getCountReady(ctx context.Context, dir string) int { + root := dir + fileSystem := os.DirFS(root) + + cnt := 0 + fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + runtime.LogError(ctx, err.Error()) + } + if !d.IsDir() { + cnt += 1 + } + return nil + }) + return cnt +} + +func (a *App) GetStatus() string { + pics := getCountPics("d:\\pics") + ready := getCountReady(a.ctx, "d:\\pics-move") + return fmt.Sprintf("%d/%d", pics, ready) } diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index a81cde0..c63a40a 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -33,6 +33,11 @@ GetStatus().then((result) => { console.log("result:", result) cnt_pic = result; + // сразу сбросим признак работающего сервера + local = local.map ( server => { + server.run = "idle" + return server + }) }); } @@ -72,7 +77,7 @@ class="text-teal-600 hover:text-teal-200 hover:font-normal hover:underline hover:underline-offset-2 cursor-pointer" on:click={weed} > - Загрузка в weed + Weed (python)