package report import ( "fmt" "reflect" "strconv" "github.com/ugorji/go/codec" "github.com/weaveworks/ps" ) // EdgeMetadatas collect metadata about each edge in a topology. Keys are the // remote node IDs, as in Adjacency. type EdgeMetadatas struct { psMap ps.Map } var emptyEdgeMetadatas = EdgeMetadatas{ps.NewMap()} // MakeEdgeMetadatas returns EmptyEdgeMetadatas func MakeEdgeMetadatas() EdgeMetadatas { return emptyEdgeMetadatas } // Add value to the counter 'key' func (c EdgeMetadatas) Add(key string, value EdgeMetadata) EdgeMetadatas { if c.psMap == nil { c = emptyEdgeMetadatas } if existingValue, ok := c.psMap.Lookup(key); ok { value = value.Merge(existingValue.(EdgeMetadata)) } return EdgeMetadatas{ c.psMap.Set(key, value), } } // Lookup the counter 'key' func (c EdgeMetadatas) Lookup(key string) (EdgeMetadata, bool) { if c.psMap != nil { existingValue, ok := c.psMap.Lookup(key) if ok { return existingValue.(EdgeMetadata), true } } return EdgeMetadata{}, false } // Size is the number of elements func (c EdgeMetadatas) Size() int { if c.psMap == nil { return 0 } return c.psMap.Size() } // Merge produces a fresh Counters, container the keys from both inputs. When // both inputs container the same key, the latter value is used. func (c EdgeMetadatas) Merge(other EdgeMetadatas) EdgeMetadatas { var ( cSize = c.Size() otherSize = other.Size() output = c.psMap iter = other.psMap ) switch { case cSize == 0: return other case otherSize == 0: return c case cSize < otherSize: output, iter = iter, output } iter.ForEach(func(key string, otherVal interface{}) { if val, ok := output.Lookup(key); ok { output = output.Set(key, otherVal.(EdgeMetadata).Merge(val.(EdgeMetadata))) } else { output = output.Set(key, otherVal) } }) return EdgeMetadatas{output} } // Flatten flattens all the EdgeMetadatas in this set and returns the result. // The original is not modified. func (c EdgeMetadatas) Flatten() EdgeMetadata { result := EdgeMetadata{} c.ForEach(func(_ string, e EdgeMetadata) { result = result.Flatten(e) }) return result } // ForEach executes f on each key value pair in the map func (c EdgeMetadatas) ForEach(fn func(k string, v EdgeMetadata)) { if c.psMap != nil { c.psMap.ForEach(func(key string, value interface{}) { fn(key, value.(EdgeMetadata)) }) } } func (c EdgeMetadatas) String() string { return mapToString(c.psMap) } // DeepEqual tests equality with other Counters func (c EdgeMetadatas) DeepEqual(d EdgeMetadatas) bool { return mapEqual(c.psMap, d.psMap, reflect.DeepEqual) } // CodecEncodeSelf implements codec.Selfer func (c *EdgeMetadatas) CodecEncodeSelf(encoder *codec.Encoder) { mapWrite(c.psMap, encoder, func(encoder *codec.Encoder, val interface{}) { e := val.(EdgeMetadata) (&e).CodecEncodeSelf(encoder) }) } // CodecDecodeSelf implements codec.Selfer func (c *EdgeMetadatas) CodecDecodeSelf(decoder *codec.Decoder) { out := mapRead(decoder, func(isNil bool) interface{} { var value EdgeMetadata if !isNil { value.CodecDecodeSelf(decoder) } return value }) *c = EdgeMetadatas{out} } // MarshalJSON shouldn't be used, use CodecEncodeSelf instead func (EdgeMetadatas) MarshalJSON() ([]byte, error) { panic("MarshalJSON shouldn't be used, use CodecEncodeSelf instead") } // UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead func (*EdgeMetadatas) UnmarshalJSON(b []byte) error { panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead") } // EdgeMetadata describes a superset of the metadata that probes can possibly // collect about a directed edge between two nodes in any topology. type EdgeMetadata struct { EgressPacketCount *uint64 `json:"egress_packet_count,omitempty"` IngressPacketCount *uint64 `json:"ingress_packet_count,omitempty"` EgressByteCount *uint64 `json:"egress_byte_count,omitempty"` // Transport layer IngressByteCount *uint64 `json:"ingress_byte_count,omitempty"` // Transport layer dummySelfer } // String returns a string representation of this EdgeMetadata // Helps with our use of Spew and diff. func (e EdgeMetadata) String() string { f := func(i *uint64) string { if i == nil { return "nil" } return strconv.FormatUint(*i, 10) } return fmt.Sprintf(`{ EgressPacketCount: %v, IngressPacketCount: %v, EgressByteCount: %v, IngressByteCount: %v, }`, f(e.EgressPacketCount), f(e.IngressPacketCount), f(e.EgressByteCount), f(e.IngressByteCount)) } // Copy returns a value copy of the EdgeMetadata. func (e EdgeMetadata) Copy() EdgeMetadata { return EdgeMetadata{ EgressPacketCount: cpu64ptr(e.EgressPacketCount), IngressPacketCount: cpu64ptr(e.IngressPacketCount), EgressByteCount: cpu64ptr(e.EgressByteCount), IngressByteCount: cpu64ptr(e.IngressByteCount), } } // Reversed returns a value copy of the EdgeMetadata, with the direction reversed. func (e EdgeMetadata) Reversed() EdgeMetadata { return EdgeMetadata{ EgressPacketCount: cpu64ptr(e.IngressPacketCount), IngressPacketCount: cpu64ptr(e.EgressPacketCount), EgressByteCount: cpu64ptr(e.IngressByteCount), IngressByteCount: cpu64ptr(e.EgressByteCount), } } func cpu64ptr(u *uint64) *uint64 { if u == nil { return nil } value := *u // oh man return &value // this sucks } // Merge merges another EdgeMetadata into the receiver and returns the result. // The receiver is not modified. The two edge metadatas should represent the // same edge on different times. func (e EdgeMetadata) Merge(other EdgeMetadata) EdgeMetadata { cp := e.Copy() cp.EgressPacketCount = merge(cp.EgressPacketCount, other.EgressPacketCount, sum) cp.IngressPacketCount = merge(cp.IngressPacketCount, other.IngressPacketCount, sum) cp.EgressByteCount = merge(cp.EgressByteCount, other.EgressByteCount, sum) cp.IngressByteCount = merge(cp.IngressByteCount, other.IngressByteCount, sum) return cp } // Flatten sums two EdgeMetadatas and returns the result. The receiver is not // modified. The two edge metadata windows should be the same duration; they // should represent different edges at the same time. func (e EdgeMetadata) Flatten(other EdgeMetadata) EdgeMetadata { cp := e.Copy() cp.EgressPacketCount = merge(cp.EgressPacketCount, other.EgressPacketCount, sum) cp.IngressPacketCount = merge(cp.IngressPacketCount, other.IngressPacketCount, sum) cp.EgressByteCount = merge(cp.EgressByteCount, other.EgressByteCount, sum) cp.IngressByteCount = merge(cp.IngressByteCount, other.IngressByteCount, sum) return cp } func merge(dst, src *uint64, op func(uint64, uint64) uint64) *uint64 { if src == nil { return dst } if dst == nil { dst = new(uint64) } (*dst) = op(*dst, *src) return dst } func sum(dst, src uint64) uint64 { return dst + src } func max(dst, src uint64) uint64 { if dst > src { return dst } return src }