service map interface and new methods

This commit is contained in:
Gustavo Massaneiro
2022-01-01 19:11:48 -03:00
parent 47f9fc9903
commit 478ffaaf7c

View File

@@ -2,12 +2,34 @@ package api
import (
"fmt"
"sync"
"github.com/up9inc/mizu/shared/logger"
)
const UnresolvedNode = "unresolved"
var instance *graph
var once sync.Once
func GetServiceMapInstance() ServiceMapGraph {
once.Do(func() {
instance = newDirectedGraph()
logger.Log.Debug("Service Map Initialized: %s")
})
return instance
}
type ServiceMapGraph interface {
AddEdge(u, v id, protocol string)
GetNodes() []string
PrintNodes()
PrintAdjacentEdges()
}
type id string
type edgeData struct {
protocol string
count int
}
@@ -16,13 +38,20 @@ type graph struct {
Edges map[id]map[id]*edgeData
}
func NewDirectedGraph() *graph {
func newDirectedGraph() *graph {
return &graph{
Nodes: make(map[id]struct{}),
Edges: make(map[id]map[id]*edgeData),
}
}
func newEdgeData(p string) *edgeData {
return &edgeData{
protocol: p,
count: 1,
}
}
func (g *graph) addNode(id id) {
if _, ok := g.Nodes[id]; ok {
return
@@ -30,7 +59,7 @@ func (g *graph) addNode(id id) {
g.Nodes[id] = struct{}{}
}
func (g *graph) AddEdge(u, v id) {
func (g *graph) AddEdge(u, v id, p string) {
if len(u) == 0 {
u = UnresolvedNode
}
@@ -52,9 +81,20 @@ func (g *graph) AddEdge(u, v id) {
if e, ok := g.Edges[u][v]; ok {
e.count++
} else {
g.Edges[u][v] = &edgeData{count: 1}
g.Edges[u][v] = &edgeData{
protocol: p,
count: 1,
}
}
}
func (g *graph) GetNodes() []string {
nodes := make([]string, 0)
for k := range g.Nodes {
nodes = append(nodes, string(k))
}
return nodes
}
func (g *graph) PrintNodes() {
fmt.Println("Printing all nodes...")