mirror of
https://github.com/weaveworks/scope.git
synced 2026-08-19 04:16:21 +00:00
Merge pull request #916 from weaveworks/854-reports-codec
Improve codec performance
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
package render_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
|
||||
"github.com/weaveworks/scope/render"
|
||||
"github.com/weaveworks/scope/report"
|
||||
"github.com/weaveworks/scope/test/fixture"
|
||||
@@ -94,6 +95,6 @@ func loadReport() (report.Report, error) {
|
||||
return rpt, err
|
||||
}
|
||||
rpt := report.MakeReport()
|
||||
err = json.Unmarshal(b, &rpt)
|
||||
err = codec.NewDecoderBytes(b, &codec.JsonHandle{}).Decode(&rpt)
|
||||
return rpt, err
|
||||
}
|
||||
|
||||
+36
-10
@@ -1,10 +1,11 @@
|
||||
package detailed
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
|
||||
"github.com/weaveworks/scope/probe/docker"
|
||||
"github.com/weaveworks/scope/probe/host"
|
||||
"github.com/weaveworks/scope/probe/kubernetes"
|
||||
@@ -103,6 +104,7 @@ func (c Counter) MetadataRows(n report.Node) []MetadataRow {
|
||||
}
|
||||
|
||||
// MetadataRow is a row for the metadata table.
|
||||
// codecgen: skip
|
||||
type MetadataRow struct {
|
||||
ID string
|
||||
Value string
|
||||
@@ -117,20 +119,44 @@ func (m MetadataRow) Copy() MetadataRow {
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalJSON marshals this MetadataRow to json. It adds a label before
|
||||
// MarshalJSON shouldn't be used, use CodecEncodeSelf instead
|
||||
func (MetadataRow) MarshalJSON() ([]byte, error) {
|
||||
panic("MarshalJSON shouldn't be used, use CodecEncodeSelf instead")
|
||||
}
|
||||
|
||||
// UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead
|
||||
func (*MetadataRow) UnmarshalJSON(b []byte) error {
|
||||
panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead")
|
||||
}
|
||||
|
||||
type labelledMetadataRow struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
Prime bool `json:"prime,omitempty"`
|
||||
}
|
||||
|
||||
// CodecEncodeSelf marshals this MetadataRow. It adds a label before
|
||||
// rendering.
|
||||
func (m MetadataRow) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
Prime bool `json:"prime,omitempty"`
|
||||
}{
|
||||
func (m *MetadataRow) CodecEncodeSelf(encoder *codec.Encoder) {
|
||||
in := labelledMetadataRow{
|
||||
ID: m.ID,
|
||||
Label: Label(m.ID),
|
||||
Value: m.Value,
|
||||
Prime: m.Prime,
|
||||
})
|
||||
}
|
||||
encoder.Encode(in)
|
||||
}
|
||||
|
||||
// CodecDecodeSelf implements codec.Selfer
|
||||
func (m *MetadataRow) CodecDecodeSelf(decoder *codec.Decoder) {
|
||||
var in labelledMetadataRow
|
||||
decoder.Decode(&in)
|
||||
*m = MetadataRow{
|
||||
ID: in.ID,
|
||||
Value: in.Value,
|
||||
Prime: in.Prime,
|
||||
}
|
||||
}
|
||||
|
||||
// NodeMetadata produces a table (to be consumed directly by the UI) based on
|
||||
|
||||
+39
-11
@@ -1,9 +1,9 @@
|
||||
package detailed
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
"github.com/weaveworks/scope/probe/docker"
|
||||
"github.com/weaveworks/scope/probe/host"
|
||||
"github.com/weaveworks/scope/probe/process"
|
||||
@@ -38,6 +38,7 @@ var (
|
||||
|
||||
// MetricRow is a tuple of data used to render a metric as a sparkline and
|
||||
// accoutrements.
|
||||
// codecgen: skip
|
||||
type MetricRow struct {
|
||||
ID string
|
||||
Format string
|
||||
@@ -61,17 +62,29 @@ func (m MetricRow) Copy() MetricRow {
|
||||
return row
|
||||
}
|
||||
|
||||
// MarshalJSON marshals this MetricRow to json. It takes the basic Metric
|
||||
// MarshalJSON shouldn't be used, use CodecEncodeSelf instead
|
||||
func (MetricRow) MarshalJSON() ([]byte, error) {
|
||||
panic("MarshalJSON shouldn't be used, use CodecEncodeSelf instead")
|
||||
}
|
||||
|
||||
// UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead
|
||||
func (*MetricRow) UnmarshalJSON(b []byte) error {
|
||||
panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead")
|
||||
}
|
||||
|
||||
type wiredMetricRow 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
|
||||
}
|
||||
|
||||
// CodecEncodeSelf marshals this MetricRow. It takes the basic Metric
|
||||
// rendering, then adds some row-specific fields.
|
||||
func (m MetricRow) MarshalJSON() ([]byte, error) {
|
||||
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
|
||||
}{
|
||||
func (m *MetricRow) CodecEncodeSelf(encoder *codec.Encoder) {
|
||||
encoder.Encode(wiredMetricRow{
|
||||
ID: m.ID,
|
||||
Label: Label(m.ID),
|
||||
Format: m.Format,
|
||||
@@ -81,6 +94,21 @@ func (m MetricRow) MarshalJSON() ([]byte, error) {
|
||||
})
|
||||
}
|
||||
|
||||
// CodecDecodeSelf implements codec.Selfer
|
||||
func (m *MetricRow) CodecDecodeSelf(decoder *codec.Decoder) {
|
||||
var in wiredMetricRow
|
||||
decoder.Decode(&in)
|
||||
|
||||
metric := in.WireMetrics.FromIntermediate()
|
||||
*m = MetricRow{
|
||||
ID: in.ID,
|
||||
Format: in.Format,
|
||||
Group: in.Group,
|
||||
Value: in.Value,
|
||||
Metric: &metric,
|
||||
}
|
||||
}
|
||||
|
||||
// NodeMetrics produces a table (to be consumed directly by the UI) based on
|
||||
// an origin ID, which is (optimistically) a node ID in one of our topologies.
|
||||
func NodeMetrics(n report.Node) []MetricRow {
|
||||
|
||||
+22
-10
@@ -1,9 +1,10 @@
|
||||
package detailed
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
|
||||
"github.com/weaveworks/scope/probe/docker"
|
||||
"github.com/weaveworks/scope/probe/host"
|
||||
"github.com/weaveworks/scope/probe/kubernetes"
|
||||
@@ -13,6 +14,8 @@ import (
|
||||
)
|
||||
|
||||
// NodeSummaryGroup is a topology-typed group of children for a Node.
|
||||
// Skip codec-generation for this type to circumvent render/detailed/summary.go
|
||||
// codecgen: skip
|
||||
type NodeSummaryGroup struct {
|
||||
Label string `json:"label"`
|
||||
Nodes []NodeSummary `json:"nodes"`
|
||||
@@ -35,21 +38,30 @@ func (g NodeSummaryGroup) Copy() NodeSummaryGroup {
|
||||
|
||||
// Column provides special json serialization for column ids, so they include
|
||||
// their label for the frontend.
|
||||
// codecgen: skip
|
||||
type Column string
|
||||
|
||||
// MarshalJSON serializes a column to json
|
||||
func (c Column) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(map[string]string{"id": string(c), "label": Label(string(c))})
|
||||
// CodecEncodeSelf implements codec.Selfer
|
||||
func (c *Column) CodecEncodeSelf(encoder *codec.Encoder) {
|
||||
in := map[string]string{"id": string(*c), "label": Label(string(*c))}
|
||||
encoder.Encode(in)
|
||||
}
|
||||
|
||||
// UnmarshalJSON deserializes a column from json
|
||||
func (c *Column) UnmarshalJSON(b []byte) error {
|
||||
// CodecDecodeSelf implements codec.Selfer
|
||||
func (c *Column) CodecDecodeSelf(decoder *codec.Decoder) {
|
||||
m := map[string]string{}
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
decoder.Decode(&m)
|
||||
*c = Column(m["id"])
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON shouldn't be used, use CodecEncodeSelf instead
|
||||
func (Column) MarshalJSON() ([]byte, error) {
|
||||
panic("MarshalJSON shouldn't be used, use CodecEncodeSelf instead")
|
||||
}
|
||||
|
||||
// UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead
|
||||
func (*Column) UnmarshalJSON(b []byte) error {
|
||||
panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead")
|
||||
}
|
||||
|
||||
// NodeSummary is summary information about a child for a Node.
|
||||
|
||||
Reference in New Issue
Block a user