Skip to content

Commit

Permalink
feat: complete TUI debugger iteration 2 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta committed Feb 29, 2024
1 parent 74bc1e2 commit d0b6468
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 69 deletions.
16 changes: 12 additions & 4 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"reflect"
"runtime/debug"
"slices"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -48,8 +49,6 @@ type Machine struct {
// Relations resolver, used to produce target states of a transition.
// Default: *DefaultRelationsResolver.
Resolver RelationsResolver
// Logging level of the machine.
LogLevel LogLevel
// Ctx is the context of the machine.
Ctx context.Context
// Err is the last error that occurred.
Expand Down Expand Up @@ -150,6 +149,7 @@ func New(ctx context.Context, states States, opts *Opts) *Machine {
}
for name := range m.States {
m.StateNames = append(m.StateNames, name)
slices.Sort(m.StateNames)
m.clock[name] = 0
}
// init context
Expand Down Expand Up @@ -632,7 +632,7 @@ func (m *Machine) On(events []string, ctx context.Context) chan *Event {
for _, e := range events {
if _, ok := m.indexEventCh[e]; ok {
if len(m.indexEventCh[e]) == 1 {
// delete the whole map, as theres many possible events
// delete the whole map, as there's many possible events
delete(m.indexEventCh, e)
} else {
m.indexEventCh[e] = lo.Without(m.indexEventCh[e], ch)
Expand Down Expand Up @@ -729,7 +729,8 @@ func (m *Machine) MustParseStates(states S) S {
}

// VerifyStates verifies an array of state names and returns an error in case
// at least one isn't defined.
// at least one isn't defined. It also retains the order and uses it for
// StateNames (only if all states have been passed).
func (m *Machine) VerifyStates(states S) error {
var errs []error
for _, s := range states {
Expand All @@ -742,6 +743,9 @@ func (m *Machine) VerifyStates(states S) error {
} else if len(errs) == 1 {
return errs[0]
}
if len(states) == len(m.StateNames) {
m.StateNames = states
}
return nil
}

Expand Down Expand Up @@ -1307,3 +1311,7 @@ func (m *Machine) Switch(states ...string) string {
}
return ""
}

func (m *Machine) GetLogLevel() LogLevel {
return m.logLevel
}
16 changes: 16 additions & 0 deletions pkg/machine/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ const (
LogEverything
)

func (l LogLevel) String() string {
switch l {
case LogNothing:
return "nothing"
case LogChanges:
return "changes"
case LogOps:
return "ops"
case LogDecisions:
return "decisions"
case LogEverything:
return "everything"
}
return "nothing"
}

type (
// map of (single) state names to a list of bindings
indexWhen map[string][]*whenBinding
Expand Down
1 change: 1 addition & 0 deletions pkg/machine/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func (t *Transition) emitEvents() Result {
m.logEntriesLock.Lock()
// TODO struct type
txArgs["pre_logs"] = m.logEntries
txArgs["queue_len"] = len(m.Queue)
m.logEntries = []string{}
m.logEntriesLock.Unlock()
m.emit(EventTransitionEnd, txArgs, nil)
Expand Down
8 changes: 6 additions & 2 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type MsgTx struct {
PreLogEntries []string
// tx was triggered by an auto state
IsAuto bool
// queue length at the start of the transition
Queue int
}

func (d *MsgTx) Clock(statesIndex am.S, state string) uint64 {
Expand Down Expand Up @@ -118,7 +120,7 @@ func MonitorTransitions(m *am.Machine, url string) error {
ID: m.ID,
StatesIndex: m.StateNames,
States: m.States,
LogLevel: m.LogLevel,
LogLevel: m.GetLogLevel(),
}
// TODO retries
err = client.sendMsgStruct(msg)
Expand All @@ -127,11 +129,12 @@ func MonitorTransitions(m *am.Machine, url string) error {
}
go func() {
// bind to transitions
txEndCh := m.On([]string{"transition-end"}, nil)
txEndCh := m.On([]string{am.EventTransitionEnd}, nil)
// send incoming transitions
for event := range txEndCh {
tx := event.Args["transition"].(*am.Transition)
preLogs := event.Args["pre_logs"].([]string)
queueLen := event.Args["queue_len"].(int)
msg := &MsgTx{
ID: tx.ID,
StatesActive: make([]bool, len(m.StateNames)),
Expand All @@ -144,6 +147,7 @@ func MonitorTransitions(m *am.Machine, url string) error {
LogEntries: tx.LogEntries,
PreLogEntries: preLogs,
IsAuto: tx.IsAuto(),
Queue: queueLen,
}
for i, state := range m.StateNames {
msg.StatesActive[i] = m.Is(am.S{state})
Expand Down
Loading

0 comments on commit d0b6468

Please sign in to comment.