• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nshkrdotcom / ElixirScope / 5cec2b5f3d6eeb9cd1b58af4e46a4f2515dd5de6

29 May 2025 07:57PM UTC coverage: 58.426% (-0.03%) from 58.453%
5cec2b5f3d6eeb9cd1b58af4e46a4f2515dd5de6

push

github

NSHkr
refactor module_data

144 of 188 new or added lines in 6 files covered. (76.6%)

2 existing lines in 2 files now uncovered.

6081 of 10408 relevant lines covered (58.43%)

3205.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.0
/lib/elixir_scope/ast_repository/module_data.ex
1
# ==============================================================================
2
# Core Module Data Structure
3
# ==============================================================================
4

5
defmodule ElixirScope.ASTRepository.ModuleData do
6
  @moduledoc """
7
  Core module representation with static AST and runtime correlation data.
8

9
  This is the main data structure that coordinates with specialized analyzers
10
  and processors for comprehensive module analysis.
11
  """
12

13
  alias ElixirScope.Utils
14
  alias ElixirScope.ASTRepository.ModuleData.{
15
    ASTAnalyzer,
16
    ComplexityCalculator,
17
    DependencyExtractor,
18
    PatternDetector,
19
    AttributeExtractor
20
  }
21

22
  @type module_name :: atom()
23
  @type ast_node_id :: binary()
24
  @type correlation_id :: binary()
25
  @type function_key :: {module_name(), atom(), non_neg_integer()}
26

27
  defstruct [
28
    # Core AST Information
29
    :module_name,
30
    :ast,
31
    :source_file,
32
    :compilation_hash,
33
    :compilation_timestamp,
34

35
    # Instrumentation Metadata
36
    :instrumentation_points,
37
    :ast_node_mapping,
38
    :correlation_metadata,
39

40
    # Static Analysis Results
41
    :module_type,
42
    :complexity_metrics,
43
    :dependencies,
44
    :exports,
45
    :callbacks,
46
    :patterns,
47
    :attributes,
48

49
    # Runtime Correlation Data
50
    :runtime_insights,
51
    :execution_frequency,
52
    :performance_data,
53
    :error_patterns,
54
    :message_flows,
55

56
    # Metadata
57
    :created_at,
58
    :updated_at,
59
    :version
60
  ]
61

62
  @type t :: %__MODULE__{}
63

64
  @doc """
65
  Creates a new ModuleData structure from parsed AST.
66
  """
67
  @spec new(module_name(), term(), keyword()) :: t()
68
  def new(module_name, ast, opts \\ []) do
69
    timestamp = Utils.monotonic_timestamp()
25✔
70
    source_file = Keyword.get(opts, :source_file)
25✔
71

72
    %__MODULE__{
25✔
73
      module_name: module_name,
74
      ast: ast,
75
      source_file: source_file,
76
      compilation_hash: generate_compilation_hash(ast, source_file),
77
      compilation_timestamp: timestamp,
78
      instrumentation_points: Keyword.get(opts, :instrumentation_points, []),
79
      ast_node_mapping: Keyword.get(opts, :ast_node_mapping, %{}),
80
      correlation_metadata: Keyword.get(opts, :correlation_metadata, %{}),
81
      module_type: ASTAnalyzer.detect_module_type(ast),
82
      complexity_metrics: ComplexityCalculator.calculate_metrics(ast),
83
      dependencies: DependencyExtractor.extract_dependencies(ast),
84
      exports: ASTAnalyzer.extract_exports(ast),
85
      callbacks: ASTAnalyzer.extract_callbacks(ast),
86
      patterns: PatternDetector.detect_patterns(ast),
87
      attributes: AttributeExtractor.extract_attributes(ast),
88
      runtime_insights: nil,
89
      execution_frequency: %{},
90
      performance_data: %{},
91
      error_patterns: [],
92
      message_flows: [],
93
      created_at: timestamp,
94
      updated_at: timestamp,
95
      version: "1.0.0"
96
    }
97
  end
98

99
  @doc """
100
  Updates the runtime insights for this module.
101
  """
102
  @spec update_runtime_insights(t(), map()) :: t()
103
  def update_runtime_insights(%__MODULE__{} = module_data, insights) do
104
    %{module_data |
1✔
105
      runtime_insights: insights,
106
      updated_at: Utils.monotonic_timestamp()
107
    }
108
  end
109

110
  @doc """
111
  Updates execution frequency data for a specific function.
112
  """
113
  @spec update_execution_frequency(t(), function_key(), non_neg_integer()) :: t()
114
  def update_execution_frequency(%__MODULE__{} = module_data, function_key, frequency) do
115
    updated_frequency = Map.put(module_data.execution_frequency, function_key, frequency)
×
116

NEW
117
    %{module_data |
×
118
      execution_frequency: updated_frequency,
119
      updated_at: Utils.monotonic_timestamp()
120
    }
121
  end
122

123
  @doc """
124
  Updates performance data for a specific function.
125
  """
126
  @spec update_performance_data(t(), function_key(), map()) :: t()
127
  def update_performance_data(%__MODULE__{} = module_data, function_key, performance_metrics) do
128
    updated_performance = Map.put(module_data.performance_data, function_key, performance_metrics)
×
129

NEW
130
    %{module_data |
×
131
      performance_data: updated_performance,
132
      updated_at: Utils.monotonic_timestamp()
133
    }
134
  end
135

136
  @doc """
137
  Adds an error pattern to the module's runtime data.
138
  """
139
  @spec add_error_pattern(t(), map()) :: t()
140
  def add_error_pattern(%__MODULE__{} = module_data, error_pattern) do
141
    updated_patterns = [error_pattern | module_data.error_patterns]
×
142

NEW
143
    %{module_data |
×
144
      error_patterns: updated_patterns,
145
      updated_at: Utils.monotonic_timestamp()
146
    }
147
  end
148

149
  @doc """
150
  Gets all function keys for this module.
151
  """
152
  @spec get_function_keys(t()) :: [function_key()]
153
  def get_function_keys(%__MODULE__{} = module_data) do
154
    module_data.exports
×
155
    |> Enum.map(fn {name, arity} -> {module_data.module_name, name, arity} end)
×
156
  end
157

158
  @doc """
159
  Checks if the module has runtime correlation data.
160
  """
161
  @spec has_runtime_data?(t()) :: boolean()
162
  def has_runtime_data?(%__MODULE__{} = module_data) do
163
    not is_nil(module_data.runtime_insights) or
×
164
    map_size(module_data.execution_frequency) > 0 or
×
165
    map_size(module_data.performance_data) > 0 or
×
166
    length(module_data.error_patterns) > 0
×
167
  end
168

169
  @doc """
170
  Gets the correlation IDs associated with this module.
171
  """
172
  @spec get_correlation_ids(t()) :: [correlation_id()]
173
  def get_correlation_ids(%__MODULE__{} = module_data) do
174
    Map.keys(module_data.correlation_metadata)
×
175
  end
176

177
  @doc """
178
  Gets the AST node IDs for this module.
179
  """
180
  @spec get_ast_node_ids(t()) :: [ast_node_id()]
181
  def get_ast_node_ids(%__MODULE__{} = module_data) do
182
    Map.keys(module_data.ast_node_mapping)
×
183
  end
184

185
  # Private helper
186
  defp generate_compilation_hash(ast, source_file) do
187
    content = "#{inspect(ast)}#{source_file}"
25✔
188
    :crypto.hash(:sha256, content) |> Base.encode16(case: :lower)
25✔
189
  end
190
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc