feat: Phase 2 — ports package, secrets port move, postgres adapter move
Problem: the hexagon's Phase 2 (plans/2026-08-15-hexagonal-architecture.md) must give the use-cases-to-be their contract surface: driven-port interfaces, test fakes, the secrets interface moved into core, and the postgres package inside the adapters tree — before the first vertical slice (Phase 3) can wire a composition root. Change: - internal/core/ports: full driven-port catalog per plan §3.3 — repositories as transaction-scoped aggregates whose inputs carry derived checks, audit, and events (§3.6), plus CommandExecutor, TargetResolver, Checker, Secrets, EventPublisher, Provisioner. Port-local payload types (Event, AuditEntry, CheckDef, KnowledgeEntry, ExecResult) keep signatures off infrastructure; TypeTree aliases internal/ontology (pure over domain) until checkdefaults is absorbed. ReadModels intentionally not declared yet — it materializes with the Phase 3 slice and grows as report handlers rewire. - secrets.Backend is now an alias of ports.Secrets; implementations (Infisical, SOPS, Manager) unchanged. mcp's local secretBackend subset is deleted; tool constructors take ports.Secrets. - internal/db → internal/adapters/postgres (mechanical import rewrite; package identifier stays db until the Phase 3 repository split). sqlc.yaml, Makefile, golangci exclusions, and docs follow the move; make generate-check verified. - internal/adapters/ssh: Executor implements ports.CommandExecutor over the actuator dial pool + RunStreaming (10-min default timeout carried over from the httpapi path). - internal/adapters/remote: Resolver implements ports.TargetResolver delegating to internal/remote (still pool-based; drops onto ports.EntityRepository when repositories land in Phase 3 — documented transitional import). - internal/core/ports/portstest: importable fakes — in-memory EntityRepo (with check-then-act SetState, side-effect recording), RecordingExecutor, FakeChecker, SpyPublisher; port-satisfaction guards; tests. Risk: ports are declared ahead of implementations — signatures firm up per phase as slices land (documented in the package doc); the remote→postgres transitional import is explicit and dissolves in Phase 3. Verification: go vet, make test (race, 19 packages), generate-check, golangci on core+adapters — 0 issues; full-repo baseline down 365→344.
This commit is contained in:
61
internal/core/ports/observation.go
Normal file
61
internal/core/ports/observation.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package ports
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/dtoro/oikos/internal/core/domain"
|
||||
)
|
||||
|
||||
// CheckDef is a health-check definition (subset of the check_defs row the
|
||||
// observe pass consumes; grows with the probe adapters in Phase 5).
|
||||
type CheckDef struct {
|
||||
ID domain.UUID
|
||||
EntityID domain.UUID
|
||||
Kind string
|
||||
Name string
|
||||
Config json.RawMessage
|
||||
IntervalS int
|
||||
Enabled bool
|
||||
Severity string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// CheckResult is what one probe run produces.
|
||||
type CheckResult struct {
|
||||
Value float64
|
||||
State string // "ok", "warning", "critical", "unknown"
|
||||
Message string
|
||||
}
|
||||
|
||||
// Checker probes one check kind. One adapter per kind under adapters/probes;
|
||||
// the ObservationService picks the adapter by CheckDef.Kind.
|
||||
type Checker interface {
|
||||
Check(ctx context.Context, def CheckDef, target Target) CheckResult
|
||||
}
|
||||
|
||||
// MetricSample is one metric observation to record.
|
||||
type MetricSample struct {
|
||||
Metric string
|
||||
Value float64
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
// MetricsRepository records observe-pass metrics (Timescale write path;
|
||||
// bucketed/trend reads belong to ReadModels).
|
||||
type MetricsRepository interface {
|
||||
InsertSamples(ctx context.Context, entityID domain.UUID, samples []MetricSample) error
|
||||
}
|
||||
|
||||
// CheckRepository — monitoring definitions. EnsureFor is the read-diff-write
|
||||
// derivation in one transaction.
|
||||
type CheckRepository interface {
|
||||
ListEnabled(ctx context.Context) ([]CheckDef, error)
|
||||
ListFor(ctx context.Context, entityID domain.UUID) ([]CheckDef, error)
|
||||
// EnsureFor inserts defs that are missing, updates changed ones, and
|
||||
// removes stale ones for the entity — atomically.
|
||||
EnsureFor(ctx context.Context, entityID domain.UUID, desired []CheckDef) error
|
||||
SetEnabled(ctx context.Context, checkID domain.UUID, enabled bool) error
|
||||
}
|
||||
Reference in New Issue
Block a user