-
Notifications
You must be signed in to change notification settings - Fork 5
/
activity.go
233 lines (174 loc) · 5.57 KB
/
activity.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
package aggregate
import (
"fmt"
"strings"
"sync"
"time"
"github.com/TIBCOSoftware/labs-air-contrib/activity/aggregate/pipeline/support"
"github.com/TIBCOSoftware/labs-air-contrib/activity/aggregate/window"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
)
const (
ivValue = "value"
ovResult = "result"
ovReport = "report"
sdWindow = "window"
)
//we can generate json from this! - we could also create a "validate-able" object from this
type Settings struct {
Function string `md:"function,required,allowed(avg,sum,min,max,count,accumulate)"`
WindowType string `md:"windowType,required,allowed(tumbling,sliding,timeTumbling,timeSliding)"`
WindowSize int `md:"windowSize,required"`
Resolution int `md:"resolution"`
ProceedOnlyOnEmit bool `md:"proceedOnlyOnEmit"`
AdditionalSettings string `md:"additionalSettings"`
}
type Input struct {
Value interface{} `md:"value"`
}
type Output struct {
Report bool `md:"report"`
Result interface{} `md:"result"`
}
func init() {
_ = activity.Register(&Activity{}, New)
}
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{ProceedOnlyOnEmit: true, Resolution: 1}
//settings.Function = "avg" // default function
//settings.WindowType = "tumbling" // default window type
//settings.WindowSize = 5 // default window resolution
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
additionalSettings, err := toParams(s.AdditionalSettings)
if err != nil {
return nil, err
}
sharedData := make(map[string]interface{})
act := &Activity{settings: s, additionalSettings: additionalSettings, sharedData: sharedData}
return act, nil
}
// Activity is an Activity that is used to Aggregate a message to the console
type Activity struct {
settings *Settings
additionalSettings map[string]string
sharedData map[string]interface{}
mutex sync.Mutex
}
// Metadata returns the activity's metadata
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
// Eval implements api.Activity.Eval - Aggregates the Message
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
ctx.Logger().Debug("In Eval function")
// sharedData := ctx.GetSharedTempData()
sharedData := a.sharedData
wv, defined := sharedData[sdWindow]
timerSupport, timerSupported := support.GetTimerSupport(ctx)
var w window.Window
//create the window & associated timer if necessary
if !defined {
a.mutex.Lock()
wv, defined = sharedData[sdWindow]
if defined {
w = wv.(window.Window)
} else {
w, err = a.createWindow(ctx)
if err != nil {
a.mutex.Unlock()
return false, err
}
sharedData[sdWindow] = w
}
a.mutex.Unlock()
} else {
w = wv.(window.Window)
}
in := ctx.GetInput(ivValue)
fmt.Printf(" --------------------------> In Eval function in = %v \n", in)
emit, result := w.AddSample(in)
fmt.Printf(" --------------------------> In Eval function result = %v \n", result)
fmt.Printf(" --------------------------> In Eval function emit = %v \n", emit)
if timerSupported {
timerSupport.UpdateTimer(true)
}
err = ctx.SetOutput(ovResult, result)
if err != nil {
return false, err
}
err = ctx.SetOutput(ovReport, emit)
if err != nil {
return false, err
}
done = !(a.settings.ProceedOnlyOnEmit && !emit)
return done, nil
}
func (a *Activity) createWindow(ctx activity.Context) (w window.Window, err error) {
settings := a.settings
timerSupport, timerSupported := support.GetTimerSupport(ctx)
windowSettings := &window.Settings{Size: settings.WindowSize, ExternalTimer: timerSupported, Resolution: settings.Resolution}
err = windowSettings.SetAdditionalSettings(a.additionalSettings)
if err != nil {
return nil, err
}
wType := strings.ToLower(settings.WindowType)
switch wType {
case "tumbling":
w, err = NewTumblingWindow(settings.Function, windowSettings)
case "sliding":
w, err = NewSlidingWindow(settings.Function, windowSettings)
case "timetumbling":
w, err = NewTumblingTimeWindow(settings.Function, windowSettings)
if err == nil && timerSupported {
err = timerSupport.CreateTimer(time.Duration(settings.WindowSize)*time.Millisecond, a.moveWindow, true)
}
case "timesliding":
w, err = NewSlidingTimeWindow(settings.Function, windowSettings)
if err == nil && timerSupported {
err = timerSupport.CreateTimer(time.Duration(settings.Resolution)*time.Millisecond, a.moveWindow, true)
}
default:
return nil, fmt.Errorf("unsupported window type: '%s'", settings.WindowType)
}
return w, err
}
func (a *Activity) PostEval(ctx activity.Context, userData interface{}) (done bool, err error) {
return true, nil
}
func (a *Activity) moveWindow(ctx activity.Context) bool {
// sharedData := ctx.GetSharedTempData()
sharedData := a.sharedData
wv, _ := sharedData[sdWindow]
w, _ := wv.(window.TimeWindow)
emit, result := w.NextBlock()
err := ctx.SetOutput(ovResult, result)
if err != nil {
//todo log error?
}
err = ctx.SetOutput(ovReport, emit)
if err != nil {
//todo log error?
}
return !(a.settings.ProceedOnlyOnEmit && !emit)
}
func toParams(values string) (map[string]string, error) {
if values == "" {
return map[string]string{}, nil
}
var params map[string]string
result := strings.Split(values, ",")
params = make(map[string]string)
for _, pair := range result {
nv := strings.Split(pair, "=")
if len(nv) != 2 {
return nil, fmt.Errorf("invalid settings")
}
params[nv[0]] = nv[1]
}
return params, nil
}