-
Notifications
You must be signed in to change notification settings - Fork 12
/
issue_dataloader.go
71 lines (61 loc) · 2.42 KB
/
issue_dataloader.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
package dataloader
import (
"context"
"errors"
"github.com/graph-gophers/dataloader"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/entity"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/tracing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"reflect"
)
func (i *Loaders) GetIssueForInteractionEvent(ctx context.Context, interactionEventId string) (*entity.IssueEntity, error) {
thunk := i.IssueForInteractionEvent.Load(ctx, dataloader.StringKey(interactionEventId))
result, err := thunk()
if err != nil {
return nil, err
}
if result == nil {
return nil, nil
}
resultObj := result.(*entity.IssueEntity)
return resultObj, nil
}
func (b *issueBatcher) getIssuesForInteractionEvents(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
span, ctx := opentracing.StartSpanFromContext(ctx, "IssueDataLoader.getIssuesForInteractionEvents")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("keys", keys), log.Int("keys_length", len(keys)))
ids, keyOrder := sortKeys(keys)
issueEntities, err := b.issueService.GetIssuesForInteractionEvents(ctx, ids)
if err != nil {
tracing.TraceErr(span, err)
// check if context deadline exceeded error occurred
if ctx.Err() == context.DeadlineExceeded {
return []*dataloader.Result{{Data: nil, Error: errors.New("deadline exceeded to get issues for interaction events")}}
}
return []*dataloader.Result{{Data: nil, Error: err}}
}
issueEntityByInteractionEventId := make(map[string]entity.IssueEntity)
for _, val := range *issueEntities {
issueEntityByInteractionEventId[val.DataloaderKey] = val
}
// construct an output array of dataloader results
results := make([]*dataloader.Result, len(keys))
for interactionEventId, _ := range issueEntityByInteractionEventId {
if ix, ok := keyOrder[interactionEventId]; ok {
val := issueEntityByInteractionEventId[interactionEventId]
results[ix] = &dataloader.Result{Data: &val, Error: nil}
delete(keyOrder, interactionEventId)
}
}
for _, ix := range keyOrder {
results[ix] = &dataloader.Result{Data: nil, Error: nil}
}
if err = assertEntitiesPtrType(results, reflect.TypeOf(entity.IssueEntity{}), true); err != nil {
tracing.TraceErr(span, err)
return []*dataloader.Result{{nil, err}}
}
span.LogFields(log.Int("results_length", len(results)))
return results
}