Skip to content

Commit

Permalink
refactor: use uint in note metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
luissimas committed Jul 1, 2024
1 parent ea1d8a4 commit e8a2265
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (c *Collector) CollectMetrics(root fs.FS, collectionTime time.Time) error {
}

func (c *Collector) collectMetrics(root fs.FS) (metrics.Metrics, error) {
noteCount := 0
linkCount := 0
var noteCount uint
var linkCount uint
notes := make(map[string]metrics.NoteMetrics)

err := fs.WalkDir(root, ".", func(path string, dir fs.DirEntry, err error) error {
Expand Down
8 changes: 4 additions & 4 deletions internal/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ Link to [one](./one.md) and also a full link [[./dir1/dir2/three]] and a [[./dir
LinkCount: 8,
Notes: map[string]metrics.NoteMetrics{
"zettel/one.md": {
Links: map[string]int{"./dir1/two.md": 2},
Links: map[string]uint{"./dir1/two.md": 2},
LinkCount: 2,
},
"zettel/dir1/two.md": {
Links: map[string]int{"one": 1},
Links: map[string]uint{"one": 1},
LinkCount: 1,
},
"zettel/dir1/dir2/three.md": {
Links: map[string]int{"one": 1, "two": 1},
Links: map[string]uint{"one": 1, "two": 1},
LinkCount: 2,
},
"zettel/four.md": {
Links: map[string]int{"./one.md": 1, "./dir1/dir2/three": 1, "./dir1/two.md": 1},
Links: map[string]uint{"./one.md": 1, "./dir1/dir2/three": 1, "./dir1/two.md": 1},
LinkCount: 3,
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/collector/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ var md = goldmark.New(
)

func CollectNoteMetrics(content []byte) metrics.NoteMetrics {
var linkCount uint
links := collectLinks(content)
linkCount := 0
for _, v := range links {
linkCount += v
}
return metrics.NoteMetrics{Links: links, LinkCount: linkCount}
}

func collectLinks(content []byte) map[string]int {
func collectLinks(content []byte) map[string]uint {
linkKinds := []ast.NodeKind{ast.KindLink, wikilink.Kind}
reader := text.NewReader(content)
root := md.Parser().Parse(reader)
links := make(map[string]int)
links := make(map[string]uint)
err := ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if entering && slices.Contains(linkKinds, n.Kind()) {
var target string
Expand Down
12 changes: 6 additions & 6 deletions internal/collector/note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@ func TestCollectNoteMetrics(t *testing.T) {
name: "empty file",
content: "",
expected: metrics.NoteMetrics{
Links: map[string]int{},
Links: map[string]uint{},
LinkCount: 0,
},
},
{
name: "wiki links",
content: "[[Link]]aksdjf[[something|another]]\n[[link]]",
expected: metrics.NoteMetrics{
Links: map[string]int{"Link": 1, "something": 1, "link": 1},
Links: map[string]uint{"Link": 1, "something": 1, "link": 1},
LinkCount: 3,
},
},
{
name: "markdown link",
content: "[Link](target.md)",
expected: metrics.NoteMetrics{
Links: map[string]int{"target.md": 1},
Links: map[string]uint{"target.md": 1},
LinkCount: 1,
},
},
{
name: "mixed links",
content: "okok[Link](target.md)\n**ddk**[[linked]]`test`[[another|link]]\n\n[test](yet-another.md)",
expected: metrics.NoteMetrics{
Links: map[string]int{"target.md": 1, "linked": 1, "another": 1, "yet-another.md": 1},
Links: map[string]uint{"target.md": 1, "linked": 1, "another": 1, "yet-another.md": 1},
LinkCount: 4,
},
},
{
name: "repeated links",
content: "[[target.md|link]]\n[link](target.md)\n[[link]]",
expected: metrics.NoteMetrics{
Links: map[string]int{"target.md": 2, "link": 1},
Links: map[string]uint{"target.md": 2, "link": 1},
LinkCount: 3,
},
},
{
name: "ignore embeddedlinks",
content: "![[target.png]]\n!()[another.jpeg]\n[[link]]",
expected: metrics.NoteMetrics{
Links: map[string]int{"link": 1},
Links: map[string]uint{"link": 1},
LinkCount: 1,
},
},
Expand Down
8 changes: 4 additions & 4 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package metrics

type Metrics struct {
NoteCount int
LinkCount int
NoteCount uint
LinkCount uint
Notes map[string]NoteMetrics
}

type NoteMetrics struct {
Links map[string]int
LinkCount int
Links map[string]uint
LinkCount uint
}

0 comments on commit e8a2265

Please sign in to comment.