-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add job filter to memory client #540
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
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-deployments-framework": minor | ||
| --- | ||
|
|
||
| JD Memory Client now supports filtering in `ListJobs` |
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,102 @@ | ||
| package memory | ||
|
|
||
| import ( | ||
| "slices" | ||
|
|
||
| jobv1 "github.com/smartcontractkit/chainlink-protos/job-distributor/v1/job" | ||
| "github.com/smartcontractkit/chainlink-protos/job-distributor/v1/shared/ptypes" | ||
| ) | ||
|
|
||
| // applyJobFilter applies the filter to the list of jobs and returns the filtered results. | ||
| func applyJobFilter( | ||
| jobs []*jobv1.Job, filter *jobv1.ListJobsRequest_Filter, | ||
| ) []*jobv1.Job { | ||
| var filtered []*jobv1.Job | ||
|
|
||
| for _, job := range jobs { | ||
| if jobMatchesFilter(job, filter) { | ||
| filtered = append(filtered, job) | ||
| } | ||
| } | ||
|
|
||
| return filtered | ||
| } | ||
|
|
||
| // jobMatchesFilter checks if a job matches the given filter criteria. | ||
| func jobMatchesFilter(job *jobv1.Job, filter *jobv1.ListJobsRequest_Filter) bool { | ||
| // Check if job is soft-deleted and should be excluded | ||
| if !jobMatchesDeletedFilter(job, filter) { | ||
| return false | ||
| } | ||
|
|
||
| // Check job IDs | ||
| if len(filter.Ids) > 0 { | ||
| if !jobMatchesJobIds(job, filter.Ids) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // Check UUIDs | ||
| if len(filter.Uuids) > 0 { | ||
| if !jobMatchesUuids(job, filter.Uuids) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // Check node IDs | ||
| if len(filter.NodeIds) > 0 { | ||
| if !jobMatchesNodeIds(job, filter.NodeIds) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // Check selectors | ||
| if len(filter.Selectors) > 0 { | ||
| for _, selector := range filter.Selectors { | ||
| if !jobMatchesSelector(job, selector) { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // jobMatchesJobIds checks if a job's ID is in the provided list of job IDs. | ||
| func jobMatchesJobIds(job *jobv1.Job, jobIds []string) bool { | ||
| return slices.Contains(jobIds, job.Id) | ||
| } | ||
|
|
||
| // jobMatchesUuids checks if a job's UUID is in the provided list of UUIDs. | ||
| func jobMatchesUuids(job *jobv1.Job, uuids []string) bool { | ||
| return slices.Contains(uuids, job.Uuid) | ||
| } | ||
|
|
||
| // jobMatchesNodeIds checks if a job's node ID is in the provided list of node IDs. | ||
| func jobMatchesNodeIds(job *jobv1.Job, nodeIds []string) bool { | ||
| return slices.Contains(nodeIds, job.NodeId) | ||
| } | ||
|
|
||
| // jobMatchesDeletedFilter checks if a job should be included based on its deleted status. | ||
| // By default, soft-deleted jobs (with DeletedAt set) are excluded unless IncludeDeleted is true. | ||
| func jobMatchesDeletedFilter(job *jobv1.Job, filter *jobv1.ListJobsRequest_Filter) bool { | ||
| // If job is soft-deleted (DeletedAt is not nil) | ||
| if job.DeletedAt != nil { | ||
| // Only include if IncludeDeleted is explicitly set to true | ||
| return filter.IncludeDeleted | ||
| } | ||
|
|
||
| // If job is not soft-deleted, always include it | ||
| return true | ||
| } | ||
|
|
||
| // jobMatchesSelector checks if a job matches a specific selector. | ||
| func jobMatchesSelector(job *jobv1.Job, selector *ptypes.Selector) bool { | ||
| // Get the job's labels as a map for easier lookup | ||
| jobLabels := make(map[string]*string) | ||
| for _, label := range job.Labels { | ||
| jobLabels[label.Key] = label.Value | ||
| } | ||
|
|
||
| return matchesSelector(jobLabels, selector) | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could these 3 function be a general
jobMatchesfunction since they have the same signature and perform the same check?eg