-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualize.go
79 lines (67 loc) · 1.41 KB
/
visualize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"os"
"slices"
)
type VisualData struct {
Nodes []VizNode `json:"nodes"`
Links []VizLink `json:"links"`
}
type VizNode struct {
ID string `json:"id"`
Group int `json:"group"`
}
type VizLink struct {
Source string `json:"source"`
Destination string `json:"target"`
}
func (a *Analysis) Visualize() {
viz := VisualData{}
nodes := map[string]int{}
rows := []Link{}
result := a.db.Find(&rows)
ohno(result.Error)
for _, row := range rows {
viz.Links = append(viz.Links, VizLink{
Source: row.SourceUrl,
Destination: row.DestinationUrl,
})
nodes[row.SourceUrl] = row.SourceType
nodes[row.DestinationUrl] = row.DestinationType
}
for node, nodeType := range nodes {
viz.Nodes = append(viz.Nodes, VizNode{
ID: node,
Group: nodeType,
})
}
slices.SortFunc(viz.Nodes, func(a, b VizNode) int {
if a.ID < b.ID {
return -1
} else if a.ID > b.ID {
return 1
} else {
return 0
}
})
slices.SortFunc(viz.Links, func(a, b VizLink) int {
if a.Source < b.Source {
return -1
} else if a.Source > b.Source {
return 1
} else {
if a.Destination < b.Destination {
return -1
} else if a.Destination > b.Destination {
return 1
} else {
return 0
}
}
})
output, err := json.MarshalIndent(viz, "", " ")
ohno(err)
err = os.WriteFile("static/index.json", output, os.FileMode(int(0660)))
ohno(err)
}