Skip to content

Commit

Permalink
resolving TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Jan 8, 2016
1 parent cb41091 commit 477c0a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 71 deletions.
27 changes: 0 additions & 27 deletions render/detailed/detailed_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,33 +238,6 @@ func parents(r report.Report, n render.RenderableNode) (result []Parent) {
return result
}

// nodeTopology (optimistically) tells us which origin topology an ID belongs
// to.
func nodeTopology(r report.Report, id string) (report.Topology, string, report.Node, bool) {
// TODO(paulbellamy): This is a duplicate of report.Topologies. Needs a
// cleanup/refactor. It's duplicated because Topology structs are not
// comparable in go, so we need an identity. Better yet, replace this with a
// polymorphism on either the topology, or the node type.

topologies := map[string]report.Topology{
"endpoint": r.Endpoint,
"address": r.Address,
"process": r.Process,
"container": r.Container,
"container_image": r.ContainerImage,
"pod": r.Pod,
"service": r.Service,
"host": r.Host,
"overlay": r.Overlay,
}
for tName, t := range topologies {
if n, ok := t.Nodes[id]; ok {
return t, tName, n, true
}
}
return report.Topology{}, "", report.Node{}, false
}

func processNodeSummary(nmd report.Node) NodeSummary {
var (
id string
Expand Down
3 changes: 1 addition & 2 deletions render/detailed/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/weaveworks/scope/report"
)

// TODO(paulbellamy): append docker labels to the metadata for containers and container images
var (
processNodeMetadata = renderMetadata([]MetadataRow{
{ID: process.PPID, Label: "Parent PID"},
Expand All @@ -32,7 +31,7 @@ var (
}, getDockerLabelRows)
containerImageNodeMetadata = renderMetadata([]MetadataRow{
{ID: docker.ImageID, Label: "Image ID"},
})
}, getDockerLabelRows)
podNodeMetadata = renderMetadata([]MetadataRow{
{ID: kubernetes.PodID, Label: "ID"},
{ID: kubernetes.Namespace, Label: "Namespace"},
Expand Down
62 changes: 23 additions & 39 deletions render/detailed/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package detailed
import (
"encoding/json"
"math"
"time"

"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/probe/host"
Expand All @@ -19,47 +18,32 @@ const (
)

type MetricRow struct {
ID string `json:"id"`
Label string `json:"label"`
Format string `json:"format,omitempty"`
Group string `json:"group,omitempty"`
Value float64 `json:"value"`
*report.Metric
ID string
Label string
Format string
Group string
Value float64
Metric *report.Metric
}

// MarshalJSON marshals this MetricRow to json. It takes the basic Metric
// rendering, then adds some row-specific fields.
func (m MetricRow) MarshalJSON() ([]byte, error) {
// TODO(paulbellamy): This is a total hack to workaround go taking the
// MarshalJSON method from the embedded report.Metric
samples := []report.Sample{}
if m.Samples != nil {
m.Samples.Reverse().ForEach(func(s interface{}) {
samples = append(samples, s.(report.Sample))
})
}
j := map[string]interface{}{
"id": m.ID,
"label": m.Label,
"value": m.Value,
"samples": samples,
"max": m.Max,
"min": m.Min,
"first": renderTime(m.First),
"last": renderTime(m.Last),
}
if m.Format != "" {
j["format"] = m.Format
}
if m.Group != "" {
j["group"] = m.Group
}
return json.Marshal(j)
}

func renderTime(t time.Time) string {
if t.IsZero() {
return ""
}
return t.Format(time.RFC3339Nano)
return json.Marshal(struct {
ID string `json:"id"`
Label string `json:"label"`
Format string `json:"format,omitempty"`
Group string `json:"group,omitempty"`
Value float64 `json:"value"`
report.WireMetrics
}{
ID: m.ID,
Label: m.Label,
Format: m.Format,
Group: m.Group,
Value: m.Value,
WireMetrics: m.Metric.ToIntermediate(),
})
}

func metricRow(id, label string, metric report.Metric, format, group string) MetricRow {
Expand Down
8 changes: 5 additions & 3 deletions report/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ func parseTime(s string) time.Time {
return t
}

func (m Metric) toIntermediate() WireMetrics {
// ToIntermediate converts the metric to a representation suitable
// for serialization.
func (m Metric) ToIntermediate() WireMetrics {
samples := []Sample{}
if m.Samples != nil {
m.Samples.Reverse().ForEach(func(s interface{}) {
Expand Down Expand Up @@ -268,7 +270,7 @@ func (m WireMetrics) fromIntermediate() Metric {
// MarshalJSON implements json.Marshaller
func (m Metric) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
in := m.toIntermediate()
in := m.ToIntermediate()
err := json.NewEncoder(&buf).Encode(in)
return buf.Bytes(), err
}
Expand All @@ -286,7 +288,7 @@ func (m *Metric) UnmarshalJSON(input []byte) error {
// GobEncode implements gob.Marshaller
func (m Metric) GobEncode() ([]byte, error) {
buf := bytes.Buffer{}
err := gob.NewEncoder(&buf).Encode(m.toIntermediate())
err := gob.NewEncoder(&buf).Encode(m.ToIntermediate())
return buf.Bytes(), err
}

Expand Down

0 comments on commit 477c0a8

Please sign in to comment.