forked from TIBCOSoftware/flogo-contrib
/
activity.go
executable file
·278 lines (214 loc) · 6.66 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package aggregate
import (
"fmt"
"strings"
"sync"
"time"
"github.com/TIBCOSoftware/flogo-contrib/activity/aggregate/window"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/project-flogo/stream/pipeline/support"
)
// activityLogger is the default logger for the Aggregate Activity
var activityLogger = logger.GetLogger("activity-aggregate")
const (
sFunction = "function"
sWindowType = "windowType"
sWindowSize = "windowSize"
sResolution = "resolution"
sProceedOnlyOnEmit = "proceedOnlyOnEmit"
sAdditionalSettings = "additionalSettings"
ivValue = "value"
ovResult = "result"
ovReport = "report"
)
//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)"`
WindowType string `md:"windowType,required,allowed(tumbling,sliding,timeTumbling,timeSliding)"`
WindowSize int `md:"windowSize,required"`
ProceedOnlyOnEmit bool
Resolution int
AdditionalSettings map[string]string
}
func init() {
activityLogger.SetLogLevel(logger.InfoLevel)
}
var metadata *activity.Metadata
func New(config *activity.Config) (activity.Activity, error) {
act := &AggregateActivity{mutex: &sync.RWMutex{}}
//todo implement
//config.Settings
return act, nil
}
// AggregateActivity is an Activity that is used to Aggregate a message to the console
type AggregateActivity struct {
settings *Settings
mutex *sync.RWMutex
}
// NewActivity creates a new AppActivity
func NewActivity(md *activity.Metadata) activity.Activity {
metadata = md
activity.RegisterFactory(md.ID, New)
return &AggregateActivity{mutex: &sync.RWMutex{}}
}
// Metadata returns the activity's metadata
func (a *AggregateActivity) Metadata() *activity.Metadata {
return metadata
}
// Eval implements api.Activity.Eval - Aggregates the Message
func (a *AggregateActivity) Eval(ctx activity.Context) (done bool, err error) {
//todo move to Activity instance creation
settings, err := getSettings(ctx)
if err != nil {
return false, err
}
ss, ok := activity.GetSharedTempDataSupport(ctx)
if !ok {
return false, fmt.Errorf("AggregateActivity not supported by this activity host")
}
sharedData := ss.GetSharedTempData()
wv, defined := sharedData["window"]
timerSupport, timerSupported := support.GetTimerSupport(ctx)
var w window.Window
//create the window & associated timer if necessary
if !defined {
a.mutex.Lock()
wv, defined = sharedData["window"]
if defined {
w = wv.(window.Window)
} else {
w, err = createWindow(ctx, settings)
if err != nil {
a.mutex.Unlock()
return false, err
}
sharedData["window"] = w
}
a.mutex.Unlock()
} else {
w = wv.(window.Window)
}
in := ctx.GetInput(ivValue)
emit, result := w.AddSample(in)
if timerSupported {
timerSupport.UpdateTimer(true)
}
ctx.SetOutput(ovResult, result)
ctx.SetOutput(ovReport, emit)
done = !(settings.ProceedOnlyOnEmit && !emit)
return done, nil
}
func createWindow(ctx activity.Context, settings *Settings) (w window.Window, err error) {
timerSupport, timerSupported := support.GetTimerSupport(ctx)
windowSettings := &window.Settings{Size: settings.WindowSize, ExternalTimer: timerSupported, Resolution: settings.Resolution}
windowSettings.SetAdditionalSettings(settings.AdditionalSettings)
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 timerSupported {
timerSupport.CreateTimer(time.Duration(settings.WindowSize)*time.Millisecond, moveWindow, true)
}
case "timesliding":
w, err = NewSlidingTimeWindow(settings.Function, windowSettings)
if timerSupported {
timerSupport.CreateTimer(time.Duration(settings.Resolution)*time.Millisecond, moveWindow, true)
}
default:
return nil, fmt.Errorf("unsupported window type: '%s'", settings.WindowType)
}
return w, err
}
func (a *AggregateActivity) PostEval(ctx activity.Context, userData interface{}) (done bool, err error) {
return true, nil
}
func moveWindow(ctx activity.Context) bool {
ss, _ := activity.GetSharedTempDataSupport(ctx)
sharedData := ss.GetSharedTempData()
wv, _ := sharedData["window"]
w, _ := wv.(window.TimeWindow)
emit, result := w.NextBlock()
ctx.SetOutput(ovResult, result)
ctx.SetOutput(ovReport, emit)
poe := true // by default only proceed on emit
poeSetting, exists := ctx.GetSetting(sProceedOnlyOnEmit)
if exists {
poe, _ = data.CoerceToBoolean(poeSetting)
}
return !(poe && !emit)
}
func getSettings(ctx activity.Context) (*Settings, error) {
settings := &Settings{}
settings.Function = "avg" // default function
setting, exists := ctx.GetSetting(sFunction)
if exists {
val, err := data.CoerceToString(setting)
if err == nil {
settings.Function = val
}
}
settings.WindowType = "tumbling" // default window type
setting, exists = ctx.GetSetting(sWindowType)
if exists {
val, err := data.CoerceToString(setting)
if err == nil {
settings.WindowType = val
}
}
settings.WindowSize = 5 // default window resolution
setting, exists = ctx.GetSetting(sWindowSize)
if exists {
val, err := data.CoerceToInteger(setting)
if err == nil {
settings.WindowSize = val
}
}
settings.Resolution = 1 // default window resolution
setting, exists = ctx.GetSetting(sResolution)
if exists {
val, err := data.CoerceToInteger(setting)
if err == nil {
settings.Resolution = val
}
}
settings.ProceedOnlyOnEmit = true // by default only proceed on emit
setting, exists = ctx.GetSetting(sProceedOnlyOnEmit)
if exists {
val, err := data.CoerceToBoolean(setting)
if err == nil {
settings.ProceedOnlyOnEmit = val
}
}
setting, exists = ctx.GetSetting(sAdditionalSettings)
if exists {
val, err := data.CoerceToString(setting)
if err == nil {
settings.AdditionalSettings, err = toParams(val)
if err != nil {
return nil, err
}
}
}
// settings validation can be done here once activities are created on configuration instead of
// setting up during runtime
return settings, nil
}
func toParams(values string) (map[string]string, error) {
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
}