metrics

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package metrics enriches graph nodes with code health signals: cyclomatic complexity (computed during parsing), git churn, git blame, and test coverage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlameAgeLabel

func BlameAgeLabel(dateStr string) string

BlameAgeLabel converts an ISO short date ("2025-01-15") to a human-readable age relative to now ("3d ago", "1w ago", "2mo ago", "1y ago"). Returns "" if the date cannot be parsed.

func EnrichBlame

func EnrichBlame(g *graph.Graph, repoRoot string)

EnrichBlame annotates every function/method node with git blame metadata: blame_author, blame_date, blame_commit, blame_subject, and staleness_score. Uses per-file granularity (one git log call per unique file) for performance. Nodes in vendored/generated paths are skipped. Must be called after EnrichChurn — staleness_score reads metadata["churn"]. Git errors are silently ignored; the graph remains usable without blame data.

func EnrichBlameForFile

func EnrichBlameForFile(g *graph.Graph, repoRoot, absFile string)

EnrichBlameForFile updates blame and churn metadata for all function/method nodes in a single file. Called by the watcher after a file is re-parsed to keep blame data current without a full re-blame of the entire graph. It is a no-op when git is unavailable, when the file has no commits, or when no function/method nodes exist in the file.

Unlike EnrichBlame (startup batch), this also computes per-file churn so that staleness_score is accurate even for nodes that were just created by an incremental reparse (which would otherwise have no churn metadata from the startup EnrichChurn scan).

func EnrichChurn

func EnrichChurn(g *graph.Graph, repoRoot string, days int)

EnrichChurn annotates every graph node with a "churn" metadata value indicating how many git commits touched the node's file in the last [days] days. Nodes in files with no recent commits are left unchanged. Errors (e.g. not a git repo) are silently ignored — the graph is still usable without churn data.

Supports umbrella workspaces where repoRoot is not itself a git repository but contains sub-repos (e.g. a monorepo with separate .git in each package). In that case, each file's git root is detected automatically.

func EnrichCommitContext

func EnrichCommitContext(g *graph.Graph, repoRoot string)

EnrichCommitContext annotates every function/method node with the last 3 commit messages that touched its file. The subjects are stored as a JSON array in metadata["commit_context"] so they can be parsed and rendered at query time without a git subprocess.

Uses per-file granularity (one git log call per unique file, same as EnrichBlame) for performance. Nodes in vendored/generated paths are skipped. Git errors are silently ignored — the graph remains usable without commit data.

func EnrichCommitContextForFile

func EnrichCommitContextForFile(g *graph.Graph, repoRoot, absFile string)

EnrichCommitContextForFile updates commit context metadata for all function/method nodes in a single file. Called by the watcher after a file is re-parsed to keep commit data current without re-enriching the full graph. It is a no-op when git is unavailable, when the file has no commits, or when no function/method nodes exist in the file.

func EnrichCoverage

func EnrichCoverage(g *graph.Graph, repoRoot, profilePath string)

EnrichCoverage parses a go test -coverprofile output file and annotates function/method nodes with a "coverage" metadata value (0.0–1.0, the fraction of statements covered by tests). Nodes without matching coverage data are left unchanged. Errors are silently ignored.

func EnrichPprof

func EnrichPprof(g *graph.Graph, repoRoot, profilePath string)

EnrichPprof parses a Go pprof CPU profile and annotates function/method nodes with metadata["cpu_pct"] — the flat CPU percentage contributed by that function. Uses `go tool pprof -top` (standard with every Go installation); errors are silently ignored so the graph remains usable without pprof data.

func StalenessLabel

func StalenessLabel(score float64) string

StalenessLabel converts a numeric staleness score to a qualitative label. low: < 30, medium: 30–149, high: ≥ 150.

Types

type CommitInfo

type CommitInfo struct {
	Hash    string `json:"hash"`
	Author  string `json:"author"`
	Date    string `json:"date"`
	Message string `json:"message"`
	// Body is the first 200 characters of the commit body (everything after the
	// subject line). Empty when the commit has no body. Gives agents the "why"
	// beyond the one-line subject — e.g. "Temporary workaround — revert before prod".
	Body string `json:"body,omitempty"`
}

CommitInfo holds a summary of a single git commit, as surfaced by get_context's recent_changes field (R34: the "why" layer).

func RecentCommitsForFile

func RecentCommitsForFile(ctx context.Context, repoRoot, filePath string, limit int) []CommitInfo

RecentCommitsForFile returns the last [limit] commits that touched filePath, using git log relative to repoRoot. Returns nil when not a git repo or when no commits exist for the file. Errors are silently swallowed — caller treats this as an optional enrichment.

Each CommitInfo includes Hash (short), Author, Date, Message (subject), and Body (first 200 chars of commit body, empty when none).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL