From 6899c9fd89b830143c0e7484c3ec79c1fa03c8f6 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 2 Dec 2024 15:52:15 +0000 Subject: [PATCH] Include previous result value on run_result_changed events --- flows/actions/base.go | 4 ++-- flows/events/run_result_changed.go | 6 +++++- flows/interfaces.go | 2 +- flows/results.go | 9 ++++++--- flows/routers/base.go | 4 ++-- flows/runs/run.go | 6 +++--- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/flows/actions/base.go b/flows/actions/base.go index 5a2dee0d2..d2e53bfdd 100644 --- a/flows/actions/base.go +++ b/flows/actions/base.go @@ -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 diff --git a/flows/events/run_result_changed.go b/flows/events/run_result_changed.go index 3e156be3d..835aca3c0 100644 --- a/flows/events/run_result_changed.go +++ b/flows/events/run_result_changed.go @@ -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, @@ -50,5 +53,6 @@ func NewRunResultChanged(result *flows.Result) *RunResultChangedEvent { CategoryLocalized: result.CategoryLocalized, Input: result.Input, Extra: result.Extra, + Previous: prev, } } diff --git a/flows/interfaces.go b/flows/interfaces.go index 827afc19a..35cd695eb 100644 --- a/flows/interfaces.go +++ b/flows/interfaces.go @@ -410,7 +410,7 @@ type Run interface { FlowReference() *assets.FlowReference Session() Session - SaveResult(*Result) + SaveResult(*Result) *Result SetStatus(RunStatus) Webhook() *WebhookCall SetWebhook(*WebhookCall) diff --git a/flows/results.go b/flows/results.go index b606490c3..47883e23b 100644 --- a/flows/results.go +++ b/flows/results.go @@ -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 diff --git a/flows/routers/base.go b/flows/routers/base.go index 2553ab72f..361a90592 100644 --- a/flows/routers/base.go +++ b/flows/routers/base.go @@ -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 diff --git a/flows/runs/run.go b/flows/runs/run.go index b5f801f28..54173769a 100644 --- a/flows/runs/run.go +++ b/flows/runs/run.go @@ -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) {