Class: Dap::Filter::FilterGeoIP2LegacyCompat

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/dap/filter/geoip2.rb

Overview

Convert GeoIP2 data as closely as possible to the legacy GeoIP data as generated by geo_ip, geo_ip_asn and geo_ip_org

Instance Attribute Summary collapse

Attributes included from Base

#name, #opts

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FilterGeoIP2LegacyCompat

Returns a new instance of FilterGeoIP2LegacyCompat.



208
209
210
211
212
# File 'lib/dap/filter/geoip2.rb', line 208

def initialize(args)
  super
  fail "Expected 1 arguments to '#{self.name}' but got #{args.size}" unless args.size == 1
  self.base_field = args.first
end

Instance Attribute Details

#base_fieldObject

Returns the value of attribute base_field.



206
207
208
# File 'lib/dap/filter/geoip2.rb', line 206

def base_field
  @base_field
end

Instance Method Details

#process(doc) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/dap/filter/geoip2.rb', line 214

def process(doc)
  # all of these values we just take directly and rename
  remap = {
    # geoip2 name -> geoip name
    "city.country.iso_code": "country_code",
    "city.country.name": "country_name",
    "city.postal.code": "postal_code",
    "city.location.latitude": "latitude",
    "city.location.longitude": "longitude",
    "city.city.name": "city",
    "city.subdivisions.0.iso_code": "region",
    "city.subdivisions.0.name": "region_name",
    "asn.asn": "asn",
    "isp.asn": "asn",
  }

  remap.each_pair do |geoip2,geoip|
    geoip2_key = "#{self.base_field}.geoip2.#{geoip2}"
    if doc.include?(geoip2_key)
      doc["#{self.base_field}.#{geoip}"] = doc[geoip2_key]
    end
  end

  # these values all require special handling

  # https://dev.maxmind.com/geoip/geoip2/whats-new-in-geoip2/#Custom_Country_Codes
  # which basically says if traits.is_anonymous_proxy is true, previously the
  # country_code would have had a special value of A1.  Similarly, if
  # traits.is_satellite_provider is true, previously the country_code would
  # have a special value of A2.
  anon_key = "#{self.base_field}.geoip2.city.traits.is_anonymous_proxy"
  if doc.include?(anon_key)
    anon_value = doc[anon_key]
    if anon_value == "true"
      doc["#{self.base_field}.country_code"] = "A1"
    end
  end

  satellite_key = "#{self.base_field}.geoip2.city.traits.is_satellite_provider"
  if doc.include?(satellite_key)
    satellite_value = doc[satellite_key]
    if satellite_value == "true"
      doc["#{self.base_field}.country_code"] = "A1"
    end
  end

  # only set dma_code if location.metro_code was set and not empty or 0
  metro_key = "#{self.base_field}.geoip2.city.location.metro_code}"
  if doc.include?(metro_key)
    metro_value = doc[metro_key]
    if !metro_value.empty? && metro_value != "0"
      doc["#{self.base_field}.dma_code"] = metro_value
    end
  end

  # get the org key from 3 possible fields in decreasing order of preference
  asn_org_key = "#{self.base_field}.geoip2.asn.asn_org"
  isp_asn_org_key = "#{self.base_field}.geoip2.isp.asn_org"
  isp_org_key = "#{self.base_field}.geoip2.isp.asn_org"
  [ isp_org_key, isp_asn_org_key, asn_org_key ].each do |k|
    v = doc[k]
    if v && !v.empty?
      doc["#{self.base_field}.org"] = v
      break
    end
  end

  [ doc ]
end