Skip to content

Commit

Permalink
Include previous result value on run_result_changed events
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 2, 2024
1 parent 38c7ff0 commit 6899c9f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions flows/actions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (a *baseAction) evaluateMessage(run flows.Run, languages []i18n.Language, a
// helper to save a run result and log it as an event
func (a *baseAction) saveResult(run flows.Run, step flows.Step, name, value, category, categoryLocalized string, input string, extra json.RawMessage, logEvent flows.EventCallback) {
result := flows.NewResult(name, value, category, categoryLocalized, step.NodeUUID(), input, extra, dates.Now())
run.SaveResult(result)
logEvent(events.NewRunResultChanged(result))
prev := run.SaveResult(result)
logEvent(events.NewRunResultChanged(result, prev))
}

// helper to save a run result based on a webhook call and log it as an event
Expand Down
6 changes: 5 additions & 1 deletion flows/events/run_result_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ type RunResultChangedEvent struct {
CategoryLocalized string `json:"category_localized,omitempty"`
Input string `json:"input,omitempty"`
Extra json.RawMessage `json:"extra,omitempty"`

// not included in JSON - used internally to track changes
Previous *flows.Result `json:"-"`
}

// NewRunResultChanged returns a new save result event for the passed in values
func NewRunResultChanged(result *flows.Result) *RunResultChangedEvent {
func NewRunResultChanged(result, prev *flows.Result) *RunResultChangedEvent {
return &RunResultChangedEvent{
BaseEvent: NewBaseEvent(TypeRunResultChanged),
Name: result.Name,
Expand All @@ -50,5 +53,6 @@ func NewRunResultChanged(result *flows.Result) *RunResultChangedEvent {
CategoryLocalized: result.CategoryLocalized,
Input: result.Input,
Extra: result.Extra,
Previous: prev,
}
}
2 changes: 1 addition & 1 deletion flows/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ type Run interface {
FlowReference() *assets.FlowReference

Session() Session
SaveResult(*Result)
SaveResult(*Result) *Result
SetStatus(RunStatus)
Webhook() *WebhookCall
SetWebhook(*WebhookCall)
Expand Down
9 changes: 6 additions & 3 deletions flows/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ func (r Results) Clone() Results {
return clone
}

// Save saves a new result in our map. The key is saved in a snakified format
func (r Results) Save(result *Result) {
r[utils.Snakify(result.Name)] = result
// Save saves a new result in our map using the snakified name as the key. Returns the old result if it existed.
func (r Results) Save(result *Result) *Result {
key := utils.Snakify(result.Name)
old := r[key]
r[key] = result
return old
}

// Get returns the result with the given key
Expand Down
4 changes: 2 additions & 2 deletions flows/routers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func (r *baseRouter) routeToCategory(run flows.Run, step flows.Step, categoryUUI
extraJSON, _ = jsonx.Marshal(extra)
}
result := flows.NewResult(r.resultName, match, category.Name(), localizedCategory, step.NodeUUID(), operand, extraJSON, dates.Now())
run.SaveResult(result)
logEvent(events.NewRunResultChanged(result))
prev := run.SaveResult(result)
logEvent(events.NewRunResultChanged(result, prev))
}

return category.ExitUUID(), nil
Expand Down
6 changes: 3 additions & 3 deletions flows/runs/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func (r *run) Contact() *flows.Contact { return r.session.Contact()
func (r *run) Events() []flows.Event { return r.events }

func (r *run) Results() flows.Results { return r.results }
func (r *run) SaveResult(result *flows.Result) {
func (r *run) SaveResult(result *flows.Result) *flows.Result {
// truncate value if necessary
result.Value = stringsx.Truncate(result.Value, r.session.Engine().Options().MaxResultChars)

r.results.Save(result)
r.modifiedOn = dates.Now()

r.legacyExtra.addResult(result)

return r.results.Save(result)
}

func (r *run) Exit(status flows.RunStatus) {
Expand Down

0 comments on commit 6899c9f

Please sign in to comment.