summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-14 11:16:45 +0300
committerPaul Buetow <paul@buetow.org>2026-04-14 11:16:45 +0300
commit2bc4e64acf93f04c8871d964d75f041ada57f89d (patch)
treec156e09a6c65c47e35006a9c108f4a85e78ef1e2 /internal
parenta58a6cf092338e0dac45608efa9f2c7b81cbe01f (diff)
refactor: share .records file discovery (ask u3)
Extract ListNonEmptyFiles and HostFromFileName into internal/recordsdir for aggregate and storage ImportFromDir. Behavior unchanged. Made-with: Cursor
Diffstat (limited to 'internal')
-rw-r--r--internal/goprecords/aggregate.go21
-rw-r--r--internal/recordsdir/recordsdir.go40
-rw-r--r--internal/recordsdir/recordsdir_test.go58
-rw-r--r--internal/storage/db.go21
4 files changed, 107 insertions, 33 deletions
diff --git a/internal/goprecords/aggregate.go b/internal/goprecords/aggregate.go
index cefb545..31a0e1c 100644
--- a/internal/goprecords/aggregate.go
+++ b/internal/goprecords/aggregate.go
@@ -5,10 +5,9 @@ import (
"context"
"fmt"
"os"
- "path/filepath"
- "strings"
"codeberg.org/snonux/goprecords/internal/recordline"
+ "codeberg.org/snonux/goprecords/internal/recordsdir"
)
// Aggregates holds all category maps. Host uses HostAggregate; others use Aggregate.
@@ -37,23 +36,13 @@ func (ag *Aggregator) Aggregate(ctx context.Context) (*Aggregates, error) {
KernelMajor: make(map[string]*Aggregate),
KernelName: make(map[string]*Aggregate),
}
- entries, err := os.ReadDir(ag.statsDir)
+ files, err := recordsdir.ListNonEmptyFiles(ag.statsDir)
if err != nil {
return nil, fmt.Errorf("read stats dir: %w", err)
}
- for _, e := range entries {
- if e.IsDir() || !strings.HasSuffix(e.Name(), ".records") {
- continue
- }
- path := filepath.Join(ag.statsDir, e.Name())
- info, err := os.Stat(path)
- if err != nil || info.Size() == 0 {
- continue
- }
- host := strings.TrimSuffix(e.Name(), filepath.Ext(e.Name()))
- if idx := strings.Index(host, "."); idx > 0 {
- host = host[:idx]
- }
+ for _, f := range files {
+ host := f.Host
+ path := f.Path
if _, exists := out.Host[host]; exists {
return nil, fmt.Errorf("record file for %s already processed - duplicate inputs?", host)
}
diff --git a/internal/recordsdir/recordsdir.go b/internal/recordsdir/recordsdir.go
new file mode 100644
index 0000000..9f3ce5b
--- /dev/null
+++ b/internal/recordsdir/recordsdir.go
@@ -0,0 +1,40 @@
+package recordsdir
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+type Entry struct {
+ Path string
+ Host string
+}
+
+func HostFromFileName(name string) string {
+ host := strings.TrimSuffix(name, filepath.Ext(name))
+ if idx := strings.Index(host, "."); idx > 0 {
+ host = host[:idx]
+ }
+ return host
+}
+
+func ListNonEmptyFiles(dir string) ([]Entry, error) {
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ return nil, err
+ }
+ var out []Entry
+ for _, e := range entries {
+ if e.IsDir() || !strings.HasSuffix(e.Name(), ".records") {
+ continue
+ }
+ path := filepath.Join(dir, e.Name())
+ info, err := os.Stat(path)
+ if err != nil || info.Size() == 0 {
+ continue
+ }
+ out = append(out, Entry{Path: path, Host: HostFromFileName(e.Name())})
+ }
+ return out, nil
+}
diff --git a/internal/recordsdir/recordsdir_test.go b/internal/recordsdir/recordsdir_test.go
new file mode 100644
index 0000000..bee8d67
--- /dev/null
+++ b/internal/recordsdir/recordsdir_test.go
@@ -0,0 +1,58 @@
+package recordsdir
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestHostFromFileName(t *testing.T) {
+ tests := []struct {
+ file string
+ want string
+ }{
+ {"earth.records", "earth"},
+ {"myhost.example.com.records", "myhost"},
+ {"single.records", "single"},
+ }
+ for _, tt := range tests {
+ t.Run(tt.file, func(t *testing.T) {
+ if got := HostFromFileName(tt.file); got != tt.want {
+ t.Fatalf("HostFromFileName(%q) = %q, want %q", tt.file, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestListNonEmptyFiles(t *testing.T) {
+ dir := t.TempDir()
+ if err := os.WriteFile(filepath.Join(dir, "skip.txt"), []byte("x"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "empty.records"), nil, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "h1.records"), []byte("line\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Mkdir(filepath.Join(dir, "nested.records"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ entries, err := ListNonEmptyFiles(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(entries) != 1 {
+ t.Fatalf("len(entries) = %d, want 1: %#v", len(entries), entries)
+ }
+ if entries[0].Host != "h1" || filepath.Base(entries[0].Path) != "h1.records" {
+ t.Fatalf("unexpected entry: %#v", entries[0])
+ }
+}
+
+func TestListNonEmptyFiles_ReadError(t *testing.T) {
+ _, err := ListNonEmptyFiles(filepath.Join(t.TempDir(), "nonexistent-subdir-nope"))
+ if err == nil {
+ t.Fatal("expected error")
+ }
+}
diff --git a/internal/storage/db.go b/internal/storage/db.go
index 7b577e7..fe789d6 100644
--- a/internal/storage/db.go
+++ b/internal/storage/db.go
@@ -6,10 +6,9 @@ import (
"database/sql"
"fmt"
"os"
- "path/filepath"
- "strings"
"codeberg.org/snonux/goprecords/internal/recordline"
+ "codeberg.org/snonux/goprecords/internal/recordsdir"
_ "modernc.org/sqlite"
)
@@ -67,7 +66,7 @@ func ImportFromDir(ctx context.Context, db *sql.DB, statsDir string) error {
if err := ResetRecords(ctx, db); err != nil {
return fmt.Errorf("reset records: %w", err)
}
- entries, err := os.ReadDir(statsDir)
+ files, err := recordsdir.ListNonEmptyFiles(statsDir)
if err != nil {
return fmt.Errorf("read dir: %w", err)
}
@@ -81,20 +80,8 @@ func ImportFromDir(ctx context.Context, db *sql.DB, statsDir string) error {
return fmt.Errorf("prepare insert: %w", err)
}
defer insert.Close()
- for _, e := range entries {
- if e.IsDir() || !strings.HasSuffix(e.Name(), ".records") {
- continue
- }
- path := filepath.Join(statsDir, e.Name())
- info, err := os.Stat(path)
- if err != nil || info.Size() == 0 {
- continue
- }
- host := strings.TrimSuffix(e.Name(), filepath.Ext(e.Name()))
- if idx := strings.Index(host, "."); idx > 0 {
- host = host[:idx]
- }
- if err := importFile(ctx, insert, path, host); err != nil {
+ for _, f := range files {
+ if err := importFile(ctx, insert, f.Path, f.Host); err != nil {
return err
}
}