Class: Profiler::Collectors::AjaxCollector

Inherits:
BaseCollector show all
Defined in:
lib/profiler/collectors/ajax_collector.rb

Instance Attribute Summary

Attributes inherited from BaseCollector

#profile

Instance Method Summary collapse

Methods inherited from BaseCollector

descendants, #has_data?, inherited, #name, #panel_content, #render_html, #render_mode

Constructor Details

#initialize(profile, storage: Profiler.storage) ⇒ AjaxCollector

Returns a new instance of AjaxCollector.



8
9
10
11
# File 'lib/profiler/collectors/ajax_collector.rb', line 8

def initialize(profile, storage: Profiler.storage)
  super(profile)
  @storage = storage
end

Instance Method Details

#collectObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/profiler/collectors/ajax_collector.rb', line 36

def collect
  ajax_profiles = @storage.find_by_parent(@profile.token)

  return store_data({}) if ajax_profiles.empty?

  # Generate summary statistics
  data = {
    "total_requests" => ajax_profiles.size,
    "total_duration" => ajax_profiles.sum(&:duration).round(2),
    "by_method" => group_by_method(ajax_profiles),
    "by_status" => group_by_status(ajax_profiles),
    "requests" => ajax_profiles.map { |p| request_summary(p) }
  }

  store_data(data)
end

#iconObject



13
14
15
# File 'lib/profiler/collectors/ajax_collector.rb', line 13

def icon
  "🌐"
end

#priorityObject



17
18
19
# File 'lib/profiler/collectors/ajax_collector.rb', line 17

def priority
  25
end

#subscribeObject



32
33
34
# File 'lib/profiler/collectors/ajax_collector.rb', line 32

def subscribe
  # Passive collector - no subscriptions needed
end

#tab_configObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/profiler/collectors/ajax_collector.rb', line 21

def tab_config
  {
    key: "ajax",
    label: "AJAX",
    icon: icon,
    priority: priority,
    enabled: true,
    default_active: false
  }
end

#toolbar_summaryObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/profiler/collectors/ajax_collector.rb', line 53

def toolbar_summary
  return "" unless @data && @data["total_requests"]&.positive?

  total = @data["total_requests"]
  duration = @data["total_duration"]

  # Color coding based on number of requests
  color = if total > 20
            "orange"
          elsif total > 50
            "red"
          else
            "green"
          end

  {
    text: "#{total} AJAX (#{duration}ms)",
    color: color
  }
end