-
Notifications
You must be signed in to change notification settings - Fork 12
/
action_dataloader.go
72 lines (62 loc) · 2.62 KB
/
action_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
72
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"
neo4jenum "github.com/openline-ai/openline-customer-os/packages/server/customer-os-neo4j-repository/enum"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"reflect"
)
func (i *Loaders) GetActionsForInteractionEvent(ctx context.Context, interactionEventId string) (*entity.ActionEntities, error) {
thunk := i.ActionsForInteractionEvent.Load(ctx, dataloader.StringKey(interactionEventId))
result, err := thunk()
if err != nil {
return nil, err
}
resultObj := result.(entity.ActionEntities)
return &resultObj, nil
}
func (b *actionBatcher) getActionsForInteractionEvents(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
span, ctx := opentracing.StartSpanFromContext(ctx, "ActionDataLoader.getActionsForInteractionEvents")
defer span.Finish()
tracing.SetDefaultServiceSpanTags(ctx, span)
span.LogFields(log.Object("keys", keys), log.Int("keys_length", len(keys)))
ids, keyOrder := sortKeys(keys)
actionsForNodes, err := b.actionService.GetActionsForNodes(ctx, neo4jenum.INTERACTION_EVENT, 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 attachments for interaction events")}}
}
return []*dataloader.Result{{Data: nil, Error: err}}
}
entitiesByInteractionEventId := make(map[string]entity.ActionEntities)
for _, val := range *actionsForNodes {
if list, ok := entitiesByInteractionEventId[val.DataloaderKey]; ok {
entitiesByInteractionEventId[val.DataloaderKey] = append(list, val)
} else {
entitiesByInteractionEventId[val.DataloaderKey] = entity.ActionEntities{val}
}
}
// construct an output array of dataloader results
results := make([]*dataloader.Result, len(keys))
for id, record := range entitiesByInteractionEventId {
if ix, ok := keyOrder[id]; ok {
results[ix] = &dataloader.Result{Data: record, Error: nil}
delete(keyOrder, id)
}
}
for _, ix := range keyOrder {
results[ix] = &dataloader.Result{Data: entity.ActionEntities{}, Error: nil}
}
if err = assertEntitiesType(results, reflect.TypeOf(entity.ActionEntities{})); err != nil {
tracing.TraceErr(span, err)
return []*dataloader.Result{{nil, err}}
}
span.LogFields(log.Object("output - results_length", len(results)))
return results
}