summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command/maintenance.pm
blob: 2db94408537f7780c86e8338a9935cc0cd7d5843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
86
87
88
89
90
91
92
93
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
140
141
142
143
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package Travelynx::Command::maintenance;
use Mojo::Base 'Mojolicious::Command';

use DateTime;

has description => 'Prune unverified users, incomplete checkins etc';

has usage => sub { shift->extract_usage };

sub run {
	my ( $self, $filename ) = @_;

	my $now                   = DateTime->now( time_zone => 'Europe/Berlin' );
	my $checkin_deadline      = $now->clone->subtract( hours => 48 );
	my $verification_deadline = $now->clone->subtract( hours => 48 );
	my $deletion_deadline     = $now->clone->subtract( hours => 72 );
	my $old_deadline          = $now->clone->subtract( years => 1 );

	my $db = $self->app->pg->db;
	my $tx = $db->begin;

	my $res = $db->delete( 'in_transit',
		{ checkin_time => { '<', $checkin_deadline } } );

	if ( my $rows = $res->rows ) {
		printf( "Removed %d incomplete checkins\n", $rows );
	}

	my $unverified = $db->select(
		'users',
		'id, email, extract(epoch from registered_at) as registered_ts',
		{
			status        => 0,
			registered_at => { '<', $verification_deadline }
		}
	);

	for my $user ( $unverified->hashes->each ) {
		my $mail     = $user->{email};
		my $reg_date = DateTime->from_epoch(
			epoch     => $user->{registered_ts},
			time_zone => 'Europe/Berlin'
		);

		my $pending
		  = $db->select( 'mail_blacklist', ['num_tries'], { email => $mail } );
		my $pending_h = $pending->hash;

		if ($pending_h) {
			my $num_tries = $pending_h->{num_tries} + 1;
			$db->update(
				'mail_blacklist',
				{
					num_tries => $num_tries,
					last_try  => $reg_date
				},
				{ email => $mail }
			);
		}
		else {
			$db->insert(
				'mail_blacklist',
				{
					email     => $mail,
					num_tries => 1,
					last_try  => $reg_date
				}
			);
		}
		$db->delete( 'pending_registrations', { user_id => $user->{id} } );
		$db->delete( 'users',                 { id      => $user->{id} } );
		printf( "Pruned unverified user %d\n", $user->{id} );
	}

	$res = $db->delete( 'pending_passwords',
		{ requested_at => { '<', $verification_deadline } } );

	if ( my $rows = $res->rows ) {
		printf( "Pruned %d pending password reset(s)\n", $rows );
	}

	$res = $db->delete( 'pending_mails',
		{ requested_at => { '<', $verification_deadline } } );

	if ( my $rows = $res->rows ) {
		printf( "Pruned %d pending mail change(s)\n", $rows );
	}

	my $to_delete = $db->select( 'users', ['id'],
		{ deletion_requested => { '<', $deletion_deadline } } );
	my @uids_to_delete = $to_delete->arrays->map( sub { shift->[0] } )->each;

	$to_delete
	  = $db->select( 'users', ['id'], { last_seen => { '<', $old_deadline } } );

	push( @uids_to_delete,
		$to_delete->arrays->map( sub { shift->[0] } )->each );

	if ( @uids_to_delete > 10 ) {
		printf STDERR (
			"About to delete %d accounts, which is quite a lot.\n",
			scalar @uids_to_delete
		);
		say STDERR 'Aborting maintenance. Please investigate.';
		exit(1);
	}

	for my $uid (@uids_to_delete) {
		say "Deleting uid ${uid}...";
		my $tokens_res   = $db->delete( 'tokens',        { user_id => $uid } );
		my $stats_res    = $db->delete( 'journey_stats', { user_id => $uid } );
		my $journeys_res = $db->delete( 'journeys',      { user_id => $uid } );
		my $transit_res  = $db->delete( 'in_transit',    { user_id => $uid } );
		my $hooks_res    = $db->delete( 'webhooks',      { user_id => $uid } );
		my $trwl_res     = $db->delete( 'traewelling',   { user_id => $uid } );

		# TODO + traewelling, webhooks
		my $password_res
		  = $db->delete( 'pending_passwords', { user_id => $uid } );
		my $user_res = $db->delete( 'users', { id => $uid } );

		printf( "    %d tokens, %d monthly stats, %d journeys\n",
			$tokens_res->rows, $stats_res->rows, $journeys_res->rows );

		if ( $user_res->rows != 1 ) {
			printf STDERR (
				"Deleted %d rows from users, expected 1. Rollback and abort.\n",
				$user_res->rows
			);
			exit(1);
		}
	}

	$tx->commit;

	# Add estimated polylines to journeys logged before 2020-01-28

	$tx = $db->begin;

	say 'Adding polylines to journeys logged before 2020-01-28';
	my $no_polyline
	  = $db->select( 'journeys', 'count(*) as count', { polyline_id => undef } )
	  ->hash;
	say "Checking $no_polyline->{count} journeys ...";

	for my $journey (
		$db->select( 'journeys', [ 'id', 'route' ], { polyline_id => undef } )
		->hashes->each )
	{

		# prior to v1.9.4, routes were stored as [["stop1"], ["stop2"], ...].
		# Nowadays, the common format is [["stop1", {}, null], ...].
		# entry[1] is non-empty only while checked in, entry[2] is non-null only
		# if the stop is unscheduled or has been cancelled.
		#
		# Here, we pretend to use the new format, as we're looking for
		# matching routes in more recent journeys.
		#
		# Note that journey->{route} is serialized JSON (i.e., a string).
		# It is not deserialized for performance reasons.
		$journey->{route}
		  =~ s/ (?<! additional ) (?<! cancelled ) "] /", {}, null]/gx;

		my $ref = $db->select(
			'journeys',
			[ 'id', 'polyline_id' ],
			{
				route       => $journey->{route},
				polyline_id => { '!=', undef },
				edited      => 0,
			},
			{ limit => 1 }
		)->hash;
		if ($ref) {
			my $rows = $db->update(
				'journeys',
				{ polyline_id => $ref->{polyline_id} },
				{ id          => $journey->{id} }
			)->rows;
			if ( $rows != 1 ) {
				say STDERR
"Database update returned $rows rows, expected 1. Rollback and abort.";
				exit(1);
			}
		}
		else {
			while ( my ( $old_name, $new_name )
				= each %{ $self->app->renamed_station } )
			{
				$journey->{route} =~ s{"\Q$old_name\E"}{"$new_name"};
			}
			my $ref = $db->select(
				'journeys',
				[ 'id', 'polyline_id' ],
				{
					route       => $journey->{route},
					polyline_id => { '!=', undef },
					edited      => 0,
				},
				{ limit => 1 }
			)->hash;
			if ($ref) {
				my $rows = $db->update(
					'journeys',
					{ polyline_id => $ref->{polyline_id} },
					{ id          => $journey->{id} }
				)->rows;
				if ( $rows != 1 ) {
					say STDERR
"Database update returned $rows rows, expected 1. Rollback and abort.";
					exit(1);
				}
			}
		}
	}

	my $remaining
	  = $db->select( 'journeys', 'count(*) as count', { polyline_id => undef } )
	  ->hash;
	say "Done! Remaining journeys without polyline: " . $remaining->{count};

	$tx->commit;
}

1;

__END__

=head1 SYNOPSIS

  Usage: index.pl maintenance

  Prunes unverified users.