-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aggregator Code Bug Fixes & Example In Go Code #31
Open
n-is
wants to merge
2
commits into
project-flogo:master
Choose a base branch
from
n-is:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/project-flogo/core/api" | ||
"github.com/project-flogo/core/engine" | ||
) | ||
|
||
func main() { | ||
|
||
app := StreamTest() | ||
|
||
e, err := api.NewEngine(app) | ||
|
||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return | ||
} | ||
|
||
engine.RunEngine(e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/project-flogo/contrib/activity/log" | ||
"github.com/project-flogo/contrib/activity/rest" | ||
restTrig "github.com/project-flogo/contrib/trigger/rest" | ||
"github.com/project-flogo/contrib/trigger/timer" | ||
"github.com/project-flogo/core/activity" | ||
"github.com/project-flogo/core/api" | ||
"github.com/project-flogo/core/data/coerce" | ||
"github.com/project-flogo/stream/activity/aggregate" | ||
) | ||
|
||
// Stores all the activities of this app | ||
var actStream map[string]activity.Activity | ||
|
||
func StreamTest() *api.App { | ||
app := api.NewApp() | ||
|
||
// REST Trigger to receive HTTP message | ||
trg := app.NewTrigger(&restTrig.Trigger{}, &restTrig.Settings{Port: 9090}) | ||
h, _ := trg.NewHandler(&restTrig.HandlerSettings{Method: "POST", Path: "/stream"}) | ||
h.NewAction(runActivitiesStream) | ||
|
||
// Timer Trigger to send HTTP message repeatedly | ||
tmrTrg := app.NewTrigger(&timer.Trigger{}, nil) | ||
tmrHandler, _ := tmrTrg.NewHandler(&timer.HandlerSettings{StartInterval: "2s", RepeatInterval: "1s"}) | ||
tmrHandler.NewAction(runTimerActivitiesStream) | ||
|
||
// A REST Activity to send data to Uri | ||
stng := &rest.Settings{Method: "POST", Uri: "http://localhost:9090/stream", | ||
Headers: map[string]string{"Accept": "application/json"}} | ||
restAct, _ := api.NewActivity(&rest.Activity{}, stng) | ||
|
||
// A log Activity for logging | ||
logAct, _ := api.NewActivity(&log.Activity{}) | ||
|
||
// Aggregate Activities to aggregate data obtained at 9090 port | ||
aggStng1 := &aggregate.Settings{Function: "accumulate", WindowType: "tumbling", | ||
WindowSize: 3, ProceedOnlyOnEmit: true} | ||
aggAct1, _ := api.NewActivity(&aggregate.Activity{}, aggStng1) | ||
|
||
aggStng2 := &aggregate.Settings{Function: "avg", WindowType: "tumbling", | ||
WindowSize: 3, ProceedOnlyOnEmit: false} | ||
aggAct2, _ := api.NewActivity(&aggregate.Activity{}, aggStng2) | ||
|
||
//Store in map to avoid activity instance recreation | ||
actStream = map[string]activity.Activity{} | ||
actStream["log"] = logAct | ||
actStream["rest"] = restAct | ||
actStream["agg1"] = aggAct1 | ||
actStream["agg2"] = aggAct2 | ||
|
||
return app | ||
} | ||
|
||
func runActivitiesStream(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error) { | ||
|
||
// Get REST Trigger Output | ||
trgOut := &restTrig.Output{} | ||
trgOut.FromMap(inputs) | ||
|
||
// Coerce the required outputs to string | ||
content, _ := coerce.ToString(trgOut.Content) | ||
|
||
response := handleStreamInput(content) | ||
|
||
reply := &restTrig.Reply{Code: 200, Data: response} | ||
return reply.ToMap(), nil | ||
} | ||
|
||
type inputStreamData struct { | ||
Value float64 `json:"value"` | ||
} | ||
|
||
func handleStreamInput(input string) map[string]interface{} { | ||
|
||
var in inputStreamData | ||
err := json.Unmarshal([]byte(input), &in) | ||
|
||
if err != nil { | ||
fmt.Println("Hello, Some problem occured during json unmarshaling") | ||
return nil | ||
} | ||
|
||
response := make(map[string]interface{}) | ||
response["value"] = in.Value | ||
|
||
output, err := api.EvalActivity(actStream["agg1"], &aggregate.Input{Value: in.Value}) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
if output["report"] == true { | ||
fmt.Println("[@9090]$ Accumulator Output : ", output["result"]) | ||
} | ||
|
||
output, err = api.EvalActivity(actStream["agg2"], &aggregate.Input{Value: in.Value}) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
if output["report"] == true { | ||
fmt.Printf("[@9090]$ Average Output : %0.4f\n", output["result"]) | ||
fmt.Println() | ||
} | ||
|
||
return response | ||
} | ||
|
||
var num float64 = 0 | ||
|
||
func runTimerActivitiesStream(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error) { | ||
|
||
num += 0.4 | ||
input := fmt.Sprintf("{\"value\": %f }", num) | ||
|
||
_, err := api.EvalActivity(actStream["rest"], | ||
&rest.Input{Content: input}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ctx.GetSharedTempData() should work. Do you have the panic that this causes?
This call should end up in state.go#107 which should create a map if one doesn't exist.