-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add MinTask and MaxTask pipeline tasks #20040
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "chainlink": minor | ||
| --- | ||
|
|
||
| #added Mix and Max pipeline tasks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package pipeline | ||
|
|
||
| import ( | ||
| "context" | ||
| stderrors "errors" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/shopspring/decimal" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
| ) | ||
|
|
||
| // Return types: | ||
| // | ||
| // *decimal.Decimal | ||
| type MaxTask struct { | ||
| BaseTask `mapstructure:",squash"` | ||
| Values string `json:"values"` | ||
| AllowedFaults string `json:"allowedFaults"` | ||
| // Lax when disabled (default) will return an error if there are no input values or if the input includes nil values. | ||
| // Lax when enabled will return nil with no error if there are no valid input values. If the input includes nil values, they will be excluded from the calculation and do not count as a fault. | ||
| Lax string | ||
| } | ||
|
|
||
| var _ Task = (*MaxTask)(nil) | ||
|
|
||
| func (t *MaxTask) Type() TaskType { | ||
| return TaskTypeMax | ||
| } | ||
|
|
||
| func (t *MaxTask) Run(_ context.Context, _ logger.Logger, vars Vars, inputs []Result) (result Result, runInfo RunInfo) { | ||
| var ( | ||
| maybeAllowedFaults MaybeUint64Param | ||
| valuesAndErrs SliceParam | ||
| decimalValues DecimalSliceParam | ||
| allowedFaults int | ||
| lax BoolParam | ||
| ) | ||
| err := stderrors.Join( | ||
| errors.Wrap(ResolveParam(&maybeAllowedFaults, From(t.AllowedFaults)), "allowedFaults"), | ||
| errors.Wrap(ResolveParam(&valuesAndErrs, From(VarExpr(t.Values, vars), JSONWithVarExprs(t.Values, vars, true), Inputs(inputs))), "values"), | ||
| errors.Wrap(ResolveParam(&lax, From(NonemptyString(t.Lax), false)), "lax"), | ||
| ) | ||
| if err != nil { | ||
| return Result{Error: err}, runInfo | ||
| } | ||
|
|
||
| // if lax is enabled, filter out nil values | ||
| // nil values are not included in the fault calculations | ||
| if lax { | ||
| valuesAndErrs, _ = valuesAndErrs.FilterNils() | ||
| } | ||
|
|
||
| if allowed, isSet := maybeAllowedFaults.Uint64(); isSet { | ||
| allowedFaults = int(allowed) //nolint:gosec // G115: it will not exceed int64 | ||
| } else { | ||
| allowedFaults = max(len(valuesAndErrs)-1, 0) | ||
| } | ||
|
|
||
| values, faults := valuesAndErrs.FilterErrors() | ||
| if faults > allowedFaults { | ||
| return Result{Error: errors.Wrapf(ErrTooManyErrors, "Number of faulty inputs %v to max task > number allowed faults %v", faults, allowedFaults)}, runInfo | ||
| } | ||
| if len(values) == 0 { | ||
| if lax { | ||
| return Result{}, runInfo // if lax is enabled, return nil result with no error | ||
| } | ||
| return Result{Error: errors.Wrap(ErrWrongInputCardinality, "no values to maxize")}, runInfo | ||
| } | ||
|
|
||
| err = decimalValues.UnmarshalPipelineParam(values) | ||
| if err != nil { | ||
| return Result{Error: errors.Wrapf(ErrBadInput, "values: %v", err)}, runInfo | ||
| } | ||
|
|
||
| maxVal := decimal.Max(decimalValues[0], decimalValues[1:]...) | ||
|
|
||
| return Result{Value: maxVal}, runInfo | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.