summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Command/integritycheck.pm
blob: 4cfc48d202a0909c08ad37b059e93f05f95a1048 (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
package Travelynx::Command::integritycheck;

# Copyright (C) 2022 Daniel Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later

use Mojo::Base 'Mojolicious::Command';
use List::Util qw();
use Travel::Status::DE::IRIS::Stations;

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

	my %station
	  = map { $_->[2] => 1 } Travel::Status::DE::IRIS::Stations::get_stations();

	my @journey_stations;

	my $res
	  = $self->app->pg->db->select( 'journeys', ['checkin_station_id'], {},
		{ group_by => ['checkin_station_id'] } );
	for my $j ( $res->hashes->each ) {
		push( @journey_stations, $j->{checkin_station_id} );
	}

	$res = $self->app->pg->db->select( 'journeys', ['checkout_station_id'], {},
		{ group_by => ['checkout_station_id'] } );
	for my $j ( $res->hashes->each ) {
		push( @journey_stations, $j->{checkout_station_id} );
	}

	@journey_stations = List::Util::uniq @journey_stations;
	my $found = 0;

	for my $eva (@journey_stations) {
		if ( not $station{$eva} ) {
			if ( not $found ) {
				say
'Journeys in the travelynx database contain the following unknown EVA IDs.';
				say '------------8<----------';
				say 'Travel::Status::DE::IRIS v'
				  . $Travel::Status::DE::IRIS::Stations::VERSION;
				$found = 1;
			}
			say $eva;
		}
	}
	if ($found) {
		say '------------8<----------';
		say '';
		$found = 0;
	}

	%station
	  = map { $_->[1] => 1 } Travel::Status::DE::IRIS::Stations::get_stations();
	my %notified;
	my $rename = $self->app->renamed_station;

	$res
	  = $self->app->pg->db->select( 'journeys', [ 'route', 'edited' ] )->expand;
	while ( my $j = $res->hash ) {
		if ( $j->{edited} & 0x0010 ) {
			next;
		}
		for my $stop ( @{ $j->{route} // [] } ) {
			my $stop_name = $stop->[0];
			if ( $rename->{$stop_name} ) {
				$stop_name = $rename->{$stop_name};
			}
			if ( not $station{$stop_name} and not $notified{$stop_name} ) {
				if ( not $found ) {
					say
'Journeys in the travelynx database contain the following unknown route entries.';
					say 'Note that this check ignores manual route entries.';
					say 'All reports refer to routes obtained via HAFAS/IRIS.';
					say '------------8<----------';
					say 'Travel::Status::DE::IRIS v'
					  . $Travel::Status::DE::IRIS::Stations::VERSION;
					$found = 1;
				}
				say $stop_name;
				$notified{$stop_name} = 1;
			}
		}
	}
	if ($found) {
		say '------------8<----------';
		say '';
	}
}

1;