refactor: replace string-based emitter with type-safe generic event system
Refactors the event emitter system from string-based messages to a type-safe generic implementation using typed events. Consolidates separate tracker packages into a unified plugins architecture. Changes: - Replace Emitter with BaseEmitter[E comparable] using generics - Add Event type with StartEvent, StepEvent, and StopEvent constants - Create Listener[E] interface with BaseListener implementation - Consolidate explanation, performance, and statistics trackers into internal/plugins package - Simplify main CLI by using plugin constructors instead of manual event subscription - Add Items() iterator method to Set for idiomatic range loops
This commit is contained in:
25
internal/plugins/debug.go
Normal file
25
internal/plugins/debug.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
type Logs struct {
|
||||
logger *slog.Logger
|
||||
process *engine.Engine
|
||||
}
|
||||
|
||||
func NewLogs(logger *slog.Logger, process *engine.Engine) *Logs {
|
||||
plugin := &Logs{logger, process}
|
||||
process.On(engine.StopEvent, plugin.Step)
|
||||
|
||||
return plugin
|
||||
}
|
||||
|
||||
func (t *Logs) Step() {
|
||||
stringified := lambda.Stringify(*t.process.Expression)
|
||||
t.logger.Info("reduction", "tree", stringified)
|
||||
}
|
||||
32
internal/plugins/explanation.go
Normal file
32
internal/plugins/explanation.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Package "explanation" provides a observer to gather the reasoning during the
|
||||
// reduction, and present a thorough explanation to the user for each step.
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
)
|
||||
|
||||
// Track the reductions made by a reduction proess.
|
||||
type Explanation struct {
|
||||
process *engine.Engine
|
||||
}
|
||||
|
||||
// Attaches a new explanation tracker to a process.
|
||||
func NewExplanation(process *engine.Engine) *Explanation {
|
||||
plugin := &Explanation{process: process}
|
||||
process.On(engine.StartEvent, plugin.Start)
|
||||
process.On(engine.StepEvent, plugin.Step)
|
||||
|
||||
return plugin
|
||||
}
|
||||
|
||||
func (t *Explanation) Start() {
|
||||
fmt.Println(lambda.Stringify(*t.process.Expression))
|
||||
}
|
||||
|
||||
func (t *Explanation) Step() {
|
||||
fmt.Println(" =", lambda.Stringify(*t.process.Expression))
|
||||
}
|
||||
59
internal/plugins/performance.go
Normal file
59
internal/plugins/performance.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Package "performance" provides a tracker to observer CPU performance during
|
||||
// execution.
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime/pprof"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
)
|
||||
|
||||
// Observes a reduction process, and publishes a CPU performance profile on
|
||||
// completion.
|
||||
type Performance struct {
|
||||
File string
|
||||
filePointer *os.File
|
||||
Error error
|
||||
}
|
||||
|
||||
// Create a performance tracker that outputs a profile to "file".
|
||||
func NewPerformance(file string, process *engine.Engine) *Performance {
|
||||
plugin := &Performance{File: file}
|
||||
process.On(engine.StartEvent, plugin.Start)
|
||||
process.On(engine.StopEvent, plugin.Stop)
|
||||
|
||||
return plugin
|
||||
}
|
||||
|
||||
// Begin profiling.
|
||||
func (t *Performance) Start() {
|
||||
var absPath string
|
||||
|
||||
absPath, t.Error = filepath.Abs(t.File)
|
||||
if t.Error != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t.Error = os.MkdirAll(filepath.Dir(absPath), 0777)
|
||||
if t.Error != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t.filePointer, t.Error = os.Create(absPath)
|
||||
if t.Error != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t.Error = pprof.StartCPUProfile(t.filePointer)
|
||||
if t.Error != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Stop profiling.
|
||||
func (t *Performance) Stop() {
|
||||
pprof.StopCPUProfile()
|
||||
t.filePointer.Close()
|
||||
}
|
||||
44
internal/plugins/statistics.go
Normal file
44
internal/plugins/statistics.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/engine"
|
||||
"git.maximhutz.com/max/lambda/internal/statistics"
|
||||
)
|
||||
|
||||
// An observer, to track reduction performance.
|
||||
type Statistics struct {
|
||||
start time.Time
|
||||
steps uint64
|
||||
}
|
||||
|
||||
// Create a new reduction performance Statistics.
|
||||
func NewStatistics(process *engine.Engine) *Statistics {
|
||||
plugin := &Statistics{}
|
||||
process.On(engine.StartEvent, plugin.Start)
|
||||
process.On(engine.StepEvent, plugin.Step)
|
||||
process.On(engine.StopEvent, plugin.Step)
|
||||
|
||||
return plugin
|
||||
}
|
||||
|
||||
func (t *Statistics) Start() {
|
||||
t.start = time.Now()
|
||||
t.steps = 0
|
||||
}
|
||||
|
||||
func (t *Statistics) Step() {
|
||||
t.steps++
|
||||
}
|
||||
|
||||
func (t *Statistics) Stop() {
|
||||
results := statistics.Results{
|
||||
StepsTaken: t.steps,
|
||||
TimeElapsed: uint64(time.Since(t.start).Milliseconds()),
|
||||
}
|
||||
|
||||
fmt.Fprint(os.Stderr, results.String())
|
||||
}
|
||||
Reference in New Issue
Block a user