-
Notifications
You must be signed in to change notification settings - Fork 1
/
bound.go
144 lines (128 loc) · 3.62 KB
/
bound.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package collecting
import (
"reflect"
"github.com/insolar/insolar/insolar"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/insolar/observer/internal/app/observer"
)
type BoundCollector struct {
results observer.ResultCollector
activates observer.ActivateCollector
chains observer.ChainCollector
}
type BoundCouple struct {
Result *observer.Result
Activate *observer.Activate
}
func NewBoundCollector(originRequest, properResult, directRequest, properActivate observer.Predicate) *BoundCollector {
results := NewResultCollector(originRequest, properResult)
activates := NewActivateCollector(directRequest, properActivate)
parent := &RelationDesc{
Is: isCoupledResult,
Origin: coupledResultOrigin,
Proper: isCoupledResult,
}
child := &RelationDesc{
Is: isCoupledActivate,
Origin: coupledActivateOrigin,
Proper: isCoupledActivate,
}
return &BoundCollector{
results: results,
activates: activates,
chains: NewChainCollector(parent, child),
}
}
func (c *BoundCollector) Collect(rec *observer.Record) *BoundCouple {
if rec == nil {
return nil
}
res := c.results.Collect(rec)
act := c.activates.Collect(rec)
var (
fullChain *observer.Chain
)
switch {
case act != nil:
fullChain = c.chains.Collect(act)
case res != nil:
fullChain = c.chains.Collect(res)
}
if fullChain == nil {
return nil
}
actRecord, resRecord := unwrapChain(fullChain)
return &BoundCouple{
Result: resRecord,
Activate: actRecord,
}
}
func isCoupledResult(chain interface{}) bool {
_, ok := chain.(*observer.CoupledResult)
return ok
}
func isCoupledActivate(chain interface{}) bool {
_, ok := chain.(*observer.CoupledActivate)
return ok
}
func coupledResultOrigin(chain interface{}) insolar.ID {
coupled, ok := chain.(*observer.CoupledResult)
if !ok {
return insolar.ID{}
}
request := coupled.Request
if !request.IsIncoming() {
log.Warnf("failed to use not incoming request to get origin")
}
return request.ID
}
func coupledActivateOrigin(chain interface{}) insolar.ID {
coupled, ok := chain.(*observer.CoupledActivate)
if !ok {
return insolar.ID{}
}
request := coupled.Request
if !request.IsIncoming() {
log.Warnf("failed to use not incoming request to get origin")
}
return request.Reason()
}
func unwrapChain(chain *observer.Chain) (*observer.Activate, *observer.Result) {
coupledAct, ok := chain.Child.(*observer.CoupledActivate)
if !ok {
log.Error(errors.Errorf("trying to use %s as *observer.Chain", reflect.TypeOf(chain.Child)))
return nil, nil
}
if coupledAct.Activate == nil {
log.Error(errors.New("invalid coupled activate chain, child is nil"))
return nil, nil
}
actRecord := coupledAct.Activate
coupledRes, ok := chain.Parent.(*observer.CoupledResult)
if !ok {
log.Error(errors.Errorf("trying to use %s as *observer.Chain", reflect.TypeOf(chain.Parent)))
return nil, nil
}
if coupledRes.Result == nil {
log.Error(errors.New("invalid coupled result chain, child is nil"))
return nil, nil
}
resRecord := coupledRes.Result
return actRecord, resRecord
}