Add log viewer

This commit is contained in:
Mikei386
2026-07-10 12:51:35 +02:00
parent f22c2e462d
commit bf06b10519
10 changed files with 85 additions and 11 deletions
+46
View File
@@ -1,6 +1,7 @@
package service
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
@@ -65,6 +66,51 @@ func (s *Service) RunQueue(ctx context.Context) { s.queue.Run(ctx) }
func (s *Service) Stop() { s.queue.Stop() }
func (s *Service) Config() model.Config { s.mu.RLock(); defer s.mu.RUnlock(); return s.config }
func (s *Service) Runs() []model.Run { return s.queue.Snapshot() }
func (s *Service) Logs() map[string]any {
return map[string]any{
"daemon": tailFile("/var/log/urbm.log", 512*1024, 300),
"runs": runsWithLogs(s.queue.Snapshot(), 25),
}
}
func runsWithLogs(runs []model.Run, limit int) []model.Run {
result := make([]model.Run, 0, limit)
for index := len(runs) - 1; index >= 0 && len(result) < limit; index-- {
if len(runs[index].LiveLog) == 0 {
continue
}
result = append(result, runs[index])
}
return result
}
func tailFile(path string, maxBytes int64, maxLines int) map[string]any {
data, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return map[string]any{"path": path, "lines": []string{}, "error": "Logdatei ist noch nicht vorhanden"}
}
return map[string]any{"path": path, "lines": []string{}, "error": err.Error()}
}
truncatedBytes := 0
if int64(len(data)) > maxBytes {
truncatedBytes = len(data) - int(maxBytes)
data = data[len(data)-int(maxBytes):]
if index := bytes.IndexByte(data, '\n'); index >= 0 && index+1 < len(data) {
data = data[index+1:]
}
}
rawLines := strings.Split(strings.TrimRight(string(data), "\n"), "\n")
if len(rawLines) == 1 && rawLines[0] == "" {
rawLines = []string{}
}
truncatedLines := 0
if len(rawLines) > maxLines {
truncatedLines = len(rawLines) - maxLines
rawLines = rawLines[len(rawLines)-maxLines:]
}
return map[string]any{"path": path, "lines": rawLines, "truncatedBytes": truncatedBytes, "truncatedLines": truncatedLines}
}
func (s *Service) BrowseDirectories(path string) ([]platform.DirectoryEntry, error) {
if path == "" || path == "/" {