Skip to content

Commit ad776f2

Browse files
NSHkrNSHkr
authored andcommitted
update test apps
1 parent b5a773d commit ad776f2

5 files changed

Lines changed: 810 additions & 116 deletions

File tree

test_apps/elixir_analyzer_demo/lib/elixir_analyzer_demo.ex

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule ElixirAnalyzerDemo do
2626
"""
2727

2828
alias ElixirScope.ASTRepository.{EnhancedRepository, MemoryManager}
29-
alias ElixirAnalyzerDemo.{AnalysisEngine, SampleDataManager, PerformanceMonitor}
29+
alias ElixirAnalyzerDemo.{SampleDataManager, PerformanceMonitor}
3030

3131
@doc """
3232
Loads sample projects for analysis and demonstration.
@@ -280,6 +280,64 @@ defmodule ElixirAnalyzerDemo do
280280
end)
281281
end
282282

283+
defp create_complex_ast(module_name), do: create_complex_ast(module_name, [])
284+
defp create_complex_ast(module_name, opts) do
285+
complexity = Keyword.get(opts, :complexity, :medium)
286+
287+
case complexity do
288+
:high ->
289+
quote do
290+
defmodule unquote(module_name) do
291+
def complex_logic(data) do
292+
case data do
293+
%{type: :process, items: items} when is_list(items) ->
294+
items
295+
|> Enum.filter(&valid_item?/1)
296+
|> Enum.group_by(&get_category/1)
297+
|> Enum.map(fn {category, items} ->
298+
{category, process_category(category, items)}
299+
end)
300+
|> Enum.into(%{})
301+
302+
%{type: :aggregate, data: data} ->
303+
aggregate_data(data)
304+
305+
_ ->
306+
{:error, :invalid_data}
307+
end
308+
end
309+
310+
defp valid_item?(%{id: id, value: value}) when is_integer(id) and is_number(value) do
311+
value > 0
312+
end
313+
defp valid_item?(_), do: false
314+
315+
defp get_category(%{category: cat}), do: cat
316+
defp get_category(_), do: :unknown
317+
318+
defp process_category(:important, items) do
319+
items |> Enum.map(&enhance_item/1) |> Enum.sort_by(& &1.priority, :desc)
320+
end
321+
defp process_category(_, items), do: items
322+
323+
defp enhance_item(item), do: Map.put(item, :priority, calculate_priority(item))
324+
defp calculate_priority(%{value: value}), do: value * 1.5
325+
326+
defp aggregate_data(data) when is_list(data) do
327+
%{
328+
count: length(data),
329+
sum: Enum.sum(data),
330+
average: Enum.sum(data) / length(data)
331+
}
332+
end
333+
end
334+
end
335+
336+
_ ->
337+
create_sample_ast(module_name)
338+
end
339+
end
340+
283341
defp benchmark_operations do
284342
# Module lookup benchmark
285343
{time_us, _result} = :timer.tc(fn ->
@@ -351,63 +409,6 @@ defmodule ElixirAnalyzerDemo do
351409
end
352410
end
353411

354-
defp create_complex_ast(module_name, opts \\ []) do
355-
complexity = Keyword.get(opts, :complexity, :medium)
356-
357-
case complexity do
358-
:high ->
359-
quote do
360-
defmodule unquote(module_name) do
361-
def complex_logic(data) do
362-
case data do
363-
%{type: :process, items: items} when is_list(items) ->
364-
items
365-
|> Enum.filter(&valid_item?/1)
366-
|> Enum.group_by(&get_category/1)
367-
|> Enum.map(fn {category, items} ->
368-
{category, process_category(category, items)}
369-
end)
370-
|> Enum.into(%{})
371-
372-
%{type: :aggregate, data: data} ->
373-
aggregate_data(data)
374-
375-
_ ->
376-
{:error, :invalid_data}
377-
end
378-
end
379-
380-
defp valid_item?(%{id: id, value: value}) when is_integer(id) and is_number(value) do
381-
value > 0
382-
end
383-
defp valid_item?(_), do: false
384-
385-
defp get_category(%{category: cat}), do: cat
386-
defp get_category(_), do: :unknown
387-
388-
defp process_category(:important, items) do
389-
items |> Enum.map(&enhance_item/1) |> Enum.sort_by(& &1.priority, :desc)
390-
end
391-
defp process_category(_, items), do: items
392-
393-
defp enhance_item(item), do: Map.put(item, :priority, calculate_priority(item))
394-
defp calculate_priority(%{value: value}), do: value * 1.5
395-
396-
defp aggregate_data(data) when is_list(data) do
397-
%{
398-
count: length(data),
399-
sum: Enum.sum(data),
400-
average: Enum.sum(data) / length(data)
401-
}
402-
end
403-
end
404-
end
405-
406-
_ ->
407-
create_sample_ast(module_name)
408-
end
409-
end
410-
411412
defp format_bytes(bytes) when bytes < 1024, do: "#{bytes} B"
412413
defp format_bytes(bytes) when bytes < 1024 * 1024, do: "#{Float.round(bytes / 1024, 1)} KB"
413414
defp format_bytes(bytes) when bytes < 1024 * 1024 * 1024, do: "#{Float.round(bytes / (1024 * 1024), 1)} MB"

test_apps/elixir_analyzer_demo/lib/elixir_analyzer_demo/analysis_engine.ex

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule ElixirAnalyzerDemo.AnalysisEngine do
1313

1414
use GenServer
1515

16-
alias ElixirScope.ASTRepository.{EnhancedRepository, MemoryManager}
16+
alias ElixirScope.ASTRepository.EnhancedRepository
1717

1818
def start_link(opts) do
1919
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
@@ -392,7 +392,7 @@ defmodule ElixirAnalyzerDemo.AnalysisEngine do
392392
defp check_inefficient_patterns(ast, bottlenecks) do
393393
{_, new_bottlenecks} = Macro.prewalk(ast, bottlenecks, fn
394394
# Check for inefficient list operations
395-
{{:., _, [{:__aliases__, _, [:Enum]}, :map]}, _, [list, {{:., _, [{:__aliases__, _, [:Enum]}, :filter]}, _, _}]}, acc ->
395+
{{:., _, [{:__aliases__, _, [:Enum]}, :map]}, _, [_list, {{:., _, [{:__aliases__, _, [:Enum]}, :filter]}, _, _}]}, acc ->
396396
bottleneck = %{
397397
type: :inefficient_enum_chain,
398398
severity: :medium,
@@ -498,7 +498,7 @@ defmodule ElixirAnalyzerDemo.AnalysisEngine do
498498
# Utility functions
499499

500500
defp update_analysis_results(module_name, analysis_result) do
501-
EnhancedRepository.update_enhanced_module(module_name, [
501+
EnhancedRepository.store_enhanced_module(module_name, [
502502
metadata: %{
503503
complexity_score: analysis_result.complexity.score,
504504
dependency_count: analysis_result.dependencies.total_dependencies,
@@ -563,8 +563,27 @@ defmodule ElixirAnalyzerDemo.AnalysisEngine do
563563

564564
defp count_patterns(_head), do: 1
565565
defp has_guards_or_pattern_matching?(_params), do: false
566-
defp has_interpolation?(_parts), do: false
567-
defp looks_like_sql?(_parts), do: false
566+
567+
defp has_interpolation?(parts) when is_list(parts) do
568+
Enum.any?(parts, fn
569+
{:"::", _, [expr, _type]} -> not is_binary(expr)
570+
binary when is_binary(binary) -> false
571+
_ -> true
572+
end)
573+
end
574+
defp has_interpolation?(_), do: false
575+
576+
defp looks_like_sql?(parts) when is_list(parts) do
577+
sql_keywords = ~w(SELECT INSERT UPDATE DELETE FROM WHERE JOIN)
578+
parts_string = Enum.map_join(parts, "", fn
579+
binary when is_binary(binary) -> binary
580+
_ -> ""
581+
end)
582+
String.upcase(parts_string)
583+
|> then(&Enum.any?(sql_keywords, fn kw -> String.contains?(&1, kw) end))
584+
end
585+
defp looks_like_sql?(_), do: false
586+
568587
defp count_by_severity(issues), do: Enum.group_by(issues, & &1.severity) |> Enum.map(fn {k, v} -> {k, length(v)} end) |> Enum.into(%{})
569588
defp calculate_risk_score(issues), do: length(issues) * 10
570589
defp estimate_performance_score(_bottlenecks), do: 75.0

test_apps/elixir_analyzer_demo/lib/elixir_analyzer_demo/application.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ defmodule ElixirAnalyzerDemo.Application do
6060
Supervisor.start_link(children, opts)
6161
end
6262

63-
# Tell Phoenix to update the endpoint configuration
64-
# whenever the application is updated.
63+
def stop(_state) do
64+
:ok
65+
end
66+
67+
# Optional callbacks
68+
6569
@impl true
6670
def config_change(changed, _new, removed) do
67-
if phoenix_enabled?() do
68-
ElixirAnalyzerDemo.Endpoint.config_change(changed, removed)
69-
end
71+
# Remove the endpoint config_change call since we don't have an Endpoint module
7072
:ok
7173
end
7274

0 commit comments

Comments
 (0)