write_debug_report.ex (2558B)
1 defmodule Credo.Execution.Task.WriteDebugReport do 2 @moduledoc false 3 4 @debug_template File.read!(".template.debug.html") 5 @debug_output_filename "credo-debug-log.html" 6 7 use Credo.Execution.Task 8 9 alias Credo.CLI.Output.UI 10 alias Credo.Execution.ExecutionTiming 11 12 def call(%Credo.Execution{debug: true} = exec, _opts) do 13 Logger.flush() 14 15 time_load = exec |> get_assign("credo.time.source_files", 0) |> div(1000) 16 time_run = exec |> get_assign("credo.time.run_checks", 0) |> div(1000) 17 time_total = time_load + time_run 18 19 all_timings = ExecutionTiming.all(exec) 20 started_at = ExecutionTiming.started_at(exec) 21 ended_at = ExecutionTiming.ended_at(exec) 22 all_timings = timings_to_map(all_timings) 23 24 file_timings = 25 all_timings 26 |> Enum.reduce(%{}, fn element, acc -> 27 if filename = element.tags[:filename] do 28 Map.put(acc, filename, (acc[filename] || 0) + element.duration) 29 else 30 acc 31 end 32 end) 33 |> Enum.sort_by(&elem(&1, 1)) 34 |> Enum.reverse() 35 36 check_timings = 37 all_timings 38 |> Enum.reduce(%{}, fn element, acc -> 39 if check = element.tags[:check] do 40 Map.put(acc, check, (acc[check] || 0) + element.duration) 41 else 42 acc 43 end 44 end) 45 |> Enum.sort_by(&elem(&1, 1)) 46 |> Enum.reverse() 47 48 check_file_timings = 49 all_timings 50 |> Enum.reduce(%{}, fn element, acc -> 51 filename = element.tags[:filename] 52 check = element.tags[:check] 53 54 if filename && check do 55 Map.put(acc, {check, filename}, (acc[{check, filename}] || 0) + element.duration) 56 else 57 acc 58 end 59 end) 60 |> Enum.sort_by(&elem(&1, 1)) 61 |> Enum.reverse() 62 63 assigns = [ 64 exec: exec, 65 started_at: started_at, 66 ended_at: ended_at, 67 duration: ended_at - started_at, 68 time_total: time_total, 69 time_load: time_load, 70 time_run: time_run, 71 all_timings: all_timings, 72 file_timings: file_timings, 73 check_timings: check_timings, 74 check_file_timings: check_file_timings 75 ] 76 77 content = EEx.eval_string(@debug_template, assigns: assigns) 78 79 File.write!(@debug_output_filename, content) 80 81 UI.puts([:green, "Debug log written to ", :reset, @debug_output_filename]) 82 83 exec 84 end 85 86 def call(exec, _opts) do 87 exec 88 end 89 90 def timings_to_map(list) do 91 Enum.map(list, fn {tags, started_at, duration} -> 92 %{tags: Enum.into(tags, %{}), started_at: started_at, duration: duration} 93 end) 94 end 95 end