Add report artifact inspection commands
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/adapters/scriptorium"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/briefing"
|
||||
@@ -32,6 +33,21 @@ type ArtifactPaths struct {
|
||||
RenderedReport string `json:"renderedReport,omitempty"`
|
||||
}
|
||||
|
||||
type ReportRecord struct {
|
||||
RunID string `json:"runId"`
|
||||
ReportID report.ID `json:"reportId"`
|
||||
Variant string `json:"variant,omitempty"`
|
||||
PromptID string `json:"promptId"`
|
||||
GeneratedAt string `json:"generatedAt"`
|
||||
ValidStart string `json:"validStart"`
|
||||
ValidEnd string `json:"validEnd"`
|
||||
MetadataPath string `json:"metadataPath"`
|
||||
BriefingPath string `json:"briefingPath"`
|
||||
ReportPath string `json:"reportPath,omitempty"`
|
||||
Warnings int `json:"warnings"`
|
||||
metadata Metadata
|
||||
}
|
||||
|
||||
func NewFilesystemStore(cfg config.WorkspaceConfig) (*FilesystemStore, error) {
|
||||
if cfg.Root == "" {
|
||||
return nil, fmt.Errorf("workspace root is required")
|
||||
@@ -213,6 +229,95 @@ func (s *FilesystemStore) FindPriorSnapshot(_ context.Context, resolved report.R
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) ListReports(_ context.Context, limit int) ([]ReportRecord, error) {
|
||||
if s == nil {
|
||||
return nil, fmt.Errorf("state store is required")
|
||||
}
|
||||
root := s.join(s.snapshotsDir)
|
||||
if _, err := os.Stat(root); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("inspect %q: %w", root, err)
|
||||
}
|
||||
var records []ReportRecord
|
||||
err := filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect %q: %w", path, err)
|
||||
}
|
||||
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".metadata.json") {
|
||||
return nil
|
||||
}
|
||||
record, err := s.reportRecord(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
records = append(records, record)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
sort.Slice(records, func(i, j int) bool {
|
||||
return records[i].metadata.GeneratedAt.After(records[j].metadata.GeneratedAt)
|
||||
})
|
||||
if limit > 0 && len(records) > limit {
|
||||
records = records[:limit]
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) LoadMetadataByRunID(ctx context.Context, runID string) (Metadata, string, error) {
|
||||
if strings.TrimSpace(runID) == "" {
|
||||
return Metadata{}, "", fmt.Errorf("run id is required")
|
||||
}
|
||||
records, err := s.ListReports(ctx, 0)
|
||||
if err != nil {
|
||||
return Metadata{}, "", err
|
||||
}
|
||||
for _, record := range records {
|
||||
if record.RunID == runID {
|
||||
return record.metadata, record.MetadataPath, nil
|
||||
}
|
||||
}
|
||||
return Metadata{}, "", fmt.Errorf("metadata for run id %q was not found", runID)
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) LoadDataPackage(_ context.Context, path string) (promptinput.Package, error) {
|
||||
if path == "" {
|
||||
return promptinput.Package{}, fmt.Errorf("data package path is required")
|
||||
}
|
||||
var pkg promptinput.Package
|
||||
if err := readJSON(path, &pkg); err != nil {
|
||||
return promptinput.Package{}, err
|
||||
}
|
||||
return pkg, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) reportRecord(path string) (ReportRecord, error) {
|
||||
var metadata Metadata
|
||||
if err := readJSON(path, &metadata); err != nil {
|
||||
return ReportRecord{}, err
|
||||
}
|
||||
return ReportRecord{
|
||||
RunID: metadata.RunID,
|
||||
ReportID: metadata.ReportID,
|
||||
Variant: metadata.Variant,
|
||||
PromptID: metadata.PromptID,
|
||||
GeneratedAt: metadata.GeneratedAt.Format(time.RFC3339Nano),
|
||||
ValidStart: metadata.ValidPeriod.Start.Format(time.RFC3339Nano),
|
||||
ValidEnd: metadata.ValidPeriod.End.Format(time.RFC3339Nano),
|
||||
MetadataPath: path,
|
||||
BriefingPath: metadata.BriefingPath,
|
||||
ReportPath: metadata.RenderedReportPath,
|
||||
Warnings: len(metadata.SourceWarnings),
|
||||
metadata: metadata,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) metadataDirectories(resolved report.Resolved, group string) ([]string, error) {
|
||||
paths, err := s.Paths(resolved)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user