github.com/rotari-dev/rotari/internal/web

Overview

package web // import "github.com/rotari-dev/rotari/internal/web"

Package web builds Web-facing projections of Rotari state.

It loads persisted model and state data into JSON-ready structures for the Web
UI, including runs, jobs, attempts, timelines, origins, and display times.
It should present state for the UI without changing queue or run semantics.

Constants

const (
	HistorySearchProject     = "project"
	HistorySearchRun         = "run"
	HistorySearchJob         = "job"
	HistorySearchPageSize    = 50
	HistorySearchMaxPageSize = 200
)

Functions

func FormatQueueDisplayTimes(state *QueueState)
func ValidateHistorySearchRequest(request HistorySearchRequest) error

Types

type Attempt struct {
	ID              string           `json:"id"`
	Result          *model.JobResult `json:"result,omitempty"`
	ExecutionStatus string           `json:"execution_status"`
	SubmittedAt     string           `json:"submitted_at,omitempty"`
	FinishedAt      string           `json:"finished_at,omitempty"`
	Elapsed         string           `json:"elapsed,omitempty"`
	SchedulerState  string           `json:"scheduler_state,omitempty"`
}

type ConfigFile struct {
	Scope   string `json:"scope,omitempty"`
	Path    string `json:"path"`
	Content string `json:"content"`
}

type EnvironmentDefinition struct {
	Name        string `json:"name"`
	Value       string `json:"value,omitempty"`
	Set         bool   `json:"set,omitempty"`
	CLIDefault  bool   `json:"cli_default"`
	Job         bool   `json:"job"`
	Array       bool   `json:"array"`
	Description string `json:"description"`
}

type HistorySearchFilter struct {
	Target string `json:"target"`
	Field  string `json:"field"`
	Word   string `json:"word"`
	Join   string `json:"join,omitempty"`
}

type HistorySearchRecord struct {
	BaseDirID           string
	BaseDirPath         string
	ProjectName         string
	RunID               string
	RunName             string
	RunStatus           string
	RunExitCode         *int
	RunStarted          string
	RunFinished         string
	RunHost             string
	RunWorkingDirectory string
	JobID               string
	JobName             string
	JobStatus           string
	JobStage            string
	Command             string
	Host                string
	WorkingDirectory    string
	Diagnosis           string
	Executor            string
	AttemptID           string
	JobExitCode         *int
	Timestamp           string
}

type HistorySearchRequest struct {
	Target        string                `json:"target,omitempty"`
	CaseSensitive bool                  `json:"case_sensitive,omitempty"`
	Fuzzy         bool                  `json:"fuzzy,omitempty"`
	Filters       []HistorySearchFilter `json:"filters"`
	From          string                `json:"from,omitempty"`
	To            string                `json:"to,omitempty"`
	Offset        int                   `json:"offset,omitempty"`
	Limit         int                   `json:"limit,omitempty"`
}

type HistorySearchResponse struct {
	Rows   []HistorySearchRow `json:"rows"`
	Total  int                `json:"total"`
	Offset int                `json:"offset"`
	Limit  int                `json:"limit"`
}

func SearchHistory(records []HistorySearchRecord, request HistorySearchRequest) (HistorySearchResponse, error)
    SearchHistory evaluates conditions against the selected hierarchy level.
    When conditions target multiple levels, results use the most specific level;
    parent-level conditions are evaluated against each result's ancestors.

type HistorySearchRow struct {
	Target      string `json:"target"`
	BaseDirID   string `json:"basedir_id"`
	BaseDirPath string `json:"basedir_path"`
	ProjectName string `json:"project_name"`
	RunID       string `json:"run_id,omitempty"`
	RunName     string `json:"run_name,omitempty"`
	RunStatus   string `json:"run_status,omitempty"`
	JobID       string `json:"job_id,omitempty"`
	JobName     string `json:"job_name,omitempty"`
	JobStatus   string `json:"job_status,omitempty"`
	Command     string `json:"command,omitempty"`
	Timestamp   string `json:"timestamp,omitempty"`
}

type HistorySearchScope struct {
	AllBasedirs bool   `json:"all_basedirs,omitempty"`
	BaseDirID   string `json:"basedir_id,omitempty"`
	ProjectName string `json:"project_name,omitempty"`
	RunID       string `json:"run_id,omitempty"`
}

type Job struct {
	ID                string    `json:"id"`
	AttemptID         string    `json:"attempt_id,omitempty"`
	Attempts          []Attempt `json:"attempts,omitempty"`
	ArrayTaskID       *int      `json:"array_task_id,omitempty"`
	ArrayFirst        int       `json:"array_first,omitempty"`
	ArrayLast         int       `json:"array_last,omitempty"`
	Name              string    `json:"name,omitempty"`
	Stage             string    `json:"stage,omitempty"`
	Command           []string  `json:"command"`
	WorkingDirectory  string    `json:"working_directory,omitempty"`
	Executor          string    `json:"executor,omitempty"`
	ExecutorOptions   []string  `json:"executor_options,omitempty"`
	LogMode           string    `json:"log_mode,omitempty"`
	DependsOn         []string  `json:"depends_on,omitempty"`
	DependsOnFinished []string  `json:"depends_on_finished,omitempty"`
	// Matrix places a matrix member in its group's grid.
	Matrix          *Matrix          `json:"matrix,omitempty"`
	Result          *model.JobResult `json:"result,omitempty"`
	Origin          *model.JobOrigin `json:"origin,omitempty"`
	Carried         bool             `json:"carried,omitempty"`
	ExecutionStatus string           `json:"execution_status"`
	AttemptDir      string           `json:"-"`
	// Environment is the job's own NAME=VALUE environment, matrix values
	// included, for the run report. The Web API leaves it out: values given
	// with --env may be secrets.
	Environment []string `json:"-"`
	SubmittedAt string   `json:"submitted_at,omitempty"`
	FinishedAt  string   `json:"finished_at,omitempty"`
	// Elapsed is how long the job ran, as `rotari jobs` shows it ("3m 05s");
	// set with the display times.
	Elapsed        string `json:"elapsed,omitempty"`
	SchedulerState string `json:"scheduler_state,omitempty"`
	Final          bool   `json:"final,omitempty"`
	// DiagnosisOutdated reports that Result's saved rule-based analysis was
	// produced by earlier diagnosis rules.
	DiagnosisOutdated bool `json:"diagnosis_outdated,omitempty"`
	// NoteLabels are the run's notes on this job, oldest first, each as
	// model.FormatRunNote describes it without the job, naming the attempt
	// when it is not the job's latest.
	NoteLabels []string `json:"note_labels,omitempty"`

	// Has unexported fields.
}

func LoadJobs(store state.Store, runDir string, files state.RunSnapshot, selectedAttemptID string) ([]Job, error)
    LoadJobs projects a run's jobs for the Web UI. Each job's result follows the
    shared jobstatus fallback chain. A non-empty selectedAttemptID shows that
    attempt, instead of the latest one, for its job.

func LoadRunJobs(store state.Store, runDir string, selectedAttemptID string) ([]Job, error)
    LoadRunJobs reads runDir's run files and projects its jobs with LoadJobs.

type JobLane struct {
	JobID   string `json:"job_id"`
	Start   string `json:"start,omitempty"`
	End     string `json:"end,omitempty"`
	Carried bool   `json:"carried,omitempty"`
}
    JobLane is one job's span on the run page's job timeline, in RFC 3339:
    from its submission to its finish, by the same rules as BuildTimeline.
    Start is empty for a job not yet submitted and End for one still running.
    A carried job ran in an earlier run, so it has no span here.

func BuildJobLanes(runFinishedAt string, jobs []JobTimelineInput) []JobLane
    BuildJobLanes returns one lane per job, in the jobs' order.

type JobTimelineInput struct {
	JobID       string
	Finished    bool
	Carried     bool
	SubmittedAt string
	FinishedAt  string
	Success     bool
}

type Matrix struct {
	GroupID    string                  `json:"group_id"`
	BaseName   string                  `json:"base_name,omitempty"`
	Dimensions []model.MatrixDimension `json:"dimensions"`
	Values     []model.MatrixValue     `json:"values"`
}
    Matrix is the part of a job's matrix provenance the Web UI needs to draw a
    grid. It omits the base environment, which may hold secrets.

type QueueLoader struct {
	ProjectName string
	Paths       state.ProjectPaths
	Queue       func() (model.Queue, error)
	Lock        func() (model.LockInfo, error)
	Runs        func() ([]string, error) // newest first
	Summary     func(runID string) (model.RunSummary, error)
	Jobs        func(runID string, summary model.RunSummary) ([]Job, error)
	Context     func(runID string) (model.RunContext, error)
	Sources     func(runID string) (model.RunSources, bool, error)
	Notes       func(runID string) ([]model.RunNote, error)
	Samples     func(runID string) []model.LoadSample
}

type QueueState struct {
	QueueName     string               `json:"project_name"`
	ConfigPath    string               `json:"config_path,omitempty"`
	ConfigSources []model.ConfigSource `json:"config_sources,omitempty"`
	Queue         model.Queue          `json:"queue"`
	Runs          []Run                `json:"runs"`
	RunCount      int                  `json:"run_count"`
	// RunOrder is every run of the project, newest first, in the shared run
	// order (state.RunsNewestFirst). Runs may hold only some of them; the
	// page orders and picks the latest run by this list.
	RunOrder        []string `json:"run_order,omitempty"`
	Revision        string   `json:"revision,omitempty"`
	RunnerPID       int      `json:"runner_pid,omitempty"`
	RunningRunID    string   `json:"running_run_id,omitempty"`
	RunnerHost      string   `json:"runner_host,omitempty"`
	RunnerStartedAt string   `json:"runner_started_at,omitempty"`
	// Server is the persisted record of the project's supervisor, which
	// exists only while a run is starting or active.
	Server ServerState `json:"server"`
}

func LoadQueueState(loader QueueLoader) (QueueState, error)

type Run struct {
	model.RunSummary
	LineageSummary runlineage.RunSummary `json:"lineage_summary,omitempty"`
	Jobs           []Job                 `json:"jobs"`
	Lifecycle      string                `json:"lifecycle,omitempty"`
	// HeartbeatWarning is runview.HeartbeatWarning for a running run.
	HeartbeatWarning string                `json:"heartbeat_warning,omitempty"`
	ClientStatus     model.RunClientStatus `json:"client_status"`
	ClientLabel      string                `json:"client_label,omitempty"`
	CWD              string                `json:"cwd,omitempty"`
	Context          model.RunContext      `json:"context,omitempty"`
	// Sources is the code the run's executed jobs ran from, and SourceLabels
	// each as model.SourceLabel describes it for `show`.
	Sources      []model.SourceRevision `json:"sources,omitempty"`
	SourceLabels []string               `json:"source_labels,omitempty"`
	// Notes are the run's notes, oldest first, including those on its jobs.
	// The run page shows the run's own notes in its report, and a job's
	// behind the job's Notes button, from Job.NoteLabels.
	Notes    []model.RunNote `json:"notes,omitempty"`
	Timeline []TimelinePoint `json:"timeline,omitempty"`
	// Lanes are the jobs' spans for the run page's job timeline.
	Lanes   []JobLane `json:"lanes,omitempty"`
	Running bool      `json:"running"`
	// Unreadable, when set, is why the run's files cannot be read: they come
	// from a newer rotari. The run then has status "unreadable" and no jobs.
	Unreadable string `json:"unreadable,omitempty"`
}

type ServerState struct {
	PID           int  `json:"pid,omitempty"`
	PIDFileExists bool `json:"pid_file_exists"`
}

type State struct {
	BaseDir       string                  `json:"base_dir"`
	ConfigPath    string                  `json:"config_path,omitempty"`
	ConfigSources []model.ConfigSource    `json:"config_sources,omitempty"`
	Queues        []QueueState            `json:"projects"`
	Environments  []EnvironmentDefinition `json:"environments"`
	UpdatedAt     string                  `json:"updated_at"`
}

type TimelinePoint struct {
	At       string `json:"at"`
	Pending  int    `json:"pending"`
	Running  int    `json:"running"`
	Finished int    `json:"finished"`
	Success  int    `json:"success"`
	Failed   int    `json:"failed"`
}

func BuildTimeline(startedAt, runFinishedAt string, jobs []JobTimelineInput) []TimelinePoint