first try of graph.json

This commit is contained in:
root 2016-05-16 11:41:45 +02:00
parent 11e584183e
commit 0f910c5483
1 changed files with 43 additions and 12 deletions

View File

@ -8,13 +8,20 @@ import (
type Graph struct { type Graph struct {
Version int `json:"version"` Version int `json:"version"`
Batadv struct { Batadv struct {
Directed bool `json:"directed"`
Graph []string `json:"graph"`
Nodes []*GraphNode `json:"nodes"`
Links []*GraphLink `json:"links"` Links []*GraphLink `json:"links"`
} `json:"batadv"` } `json:"batadv"`
} }
type GraphNode struct {
ID string `json:"id"`
NodeID string `json:"node_id"`
}
type GraphLink struct { type GraphLink struct {
Source string `json:"source"` Source interface{} `json:"source"`
Target string `json:"target"` Target interface{} `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"`
@ -35,8 +42,9 @@ func (nodes *Nodes) BuildGraph() *Graph {
builder.readNodes(nodes.List) builder.readNodes(nodes.List)
graph := &Graph{Version: 2} graph := &Graph{Version: 1}
graph.Batadv.Links = builder.Links() graph.Batadv.Directed = false
graph.Batadv.Nodes, graph.Batadv.Links = builder.Extract()
return graph return graph
} }
@ -73,15 +81,40 @@ func (builder *GraphBuilder) readNodes(nodes map[string]*Node) {
} }
} }
func (builder *GraphBuilder) Links() []*GraphLink { func (builder *GraphBuilder) Extract() ([]*GraphNode,[]*GraphLink) {
i := 0 iNodes := 0
iLinks := 0
links := make([]*GraphLink, len(builder.links)) links := make([]*GraphLink, len(builder.links))
nodes := make([]*GraphNode, len(builder.macToID))
for _, link := range builder.links { for mac, nodeID := range builder.macToID {
links[i] = link nodes[iNodes] = &GraphNode{
i += 1 ID: mac,
NodeID: nodeID,
} }
return links iNodes += 1
}
for key, link := range builder.links {
linkPart :=strings.Split(key,"-")
both := 0
for i,node := range nodes{
if(linkPart[0] == node.NodeID){
link.Source = i
both += 1
continue
}
if(linkPart[1]==node.NodeID){
link.Target = i
both += 1
break
}
}
if both == 2 {
links[iLinks] = link
iLinks += 1
}
}
return nodes, links
} }
func (builder *GraphBuilder) isVPN(ids ...string) bool { func (builder *GraphBuilder) isVPN(ids ...string) bool {
@ -109,8 +142,6 @@ func (builder *GraphBuilder) addLink(targetId string, sourceId string, linkTq in
if link, ok := builder.links[key]; !ok { if link, ok := builder.links[key]; !ok {
builder.links[key] = &GraphLink{ builder.links[key] = &GraphLink{
Source: sourceId,
Target: targetId,
VPN: builder.isVPN(sourceId, targetId), VPN: builder.isVPN(sourceId, targetId),
TQ: tq, TQ: tq,
} }