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

# Copyright (C) 2022 Birte Kristina 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 $found  = 0;
	my $db     = $self->app->pg->db;

	my $res1 = $db->query(
		qq{
			select checkin_station_id
			from journeys
			left join stations on journeys.checkin_station_id = stations.eva
			where stations.eva is null;
		}
	);

	my $res2 = $db->query(
		qq{
			select checkout_station_id
			from journeys
			left join stations on journeys.checkout_station_id = stations.eva
			where stations.eva is null;
		}
	);

	my %notified;
	while ( my $row = $res1->hash ) {
		my $eva = $row->{checkin_station_id};
		if ( not $found ) {
			$found = 1;
			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;
		}
		if ( not $notified{$eva} ) {
			say $eva;
			$notified{$eva} = 1;
		}
	}

	while ( my $row = $res2->hash ) {
		my $eva = $row->{checkout_station_id};
		if ( not $found ) {
			$found = 1;
			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;
		}
		if ( not $notified{$eva} ) {
			say $eva;
			$notified{$eva} = 1;
		}
	}

	if ($found) {
		say '------------8<----------';
		say '';
		$found = 0;
	}

	my $rename = $self->app->renamed_station;

	my $res = $db->select( 'journeys', [ 'route', 'edited' ] )->expand;
	while ( my $j = $res->hash ) {
		if ( $j->{edited} & 0x0010 ) {
			next;
		}
		my @stops = @{ $j->{route} // [] };
		for my $stop (@stops) {
			my $stop_name = $stop->[0];
			if ( $rename->{ $stop->[0] } ) {
				$stop->[0] = $rename->{ $stop->[0] };
			}
		}
		my @unknown
		  = $self->app->stations->grep_unknown( map { $_->[0] } @stops );
		for my $stop_name (@unknown) {
			if ( 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 '';
		$found = 0;
	}

	$res = $db->select(
		'journeys_str',
		[ 'journey_id', 'sched_arr_ts', 'route', 'arr_name', 'arr_eva' ],
		{ backend_id => 0 }
	)->expand;
	journey: while ( my $j = $res->hash ) {
		my $found_in_route;
		my $found_arr;
		for my $stop ( @{ $j->{route} // [] } ) {
			if ( not $stop->[1] ) {
				next journey;
			}
			if ( $stop->[1] == $j->{arr_eva} ) {
				$found_in_route = 1;
				last;
			}
			if (    $stop->[2]{sched_arr}
				and $j->{sched_arr_ts}
				and $stop->[2]{sched_arr} == int( $j->{sched_arr_ts} ) )
			{
				$found_arr = $stop;
			}
		}
		if ( $found_arr and not $found_in_route ) {
			if ( not $found ) {
				say q{};
				say
'The following journeys have route entries which do not agree with checkout EVA ID.';
				say
'checkout station ID (left) vs route entry with matching checkout time (right)';
				say '------------8<----------';
				$found = 1;
			}
			printf(
				"%7d  %d (%s) vs %d (%s)\n",
				$j->{journey_id}, $j->{arr_eva}, $j->{arr_name},
				$found_arr->[1],  $found_arr->[0]
			);
		}
	}
}

1;