From 16227c874f675ce29f5d53f6f030af56e3a8b1dd Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Wed, 9 Aug 2023 11:23:45 +0200 Subject: [PATCH] Updating location data. --- lib/BurguillosInfo/DB/Migrations.pm | 45 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/BurguillosInfo/DB/Migrations.pm b/lib/BurguillosInfo/DB/Migrations.pm index 065237c..6d4a97f 100644 --- a/lib/BurguillosInfo/DB/Migrations.pm +++ b/lib/BurguillosInfo/DB/Migrations.pm @@ -38,6 +38,10 @@ sub MIGRATIONS { 'CREATE INDEX request_country_index on requests (country);', 'ALTER TABLE requests ADD COLUMN subdivision TEXT;', 'CREATE INDEX request_subdivision_index on requests (subdivision);', + \&_populate_locations, + \&_populate_locations, + \&_populate_locations, + \&_populate_locations, ); } @@ -45,25 +49,32 @@ sub _populate_locations ($dbh) { require BurguillosInfo; require BurguillosInfo::Tracking; my $tracking = BurguillosInfo::Tracking->new( BurguillosInfo->new ); - my $page = 0; + my $page = 0; while (1) { - my $data = $dbh->selectall_arrayref( <<"EOF", { Slice => {} } ); -SELECT uuid, remote_address -FROM requests -WHERE date > NOW() - interval '2 months' -OFFSET $page -LIMIT 100; -EOF - if (!@$data) { - return; - } - for my $request (@$data) { - my ( $uuid, $remote_address ) = - $request->@{ 'uuid', 'remote_address' }; - $tracking->update_country_and_subdivision( $dbh, $uuid, - $remote_address ); - } + last if !_update_request_page( $dbh, $tracking, $page ); $page += 100; } } + +sub _update_request_page ( $dbh, $tracking, $page ) { + my $data = $dbh->selectall_arrayref( <<"EOF", { Slice => {} }, $page ); +SELECT uuid, remote_address +FROM requests +WHERE date > NOW() - interval '1 month' + AND country IS NULL + AND subdivision IS NULL +ORDER BY date desc +OFFSET $page +LIMIT ?; +EOF + if ( !@$data ) { + return; + } + for my $request (@$data) { + my ( $uuid, $remote_address ) = $request->@{ 'uuid', 'remote_address' }; + $tracking->update_country_and_subdivision( $dbh, $uuid, + $remote_address ); + } + return 1; +} 1;