-
Notifications
You must be signed in to change notification settings - Fork 712
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce renderers; allow them to recurse.
- Loading branch information
Tom Wilkie
committed
Jun 15, 2015
1 parent
d71f107
commit 23bdae3
Showing
6 changed files
with
125 additions
and
52 deletions.
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
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
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,51 @@ | ||
package render | ||
|
||
import ( | ||
"github.com/weaveworks/scope/report" | ||
) | ||
|
||
// Renderer is something that can render a report to a set of RenderableNodes | ||
type Renderer interface { | ||
Render(report.Report) report.RenderableNodes | ||
EdgeMetadata(rpt report.Report, localID, remoteID string) report.AggregateMetadata | ||
} | ||
|
||
// Reduce renderer is a Renderer which merges together the output of several | ||
// other renderers | ||
type Reduce []Renderer | ||
|
||
// Render produces a set of RenderableNodes given a Report | ||
func (r Reduce) Render(rpt report.Report) report.RenderableNodes { | ||
result := report.RenderableNodes{} | ||
for _, renderer := range r { | ||
result.Merge(renderer.Render(rpt)) | ||
} | ||
return result | ||
} | ||
|
||
// EdgeMetadata produces an AggregateMetadata for a given edge | ||
func (r Reduce) EdgeMetadata(rpt report.Report, localID, remoteID string) report.AggregateMetadata { | ||
metadata := report.AggregateMetadata{} | ||
for _, renderer := range r { | ||
metadata.Merge(renderer.EdgeMetadata(rpt, localID, remoteID)) | ||
} | ||
return metadata | ||
} | ||
|
||
// Map is a Renderer which produces a set of RendererNodes by using a | ||
// Mapper functions and topology selector. | ||
type Map struct { | ||
Selector report.TopologySelector | ||
Mapper report.MapFunc | ||
Pseudo report.PseudoFunc | ||
} | ||
|
||
// Render produces a set of RenderableNodes given a Report | ||
func (m Map) Render(rpt report.Report) report.RenderableNodes { | ||
return m.Selector(rpt).RenderBy(m.Mapper, m.Pseudo) | ||
} | ||
|
||
// EdgeMetadata produces an AggregateMetadata for a given edge | ||
func (m Map) EdgeMetadata(rpt report.Report, localID, remoteID string) report.AggregateMetadata { | ||
return m.Selector(rpt).EdgeMetadata(m.Mapper, localID, remoteID).Transform() | ||
} |
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,47 @@ | ||
package render_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/weaveworks/scope/render" | ||
"github.com/weaveworks/scope/report" | ||
) | ||
|
||
type mockRenderer struct { | ||
nodes report.RenderableNodes | ||
amd report.AggregateMetadata | ||
} | ||
|
||
func (m mockRenderer) Render(rpt report.Report) report.RenderableNodes { return m.nodes } | ||
func (m mockRenderer) EdgeMetadata(rpt report.Report, localID, remoteID string) report.AggregateMetadata { | ||
return m.amd | ||
} | ||
|
||
func TestReduceRender(t *testing.T) { | ||
renderer := render.Reduce{ | ||
mockRenderer{nodes: report.RenderableNodes{"foo": {ID: "foo"}}}, | ||
mockRenderer{nodes: report.RenderableNodes{"bar": {ID: "bar"}}}, | ||
} | ||
|
||
want := report.RenderableNodes{"foo": {ID: "foo"}, "bar": {ID: "bar"}} | ||
have := renderer.Render(report.MakeReport()) | ||
|
||
if !reflect.DeepEqual(want, have) { | ||
t.Errorf("want %+v, have %+v", want, have) | ||
} | ||
} | ||
|
||
func TestReduceEdge(t *testing.T) { | ||
renderer := render.Reduce{ | ||
mockRenderer{amd: report.AggregateMetadata{"foo": 1}}, | ||
mockRenderer{amd: report.AggregateMetadata{"bar": 2}}, | ||
} | ||
|
||
want := report.AggregateMetadata{"foo": 1, "bar": 2} | ||
have := renderer.EdgeMetadata(report.MakeReport(), "", "") | ||
|
||
if !reflect.DeepEqual(want, have) { | ||
t.Errorf("want %+v, have %+v", want, have) | ||
} | ||
} |