Remove historical inspection commands
This commit is contained in:
@@ -2,13 +2,10 @@ package state
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/config"
|
||||
"gitea.maximumdirect.net/eric/weatherreporter/internal/fileutil"
|
||||
@@ -39,20 +36,6 @@ type ArtifactPaths struct {
|
||||
RenderContext string `json:"renderContext,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"`
|
||||
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")
|
||||
@@ -262,228 +245,6 @@ func (s *FilesystemStore) SaveMetadata(_ context.Context, metadata Metadata) (st
|
||||
return metadata.MetadataPath, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) FindPriorSnapshot(_ context.Context, resolved report.Resolved) (*PriorSnapshot, error) {
|
||||
if resolved.Definition.ComparisonStrategy != report.CompareSameValidDate {
|
||||
return nil, nil
|
||||
}
|
||||
group := resolved.Definition.ArtifactGroup
|
||||
if group == "" {
|
||||
return nil, fmt.Errorf("report %q has no artifact group", resolved.Definition.ID)
|
||||
}
|
||||
dirs, err := s.metadataDirectories(resolved, group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var candidates []Metadata
|
||||
for _, dir := range dirs {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("read snapshot metadata directory %q: %w", dir, err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || !isMetadataFilename(entry.Name()) {
|
||||
continue
|
||||
}
|
||||
path := filepath.Join(dir, entry.Name())
|
||||
var metadata Metadata
|
||||
if err := readJSON(path, &metadata); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if metadata.RunID == resolved.Metadata().RunID {
|
||||
continue
|
||||
}
|
||||
if !resolved.Definition.CompatibleWithPrior(metadata.ReportID) {
|
||||
continue
|
||||
}
|
||||
if !comparablePeriod(metadata, resolved) {
|
||||
continue
|
||||
}
|
||||
if !metadata.GeneratedAt.Before(resolved.GeneratedAt) {
|
||||
continue
|
||||
}
|
||||
candidates = append(candidates, metadata)
|
||||
}
|
||||
}
|
||||
if len(candidates) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
sort.Slice(candidates, func(i, j int) bool {
|
||||
return candidates[i].GeneratedAt.After(candidates[j].GeneratedAt)
|
||||
})
|
||||
return &PriorSnapshot{
|
||||
Metadata: candidates[0],
|
||||
ModuleSnapshotPath: candidates[0].ModuleSnapshotPath,
|
||||
}, 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() || !isMetadataFilename(entry.Name()) {
|
||||
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")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return promptinput.Package{}, fmt.Errorf("read %q: %w", path, err)
|
||||
}
|
||||
pkg, err := promptinput.LoadYAML(data)
|
||||
if err != nil {
|
||||
return promptinput.Package{}, err
|
||||
}
|
||||
return pkg, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) LoadModuleSnapshot(_ context.Context, path string) (module.Snapshot, error) {
|
||||
if path == "" {
|
||||
return module.Snapshot{}, fmt.Errorf("module snapshot path is required")
|
||||
}
|
||||
var snapshot module.Snapshot
|
||||
if err := readJSON(path, &snapshot); err != nil {
|
||||
return module.Snapshot{}, err
|
||||
}
|
||||
if err := snapshot.Validate(); err != nil {
|
||||
return module.Snapshot{}, err
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) LoadGeneratedText(_ context.Context, path string) ([]byte, error) {
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("generated text path is required")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %q: %w", path, err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) LoadPromptPreparation(_ context.Context, path string) (PromptPreparationArtifact, error) {
|
||||
if path == "" {
|
||||
return PromptPreparationArtifact{}, fmt.Errorf("prompt preparation path is required")
|
||||
}
|
||||
var artifact PromptPreparationArtifact
|
||||
if err := readJSON(path, &artifact); err != nil {
|
||||
return PromptPreparationArtifact{}, err
|
||||
}
|
||||
if err := artifact.Validate(); err != nil {
|
||||
return PromptPreparationArtifact{}, err
|
||||
}
|
||||
return artifact, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) LoadPromptExecution(_ context.Context, path string) (PromptExecutionArtifact, error) {
|
||||
if path == "" {
|
||||
return PromptExecutionArtifact{}, fmt.Errorf("prompt execution path is required")
|
||||
}
|
||||
var artifact PromptExecutionArtifact
|
||||
if err := readJSON(path, &artifact); err != nil {
|
||||
return PromptExecutionArtifact{}, err
|
||||
}
|
||||
if err := artifact.Validate(); err != nil {
|
||||
return PromptExecutionArtifact{}, err
|
||||
}
|
||||
return artifact, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) LoadRenderContext(_ context.Context, path string, target any) error {
|
||||
if path == "" {
|
||||
return fmt.Errorf("render context path is required")
|
||||
}
|
||||
if target == nil {
|
||||
return fmt.Errorf("render context target is required")
|
||||
}
|
||||
return readJSON(path, target)
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) reportRecord(path string) (ReportRecord, error) {
|
||||
var metadata Metadata
|
||||
if err := readJSON(path, &metadata); err != nil {
|
||||
return ReportRecord{}, err
|
||||
}
|
||||
metadata.MetadataPath = path
|
||||
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,
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
return []string{filepath.Dir(paths.Metadata)}, nil
|
||||
}
|
||||
|
||||
func (s *FilesystemStore) join(parts ...string) string {
|
||||
all := append([]string{s.root}, parts...)
|
||||
return filepath.Join(all...)
|
||||
@@ -553,30 +314,3 @@ func validatePathSegment(name string, value string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isMetadataFilename(name string) bool {
|
||||
if !strings.HasPrefix(name, "metadata.") || !strings.HasSuffix(name, ".json") {
|
||||
return false
|
||||
}
|
||||
runID := strings.TrimSuffix(strings.TrimPrefix(name, "metadata."), ".json")
|
||||
return strings.TrimSpace(runID) != "" && !strings.ContainsAny(runID, `/\`) && runID != "." && runID != ".."
|
||||
}
|
||||
|
||||
func readJSON(path string, target any) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read %q: %w", path, err)
|
||||
}
|
||||
if err := json.Unmarshal(data, target); err != nil {
|
||||
return fmt.Errorf("decode %q: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sameValidDate(metadata Metadata, resolved report.Resolved) bool {
|
||||
return metadata.ValidPeriod.Start.Format("2006-01-02") == resolved.ValidPeriod.Start.Format("2006-01-02")
|
||||
}
|
||||
|
||||
func comparablePeriod(metadata Metadata, resolved report.Resolved) bool {
|
||||
return resolved.Definition.ComparisonStrategy == report.CompareSameValidDate && sameValidDate(metadata, resolved)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user