Improve graphs
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-07-08 00:45:06 +02:00
parent e6345edef9
commit 243c5df043
2 changed files with 20 additions and 13 deletions

View File

@ -32,12 +32,17 @@ func NewGraphBuilder(mp PagesRepository) (*graphBuilder, error) {
return nil, err
}
return &graphBuilder{
gb := &graphBuilder{
refs: refs,
nodeMap: make(NodeMap),
nodes: nil,
edges: nil,
}, nil
}
gb.nodeMap["Daily_Notes"] = Node{Id: -1}
gb.nodeMap["TODO"] = Node{Id: -1}
gb.nodeMap["DONE"] = Node{Id: -1}
gb.nodeMap["Projects"] = Node{Id: -1}
return gb, nil
}
func (gb *graphBuilder) prepareGraph() error {
@ -66,7 +71,10 @@ func (gb *graphBuilder) RemoveNodeWithSuffix(suffix string) {
}
func (gb *graphBuilder) buildFromCenter(name string) error {
_ = gb.addNode(name, "#ef2929")
id := gb.addNode(name, "#ef2929")
if id < 0 {
return nil
}
if ref, e := gb.refs[name]; e {
for _, item := range ref {
@ -89,9 +97,9 @@ func prepareEdges(refs Refs, nodeMap NodeMap) []Edge {
var edges []Edge
edgeSet := make(map[Edge]bool)
for key, references := range refs {
if to, e := nodeMap[key]; e {
if to, e := nodeMap[key]; e && to.Id >= 0 {
for _, item := range references {
if from, e := nodeMap[item.Name]; e {
if from, e := nodeMap[item.Name]; e && from.Id >= 0 {
edge := Edge{
From: from.Id,
To: to.Id,
@ -110,6 +118,9 @@ func prepareEdges(refs Refs, nodeMap NodeMap) []Edge {
func prepareNodes(nodeMap NodeMap, apply func(node Node) Node) []Node {
var nodes []Node
for _, node := range nodeMap {
if node.Id < 0 {
continue
}
nodes = append(nodes, apply(Node{
Id: node.Id,
Label: node.Label,

View File

@ -809,6 +809,7 @@ func main() {
http.Error(w, err.Error(), 500)
return
}
gb.RemoveNode(name)
err = gb.buildFromCenter(name)
if err != nil {
@ -819,17 +820,12 @@ func main() {
// Keep a copy of the nodes, buildFromCenter appends to the nodeMap
var nodes []string
for k, _ := range gb.nodeMap {
if k == "Daily_Notes" || strings.HasSuffix(k, "Mei_2020") {
continue
}
nodes = append(nodes, k)
}
if name != "Daily_Notes" {
for _, node := range nodes {
err = gb.buildFromCenter(node)
}
}
err = gb.prepareGraph()
if err != nil {