Ragex.Analysis.QualityStore (Ragex v0.10.1)

View Source

Stores and queries code quality metrics in the knowledge graph.

Extends the graph with quality metrics nodes that store analysis results from MetastaticBridge. Provides querying capabilities for finding code that exceeds quality thresholds and generating project-wide statistics.

Usage

alias Ragex.Analysis.{MetastaticBridge, QualityStore}

# Analyze and store metrics
{:ok, result} = MetastaticBridge.analyze_file("lib/my_module.ex")
:ok = QualityStore.store_metrics(result)

# Query metrics
{:ok, metrics} = QualityStore.get_metrics("lib/my_module.ex")

# Find complex files
complex_files = QualityStore.find_by_threshold(:cyclomatic, 10)

# Get project stats
stats = QualityStore.project_stats()

Summary

Functions

Clears all quality metrics from the graph.

Returns the number of files with quality metrics stored.

Finds all files where a specific metric exceeds a threshold.

Finds impure functions (files with side effects).

Finds files with any warning.

Retrieves quality metrics for a specific file.

Returns the top N most complex files.

Returns project-wide quality statistics.

Returns quality metrics grouped by language.

Stores quality metrics for a file in the knowledge graph.

Functions

clear_all()

@spec clear_all() :: :ok

Clears all quality metrics from the graph.

Removes only quality_metrics nodes, leaving other graph data intact.

Examples

:ok = QualityStore.clear_all()

count()

@spec count() :: non_neg_integer()

Returns the number of files with quality metrics stored.

Examples

count = QualityStore.count()  # => 42

find_by_threshold(metric, threshold, opts \\ [])

@spec find_by_threshold(metric :: atom(), threshold :: number(), opts :: keyword()) ::
  [String.t()]

Finds all files where a specific metric exceeds a threshold.

Options

  • :operator - Comparison operator: :gt, :gte, :lt, :lte, :eq (default: :gt)

Examples

# Find files with cyclomatic complexity > 10
files = QualityStore.find_by_threshold(:cyclomatic, 10)

# Find files with cognitive complexity >= 15
files = QualityStore.find_by_threshold(:cognitive, 15, operator: :gte)

find_impure()

@spec find_impure() :: [String.t()]

Finds impure functions (files with side effects).

Examples

impure_files = QualityStore.find_impure()

find_with_warnings()

@spec find_with_warnings() :: [{String.t(), [String.t()]}]

Finds files with any warning.

Examples

files_with_warnings = QualityStore.find_with_warnings()

get_metrics(path)

@spec get_metrics(path :: String.t()) :: {:ok, map()} | {:error, :not_found}

Retrieves quality metrics for a specific file.

Examples

{:ok, metrics} = QualityStore.get_metrics("lib/my_module.ex")
metrics.cyclomatic  # => 5

most_complex(opts \\ [])

@spec most_complex(opts :: keyword()) :: [{String.t(), number()}]

Returns the top N most complex files.

Options

  • :metric - Which metric to use: :cyclomatic (default), :cognitive, :nesting
  • :limit - Number of results (default: 10)

Examples

top_complex = QualityStore.most_complex(metric: :cyclomatic, limit: 5)

project_stats()

@spec project_stats() :: map()

Returns project-wide quality statistics.

Aggregates metrics across all analyzed files.

Examples

stats = QualityStore.project_stats()
stats.total_files           # => 42
stats.avg_cyclomatic        # => 3.5
stats.files_with_warnings   # => 5

stats_by_language()

@spec stats_by_language() :: %{required(atom()) => map()}

Returns quality metrics grouped by language.

Examples

by_lang = QualityStore.stats_by_language()
by_lang[:elixir].avg_cyclomatic  # => 4.2

store_metrics(result)

@spec store_metrics(analysis_result :: map()) :: :ok | {:error, term()}

Stores quality metrics for a file in the knowledge graph.

Creates or updates a quality_metrics node with the analysis results.

Examples

result = %{
  path: "lib/my_module.ex",
  language: :elixir,
  complexity: %{cyclomatic: 5, cognitive: 3},
  purity: %{pure?: false, effects: [:io]},
  timestamp: DateTime.utc_now()
}

:ok = QualityStore.store_metrics(result)