mirror of
https://github.com/weaveworks/scope.git
synced 2026-04-19 08:56:53 +00:00
Merge pull request #3268 from weaveworks/optimise-withlatests
Optimise Node.WithLatests()
This commit is contained in:
@@ -143,11 +143,9 @@ func (r containerWithImageNameRenderer) Render(rpt report.Report) Nodes {
|
||||
imageNameWithoutTag := docker.ImageNameWithoutTag(imageName)
|
||||
imageNodeID := report.MakeContainerImageNodeID(imageNameWithoutTag)
|
||||
|
||||
c = propagateLatest(docker.ImageName, image, c)
|
||||
c = propagateLatest(docker.ImageTag, image, c)
|
||||
c = propagateLatest(docker.ImageSize, image, c)
|
||||
c = propagateLatest(docker.ImageVirtualSize, image, c)
|
||||
c = propagateLatest(docker.ImageLabelPrefix+"works.weave.role", image, c)
|
||||
c.Latest = c.Latest.Propagate(image.Latest, docker.ImageName, docker.ImageTag,
|
||||
docker.ImageSize, docker.ImageVirtualSize, docker.ImageLabelPrefix+"works.weave.role")
|
||||
|
||||
c.Parents = c.Parents.
|
||||
Delete(report.ContainerImage).
|
||||
AddString(report.ContainerImage, imageNodeID)
|
||||
|
||||
@@ -115,13 +115,6 @@ func (m Map) Render(rpt report.Report) Nodes {
|
||||
return output.result(input)
|
||||
}
|
||||
|
||||
func propagateLatest(key string, from, to report.Node) report.Node {
|
||||
if value, timestamp, ok := from.Latest.LookupEntry(key); ok {
|
||||
to.Latest = to.Latest.Set(key, timestamp, value)
|
||||
}
|
||||
return to
|
||||
}
|
||||
|
||||
// Condition is a predecate over the entire report that can evaluate to true or false.
|
||||
type Condition func(report.Report) bool
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
"github.com/weaveworks/ps"
|
||||
@@ -138,3 +139,50 @@ func mapWrite(m ps.Map, encoder *codec.Encoder, encodeValue func(*codec.Encoder,
|
||||
})
|
||||
z.EncSendContainerState(containerMapEnd)
|
||||
}
|
||||
|
||||
// Now follow helpers for StringLatestMap
|
||||
|
||||
// These let us sort a StringLatestMap strings by key
|
||||
func (m StringLatestMap) Len() int { return len(m) }
|
||||
func (m StringLatestMap) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
|
||||
func (m StringLatestMap) Less(i, j int) bool { return m[i].key < m[j].key }
|
||||
|
||||
// sort entries and shuffle down any duplicates
|
||||
func (m StringLatestMap) fixup() {
|
||||
sort.Sort(m)
|
||||
for i := 1; i < len(m); {
|
||||
if m[i-1].key == m[i].key {
|
||||
if m[i-1].Timestamp.Before(m[i].Timestamp) {
|
||||
m = append(m[:i-1], m[i:]...)
|
||||
} else {
|
||||
m = append(m[:i], m[i+1:]...)
|
||||
}
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add several entries at the same timestamp
|
||||
func (m StringLatestMap) addMapEntries(ts time.Time, n map[string]string) StringLatestMap {
|
||||
out := make(StringLatestMap, len(m), len(m)+len(n))
|
||||
copy(out, m)
|
||||
for k, v := range n {
|
||||
out = append(out, stringLatestEntry{key: k, Value: v, Timestamp: ts})
|
||||
}
|
||||
out.fixup()
|
||||
return out
|
||||
}
|
||||
|
||||
// Propagate a set of latest values from one set to another.
|
||||
func (m StringLatestMap) Propagate(from StringLatestMap, keys ...string) StringLatestMap {
|
||||
out := make(StringLatestMap, len(m), len(m)+len(keys))
|
||||
copy(out, m)
|
||||
for _, k := range keys {
|
||||
if v, ts, ok := from.LookupEntry(k); ok {
|
||||
out = append(out, stringLatestEntry{key: k, Value: v, Timestamp: ts})
|
||||
}
|
||||
}
|
||||
out.fixup()
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -73,9 +73,7 @@ func (n Node) After(other Node) bool {
|
||||
// WithLatests returns a fresh copy of n, with Metadata m merged in.
|
||||
func (n Node) WithLatests(m map[string]string) Node {
|
||||
ts := mtime.Now()
|
||||
for k, v := range m {
|
||||
n.Latest = n.Latest.Set(k, ts, v)
|
||||
}
|
||||
n.Latest = n.Latest.addMapEntries(ts, m)
|
||||
return n
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user