Fix GraphBuilder

This commit is contained in:
Julian Kornberger 2016-06-16 21:17:06 +02:00
parent 08565d1839
commit 630eb62621

View File

@ -20,8 +20,8 @@ type GraphNode struct {
NodeID string `json:"node_id"` NodeID string `json:"node_id"`
} }
type GraphLink struct { type GraphLink struct {
Source interface{} `json:"source"` Source int `json:"source"`
Target interface{} `json:"target"` Target int `json:"target"`
VPN bool `json:"vpn"` VPN bool `json:"vpn"`
TQ float32 `json:"tq"` TQ float32 `json:"tq"`
Bidirect bool `json:"bidirect"` Bidirect bool `json:"bidirect"`
@ -82,39 +82,33 @@ func (builder *GraphBuilder) readNodes(nodes map[string]*Node) {
} }
func (builder *GraphBuilder) Extract() ([]*GraphNode, []*GraphLink) { func (builder *GraphBuilder) Extract() ([]*GraphNode, []*GraphLink) {
iNodes := 0
iLinks := 0
links := make([]*GraphLink, len(builder.links)) links := make([]*GraphLink, len(builder.links))
nodes := make([]*GraphNode, len(builder.macToID)) nodes := make([]*GraphNode, len(builder.macToID))
idToIndex := make(map[string]int)
// collect nodes and create mapping to index
i := 0
for mac, nodeID := range builder.macToID { for mac, nodeID := range builder.macToID {
nodes[iNodes] = &GraphNode{ nodes[i] = &GraphNode{
ID: mac, ID: mac,
NodeID: nodeID, NodeID: nodeID,
} }
iNodes++ idToIndex[nodeID] = i
i++
} }
// collect links
i = 0
for key, link := range builder.links { for key, link := range builder.links {
linkPart := strings.Split(key, "-") pos := strings.IndexByte(key, '-')
both := 0
for i, node := range nodes { link.Source = idToIndex[key[:pos]]
if linkPart[0] == node.NodeID { link.Target = idToIndex[key[pos+1:]]
link.Source = i links[i] = link
both++ i++
continue
} }
if linkPart[1] == node.NodeID {
link.Target = i return nodes, links
both++
continue
}
}
if both == 2 {
links[iLinks] = link
iLinks++
}
}
return nodes, links[:iLinks]
} }
func (builder *GraphBuilder) isVPN(ids ...string) bool { func (builder *GraphBuilder) isVPN(ids ...string) bool {