Class: BatchEventDispatcherUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/wingify/utils/batch_event_dispatcher_util.rb

Class Method Summary collapse

Class Method Details

.dispatch(properties, callback = -> {}, query_params) ⇒ Object

Dispatches a batch of events to the VWO server

Parameters:

  • properties (Hash)

    The event properties to send

  • callback (Proc) (defaults to: -> {})

    Optional callback function to execute after the request (defaults to empty proc)

  • query_params (Hash)

    Query parameters to include in the request



35
36
37
38
# File 'lib/wingify/utils/batch_event_dispatcher_util.rb', line 35

def dispatch(properties, callback = -> {}, query_params)
  # Send the prepared payload via POST API request
  send_batch_post_api_request(query_params, properties, callback)
end

.extract_event_counts(payload) ⇒ Hash

Extracts event counts from a batch payload

Parameters:

  • payload (Hash)

    The payload containing events

Returns:

  • (Hash)

    Hash with variationShownCount, setAttributeCount, and customEventCount



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/wingify/utils/batch_event_dispatcher_util.rb', line 144

def extract_event_counts(payload)
  counts = {
    variation_shown_count: 0,
    set_attribute_count: 0,
    custom_event_count: 0
  }

  # Get all standard event names from EventEnum
  standard_event_names = EventEnum.constants.map { |const| EventEnum.const_get(const) }.to_set
  events = payload&.dig(:ev) || []

  events.each do |entry|
    name = entry&.dig(:d, :event, :name)

    next unless name

    if name == EventEnum::VARIATION_SHOWN
      counts[:variation_shown_count] += 1
      next
    end

    if name == EventEnum::SYNC_VISITOR_PROP
      counts[:set_attribute_count] += 1
      next
    end

    unless standard_event_names.include?(name)
      counts[:custom_event_count] += 1
    end
  end

  counts
end

.handle_batch_response(end_point, payload, query_params, res, raw_data, callback) ⇒ Object

Handles the response from a batch event API call

Parameters:

  • end_point (String)

    The API endpoint that was called

  • payload (Hash)

    The payload that was sent in the request

  • query_params (Hash)

    The query parameters used in the request

  • res (ResponseModel)

    The response object from the API call

  • raw_data (String)

    The raw response data from the API

  • callback (Proc)

    Optional callback to be executed after handling the response



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/wingify/utils/batch_event_dispatcher_util.rb', line 94

def handle_batch_response(end_point, payload, query_params, res, raw_data, callback)
  # TODO: update this method with debug event logs
  events_per_request = payload[:ev].length
   = query_params[:a]

  error = res.get_error
  if error
    LoggerService.log(LogLevelEnum::INFO, "IMPRESSION_BATCH_FAILED")
    LoggerService.log(LogLevelEnum::ERROR, "NETWORK_CALL_FAILED", {
      method: "#{HttpMethodEnum::POST} #{UrlEnum::BATCH_EVENTS}",
      err: error
    }, false)
    callback.call(error, payload.to_json) if callback.respond_to?(:call)
    return {status: "error", events: payload}
  else
    case res.get_status_code
    when 200
      LoggerService.log(LogLevelEnum::INFO, "IMPRESSION_BATCH_SUCCESS", {
        accountId: ,
        endPoint: end_point,
      })
      callback.call(nil, payload.to_json) if callback.respond_to?(:call)
      return {status: "success", events: payload}
    when 413
      LoggerService.log(LogLevelEnum::DEBUG, "CONFIG_BATCH_EVENT_LIMIT_EXCEEDED", {
          accountId: ,
          endPoint: end_point,
          eventsPerRequest: events_per_request
      })
      LoggerService.log(LogLevelEnum::ERROR, "NETWORK_CALL_FAILED", {
          method: "#{HttpMethodEnum::POST} #{UrlEnum::BATCH_EVENTS}",
          err: error
      }, false)
      callback.call(error, payload.to_json) if callback.respond_to?(:call)
      return {status: "error", events: payload}
    else
      LoggerService.log(LogLevelEnum::INFO, "IMPRESSION_BATCH_FAILED")
      LoggerService.log(LogLevelEnum::ERROR, "NETWORK_CALL_FAILED", {
          method: "#{HttpMethodEnum::POST} #{UrlEnum::BATCH_EVENTS}",
          err: error
      }, false)
      callback.call(error, payload.to_json) if callback.respond_to?(:call)
      return {status: "error", events: payload}
    end
  end
end

.send_batch_post_api_request(properties, payload, callback) ⇒ Object

Sends a POST API request with given properties and payload



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wingify/utils/batch_event_dispatcher_util.rb', line 41

def send_batch_post_api_request(properties, payload, callback)
  network_instance = NetworkManager.instance
  headers = {}
  headers['Authorization'] = "#{SettingsService.instance.sdk_key}"

  request = RequestModel.new(
    SettingsService.instance.events_hostname,
    HttpMethodEnum::POST,
    SettingsService.instance.get_updated_endpoint_with_collection_prefix(UrlEnum::BATCH_EVENTS, SettingsService.instance.is_gateway_service_provided),
    properties,
    payload,
    headers,
    SettingsService.instance.protocol,
    SettingsService.instance.port
  )
  
  event_counts = extract_event_counts(payload)
  extra_data = "#{Constants::BATCH_EVENTS} having"
  if event_counts[:variation_shown_count] > 0
    extra_data += "getFlag events: #{event_counts[:variation_shown_count]}, "
  end
  if event_counts[:custom_event_count] > 0
    extra_data += "conversion events: #{event_counts[:custom_event_count]}, "
  end
  if event_counts[:set_attribute_count] > 0
    extra_data += "setAttribute events: #{event_counts[:set_attribute_count]}, "
  end

  begin
    response = network_instance.post(request)
    # Only send debug event if response is valid and has retry attempts
    if response.is_a?(ResponseModel) && response.get_total_attempts && response.get_total_attempts > 0
      debug_event_props = NetworkUtil.create_network_and_retry_debug_event(response, nil, Constants::BATCH_EVENTS, extra_data)
      # send debug event
      DebuggerServiceUtil.send_debugger_event(debug_event_props) if debug_event_props
    end
    handle_batch_response(UrlEnum::BATCH_EVENTS, payload, properties, response, response.get_data, callback)
  rescue StandardError => err
    # TODO: remove this log after testing
    LoggerService.log(LogLevelEnum::ERROR, "NETWORK_CALL_FAILED", {
          method: extra_data,
          err: get_formatted_error_message(err)
      })
  end
end