From 243c5df04337c9cc13ce5af5e4b9174f388d1ee2 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Wed, 8 Jul 2020 00:45:06 +0200 Subject: [PATCH] Improve graphs --- graph.go | 21 ++++++++++++++++----- main.go | 12 ++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/graph.go b/graph.go index 6cc613c..406df30 100644 --- a/graph.go +++ b/graph.go @@ -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, diff --git a/main.go b/main.go index bc95e8a..48b84b8 100644 --- a/main.go +++ b/main.go @@ -104,7 +104,7 @@ type Node struct { Id int `json:"id"` Label string `json:"label"` Color *string `json:"color"` - Opacity float64 `json:"opacity"` + Opacity float64 `json:"opacity"` } type Edge struct { @@ -809,6 +809,7 @@ func main() { http.Error(w, err.Error(), 500) return } + gb.RemoveNode(name) err = gb.buildFromCenter(name) if err != nil { @@ -819,16 +820,11 @@ 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) - } + for _, node := range nodes { + err = gb.buildFromCenter(node) } err = gb.prepareGraph()