Add log viewer
This commit is contained in:
@@ -37,6 +37,7 @@ func New(socket string, svc *service.Service, log *slog.Logger) *Server {
|
||||
mux.HandleFunc("DELETE /v1/secrets/{id}", s.deleteSecret)
|
||||
mux.HandleFunc("GET /v1/runs", s.runs)
|
||||
mux.HandleFunc("DELETE /v1/runs", s.clearRuns)
|
||||
mux.HandleFunc("GET /v1/logs", s.logs)
|
||||
mux.HandleFunc("GET /v1/filesystem/directories", s.directories)
|
||||
mux.HandleFunc("GET /v1/workloads/{kind}", s.workloads)
|
||||
mux.HandleFunc("POST /v1/jobs/{id}/run", s.runJob)
|
||||
@@ -129,6 +130,7 @@ func (s *Server) runs(w http.ResponseWriter, _ *http.Request) { writeJSON(w, 200
|
||||
func (s *Server) clearRuns(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, 200, map[string]int{"cleared": s.service.ClearRunHistory()})
|
||||
}
|
||||
func (s *Server) logs(w http.ResponseWriter, _ *http.Request) { writeJSON(w, 200, s.service.Logs()) }
|
||||
func (s *Server) directories(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := s.service.BrowseDirectories(r.URL.Query().Get("path"))
|
||||
if err != nil {
|
||||
|
||||
@@ -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 == "/" {
|
||||
|
||||
Reference in New Issue
Block a user