Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoIP2 implementation as a plugin #12699

Merged
merged 42 commits into from May 4, 2018
Merged

GeoIP2 implementation as a plugin #12699

merged 42 commits into from May 4, 2018

Conversation

sgiehl
Copy link
Member

@sgiehl sgiehl commented Apr 5, 2018

As using GeoIP2 requires a schema change on log tables to be able to track ISO region codes, GeoIP2 will be included as a new plugin. This way we can avoid an automatic schema change while updating Matomo.

This plugin will be automatically enabled for new installs. Instances updating to the new version will have the plugin included disabled. Enabling the plugin will then trigger the schema change.

As long as the GeoIP2 plugin is disabled nothing will change for Geolocation configuration, tracked data and API.

As soon as the GeoIP2 plugin is enabled some stuff changes:

Autoupdating of GeoIP databases
Autoupdating feature for databases will switch to configure updates for GeoIP2 databases.
For existing installs with configured updates for GeoIP, it will still show those settings as read only. This settings will be automatically removed as soon as GeoIP2 updates are configured

Tracking
As soon as a GeoIP2 provider is chosen region codes will be stored as ISO codes. [GeoIP Legacy used FIPS codes]. To convert old log data from FIPS to ISO the command usercountry:convert-region-codes can be used. The command will convert all codes stored before the date of switching to a GeoIP2 provider.

Changes in API
Until a GeoIP2 provider is selected nothing changes and still old FIPS codes and region names will be returned.
After switching to one all old codes included in archives will be converted [FIPS => ISO] if possible and region names will be used accordingly.

Switching back from GeoIP2 to GeoIP is only possible by disabling GeoIP2 plugin. This is on purpose as it may cause problems if FIPS and ISO codes are mixed too much in the database.

replaces #12553
fixes #4487

@sgiehl sgiehl added the Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. label Apr 5, 2018
@sgiehl sgiehl added this to the 3.4.1 milestone Apr 5, 2018
@sgiehl sgiehl mentioned this pull request Apr 5, 2018
19 tasks
Copy link
Member

@diosmosis diosmosis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments & suggestions, haven't tested locally yet.

plugins/GeoIp2/Commands/ConvertRegionCodesToIso.php Outdated Show resolved Hide resolved
plugins/GeoIp2/GeoIP2AutoUpdater.php Show resolved Hide resolved
if (file_exists($outputPath)) {
unlink($outputPath);
}
unlink($path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debugging purposes, would we want to keep the files around so users can see what was downloaded? Maybe just the archive, not the extracted files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't spent much thoughts on that. It's currently the same as it is done in the already existing GeoIPAutoUpdater.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts @mattab? If it is useful, I think it can be added in another PR, though, so no need to worry about it now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the existing GeoipAutoUpdater behavior for now 👍

plugins/GeoIp2/LocationProvider/GeoIp2.php Show resolved Hide resolved
plugins/UserCountry/API.php Outdated Show resolved Hide resolved
plugins/UserCountry/API.php Show resolved Hide resolved
plugins/UserCountry/LocationProvider.php Show resolved Hide resolved
plugins/UserCountry/LocationProvider.php Show resolved Hide resolved
tests/PHPUnit/Fixtures/ManyVisitsWithGeoIP.php Outdated Show resolved Hide resolved
@sgiehl sgiehl force-pushed the geoip2plugin branch 3 times, most recently from 20577a6 to 26df27c Compare April 6, 2018 09:22
@diosmosis
Copy link
Member

diosmosis commented Apr 6, 2018

Noticed there's a bug in pear/archive_tar which piwik/decompress uses which causes a fatal error when downloading databases on PHP 7: https://pear.php.net/bugs/bug.php?id=21218

I think matomo/decompress uses the fixed version.

EDIT: one way to fix this is to run composer require pear/archive_tar locally.

@diosmosis
Copy link
Member

@mattab tested locally all features seem to work, except unarchiving on PHP 7. Leaving up to you whether my remaining comments should be addressed.

@sgiehl sgiehl force-pushed the geoip2plugin branch 2 times, most recently from cff3c4d to 8fbe252 Compare April 7, 2018 19:18
@diosmosis
Copy link
Member

@sgiehl Looked again, looks good to me except for php 7 archive tar fatal error (see my comment above for more info).

@sgiehl
Copy link
Member Author

sgiehl commented Apr 8, 2018

I read it. But that seems not a "bug" related to the changes for GeoIP2, right? It should currently occur for GeoIP Legacy as well I guess.
Maybe we could create a issue to fix that later. I'm not able to handle that the next days, if you see an easy solution feel free to fix it ;)

@diosmosis
Copy link
Member

diosmosis commented Apr 8, 2018

Easiest fix is to run composer require pear/archive_tar locally, I'll do this later today.

@diosmosis
Copy link
Member

PHP 7 bug no longer exists after 8fbe252, the offending method is no longer called. Moving back to extractInString fixed this.

$dt->filter('GroupBy', array(
'label',
function ($label) {
if ($label == '1|ti') {
return '14|cn';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this hard-coded conversion here and start from line 172 is still required for the old format.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. My original implementation converted all codes so it wasn't needed anymore, but with this implementation it's still needed. Will add it again

@sgiehl sgiehl force-pushed the geoip2plugin branch 4 times, most recently from 69e64d0 to 4e3a77b Compare April 23, 2018 13:41
@sgiehl
Copy link
Member Author

sgiehl commented Apr 23, 2018

I've now tested the maps with geoip2 changes_

Realtime map should work as before, as it doesn't use any region data.

The other maps will lack some information on region level. (Country and City level will still work as before) All countries where the new iso codes do not match any old fips codes the map will show those visits as "unknown region". (Note: US and Canada are not affected, as they used iso codes before, also some countries where the codes didn't change between fips and iso will still show up correctly).

To have working maps on region level with geoip2 we need to create completely new maps that also show correct regions (based in ISO) and then force all location providers to use ISO region codes in order to show region data on the map

@mattab mattab added the Major Indicates the severity or impact or benefit of an issue is much higher than normal but not critical. label Apr 23, 2018
@sgiehl sgiehl force-pushed the geoip2plugin branch 2 times, most recently from c8a5cc9 to 142316c Compare April 30, 2018 10:39
@diosmosis
Copy link
Member

Did another code review, left two comments on the conversion command, will do another local test tomorrow.

sgiehl added a commit to matomo-org/plugin-CustomDimensions that referenced this pull request May 3, 2018
@mattab mattab merged commit 18ad4f7 into 3.x-dev May 4, 2018
@mattab mattab deleted the geoip2plugin branch May 4, 2018 05:15
@sgiehl sgiehl mentioned this pull request Aug 5, 2018
InfinityVoid pushed a commit to InfinityVoid/matomo that referenced this pull request Oct 11, 2018
* require geoip2/geoip2 composer package

* Determine region name based on Location Provider

* Adds empty GeoIp2 plugin

* move location_region column definition to GeoIp2 plugin
so it's get changed as soon as the plugin is activated

* Adds GeoIP2 location providers

* ignore GeoIP2 mmdb files

* Adds script to generate GeoIP2 test databases

* Adds Command to convert region codes from FIPS to ISO for old log table records

* Adds GeoIP2 AutoUpdater

* Use GeoIP2 in tests

* update test files

* code fixes

* adds tests

* rename old GeoIP providers to Legacy

* Let GeoIP autoupdater UI handle GeoIp2 as well

* convert region codes to ISO in API after switch to GeoIP2

* do not show GeoIP providers if GeoIP2 plugin is enabled an no GeoIP Legacy provider is still in use

* small fixes

* review changes

* Use correct region translations

* Show correct message if no database can be found

* if log tables have been converted, use archive date to check if region codes still need to be converted to iso

* fix tests

* Improves extracting GeoIP2 databases

* Adjust GeoLocation diagnostics

* readds old taiwan fixes

* Assume all third party location providers as 'recommended'

* Download database over HTTPS

* remove outdated comment (see matomo-org#12411)

* Remove indication that Geoip2 may be slow, since we found it should be quite fast and this should not be an issue in theory

* skip detection if IP is empty & do not try convert IP to IPv4

* remove downloaded file if an error occurs while extracting

* command should be runnable multiple times

* use ISO codes for suggested region codes

* reload after wizard success

* Drop table if exists.

* Fix two translation keys.

* add special region handling for UK

* update system test

* update UI files

* submodule update

* update test files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Major Indicates the severity or impact or benefit of an issue is much higher than normal but not critical. Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make geoip backend work with geoip2
4 participants