-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
getters.go
254 lines (199 loc) · 5.88 KB
/
getters.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package loader
import (
"context"
"github.com/graph-gophers/dataloader"
"github.com/pkg/errors"
"go.uber.org/multierr"
"github.com/smartcontractkit/chainlink/core/chains/evm/txmgr"
"github.com/smartcontractkit/chainlink/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/core/services/feeds"
"github.com/smartcontractkit/chainlink/core/services/job"
"github.com/smartcontractkit/chainlink/core/services/pipeline"
"github.com/smartcontractkit/chainlink/core/utils/stringutils"
)
// ErrInvalidType indicates that results loaded is not the type expected
var ErrInvalidType = errors.New("invalid type")
// GetChainByID fetches the chain by it's id.
func GetChainByID(ctx context.Context, id string) (*types.DBChain, error) {
ldr := For(ctx)
thunk := ldr.ChainsByIDLoader.Load(ctx, dataloader.StringKey(id))
result, err := thunk()
if err != nil {
return nil, err
}
chain, ok := result.(types.DBChain)
if !ok {
return nil, ErrInvalidType
}
return &chain, nil
}
// GetNodesByChainID fetches the nodes for a chain.
func GetNodesByChainID(ctx context.Context, id string) ([]types.Node, error) {
ldr := For(ctx)
thunk := ldr.NodesByChainIDLoader.Load(ctx, dataloader.StringKey(id))
result, err := thunk()
if err != nil {
return nil, err
}
nodes, ok := result.([]types.Node)
if !ok {
return nil, ErrInvalidType
}
return nodes, nil
}
// GetFeedsManagerByID fetches the feed manager by ID.
func GetFeedsManagerByID(ctx context.Context, id string) (*feeds.FeedsManager, error) {
ldr := For(ctx)
thunk := ldr.FeedsManagersByIDLoader.Load(ctx, dataloader.StringKey(id))
result, err := thunk()
if err != nil {
return nil, err
}
mgr, ok := result.(feeds.FeedsManager)
if !ok {
return nil, ErrInvalidType
}
return &mgr, nil
}
// GetJobRunsByIDs fetches the job runs by their ID.
func GetJobRunsByIDs(ctx context.Context, ids []int64) ([]pipeline.Run, error) {
ldr := For(ctx)
strIDs := make([]string, len(ids))
for i, id := range ids {
strIDs[i] = stringutils.FromInt64(id)
}
thunk := ldr.JobRunsByIDLoader.LoadMany(ctx, dataloader.NewKeysFromStrings(strIDs))
results, errs := thunk()
if errs != nil {
merr := multierr.Combine(errs...)
return nil, errors.Wrap(merr, "errors fetching runs")
}
runs := []pipeline.Run{}
for _, result := range results {
if run, ok := result.(pipeline.Run); ok {
runs = append(runs, run)
}
}
return runs, nil
}
// GetSpecsByJobProposalID fetches the spec for a job proposal id.
func GetSpecsByJobProposalID(ctx context.Context, jpID string) ([]feeds.JobProposalSpec, error) {
ldr := For(ctx)
thunk := ldr.JobProposalSpecsByJobProposalID.Load(ctx, dataloader.StringKey(jpID))
result, err := thunk()
if err != nil {
return nil, err
}
specs, ok := result.([]feeds.JobProposalSpec)
if !ok {
return nil, ErrInvalidType
}
return specs, nil
}
// GetLatestSpecByJobProposalID fetches the latest spec for a job proposal id.
func GetLatestSpecByJobProposalID(ctx context.Context, jpID string) (*feeds.JobProposalSpec, error) {
ldr := For(ctx)
thunk := ldr.JobProposalSpecsByJobProposalID.Load(ctx, dataloader.StringKey(jpID))
result, err := thunk()
if err != nil {
return nil, err
}
specs, ok := result.([]feeds.JobProposalSpec)
if !ok {
return nil, errors.Wrapf(ErrInvalidType, "Result : %T", result)
}
max := specs[0]
for _, spec := range specs {
if spec.Version > max.Version {
max = spec
}
}
return &max, nil
}
// GetJobProposalsByFeedsManagerID fetches the job proposals by feeds manager ID.
func GetJobProposalsByFeedsManagerID(ctx context.Context, id string) ([]feeds.JobProposal, error) {
ldr := For(ctx)
thunk := ldr.JobProposalsByManagerIDLoader.Load(ctx, dataloader.StringKey(id))
result, err := thunk()
if err != nil {
return nil, err
}
jbRuns, ok := result.([]feeds.JobProposal)
if !ok {
return nil, ErrInvalidType
}
return jbRuns, nil
}
// GetJobByExternalJobID fetches the job proposals by external job ID
func GetJobByExternalJobID(ctx context.Context, id string) (*job.Job, error) {
ldr := For(ctx)
thunk := ldr.JobsByExternalJobIDs.Load(ctx, dataloader.StringKey(id))
result, err := thunk()
if err != nil {
return nil, err
}
job, ok := result.(job.Job)
if !ok {
return nil, ErrInvalidType
}
return &job, nil
}
// GetJobByPipelineSpecID fetches the job by pipeline spec ID.
func GetJobByPipelineSpecID(ctx context.Context, id string) (*job.Job, error) {
ldr := For(ctx)
thunk := ldr.JobsByPipelineSpecIDLoader.Load(ctx, dataloader.StringKey(id))
result, err := thunk()
if err != nil {
return nil, err
}
jb, ok := result.(job.Job)
if !ok {
return nil, ErrInvalidType
}
return &jb, nil
}
// GetEthTxAttemptsByEthTxID fetches the attempts for an eth transaction.
func GetEthTxAttemptsByEthTxID(ctx context.Context, id string) ([]txmgr.EthTxAttempt, error) {
ldr := For(ctx)
thunk := ldr.EthTxAttemptsByEthTxIDLoader.Load(ctx, dataloader.StringKey(id))
result, err := thunk()
if err != nil {
return nil, err
}
attempts, ok := result.([]txmgr.EthTxAttempt)
if !ok {
return nil, ErrInvalidType
}
return attempts, nil
}
func GetFeedsManagerChainConfigsByManagerID(ctx context.Context, mgrID int64) ([]feeds.ChainConfig, error) {
ldr := For(ctx)
thunk := ldr.FeedsManagerChainConfigsByManagerIDLoader.Load(ctx,
dataloader.StringKey(stringutils.FromInt64(mgrID)),
)
result, err := thunk()
if err != nil {
return nil, err
}
cfgs, ok := result.([]feeds.ChainConfig)
if !ok {
return nil, ErrInvalidType
}
return cfgs, nil
}
// GetJobSpecErrorsByJobID fetches the Spec Errors for a Job.
func GetJobSpecErrorsByJobID(ctx context.Context, jobID int32) ([]job.SpecError, error) {
ldr := For(ctx)
thunk := ldr.SpecErrorsByJobIDLoader.Load(ctx,
dataloader.StringKey(stringutils.FromInt32(jobID)),
)
result, err := thunk()
if err != nil {
return nil, err
}
specErrs, ok := result.([]job.SpecError)
if !ok {
return nil, ErrInvalidType
}
return specErrs, nil
}