Skip to content

Commit

Permalink
feat: collect basic statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
luissimas committed May 26, 2024
1 parent 23c7975 commit 09959eb
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
- [X] Parse obsidian wiki links
- [X] Register total links
- [X] Register links per note
- [ ] Read config
- [ ] Expose prometheus metrics endpoint
- [ ] Handle uncreated links
- [ ] Collect backlinks
- [ ] Configurable ignore patterns
- [ ] Parse markdown links
- [ ] Get zettelkasten from git url
Expand Down
36 changes: 36 additions & 0 deletions cmd/zettelkasten-exporter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"log/slog"
"os"
"path/filepath"

"github.com/luissimas/zettelkasten-exporter/internal/collector"
)

func main() {
if len(os.Args) != 2 {
slog.Error("Wrong arguments")
os.Exit(1)
}

dir := os.Args[1]
absolute_path, err := filepath.Abs(dir)
if err != nil {
slog.Error("Error getting absolute file path", slog.Any("error", err))
os.Exit(1)
}

metrics, err := collector.CollectMetrics(collector.CollectorConfig{Path: absolute_path})
if err != nil {
slog.Error("Error collecting note metrics", slog.Any("error", err))
os.Exit(1)
}

fmt.Printf("There are %d markdown files in %s\n", metrics.NoteCount, absolute_path)
fmt.Printf("There is a total of %d links in%s\n", metrics.LinkCount, absolute_path)
for name, metrics := range metrics.Notes {
fmt.Printf("\t%s: %v\n", name, metrics)
}
}
43 changes: 43 additions & 0 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package collector

import (
"log/slog"
"os"
"path/filepath"
)

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

type CollectorConfig struct {
Path string
}

func CollectMetrics(config CollectorConfig) (Metrics, error) {
pattern := filepath.Join(config.Path, "**/*.md")
files, err := filepath.Glob(pattern)
if err != nil {
slog.Error("Error getting files", slog.Any("error", err))
return Metrics{}, err
}

noteCount := len(files)
linkCount := 0
notes := make(map[string]NoteMetrics)

for _, file := range files {
content, err := os.ReadFile(file)
if err != nil {
slog.Error("Error reading file", slog.Any("error", err))
continue
}
metrics := CollectNoteMetrics(content)
notes[file] = metrics
linkCount += metrics.LinkCount
}

return Metrics{NoteCount: noteCount, LinkCount: linkCount, Notes: notes}, nil
}
19 changes: 19 additions & 0 deletions internal/collector/note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package collector

import (
"regexp"
)

type NoteMetrics struct {
LinkCount int
}

func CollectNoteMetrics(content []byte) NoteMetrics {
return NoteMetrics{LinkCount: collectLinkCount(content)}
}

func collectLinkCount(content []byte) int {
r, _ := regexp.Compile(`\[\[[^]]+\]\]`)
matches := r.FindAll(content, -1)
return len(matches)
}
25 changes: 25 additions & 0 deletions internal/collector/note_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package collector

import "testing"

func TestCollectNoteMetrics(t *testing.T) {
data := []struct {
name string
content string
expected NoteMetrics
}{
{"empty file", "", NoteMetrics{LinkCount: 0}},
{"file with a single link", "[[Link]]", NoteMetrics{LinkCount: 1}},
{"file with multiple links", "[[Link]]aksdjf[[anotherlink]]\n[[link]]", NoteMetrics{LinkCount: 3}},
{"wikilink dividers", "[[something|another]]\n\n[[link]]\n[[382dlk djfs link|yeah]]", NoteMetrics{LinkCount: 3}},
}

for _, d := range data {
t.Run(d.name, func(t *testing.T) {
result := CollectNoteMetrics([]byte(d.content))
if result != d.expected {
t.Errorf("Expected %v, got %v", d.expected, result)
}
})
}
}

0 comments on commit 09959eb

Please sign in to comment.