/
graphEngine.go
513 lines (452 loc) · 13.9 KB
/
graphEngine.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package freepsgraph
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/hannesrauhe/freeps/base"
"github.com/hannesrauhe/freeps/utils"
log "github.com/sirupsen/logrus"
)
// GraphEngineConfig is the configuration for the GraphEngine
type GraphEngineConfig struct {
Graphs map[string]GraphDesc
GraphsFromURL []string
GraphsFromFile []string
AlertDuration time.Duration
}
var DefaultGraphEngineConfig = GraphEngineConfig{GraphsFromFile: []string{}, GraphsFromURL: []string{}, Graphs: map[string]GraphDesc{}, AlertDuration: time.Hour}
// GraphEngine holds all available graphs and operators
type GraphEngine struct {
cr *utils.ConfigReader
graphs map[string]*GraphDesc
operators map[string]base.FreepsBaseOperator
hooks map[string]GraphEngineHook
config GraphEngineConfig
reloadRequested bool
graphLock sync.Mutex
operatorLock sync.Mutex
hookMapLock sync.Mutex
}
// NewGraphEngine creates the graph engine from the config
func NewGraphEngine(ctx *base.Context, cr *utils.ConfigReader, cancel context.CancelFunc) *GraphEngine {
ge := &GraphEngine{cr: cr, graphs: make(map[string]*GraphDesc), reloadRequested: false}
ge.operators = make(map[string]base.FreepsBaseOperator)
ge.operators["graph"] = &OpGraph{ge: ge}
ge.operators["graphbytag"] = &OpGraphByTag{ge: ge}
ge.operators["system"] = NewSytemOp(ge, cancel)
ge.operators["eval"] = &OpEval{}
ge.hooks = make(map[string]GraphEngineHook)
// probably deprecated anyhow to start without config
if cr != nil {
ge.config = ge.ReadConfig()
ge.loadStoredAndEmbeddedGraphs(ctx)
ge.loadExternalGraphs(ctx)
g := ge.GetAllGraphDesc()
addedGraphs := make([]string, 0, len(g))
for n := range g {
addedGraphs = append(addedGraphs, n)
}
} else {
ge.config = DefaultGraphEngineConfig
}
return ge
}
// getHookMapCopy returns a copy of the hook-map to reduce locking time (hook map does not need to be locked while hook is executed)
func (ge *GraphEngine) getHookMapCopy() map[string]GraphEngineHook {
ge.hookMapLock.Lock()
defer ge.hookMapLock.Unlock()
r := make(map[string]GraphEngineHook)
for k, v := range ge.hooks {
if v == nil {
continue
}
r[k] = v
}
return r
}
// ReadConfig reads the config from the config reader
func (ge *GraphEngine) ReadConfig() GraphEngineConfig {
config := DefaultGraphEngineConfig
if ge.cr != nil {
err := ge.cr.ReadSectionWithDefaults("graphs", &config)
if err != nil {
log.Fatal(err)
}
ge.cr.WriteBackConfigIfChanged()
if err != nil {
log.Print(err)
}
}
return config
}
// ReloadRequested returns true if a reload was requested instead of a restart
func (ge *GraphEngine) ReloadRequested() bool {
return ge.reloadRequested
}
func (ge *GraphEngine) getGraphDescUnlocked(graphName string) (*GraphDesc, bool) {
gi, exists := ge.graphs[graphName]
if exists {
return gi, exists
}
return nil, false
}
// CheckGraph checks if the graph is valid
func (ge *GraphEngine) CheckGraph(graphID string) *base.OperatorIO {
_, o := ge.prepareGraphExecution(nil, graphID)
return o
}
// GetGraphDesc returns the graph description stored under graphID
func (ge *GraphEngine) GetGraphDesc(graphID string) (*GraphDesc, bool) {
ge.graphLock.Lock()
defer ge.graphLock.Unlock()
gi, exists := ge.getGraphDescUnlocked(graphID)
if exists {
return gi, exists
}
return nil, exists
}
// GetCompleteGraphDesc returns the sanitized, validated and complete graph description stored under graphName
func (ge *GraphEngine) GetCompleteGraphDesc(graphID string) (*GraphDesc, error) {
ge.graphLock.Lock()
defer ge.graphLock.Unlock()
gi, exists := ge.getGraphDescUnlocked(graphID)
if exists {
return gi.GetCompleteDesc(graphID, ge)
}
return nil, fmt.Errorf("Graph with ID \"%v\" does not exist", graphID)
}
// GetTags returns a map of all used tags TODO(HR): deprecate
func (ge *GraphEngine) GetTags() map[string]string {
r := map[string]string{}
for _, d := range ge.GetAllGraphDesc() {
if d.Tags == nil {
continue
}
for _, t := range d.Tags {
r[t] = t
}
}
return r
}
// SplitTag returns the tag name and the value of the tag if any (split by the first ":")
func SplitTag(tag string) (string, string) {
if utils.StringStartsWith(tag, ":") {
return tag, ""
}
tmp := strings.Split(tag, ":")
if len(tmp) == 1 {
return tmp[0], ""
}
if len(tmp) == 2 {
return tmp[0], tmp[1]
}
if len(tmp) > 2 {
return tmp[0], strings.Join(tmp[1:], ":")
}
return "", ""
}
// GetTagValues returns a slice of all used values for the given tag
func (ge *GraphEngine) GetTagValues(keytag string) []string {
r := []string{}
l := len(keytag)
if l == 0 {
return r
}
for _, d := range ge.GetAllGraphDesc() {
if d.Tags == nil {
continue
}
for _, t := range d.Tags {
k, v := SplitTag(t)
if k == keytag && v != "" {
r = append(r, v)
}
}
}
return r
}
// GetTagMap returns a map of all used tags and their values (empty array if no value)
func (ge *GraphEngine) GetTagMap() map[string][]string {
r := map[string][]string{}
for _, d := range ge.GetAllGraphDesc() {
if d.Tags == nil {
continue
}
for _, t := range d.Tags {
k, v := SplitTag(t)
if k == "" {
continue
}
if _, exists := r[k]; !exists {
r[k] = []string{}
}
if v == "" {
continue
}
//only append if not yet in list
for _, e := range r[k] {
if e == v {
continue
}
}
r[k] = append(r[k], v)
}
}
// sort arrays in map
for k := range r {
sort.Strings(r[k])
}
return r
}
// GetAllGraphDesc returns all graphs by name
func (ge *GraphEngine) GetAllGraphDesc() map[string]*GraphDesc {
r := make(map[string]*GraphDesc)
ge.graphLock.Lock()
defer ge.graphLock.Unlock()
for n, g := range ge.graphs {
r[n] = g
}
return r
}
// GetGraphDescByTag returns the GraphInfo for all Graphs that cointain all of the given tags
func (ge *GraphEngine) GetGraphDescByTag(tags []string) map[string]GraphDesc {
taggroups := [][]string{}
for _, t := range tags {
taggroups = append(taggroups, []string{t})
}
return ge.GetGraphDescByTagExtended(taggroups...)
}
// GetGraphDescByTagExtended returns the GraphInfo for all Graphs that contain at least one tag of each group
func (ge *GraphEngine) GetGraphDescByTagExtended(tagGroups ...[]string) map[string]GraphDesc {
r := make(map[string]GraphDesc)
ge.graphLock.Lock()
defer ge.graphLock.Unlock()
for n, g := range ge.graphs {
if g.HasAtLeastOneTagPerGroup(tagGroups...) {
r[n] = *g
}
}
return r
}
// AddOperator adds an operator to the graph engine
func (ge *GraphEngine) AddOperator(op base.FreepsBaseOperator) {
ge.AddOperators([]base.FreepsBaseOperator{op})
}
// AddOperators adds multiple operators to the graph engine
func (ge *GraphEngine) AddOperators(ops []base.FreepsBaseOperator) {
if ops == nil {
return
}
ge.operatorLock.Lock()
defer ge.operatorLock.Unlock()
for _, op := range ops {
ge.operators[utils.StringToLower(op.GetName())] = op
h := op.GetHook()
if h != nil {
geh, ok := h.(GraphEngineHook)
if !ok {
geh = NewFreepsHookWrapper(h)
}
ge.AddHook(geh)
}
}
}
// HasOperator returns true if this operator is available in the engine
func (ge *GraphEngine) HasOperator(opName string) bool {
ge.operatorLock.Lock()
defer ge.operatorLock.Unlock()
_, exists := ge.operators[utils.StringToLower(opName)]
return exists
}
// GetOperators returns the list of available operators
func (ge *GraphEngine) GetOperators() []string {
ge.operatorLock.Lock()
defer ge.operatorLock.Unlock()
r := make([]string, 0, len(ge.operators))
for _, op := range ge.operators {
r = append(r, op.GetName())
}
return r
}
// GetOperator returns the operator with the given name
func (ge *GraphEngine) GetOperator(opName string) base.FreepsBaseOperator {
ge.operatorLock.Lock()
defer ge.operatorLock.Unlock()
op, exists := ge.operators[utils.StringToLower(opName)]
if !exists {
return nil
}
return op
}
// AddHook adds a hook to the graph engine
func (ge *GraphEngine) AddHook(h GraphEngineHook) {
ge.hookMapLock.Lock()
defer ge.hookMapLock.Unlock()
ge.hooks[h.GetName()] = h
}
// TriggerOnExecuteHooks adds a hook to the graph engine
func (ge *GraphEngine) TriggerOnExecuteHooks(ctx *base.Context, graphName string, mainArgs map[string]string, mainInput *base.OperatorIO) {
hooks := ge.getHookMapCopy()
for name, h := range hooks {
fh, ok := h.(FreepsExecutionHook)
if !ok {
continue
}
err := fh.OnExecute(ctx, graphName, mainArgs, mainInput)
if err != nil {
upErr := fmt.Errorf("Execution of Hook \"%v\" failed with error: %v", name, err.Error())
ge.SetSystemAlert(ctx, "ExecuteHook"+name, "system", 3, upErr, &ge.config.AlertDuration)
}
}
}
// TriggerOnExecutionFinishedHooks executes hooks when Execution of a graph finishes
func (ge *GraphEngine) TriggerOnExecutionFinishedHooks(ctx *base.Context, graphName string, mainArgs map[string]string, mainInput *base.OperatorIO) {
hooks := ge.getHookMapCopy()
for name, h := range hooks {
fh, ok := h.(FreepsExecutionHook)
if !ok {
continue
}
err := fh.OnExecutionFinished(ctx, graphName, mainArgs, mainInput)
if err != nil {
upErr := fmt.Errorf("Execution of FinishedHook \"%v\" failed with error: %v", name, err.Error())
ge.SetSystemAlert(ctx, "ExecutionFinishedHook"+name, "system", 3, upErr, &ge.config.AlertDuration)
}
}
}
// TriggerOnExecuteOperationHooks executes hooks immediately after an operation was executed
func (ge *GraphEngine) TriggerOnExecuteOperationHooks(ctx *base.Context, input *base.OperatorIO, output *base.OperatorIO, graphName string, od *GraphOperationDesc) {
hooks := ge.getHookMapCopy()
for name, h := range hooks {
fh, ok := h.(FreepsExecutionHook)
if !ok {
continue
}
err := fh.OnExecuteOperation(ctx, input, output, graphName, od)
if err != nil {
upErr := fmt.Errorf("Execution of FailedHook \"%v\" failed with error: %v", name, err.Error())
ge.SetSystemAlert(ctx, "ExecutionErrorHook"+name, "system", 3, upErr, &ge.config.AlertDuration)
}
}
}
// TriggerGraphChangedHooks triggers hooks whenever a graph was added or removed
func (ge *GraphEngine) TriggerGraphChangedHooks(ctx *base.Context, addedGraphNames []string, removedGraphNames []string) {
hooks := ge.getHookMapCopy()
for name, h := range hooks {
fh, ok := h.(FreepsGraphChangedHook)
if !ok {
continue
}
err := fh.OnGraphChanged(ctx, addedGraphNames, removedGraphNames)
if err != nil {
upErr := fmt.Errorf("Execution of GraphChangedHook \"%v\" failed with error: %v", name, err.Error())
ge.SetSystemAlert(ctx, "GraphChangedHook"+name, "system", 3, upErr, &ge.config.AlertDuration)
}
}
}
// SetSystemAlert triggers the hooks with the same name
func (ge *GraphEngine) SetSystemAlert(ctx *base.Context, name string, category string, severity int, err error, expiresIn *time.Duration) {
hooks := ge.getHookMapCopy()
for _, h := range hooks {
fh, ok := h.(FreepsAlertHook)
if !ok {
continue
}
err := fh.OnSystemAlert(ctx, name, category, severity, err, expiresIn)
if err != nil {
ctx.GetLogger().Errorf("Couldn't set SystemAlert: %v", err.Error())
}
}
}
// ResetSystemAlert triggers the hooks with the same name
func (ge *GraphEngine) ResetSystemAlert(ctx *base.Context, name string, category string) {
hooks := ge.getHookMapCopy()
for _, h := range hooks {
fh, ok := h.(FreepsAlertHook)
if !ok {
continue
}
err := fh.OnResetSystemAlert(ctx, name, category)
if err != nil {
ctx.GetLogger().Errorf("Couldn't reset SystemAlert: %v", err.Error())
}
}
}
// AddGraph adds a graph from an external source and stores it on disk, after checking if the graph is valid
func (ge *GraphEngine) AddGraph(ctx *base.Context, graphID string, gd GraphDesc, overwrite bool) error {
// check if graph is valid
_, err := gd.GetCompleteDesc(graphID, ge)
if err != nil {
return err
}
defer ge.TriggerGraphChangedHooks(ctx, []string{graphID}, []string{})
ge.graphLock.Lock()
defer ge.graphLock.Unlock()
return ge.addGraphUnderLock(ctx, graphID, gd, true, overwrite)
}
func (ge *GraphEngine) addGraphUnderLock(ctx *base.Context, graphName string, gd GraphDesc, writeToDisk bool, overwrite bool) error {
oldGraph, ok := ge.graphs[graphName]
if ok {
if overwrite {
log.Warnf("Graph \"%v\" already exists (Source \"%v\"), overwriting with new source \"%v\"", graphName, oldGraph.Source, gd.Source)
} else {
return fmt.Errorf("Graph \"%v\" already exists, please explicitly delete the graph to continue", graphName)
}
}
if gd.Tags == nil {
gd.Tags = []string{}
}
if writeToDisk {
fileName := "graphs/" + graphName + ".json"
err := ge.cr.WriteObjectToFile(gd, fileName)
if err != nil {
ge.SetSystemAlert(ctx, "GraphWriteError", "system", 2, err, &ge.config.AlertDuration)
return fmt.Errorf("Error writing graphs to file %s: %s", fileName, err.Error())
}
}
ge.graphs[graphName] = &gd
return nil
}
// DeleteGraph removes a graph from the engine and from the storage
func (ge *GraphEngine) DeleteGraph(ctx *base.Context, graphID string) (*GraphDesc, error) {
if graphID == "" {
return nil, errors.New("No name given")
}
defer ge.TriggerGraphChangedHooks(ctx, []string{}, []string{graphID})
ge.graphLock.Lock()
defer ge.graphLock.Unlock()
/* remove the graph from memory*/
deletedGraph, exists := ge.graphs[graphID]
if !exists {
return nil, errors.New("Graph not found")
}
delete(ge.graphs, graphID)
fname := "graphs/" + graphID + ".json"
err := ge.cr.RemoveFile(fname)
return deletedGraph, err
}
// StartListening starts all listening operators
func (ge *GraphEngine) StartListening(ctx *base.Context) {
defer ge.TriggerGraphChangedHooks(ctx, []string{}, []string{})
ge.operatorLock.Lock()
defer ge.operatorLock.Unlock()
for _, op := range ge.operators {
if op != nil {
op.StartListening(ctx)
}
}
}
// Shutdown should be called for graceful shutdown
func (ge *GraphEngine) Shutdown(ctx *base.Context) {
ge.operatorLock.Lock()
defer ge.operatorLock.Unlock()
for _, op := range ge.operators {
if op != nil {
op.Shutdown(ctx)
}
}
}