Skip to content

Commit 054dc1b

Browse files
authored
Use a custom cache record for EnterpriseResponse (#125809) (#125813)
1 parent 266e306 commit 054dc1b

File tree

1 file changed

+153
-109
lines changed

1 file changed

+153
-109
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/MaxmindIpDataLookups.java

Lines changed: 153 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -480,121 +480,167 @@ protected Map<String, Object> transform(final DomainResponse response) {
480480
}
481481
}
482482

483-
static class Enterprise extends AbstractBase<EnterpriseResponse, EnterpriseResponse> {
483+
record CacheableEnterpriseResponse(
484+
Integer countryConfidence,
485+
Boolean isInEuropeanUnion,
486+
String countryIsoCode,
487+
String countryName,
488+
String continentCode,
489+
String continentName,
490+
String regionIsoCode,
491+
String regionName,
492+
Integer cityConfidence,
493+
String cityName,
494+
String timezone,
495+
Double latitude,
496+
Double longitude,
497+
Integer accuracyRadius,
498+
String postalCode,
499+
Integer postalConfidence,
500+
Long asn,
501+
String organizationName,
502+
boolean isHostingProvider,
503+
boolean isTorExitNode,
504+
boolean isAnonymousVpn,
505+
boolean isAnonymous,
506+
boolean isPublicProxy,
507+
boolean isResidentialProxy,
508+
String domain,
509+
String isp,
510+
String ispOrganization,
511+
String mobileCountryCode,
512+
String mobileNetworkCode,
513+
String userType,
514+
String connectionType,
515+
Boolean registeredCountryIsInEuropeanUnion,
516+
String registeredCountryIsoCode,
517+
String registeredCountryName
518+
) {}
519+
520+
static class Enterprise extends AbstractBase<EnterpriseResponse, Result<CacheableEnterpriseResponse>> {
484521
Enterprise(final Set<Database.Property> properties) {
485522
super(properties, EnterpriseResponse.class, EnterpriseResponse::new);
486523
}
487524

488525
@Override
489-
protected EnterpriseResponse cacheableRecord(EnterpriseResponse response) {
490-
return response;
526+
protected Result<CacheableEnterpriseResponse> cacheableRecord(EnterpriseResponse response) {
527+
final com.maxmind.geoip2.record.Country country = response.getCountry();
528+
final Continent continent = response.getContinent();
529+
final Subdivision subdivision = response.getMostSpecificSubdivision();
530+
final com.maxmind.geoip2.record.City city = response.getCity();
531+
final Location location = response.getLocation();
532+
final Postal postal = response.getPostal();
533+
final com.maxmind.geoip2.record.Country registeredCountry = response.getRegisteredCountry();
534+
final Traits traits = response.getTraits();
535+
536+
return new Result<>(
537+
new CacheableEnterpriseResponse(
538+
country.getConfidence(),
539+
isInEuropeanUnion(country),
540+
country.getIsoCode(),
541+
country.getName(),
542+
continent.getCode(),
543+
continent.getName(),
544+
regionIsoCode(country, subdivision),
545+
subdivision.getName(),
546+
city.getConfidence(),
547+
city.getName(),
548+
location.getTimeZone(),
549+
location.getLatitude(),
550+
location.getLongitude(),
551+
location.getAccuracyRadius(),
552+
postal.getCode(),
553+
postal.getConfidence(),
554+
traits.getAutonomousSystemNumber(),
555+
traits.getAutonomousSystemOrganization(),
556+
traits.isHostingProvider(),
557+
traits.isTorExitNode(),
558+
traits.isAnonymousVpn(),
559+
traits.isAnonymous(),
560+
traits.isPublicProxy(),
561+
traits.isResidentialProxy(),
562+
traits.getDomain(),
563+
traits.getIsp(),
564+
traits.getOrganization(),
565+
traits.getMobileCountryCode(),
566+
traits.getMobileNetworkCode(),
567+
traits.getUserType(),
568+
traits.getConnectionType() == null ? null : traits.getConnectionType().toString(),
569+
isInEuropeanUnion(registeredCountry),
570+
registeredCountry.getIsoCode(),
571+
registeredCountry.getName()
572+
),
573+
traits.getIpAddress(),
574+
traits.getNetwork().toString()
575+
);
491576
}
492577

493578
@Override
494-
protected Map<String, Object> transform(final EnterpriseResponse response) {
495-
com.maxmind.geoip2.record.Country country = response.getCountry();
496-
com.maxmind.geoip2.record.Country registeredCountry = response.getRegisteredCountry();
497-
com.maxmind.geoip2.record.City city = response.getCity();
498-
Location location = response.getLocation();
499-
Continent continent = response.getContinent();
500-
Subdivision subdivision = response.getMostSpecificSubdivision();
501-
Postal postal = response.getPostal();
502-
503-
Long asn = response.getTraits().getAutonomousSystemNumber();
504-
String organizationName = response.getTraits().getAutonomousSystemOrganization();
505-
Network network = response.getTraits().getNetwork();
506-
507-
String isp = response.getTraits().getIsp();
508-
String ispOrganization = response.getTraits().getOrganization();
509-
String mobileCountryCode = response.getTraits().getMobileCountryCode();
510-
String mobileNetworkCode = response.getTraits().getMobileNetworkCode();
511-
512-
boolean isHostingProvider = response.getTraits().isHostingProvider();
513-
boolean isTorExitNode = response.getTraits().isTorExitNode();
514-
boolean isAnonymousVpn = response.getTraits().isAnonymousVpn();
515-
boolean isAnonymous = response.getTraits().isAnonymous();
516-
boolean isPublicProxy = response.getTraits().isPublicProxy();
517-
boolean isResidentialProxy = response.getTraits().isResidentialProxy();
518-
519-
String userType = response.getTraits().getUserType();
520-
521-
String domain = response.getTraits().getDomain();
522-
523-
ConnectionTypeResponse.ConnectionType connectionType = response.getTraits().getConnectionType();
579+
protected Map<String, Object> transform(final Result<CacheableEnterpriseResponse> result) {
580+
CacheableEnterpriseResponse response = result.result();
524581

525582
Map<String, Object> data = new HashMap<>();
526583
for (Database.Property property : this.properties) {
527584
switch (property) {
528-
case IP -> data.put("ip", response.getTraits().getIpAddress());
585+
case IP -> data.put("ip", result.ip());
529586
case COUNTRY_CONFIDENCE -> {
530-
Integer countryConfidence = country.getConfidence();
531-
if (countryConfidence != null) {
532-
data.put("country_confidence", countryConfidence);
587+
if (response.countryConfidence != null) {
588+
data.put("country_confidence", response.countryConfidence);
533589
}
534590
}
535591
case COUNTRY_IN_EUROPEAN_UNION -> {
536-
Boolean isInEuropeanUnion = isInEuropeanUnion(country);
537-
if (isInEuropeanUnion != null) {
538-
data.put("country_in_european_union", isInEuropeanUnion);
592+
if (response.isInEuropeanUnion != null) {
593+
data.put("country_in_european_union", response.isInEuropeanUnion);
539594
}
540595
}
541596
case COUNTRY_ISO_CODE -> {
542-
String countryIsoCode = country.getIsoCode();
543-
if (countryIsoCode != null) {
544-
data.put("country_iso_code", countryIsoCode);
597+
if (response.countryIsoCode != null) {
598+
data.put("country_iso_code", response.countryIsoCode);
545599
}
546600
}
547601
case COUNTRY_NAME -> {
548-
String countryName = country.getName();
549-
if (countryName != null) {
550-
data.put("country_name", countryName);
602+
if (response.countryName != null) {
603+
data.put("country_name", response.countryName);
551604
}
552605
}
553606
case CONTINENT_CODE -> {
554-
String continentCode = continent.getCode();
555-
if (continentCode != null) {
556-
data.put("continent_code", continentCode);
607+
if (response.continentCode != null) {
608+
data.put("continent_code", response.continentCode);
557609
}
558610
}
559611
case CONTINENT_NAME -> {
560-
String continentName = continent.getName();
561-
if (continentName != null) {
562-
data.put("continent_name", continentName);
612+
if (response.continentName != null) {
613+
data.put("continent_name", response.continentName);
563614
}
564615
}
565616
case REGION_ISO_CODE -> {
566-
String regionIsoCode = regionIsoCode(country, subdivision);
567-
if (regionIsoCode != null) {
568-
data.put("region_iso_code", regionIsoCode);
617+
if (response.regionIsoCode != null) {
618+
data.put("region_iso_code", response.regionIsoCode);
569619
}
570620
}
571621
case REGION_NAME -> {
572-
String subdivisionName = subdivision.getName();
573-
if (subdivisionName != null) {
574-
data.put("region_name", subdivisionName);
622+
if (response.regionName != null) {
623+
data.put("region_name", response.regionName);
575624
}
576625
}
577626
case CITY_CONFIDENCE -> {
578-
Integer cityConfidence = city.getConfidence();
579-
if (cityConfidence != null) {
580-
data.put("city_confidence", cityConfidence);
627+
if (response.cityConfidence != null) {
628+
data.put("city_confidence", response.cityConfidence);
581629
}
582630
}
583631
case CITY_NAME -> {
584-
String cityName = city.getName();
585-
if (cityName != null) {
586-
data.put("city_name", cityName);
632+
if (response.cityName != null) {
633+
data.put("city_name", response.cityName);
587634
}
588635
}
589636
case TIMEZONE -> {
590-
String locationTimeZone = location.getTimeZone();
591-
if (locationTimeZone != null) {
592-
data.put("timezone", locationTimeZone);
637+
if (response.timezone != null) {
638+
data.put("timezone", response.timezone);
593639
}
594640
}
595641
case LOCATION -> {
596-
Double latitude = location.getLatitude();
597-
Double longitude = location.getLongitude();
642+
Double latitude = response.latitude;
643+
Double longitude = response.longitude;
598644
if (latitude != null && longitude != null) {
599645
Map<String, Object> locationObject = new HashMap<>();
600646
locationObject.put("lat", latitude);
@@ -603,103 +649,101 @@ protected Map<String, Object> transform(final EnterpriseResponse response) {
603649
}
604650
}
605651
case ACCURACY_RADIUS -> {
606-
Integer accuracyRadius = location.getAccuracyRadius();
607-
if (accuracyRadius != null) {
608-
data.put("accuracy_radius", accuracyRadius);
652+
if (response.accuracyRadius != null) {
653+
data.put("accuracy_radius", response.accuracyRadius);
609654
}
610655
}
611656
case POSTAL_CODE -> {
612-
if (postal.getCode() != null) {
613-
data.put("postal_code", postal.getCode());
657+
if (response.postalCode != null) {
658+
data.put("postal_code", response.postalCode);
614659
}
615660
}
616661
case POSTAL_CONFIDENCE -> {
617-
if (postal.getConfidence() != null) {
618-
data.put("postal_confidence", postal.getConfidence());
662+
if (response.postalConfidence != null) {
663+
data.put("postal_confidence", response.postalConfidence);
619664
}
620665
}
621666
case ASN -> {
622-
if (asn != null) {
623-
data.put("asn", asn);
667+
if (response.asn != null) {
668+
data.put("asn", response.asn);
624669
}
625670
}
626671
case ORGANIZATION_NAME -> {
627-
if (organizationName != null) {
628-
data.put("organization_name", organizationName);
672+
if (response.organizationName != null) {
673+
data.put("organization_name", response.organizationName);
629674
}
630675
}
631676
case NETWORK -> {
632-
if (network != null) {
633-
data.put("network", network.toString());
677+
if (result.network() != null) {
678+
data.put("network", result.network());
634679
}
635680
}
636681
case HOSTING_PROVIDER -> {
637-
data.put("hosting_provider", isHostingProvider);
682+
data.put("hosting_provider", response.isHostingProvider);
638683
}
639684
case TOR_EXIT_NODE -> {
640-
data.put("tor_exit_node", isTorExitNode);
685+
data.put("tor_exit_node", response.isTorExitNode);
641686
}
642687
case ANONYMOUS_VPN -> {
643-
data.put("anonymous_vpn", isAnonymousVpn);
688+
data.put("anonymous_vpn", response.isAnonymousVpn);
644689
}
645690
case ANONYMOUS -> {
646-
data.put("anonymous", isAnonymous);
691+
data.put("anonymous", response.isAnonymous);
647692
}
648693
case PUBLIC_PROXY -> {
649-
data.put("public_proxy", isPublicProxy);
694+
data.put("public_proxy", response.isPublicProxy);
650695
}
651696
case RESIDENTIAL_PROXY -> {
652-
data.put("residential_proxy", isResidentialProxy);
697+
data.put("residential_proxy", response.isResidentialProxy);
653698
}
654699
case DOMAIN -> {
655-
if (domain != null) {
656-
data.put("domain", domain);
700+
if (response.domain != null) {
701+
data.put("domain", response.domain);
657702
}
658703
}
659704
case ISP -> {
660-
if (isp != null) {
661-
data.put("isp", isp);
705+
if (response.isp != null) {
706+
data.put("isp", response.isp);
662707
}
663708
}
664709
case ISP_ORGANIZATION_NAME -> {
665-
if (ispOrganization != null) {
666-
data.put("isp_organization_name", ispOrganization);
710+
if (response.ispOrganization != null) {
711+
data.put("isp_organization_name", response.ispOrganization);
667712
}
668713
}
669714
case MOBILE_COUNTRY_CODE -> {
670-
if (mobileCountryCode != null) {
671-
data.put("mobile_country_code", mobileCountryCode);
715+
if (response.mobileCountryCode != null) {
716+
data.put("mobile_country_code", response.mobileCountryCode);
672717
}
673718
}
674719
case MOBILE_NETWORK_CODE -> {
675-
if (mobileNetworkCode != null) {
676-
data.put("mobile_network_code", mobileNetworkCode);
720+
if (response.mobileNetworkCode != null) {
721+
data.put("mobile_network_code", response.mobileNetworkCode);
677722
}
678723
}
679724
case USER_TYPE -> {
680-
if (userType != null) {
681-
data.put("user_type", userType);
725+
if (response.userType != null) {
726+
data.put("user_type", response.userType);
682727
}
683728
}
684729
case CONNECTION_TYPE -> {
685-
if (connectionType != null) {
686-
data.put("connection_type", connectionType.toString());
730+
if (response.connectionType != null) {
731+
data.put("connection_type", response.connectionType);
687732
}
688733
}
689734
case REGISTERED_COUNTRY_IN_EUROPEAN_UNION -> {
690-
Boolean isInEuropeanUnion = isInEuropeanUnion(registeredCountry);
691-
if (isInEuropeanUnion != null) {
692-
data.put("registered_country_in_european_union", isInEuropeanUnion);
735+
if (response.registeredCountryIsInEuropeanUnion != null) {
736+
data.put("registered_country_in_european_union", response.registeredCountryIsInEuropeanUnion);
693737
}
694738
}
695739
case REGISTERED_COUNTRY_ISO_CODE -> {
696-
if (registeredCountry.getIsoCode() != null) {
697-
data.put("registered_country_iso_code", registeredCountry.getIsoCode());
740+
if (response.registeredCountryIsoCode != null) {
741+
data.put("registered_country_iso_code", response.registeredCountryIsoCode);
698742
}
699743
}
700744
case REGISTERED_COUNTRY_NAME -> {
701-
if (registeredCountry.getName() != null) {
702-
data.put("registered_country_name", registeredCountry.getName());
745+
if (response.registeredCountryName != null) {
746+
data.put("registered_country_name", response.registeredCountryName);
703747
}
704748
}
705749
}

0 commit comments

Comments
 (0)