-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
job_spec_errors.go
61 lines (50 loc) · 1.71 KB
/
job_spec_errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package loader
import (
"context"
"github.com/graph-gophers/dataloader"
"github.com/smartcontractkit/chainlink/core/services/chainlink"
"github.com/smartcontractkit/chainlink/core/services/job"
"github.com/smartcontractkit/chainlink/core/services/pg"
"github.com/smartcontractkit/chainlink/core/utils/stringutils"
)
type jobSpecErrorsBatcher struct {
app chainlink.Application
}
func (b *jobSpecErrorsBatcher) loadByJobIDs(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
// Create a map for remembering the order of keys passed in
keyOrder := make(map[string]int, len(keys))
// Collect the keys to search for
var jobIDs []int32
for ix, key := range keys {
id, err := stringutils.ToInt32(key.String())
if err == nil {
jobIDs = append(jobIDs, id)
}
keyOrder[key.String()] = ix
}
specErrors, err := b.app.JobORM().FindSpecErrorsByJobIDs(jobIDs, pg.WithParentCtx(ctx))
if err != nil {
return []*dataloader.Result{{Data: nil, Error: err}}
}
// Generate a map of jobIDs to spec errors
specErrorsForJobs := map[string][]job.SpecError{}
for _, s := range specErrors {
jobID := stringutils.FromInt32(s.JobID)
specErrorsForJobs[jobID] = append(specErrorsForJobs[jobID], s)
}
// Construct the output array of dataloader results
results := make([]*dataloader.Result, len(keys))
for k, s := range specErrorsForJobs {
ix, ok := keyOrder[k]
// if found, remove from index lookup map, so we know elements were found
if ok {
results[ix] = &dataloader.Result{Data: s, Error: nil}
delete(keyOrder, k)
}
}
// fill array positions without any nodes as an empty slice
for _, ix := range keyOrder {
results[ix] = &dataloader.Result{Data: []job.SpecError{}, Error: nil}
}
return results
}