Skip to content

Commit

Permalink
Merge branch 'main' into 13.6
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Dec 2, 2024
2 parents e3d450e + f3f6ba2 commit 006510e
Show file tree
Hide file tree
Showing 42 changed files with 148 additions and 215 deletions.
6 changes: 4 additions & 2 deletions flows/actions/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ 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())
prev := run.SaveResult(result)
logEvent(events.NewRunResultChanged(result, prev))
prev, changed := run.SaveResult(result)
if changed {
logEvent(events.NewRunResultChanged(result, prev))
}
}

// helper to save a run result based on a webhook call and log it as an event
Expand Down
10 changes: 3 additions & 7 deletions flows/actions/testdata/call_classifier.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Intent",
"value": "0",
"category": "Failure",
"input": "Hi everybody"
"category": "Failure"
}
],
"templates": [
Expand Down Expand Up @@ -179,7 +178,6 @@
"name": "Intent",
"value": "book_flight",
"category": "Success",
"input": "Hi everybody",
"extra": {
"intents": [
{
Expand Down Expand Up @@ -283,8 +281,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Intent",
"value": "0",
"category": "Failure",
"input": "Hi everybody"
"category": "Failure"
}
],
"templates": [
Expand Down Expand Up @@ -370,8 +367,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Intent",
"value": "0",
"category": "Failure",
"input": "Hi everybody"
"category": "Failure"
}
],
"templates": [
Expand Down
5 changes: 1 addition & 4 deletions flows/actions/testdata/call_resthook.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "My Result",
"value": "503",
"category": "Failure",
"input": "POST http://unavailable.com/"
"category": "Failure"
}
],
"webhook": {
Expand Down Expand Up @@ -411,7 +410,6 @@
"name": "My Result",
"value": "200",
"category": "Success",
"input": "POST http://temba.io/",
"extra": {
"ok": "true"
}
Expand Down Expand Up @@ -643,7 +641,6 @@
"name": "My Result",
"value": "503",
"category": "Failure",
"input": "POST http://unavailable.com/",
"extra": {
"errors": [
"service unavailable"
Expand Down
14 changes: 4 additions & 10 deletions flows/actions/testdata/call_webhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@
"name": "My Webhook",
"value": "200",
"category": "Success",
"input": "POST http://temba.io/",
"extra": {
"ok": true
}
Expand Down Expand Up @@ -391,8 +390,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "My Webhook",
"value": "200",
"category": "Success",
"input": "GET http://temba.io/"
"category": "Success"
}
],
"webhook": {
Expand Down Expand Up @@ -463,8 +461,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "My Webhook",
"value": "200",
"category": "Success",
"input": "GET http://temba.io/"
"category": "Success"
}
],
"webhook": {
Expand Down Expand Up @@ -537,7 +534,6 @@
"name": "My Webhook",
"value": "400",
"category": "Failure",
"input": "POST http://temba.io/",
"extra": {
"errors": [
"bad_request"
Expand Down Expand Up @@ -615,8 +611,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "My Webhook",
"value": "0",
"category": "Failure",
"input": "POST http://temba.io/"
"category": "Failure"
}
],
"webhook": {
Expand Down Expand Up @@ -693,8 +688,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "My Webhook",
"value": "200",
"category": "Failure",
"input": "GET http://temba.io/"
"category": "Failure"
}
],
"webhook": {
Expand Down
6 changes: 3 additions & 3 deletions flows/actions/testdata/send_broadcast.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@
"action": {
"type": "send_broadcast",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"urns": [
"tel:+1234567890"
],
"groups": [
{
"uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
Expand All @@ -144,6 +141,9 @@
}
],
"contact_query": "name = \"Bob\"",
"urns": [
"tel:+1234567890"
],
"legacy_vars": [
"@(\"\")",
"@contact.fields.gender",
Expand Down
3 changes: 1 addition & 2 deletions flows/actions/testdata/set_run_result.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Preference",
"value": "yeah",
"category": "Yes",
"category_localized": "Si"
"category": "Yes"
}
],
"templates": [
Expand Down
6 changes: 3 additions & 3 deletions flows/actions/testdata/start_session.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,6 @@
"action": {
"type": "start_session",
"uuid": "ad154980-7bf7-4ab8-8728-545fd6378912",
"urns": [
"tel:+1234567890"
],
"groups": [
{
"uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
Expand All @@ -291,6 +288,9 @@
}
],
"contact_query": "age > 20 OR gender = @fields.gender",
"urns": [
"tel:+1234567890"
],
"legacy_vars": [
"@(\"5129165834\")"
],
Expand Down
50 changes: 26 additions & 24 deletions flows/events/run_result_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,48 @@ func init() {
// TypeRunResultChanged is the type of our run result event
const TypeRunResultChanged string = "run_result_changed"

// RunResultChangedEvent events are created when a run result is saved. They contain not only
// the name, value and category of the result, but also the UUID of the node where
// the result was generated.
type PreviousResult struct {
Value string `json:"value"`
Category string `json:"category"`
}

// RunResultChangedEvent events are created when a run result is changed.
//
// {
// "type": "run_result_changed",
// "created_on": "2006-01-02T15:04:05Z",
// "name": "Gender",
// "value": "m",
// "category": "Male",
// "category_localized": "Homme",
// "node_uuid": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
// "input": "M"
// "category": "Male"
// }
//
// @event run_result_changed
type RunResultChangedEvent struct {
BaseEvent

Name string `json:"name" validate:"required"`
Value string `json:"value"`
Category string `json:"category"`
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:"-"`
Name string `json:"name" validate:"required"`
Value string `json:"value"`
Category string `json:"category"`
Extra json.RawMessage `json:"extra,omitempty"`
Previous *PreviousResult `json:"previous,omitempty"`
}

// NewRunResultChanged returns a new save result event for the passed in values
func NewRunResultChanged(result, prev *flows.Result) *RunResultChangedEvent {
var p *PreviousResult
if prev != nil {
p = &PreviousResult{
Value: prev.Value,
Category: prev.Category,
}
}

return &RunResultChangedEvent{
BaseEvent: NewBaseEvent(TypeRunResultChanged),
Name: result.Name,
Value: result.Value,
Category: result.Category,
CategoryLocalized: result.CategoryLocalized,
Input: result.Input,
Extra: result.Extra,
Previous: prev,
BaseEvent: NewBaseEvent(TypeRunResultChanged),
Name: result.Name,
Value: result.Value,
Category: result.Category,
Extra: result.Extra,
Previous: p,
}
}
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) *Result
SaveResult(*Result) (*Result, bool)
SetStatus(RunStatus)
Webhook() *WebhookCall
SetWebhook(*WebhookCall)
Expand Down
8 changes: 6 additions & 2 deletions flows/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ func (r Results) Clone() Results {
}

// 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 {
func (r Results) Save(result *Result) (*Result, bool) {
key := utils.Snakify(result.Name)
old := r[key]
r[key] = result
return old

if old == nil || (old.Value != result.Value || old.Category != result.Category) {
return old, true
}
return nil, false
}

// Get returns the result with the given key
Expand Down
3 changes: 1 addition & 2 deletions flows/resumes/testdata/dial.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"step_uuid": "9688d21d-95aa-4bed-afc7-f31b35731a3d",
"name": "Redirect",
"value": "answered",
"category": "Success",
"input": "answered"
"category": "Success"
}
],
"run_status": "completed",
Expand Down
3 changes: 1 addition & 2 deletions flows/resumes/testdata/msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"step_uuid": "9688d21d-95aa-4bed-afc7-f31b35731a3d",
"name": "Favorite Color",
"value": "red",
"category": "Red",
"input": "red"
"category": "Red"
}
],
"run_status": "completed",
Expand Down
6 changes: 4 additions & 2 deletions flows/routers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ 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())
prev := run.SaveResult(result)
logEvent(events.NewRunResultChanged(result, prev))
prev, changed := run.SaveResult(result)
if changed {
logEvent(events.NewRunResultChanged(result, prev))
}
}

return category.ExitUUID(), nil
Expand Down
3 changes: 1 addition & 2 deletions flows/routers/testdata/random.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Random Result",
"value": "1",
"category": "No",
"input": "0.3849275689214193"
"category": "No"
}
],
"localizables": [
Expand Down
12 changes: 4 additions & 8 deletions flows/routers/testdata/switch.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Favorite Color",
"value": "YES",
"category": "Yes",
"input": "YES!!"
"category": "Yes"
}
],
"templates": [
Expand Down Expand Up @@ -284,8 +283,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Is Member",
"value": "[]",
"category": "Other",
"input": "[]"
"category": "Other"
}
],
"templates": [
Expand Down Expand Up @@ -374,8 +372,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "In Group",
"value": "[]",
"category": "Other",
"input": "[]"
"category": "Other"
}
],
"templates": [
Expand Down Expand Up @@ -483,8 +480,7 @@
"step_uuid": "59d74b86-3e2f-4a93-aece-b05d2fdcde0c",
"name": "Favorite Color",
"value": "YES",
"category": "Yes",
"input": "YES!!"
"category": "Yes"
}
],
"templates": [
Expand Down
2 changes: 1 addition & 1 deletion flows/runs/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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) *flows.Result {
func (r *run) SaveResult(result *flows.Result) (*flows.Result, bool) {
// truncate value if necessary
result.Value = stringsx.Truncate(result.Value, r.session.Engine().Options().MaxResultChars)

Expand Down
Loading

0 comments on commit 006510e

Please sign in to comment.