new service map count methods

This commit is contained in:
Gustavo Massaneiro
2022-01-01 19:50:56 -03:00
parent 759d1d5a3a
commit 5c128893bc

View File

@@ -9,22 +9,37 @@ import (
const UnresolvedNode = "unresolved"
var instance *graph
var instance *serviceMap
var once sync.Once
func GetServiceMapInstance() ServiceMapGraph {
func GetServiceMapInstance() ServiceMap {
once.Do(func() {
instance = newDirectedGraph()
instance = newServiceMap()
logger.Log.Debug("Service Map Initialized: %s")
})
return instance
}
type ServiceMapGraph interface {
AddEdge(u, v id, protocol string)
type serviceMap struct {
graph *graph
entriesProcessed int
}
type ServiceMap interface {
AddEdge(source, destination id, protocol string)
GetNodes() []string
PrintNodes()
PrintAdjacentEdges()
GetEntriesProcessedCount() int
GetNodesCount() int
GetEdgesCount() int
}
func newServiceMap() *serviceMap {
return &serviceMap{
entriesProcessed: 0,
graph: newDirectedGraph(),
}
}
type id string
@@ -52,14 +67,14 @@ func newEdgeData(p string) *edgeData {
}
}
func (g *graph) addNode(id id) {
if _, ok := g.Nodes[id]; ok {
func (s *serviceMap) addNode(id id) {
if _, ok := s.graph.Nodes[id]; ok {
return
}
g.Nodes[id] = struct{}{}
s.graph.Nodes[id] = struct{}{}
}
func (g *graph) AddEdge(u, v id, p string) {
func (s *serviceMap) AddEdge(u, v id, p string) {
if len(u) == 0 {
u = UnresolvedNode
}
@@ -67,49 +82,63 @@ func (g *graph) AddEdge(u, v id, p string) {
v = UnresolvedNode
}
if _, ok := g.Nodes[u]; !ok {
g.addNode(u)
if _, ok := s.graph.Nodes[u]; !ok {
s.addNode(u)
}
if _, ok := g.Nodes[v]; !ok {
g.addNode(v)
if _, ok := s.graph.Nodes[v]; !ok {
s.addNode(v)
}
if _, ok := g.Edges[u]; !ok {
g.Edges[u] = make(map[id]*edgeData)
if _, ok := s.graph.Edges[u]; !ok {
s.graph.Edges[u] = make(map[id]*edgeData)
}
if e, ok := g.Edges[u][v]; ok {
if e, ok := s.graph.Edges[u][v]; ok {
e.count++
} else {
g.Edges[u][v] = &edgeData{
s.graph.Edges[u][v] = &edgeData{
protocol: p,
count: 1,
}
}
s.entriesProcessed++
}
func (g *graph) GetNodes() []string {
func (s *serviceMap) GetNodes() []string {
nodes := make([]string, 0)
for k := range g.Nodes {
for k := range s.graph.Nodes {
nodes = append(nodes, string(k))
}
return nodes
}
func (g *graph) PrintNodes() {
func (s *serviceMap) PrintNodes() {
fmt.Println("Printing all nodes...")
for k := range g.Nodes {
for k := range s.graph.Nodes {
fmt.Printf("Node: %v\n", k)
}
}
func (g *graph) PrintAdjacentEdges() {
func (s *serviceMap) PrintAdjacentEdges() {
fmt.Println("Printing all edges...")
for u, m := range g.Edges {
for u, m := range s.graph.Edges {
for v := range m {
// Edge exists from u to v.
fmt.Printf("Edge: %v -> %v - Count: %v\n", u, v, g.Edges[u][v].count)
fmt.Printf("Edge: %v -> %v - Count: %v\n", u, v, s.graph.Edges[u][v].count)
}
}
}
func (s *serviceMap) GetEntriesProcessedCount() int {
return s.entriesProcessed
}
func (s *serviceMap) GetNodesCount() int {
return len(s.graph.Nodes)
}
func (s *serviceMap) GetEdgesCount() int {
return len(s.graph.Edges)
}