Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include previous result value on run_result_changed events #1290

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading