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
  • Loading branch information
mattdurham committed Sep 7, 2022
commit af9249c84b2f6f739ed7603f51d943191fe867bf
19 changes: 6 additions & 13 deletions pkg/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ func (c *Flow) LoadFile(file *File) error {
}

// ComponentInfos returns the component infos.
func (c *Flow) ComponentInfos() []*ComponentField {
func (c *Flow) ComponentInfos() []*ComponentInfo {
c.loadMut.RLock()
defer c.loadMut.RUnlock()

cns := c.loader.Components()
infos := make([]*ComponentField, len(cns))
infos := make([]*ComponentInfo, len(cns))
edges := c.loader.Graph().NonTransitiveEdges()
for i, com := range cns {
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
nn := newFromNode(com, edges)
Expand All @@ -235,7 +235,7 @@ func (c *Flow) Close() error {
return c.sched.Close()
}

func newFromNode(cn *controller.ComponentNode, edges []dag.Edge) *ComponentField {
func newFromNode(cn *controller.ComponentNode, edges []dag.Edge) *ComponentInfo {
references := make([]string, 0)
referencedBy := make([]string, 0)
for _, e := range edges {
Expand All @@ -246,7 +246,7 @@ func newFromNode(cn *controller.ComponentNode, edges []dag.Edge) *ComponentField
}
}
h := cn.CurrentHealth()
ci := &ComponentField{
ci := &ComponentInfo{
Label: cn.Label(),
ID: cn.NodeID(),
Field: encoding.Field{
Expand All @@ -264,15 +264,8 @@ func newFromNode(cn *controller.ComponentNode, edges []dag.Edge) *ComponentField
return ci
}

// Health contains information on the health of a component.
type Health struct {
State string `json:"state"`
Message string `json:"message"`
UpdateTime time.Time `json:"updatedTime"`
}

// ComponentField represents a component in river.
type ComponentField struct {
// 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"`
Expand Down
12 changes: 6 additions & 6 deletions pkg/flow/flow_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c *Flow) configBytes(w io.Writer, debugInfo bool) (n int64, err error) {
}

// ComponentJSON returns the json representation of the flow component.
func (c *Flow) ComponentJSON(w io.Writer, ci *ComponentField) error {
func (c *Flow) ComponentJSON(w io.Writer, ci *ComponentInfo) error {
c.loadMut.RLock()
defer c.loadMut.RUnlock()

Expand All @@ -109,35 +109,35 @@ func (c *Flow) ComponentJSON(w io.Writer, ci *ComponentField) error {
}
}
if foundComponent == nil {
return fmt.Errorf("unable to find component named %s", ci.ID)
return fmt.Errorf("unable to find component named %q", ci.ID)
}

var err error
args, err := encoding.ConvertComponentChild(foundComponent.Arguments())
args, err := encoding.ConvertRiverBlock(foundComponent.Arguments())
if err != nil {
return err
}
if args != nil && !reflect.ValueOf(args).IsNil() {
ci.Arguments = args
}

exports, err := encoding.ConvertComponentChild(foundComponent.Exports())
exports, err := encoding.ConvertRiverBlock(foundComponent.Exports())
if err != nil {
return err
}
if exports != nil && !reflect.ValueOf(exports).IsNil() {
ci.Exports = exports
}

debugInfo, err := encoding.ConvertComponentChild(foundComponent.DebugInfo())
debugInfo, err := encoding.ConvertRiverBlock(foundComponent.DebugInfo())
if err != nil {
return err
}
if debugInfo != nil && !reflect.ValueOf(debugInfo).IsNil() {
ci.DebugInfo = debugInfo
}

bb, err := json.MarshalIndent(ci, "", " ")
bb, err := json.Marshal(ci)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/flow/internal/controller/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (cn *ComponentNode) ID() ComponentID { return cn.id }
// Label returns the label for the block or "" if none was specified.
func (cn *ComponentNode) Label() string { return cn.label }

// NodeType returns the nodes type, ie `local.file.test` returns `local.file`.
// NodeType returns the component's type, i.e. `local.file.test` returns `local.file`.
func (cn *ComponentNode) NodeType() string { return cn.nodeType }

// NodeID implements dag.Node and returns the unique ID for this node. The
Expand Down
13 changes: 5 additions & 8 deletions pkg/river/encoding/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
const attr = "attr"
const object = "object"

// ConvertComponentChild is used to convertBase arguments, exports, health and debuginfo.
func ConvertComponentChild(input interface{}) ([]interface{}, error) {
// ConvertRiverBlock is used to convertBase arguments, exports, health and debuginfo.
func ConvertRiverBlock(input interface{}) ([]interface{}, error) {
if input == nil {
return nil, nil
}
val := value.Encode(input)
fields := make([]interface{}, 0)
var fields []interface{}
rt := rivertags.Get(val.Reflect().Type())
for _, t := range rt {
fieldValue := val.Reflect().FieldByIndex(t.Index)
Expand Down Expand Up @@ -55,13 +55,10 @@ func ConvertComponentChild(input interface{}) ([]interface{}, error) {
}
}
}
if len(fields) == 0 {
return nil, nil
}
return fields, nil
}

func isValue(val value.Value) bool {
func isFieldValue(val value.Value) bool {
switch val.Type() {
case value.TypeNull, value.TypeNumber, value.TypeString, value.TypeBool, value.TypeFunction, value.TypeCapsule:
return true
Expand Down Expand Up @@ -131,7 +128,7 @@ func isStruct(val value.Value) bool {
}

func convertRiverValue(val value.Value) (vf *ValueField, af *ArrayField, mf *MapField, sf *StructField, err error) {
if isValue(val) {
if isFieldValue(val) {
vf, err = convertValue(val)
return
} else if isArray(val) {
Expand Down
4 changes: 2 additions & 2 deletions web/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (f *FlowAPI) RegisterRoutes(urlPrefix string, r *mux.Router) {
func (f *FlowAPI) listComponentsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
infos := f.flow.ComponentInfos()
bb, err := json.MarshalIndent(infos, "", " ")
bb, err := json.Marshal(infos)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -65,7 +65,7 @@ func (f *FlowAPI) listComponentHandler() http.HandlerFunc {
}

// JSON returns the json representation of ComponentInfoDetailed.
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
func (f *FlowAPI) JSON(c *flow.ComponentField) (bytes.Buffer, error) {
func (f *FlowAPI) JSON(c *flow.ComponentInfo) (bytes.Buffer, error) {
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
mattdurham marked this conversation as resolved.
Show resolved Hide resolved
var buf bytes.Buffer
err := f.flow.ComponentJSON(&buf, c)
if err != nil {
Expand Down