Skip to content
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

Add flow API support #2094

Merged
merged 26 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
76e3054
Flow API
mattdurham Aug 30, 2022
85bee3e
Fix #
mattdurham Aug 30, 2022
cb5b559
Fix spacing
mattdurham Aug 30, 2022
481cfe2
Fix spacing part 2
mattdurham Aug 30, 2022
75f3311
use predefined edges instead of calculating it again
mattdurham Sep 2, 2022
187491a
Use original edges
mattdurham Sep 2, 2022
e28a3e7
Rename and simplify converting to json
mattdurham Sep 2, 2022
2d73709
Rewrite to a longer but more easily understandable format.
mattdurham Sep 3, 2022
739a376
Unexport makeNumberKind
mattdurham Sep 3, 2022
af9249c
pr feedback
mattdurham Sep 7, 2022
1e88938
remove struct field and simplify
mattdurham Sep 8, 2022
1051fa1
simplify tests and fix gzip handling issue
mattdurham Sep 8, 2022
ba6a857
fix linting
mattdurham Sep 8, 2022
9e279c9
fix linting with receiver name
mattdurham Sep 8, 2022
e4f24a5
Simplified based on PR feedback
mattdurham Sep 13, 2022
b358c8e
Rename RiverValue to RiverField
mattdurham Sep 13, 2022
0ea4c7f
Merge branch 'main' into flow_api_pr
mattdurham Sep 14, 2022
d45ecff
PR feedback on changing exported fields to unexported and various oth…
mattdurham Sep 15, 2022
fdfe894
Fix linting for json
mattdurham Sep 15, 2022
b67716e
Changed json.RawMessage to not be a pointer and change name of missed…
mattdurham Sep 15, 2022
5790e1e
Update pkg/river/encoding/block.go
mattdurham Sep 16, 2022
1eafb8e
Update pkg/river/internal/value/number_value.go
mattdurham Sep 16, 2022
4340ffd
Update web/api/api.go
mattdurham Sep 16, 2022
aeac4ae
Update web/api/api.go
mattdurham Sep 16, 2022
882e7f5
Allow blocks to reference other arrays of blocks.
mattdurham Sep 16, 2022
67e24bb
Merge from remote
mattdurham Sep 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PR feedback on changing exported fields to unexported and various oth…
…er improvements.,
  • Loading branch information
mattdurham committed Sep 15, 2022
commit d45ecff8e3afcb1dc36c289daa831231d3c3c99b
34 changes: 16 additions & 18 deletions pkg/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ package flow

import (
"context"
"encoding/json"
"fmt"
"io"
"sync"
"time"

"github.com/grafana/agent/pkg/river/encoding"

"github.com/go-kit/log/level"
"github.com/grafana/agent/pkg/flow/internal/controller"
"github.com/grafana/agent/pkg/flow/internal/dag"
Expand Down Expand Up @@ -253,12 +252,10 @@ func newFromNode(cn *controller.ComponentNode, edges []dag.Edge) *ComponentInfo
}
h := cn.CurrentHealth()
ci := &ComponentInfo{
Label: cn.Label(),
ID: cn.NodeID(),
Field: encoding.Field{
Name: cn.ComponentName(),
Type: "block",
},
Label: cn.Label(),
ID: cn.NodeID(),
Name: cn.ComponentName(),
Type: "block",
References: references,
ReferencedBy: referencedBy,
Health: &ComponentHealth{
Expand All @@ -272,16 +269,17 @@ func newFromNode(cn *controller.ComponentNode, edges []dag.Edge) *ComponentInfo

// ComponentInfo represents a component in river.
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
type ComponentInfo struct {
encoding.Field `json:",omitempty"`
ID string `json:"id,omitempty"`
Label string `json:"label,omitempty"`
References []string `json:"referencesTo"`
ReferencedBy []string `json:"referencedBy"`
Health *ComponentHealth `json:"health"`
Original string `json:"original"`
Arguments interface{} `json:"arguments,omitempty"`
Exports interface{} `json:"exports,omitempty"`
DebugInfo interface{} `json:"debugInfo,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
Label string `json:"label,omitempty"`
References []string `json:"referencesTo"`
ReferencedBy []string `json:"referencedBy"`
Health *ComponentHealth `json:"health"`
Original string `json:"original"`
Arguments *json.RawMessage `json:"arguments,omitempty"`
Exports *json.RawMessage `json:"exports,omitempty"`
DebugInfo *json.RawMessage `json:"debugInfo,omitempty"`
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
}

// ComponentHealth represents the health of a component.
Expand Down
18 changes: 6 additions & 12 deletions pkg/flow/flow_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,23 @@ func (f *Flow) ComponentJSON(w io.Writer, ci *ComponentInfo) error {
}

var err error
args, err := encoding.ConvertRiverBlock(foundComponent.Arguments())
args, err := encoding.ConvertRiverBodyToJson(foundComponent.Arguments())
if err != nil {
return err
}
if len(args) > 0 {
ci.Arguments = args
}
ci.Arguments = args

exports, err := encoding.ConvertRiverBlock(foundComponent.Exports())
exports, err := encoding.ConvertRiverBodyToJson(foundComponent.Exports())
if err != nil {
return err
}
if len(exports) > 0 {
ci.Exports = exports
}
ci.Exports = exports

debugInfo, err := encoding.ConvertRiverBlock(foundComponent.DebugInfo())
debugInfo, err := encoding.ConvertRiverBodyToJson(foundComponent.DebugInfo())
if err != nil {
return err
}
if len(debugInfo) > 0 {
ci.DebugInfo = debugInfo
}
ci.DebugInfo = debugInfo

bb, err := json.Marshal(ci)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions pkg/flow/internal/controller/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func (l *Loader) Apply(parentScope *vm.Scope, blocks []*ast.BlockStmt) diag.Diag
diags = append(diags, multierrToDiags(err)...)
return diags
}
// Copy the original graph
// Copy the original graph, this is so we can have access to the original graph for things like displaying a UI or
// debug information.
l.originalGraph = newGraph.Clone()
// Perform a transitive reduction of the graph to clean it up.
dag.Reduce(&newGraph)
Expand Down Expand Up @@ -210,7 +211,8 @@ func (l *Loader) Graph() *dag.Graph {
return l.graph.Clone()
}

// OriginalGraph returns a copy of the graph before Reduce was called.
// OriginalGraph returns a copy of the graph before Reduce was called. This can be used if you want to show a UI of the
// original graph before the reduce function was called.
func (l *Loader) OriginalGraph() *dag.Graph {
l.mut.RLock()
defer l.mut.RUnlock()
Expand Down
19 changes: 9 additions & 10 deletions pkg/river/encoding/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,31 @@ import (
"github.com/grafana/agent/pkg/river/internal/value"
)

// ArrayField represents an array node.
type ArrayField struct {
// arrayField represents an array node.
type arrayField struct {
Type string `json:"type"`
Value []RiverField `json:"value,omitempty"`
Value []riverField `json:"value,omitempty"`
}

func newArray(val value.Value) (*ArrayField, error) {
af := &ArrayField{Type: "array"}
func newRiverArray(val value.Value) (*arrayField, error) {
af := &arrayField{Type: "array"}
return af, af.convertArray(val)
}

func (af *ArrayField) hasValue() bool {
func (af *arrayField) hasValue() bool {
if af == nil {
return false
}
return len(af.Value) > 0
}

func (af *ArrayField) convertArray(val value.Value) error {
if !isArray(val) {
func (af *arrayField) convertArray(val value.Value) error {
if !isRiverArray(val) {
return fmt.Errorf("convertArray requires a field that is an slice/array got %T", val.Interface())
}
af.Type = "array"
for i := 0; i < val.Len(); i++ {
arrEle := val.Index(i).Interface()
arrVal := value.Encode(arrEle)
arrVal := val.Index(i)

rv, err := convertRiverValue(arrVal)
if err != nil {
Expand Down
180 changes: 176 additions & 4 deletions pkg/river/encoding/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,54 @@ func TestSimpleArray(t *testing.T) {
for i := 0; i < 10; i++ {
intArr[i] = i
}
reqString := `{"type":"array","value":[{"type":"number","value":0},{"type":"number","value":1},{"type":"number","value":2},{"type":"number","value":3},{"type":"number","value":4},{"type":"number","value":5},{"type":"number","value":6},{"type":"number","value":7},{"type":"number","value":8},{"type":"number","value":9}]}`
arrF, err := newArray(value.Encode(intArr))
reqString := `
{
"type": "array",
"value": [
{
"type": "number",
"value": 0
},
{
"type": "number",
"value": 1
},
{
"type": "number",
"value": 2
},
{
"type": "number",
"value": 3
},
{
"type": "number",
"value": 4
},
{
"type": "number",
"value": 5
},
{
"type": "number",
"value": 6
},
{
"type": "number",
"value": 7
},
{
"type": "number",
"value": 8
},
{
"type": "number",
"value": 9
}
]
}
`
arrF, err := newRiverArray(value.Encode(intArr))
require.NoError(t, err)
bb, err := json.Marshal(arrF)
require.NoError(t, err)
Expand All @@ -30,8 +76,134 @@ func TestStructArray(t *testing.T) {
intArr[i] = &sa{Age: i}
}

reqString := `{"type":"array","value":[{"type":"object","value":[{"value":{"type":"number","value":0},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":1},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":2},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":3},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":4},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":5},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":6},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":7},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":8},"key":"age"}]},{"type":"object","value":[{"value":{"type":"number","value":9},"key":"age"}]}]}`
arrF, err := newArray(value.Encode(intArr))
reqString := `
{
"type": "array",
"value": [
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 0
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 1
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 2
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 3
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 4
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 5
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 6
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 7
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 8
},
"key": "age"
}
]
},
{
"type": "object",
"value": [
{
"value": {
"type": "number",
"value": 9
},
"key": "age"
}
]
}
]
}
`
arrF, err := newRiverArray(value.Encode(intArr))
require.NoError(t, err)
bb, err := json.Marshal(arrF)
require.NoError(t, err)
Expand Down
Loading