You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We previously had a two other router types in the engine, which were removed during a refactor, so stashing their code here for future use/discussion.
router which always picks the first exit:
package routers
import (
"github.com/nyaruka/goflow/flows""github.com/nyaruka/goflow/utils"
)
funcinit() {
RegisterType(TypeFirst, func() flows.Router { return&FirstRouter{} })
}
// TypeFirst is the type for FirstRoutersconstTypeFirststring="first"// FirstRouter is a simple router that always takes the first exittypeFirstRouterstruct {
BaseRouter
}
funcNewFirstRouter(resultNamestring) *FirstRouter {
return&FirstRouter{BaseRouter: newBaseRouter(TypeFirst, resultName)}
}
// Validate validates the arguments on this routerfunc (r*FirstRouter) Validate(exits []flows.Exit) error {
returnutils.Validate(r)
}
// PickRoute always picks the first exit if available for this routerfunc (r*FirstRouter) PickRoute(run flows.FlowRun, exits []flows.Exit, step flows.Step) (*string, flows.Route, error) {
iflen(exits) ==0 {
returnnil, flows.NoRoute, nil
}
returnnil, flows.NewRoute(exits[0].UUID(), "", nil), nil
}
// Inspect inspects this object and any childrenfunc (r*FirstRouter) Inspect(inspectfunc(flows.Inspectable)) {
inspect(r)
}
random router which remembers which exits it took previously and doesn't pick the same more than once:
package routers
import (
"github.com/nyaruka/goflow/flows""github.com/nyaruka/goflow/utils""github.com/pkg/errors""github.com/shopspring/decimal"
)
funcinit() {
RegisterType(TypeRandomOnce, func() flows.Router { return&RandomOnceRouter{} })
}
// TypeRandomOnce is the constant for our random once routerconstTypeRandomOncestring="random_once"// RandomOnceRouter exits of our exits once (randomly) before taking exittypeRandomOnceRouterstruct {
BaseRouterDefault flows.CategoryUUID`json:"default_category_uuid" validate:"required,uuid4"`
}
// NewRandomOnceRouter creates a new random-once routerfuncNewRandomOnceRouter(defaultExit flows.ExitUUID, resultNamestring) *RandomOnceRouter {
return&RandomOnceRouter{
BaseRouter: newBaseRouter(TypeRandomOnce, resultName),
Default: defaultExit,
}
}
// Validate validates the parameters on this routerfunc (r*RandomOnceRouter) Validate(exits []flows.Exit) error {
// check the default category is validifr.Default!=""&&!r.isValidCategory(r.Default) {
returnerrors.Errorf("default category %s is not a valid category", r.Default)
}
returnr.validate(exits)
}
// PickRoute will attempt to take a random exit it hasn't taken before. If all exits have been taken, then it will// take the exit specified in it's Exit parameterfunc (r*RandomOnceRouter) PickRoute(run flows.FlowRun, exits []flows.Exit, step flows.Step) (*string, flows.Route, error) {
iflen(exits) ==0 {
returnnil, flows.NoRoute, nil
}
// find all the exits we have takentakenBefore:=make(map[flows.ExitUUID]bool)
for_, s:=rangerun.Path() {
ifs.NodeUUID() ==step.NodeUUID() {
takenBefore[s.ExitUUID()] =true
}
}
// build up a list of the valid exitsvarvalidExits []flows.ExitUUIDfori:=rangeexits {
// this isn't our default exit and we haven't taken it yetifexits[i].UUID() !=r.Default&&!takenBefore[exits[i].UUID()] {
validExits=append(validExits, exits[i].UUID())
}
}
// no valid choices? exit!iflen(validExits) ==0 {
returnnil, flows.NewRoute(r.Default, "", nil), nil
}
// ok, now pick one randomlyrand:=utils.RandDecimal()
exitNum:=rand.Mul(decimal.New(int64(len(validExits)), 0)).IntPart()
returnnil, flows.NewRoute(validExits[exitNum], rand.String(), nil), nil
}
// Inspect inspects this object and any childrenfunc (r*RandomOnceRouter) Inspect(inspectfunc(flows.Inspectable)) {
inspect(r)
}
The text was updated successfully, but these errors were encountered:
The second of these is actually a pretty useful for anyone who wants to do more scientific polling that randomizes question ordering. (this is a feature on some other platforms)
Just mentioning here that a similar usecase came up recently that wouldn't have been satisfied by this solution because the random questions were being sent weekly and wouldn't have been in the same session. For that you would need a way to track questions answered via a contact field and randomly pop new questions from that. Having said that, the more common usecase is probably shuffling questions within the same session.
We previously had a two other router types in the engine, which were removed during a refactor, so stashing their code here for future use/discussion.
router which always picks the first exit:
random router which remembers which exits it took previously and doesn't pick the same more than once:
The text was updated successfully, but these errors were encountered: