Class: Bidi2pdf::Bidi::NetworkEvents
- Inherits:
-
Object
- Object
- Bidi2pdf::Bidi::NetworkEvents
- Defined in:
- lib/bidi2pdf/bidi/network_events.rb
Instance Attribute Summary collapse
-
#context_id ⇒ Object
readonly
Returns the value of attribute context_id.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#network_event_formatter ⇒ Object
readonly
Returns the value of attribute network_event_formatter.
Instance Method Summary collapse
-
#all_events ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #handle_event(data) ⇒ Object
-
#handle_response(method, event) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#initialize(context_id) ⇒ NetworkEvents
constructor
A new instance of NetworkEvents.
- #log_network_traffic(format: :console) ⇒ Object
- #wait_until_network_idle(timeout: 10, poll_interval: 0.01) ⇒ Object
Constructor Details
#initialize(context_id) ⇒ NetworkEvents
Returns a new instance of NetworkEvents.
11 12 13 14 15 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 11 def initialize(context_id) @context_id = context_id @events = {} @network_event_formatter = NetworkEventFormatters::NetworkEventConsoleFormatter.new end |
Instance Attribute Details
#context_id ⇒ Object (readonly)
Returns the value of attribute context_id.
9 10 11 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 9 def context_id @context_id end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
9 10 11 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 9 def events @events end |
#network_event_formatter ⇒ Object (readonly)
Returns the value of attribute network_event_formatter.
9 10 11 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 9 def network_event_formatter @network_event_formatter end |
Instance Method Details
#all_events ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
74 75 76 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 74 def all_events events.values.sort_by(&:start_timestamp) end |
#handle_event(data) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 17 def handle_event(data) event = data["params"] method = data["method"] if event["context"] == context_id handle_response(method, event) else Bidi2pdf.logger.debug3 "Ignoring Network event: #{method}, #{context_id}, params: #{event}" end rescue StandardError => e Bidi2pdf.logger.error "Error handling network event: #{e.message}" end |
#handle_response(method, event) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 31 def handle_response(method, event) return unless event && event["request"] request = event["request"] response = event["response"] http_status_code = response&.dig("status") bytes_received = response&.dig("bytesReceived") id = request["request"] url = request["url"] timing = request["timings"] http_method = request["method"] = event["timestamp"] Bidi2pdf.notification_service.instrument("network_event_received.bidi2pdf", { id: id, method: method, url: url, http_status_code: http_status_code }) do |instrumentation_payload| if method == "network.beforeRequestSent" events[id] ||= NetworkEvent.new( id: id, url: url, timestamp: , timing: timing, state: method, http_method: http_method ) elsif events.key?(id) events[id].update_state(method, timestamp: , timing: timing, http_status_code: http_status_code, bytes_received: bytes_received) else Bidi2pdf.logger.warn "Received response for unknown request ID: #{id}, URL: #{url}" end instrumentation_payload[:event] = events[id]&.dup end end |
#log_network_traffic(format: :console) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 78 def log_network_traffic(format: :console) format = format.to_sym if format == :console NetworkEventFormatters::NetworkEventConsoleFormatter.new.log all_events elsif format == :html NetworkEventFormatters::NetworkEventHtmlFormatter.new.render(all_events) else raise ArgumentError, "Unknown network event format: #{format}" end end |
#wait_until_network_idle(timeout: 10, poll_interval: 0.01) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/bidi2pdf/bidi/network_events.rb', line 90 def wait_until_network_idle(timeout: 10, poll_interval: 0.01) start_time = Time.now loop do unless events.values.any?(&:in_progress?) Bidi2pdf.logger.debug "✅ All network events completed." break end if Time.now - start_time > timeout Bidi2pdf.logger.warn "⏰ Timeout while waiting for network events to complete. Still in progress: #{in_progress.map(&:id)}" # rubocop:enable Layout/LineLength break end sleep(poll_interval) end end |