summaryrefslogtreecommitdiff
path: root/utils/rhrd-sanity-check
blob: cdfa0b445281ab067f465ac40439fac9cabda230 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/perl -w
#
#  rhrdlibs
#
#  Copyright (C) 2015-2016 Christian Pointner <equinox@helsinki.at>
#
#  This file is part of rhrdlibs.
#
#  rhrdlibs is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  rhrdlibs is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with rhrdlibs. If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use RHRD::rddb;

sub print_usage
{
  print STDERR "Usage: rhrd-sanity-check\n";
}

my $num_args = $#ARGV + 1;
if($num_args > 0) {
  print_usage();
  exit(1);
}



sub check_showids
{
  my ($ctx) = @_;

  my $errors = 0,
  print "showids:\n";

  my @showids = RHRD::rddb::list_showids($ctx);
  if(!defined $showids[0] && defined $showids[1]) {
    print STDERR "$showids[1]: $showids[2]";
    return -1;
  }
  for my $showid (@showids) {
    my @carts = RHRD::rddb::get_show_carts($ctx, $showid);
    if(!defined $carts[0] && defined $carts[1]) {
      print " showid '" . $showid . "': $carts[2]\n";
      $errors++;
    }
    if(scalar @carts == 0) {
      print " showid '" . $showid . "': log is empty\n";
      $errors++;
    }

    my ($group, $status, $errorstring) = RHRD::rddb::get_show_group($ctx, $showid);
    unless(defined $group) {
      print " showid '" . $showid . "': has no dropbox assigned\n";
      $errors++;
    } else {
    }
  }
  print "\n " . $errors . " errors found\n";

  return $errors;
}



sub check_logs
{
  my ($ctx) = @_;

  my $errors = 0,
  print "logs:\n";

  my @logs = RHRD::rddb::list_logs($ctx);
  if(!defined $logs[0] && defined $logs[1]) {
    print STDERR "$logs[1]: $logs[2]";
    return -1;
  }
  for my $log (@logs) {
    my ($log_exists, $status, $errorstring) = RHRD::rddb::check_log_exists($ctx, $log);
    unless(defined $log_exists) {
      print STDERR "$status: $errorstring";
      return -1;
    }
    unless($log_exists) {
      print " log '" . $log . "': does not exist\n";
      $errors++;
    }

    (my $log_tab_exists, $status, $errorstring) = RHRD::rddb::check_log_table_exists($ctx, $log);
    unless(defined $log_tab_exists) {
      print STDERR "$status: $errorstring";
      return -1;
    }
    if($log_tab_exists) {
      unless($log_exists) {
        print " log '" . $log . "': this log shouldn't not exist but there is a table named after it\n";
        $errors++;
      }
    } else {
      if($log_exists) {
        print " log '" . $log . "': this log should exist but there is no table named after it\n";
        $errors++;
      }
    }

  }
  print "\n " . $errors . " errors found\n";

  return $errors;
}



# -2 .. range is entirely below class range
# -1 .. range overlaps with class range (low boundary)
#  0 .. range is inside class range
#  1 .. range overlaps with class range (high boundary)
#  2 .. range is entirely above class range
sub check_groups__check_cart_range
{
  my ($low, $high, $class_low, $class_high) = @_;

  if($low < $class_low) {
    return -1 if($high >= $class_low);
    return -2;
  }

  if($low <= $class_high) {
    return 0 if($high <= $class_high);
    return 1;
  }

  return 2;
}

sub check_groups__check_show_group
{
  my ($ctx, $group, $low, $high, $type) = @_;

  my $errors = 0;

  my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $group);
  if(!defined $carts[0] && defined $carts[1]) {
    print STDERR $carts[1] . ": " . $carts[2] . "\n";
    return -1;
  }
  if(scalar @carts == 0) {
    print " group '" . $group . "': carts are not used by any show\n";
    $errors++;
  }

  my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($ctx, $group);
  unless(defined $nownext) {
    print STDERR $traffic . ": " . $music . "\n";
    return -1;
  }
  unless($nownext eq 'Y') {
    print " group '" . $group . "': show carts with now/next disabled\n";
    $errors++;
  }
  unless($traffic eq 'N') {
    print " group '" . $group . "': show carts with traffic reports enabled\n";
    $errors++;
  }
  unless($music eq 'N') {
    print " group '" . $group . "': show carts with music reports enabled\n";
    $errors++;
  }

  return $errors;
}

sub check_groups__check_musicpool_group
{
  my ($ctx, $group, $low, $high, $type) = @_;

  my $errors = 0;

  # TODO: check for pool size: should be > 150

  my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($ctx, $group);
  unless(defined $nownext) {
    print STDERR $traffic . ": " . $music . "\n";
    return -1;
  }
  unless($nownext eq 'Y') {
    print " group '" . $group . "': musicpool carts with now/next disabled\n";
    $errors++;
  }
  unless($traffic eq 'N') {
    print " group '" . $group . "': musicpool carts with traffic reports enabled\n";
    $errors++;
  }
  unless($music eq 'Y') {
    print " group '" . $group . "': musicpool carts with music reports disabled\n";
    $errors++;
  }

  return $errors;
}

sub check_groups__check_jingle_group
{
  my ($ctx, $group, $low, $high, $type) = @_;

  my $errors = 0;

  # TODO: check whether enough evergreens are imported: should be > 3

  my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($ctx, $group);
  unless(defined $nownext) {
    print STDERR $traffic . ": " . $music . "\n";
    return -1;
  }
  unless($nownext eq 'Y') {
    print " group '" . $group . "': jingle carts with now/next disabled\n";
    $errors++;
  }
  unless($traffic eq 'N') {
    print " group '" . $group . "': jingle carts with traffic reports enabled\n";
    $errors++;
  }
  unless($music eq 'N') {
    print " group '" . $group . "': jingle carts with music reports enabled\n";
    $errors++;
  }

  return $errors;
}

sub check_groups
{
  my ($ctx) = @_;

  my $errors = 0;
  print "groups:\n";

  my ($shows_low_cart, $shows_high_cart, $shows_chunk_size) = RHRD::rddb::get_shows_cart_range($ctx);
  unless(defined $shows_low_cart) {
    print "$shows_high_cart: $shows_chunk_size\n";
    return -1;
  }

  my ($musicpools_low_cart, $musicpools_high_cart, $musicpools_chunk_size) = RHRD::rddb::get_musicpools_cart_range($ctx);
  unless(defined $musicpools_low_cart) {
    print "$musicpools_high_cart: $musicpools_chunk_size\n";
    return -1;
  }

  my ($jingles_low_cart, $jingles_high_cart, $jingles_chunk_size) = RHRD::rddb::get_jingles_cart_range($ctx);
  unless(defined $jingles_low_cart) {
    print "$jingles_high_cart: $jingles_chunk_size\n";
    return -1;
  }

  my @groups = RHRD::rddb::list_groups($ctx);
  if(!defined $groups[0] && defined $groups[1]) {
    print STDERR "$groups[1]: $groups[2]";
    return -1;
  }
  for my $group (@groups) {
    next if($group eq $ctx->{'config'}{'specialgroups'}{'system'});
    next if($group eq $ctx->{'config'}{'specialgroups'}{'shows'});
    next if($group eq $ctx->{'config'}{'specialgroups'}{'multishows'});
    next if($group eq $ctx->{'config'}{'specialgroups'}{'allshows'});
    next if($group eq $ctx->{'config'}{'specialgroups'}{'allpools'});
    next if($group eq $ctx->{'config'}{'specialgroups'}{'alljingles'});

    my ($low_cart, $high_cart, $cart_type, $enforce_range) = RHRD::rddb::get_group_cart_range($ctx, $group);
    if($low_cart > $high_cart) {
      print " group '" . $group . "': cart range is invalid low > high\n";
      $errors++;
    }
    if($enforce_range ne 'Y') {
      print " group '" . $group . "': cart range is not enforced\n";
      $errors++;
    }

    my @users = RHRD::rddb::get_group_members($ctx, $group);
    if(!defined $users[0] && defined $users[1]) {
      print STDERR "$users[2]\n";
      return -1;
    }
    if(scalar @users == 0) {
      print " group '" . $group . "': has no members\n";
      $errors++;
    }

    my $type = undef;
    my $res = check_groups__check_cart_range($low_cart, $high_cart, $shows_low_cart, $shows_high_cart);
    if($res == 0) {
      $type = 'S';
      $res = check_groups__check_show_group($ctx, $group, $low_cart, $high_cart);
      return $res if $res < 0;
      $errors += $res;
    } elsif($res == -1 || $res == 1) {
      print " group '" . $group . "': cart range is overlapping with show cart range but is not a subset of it\n";
      $errors++;
    }

    $res = check_groups__check_cart_range($low_cart, $high_cart, $musicpools_low_cart, $musicpools_high_cart);
    if($res == 0) {
      $type = 'M';
      $res = check_groups__check_musicpool_group($ctx, $group, $low_cart, $high_cart);
      return $res if $res < 0;
      $errors += $res;
    } elsif($res == -1 || $res == 1) {
      print " group '" . $group . "': cart range is overlapping with musicpool cart range but is not a subset of it\n";
      $errors++;
    }

    $res = check_groups__check_cart_range($low_cart, $high_cart, $jingles_low_cart, $jingles_high_cart);
    if($res == 0) {
      $type = 'J';
      $res = check_groups__check_jingle_group($ctx, $group, $low_cart, $high_cart);
      return $res if $res < 0;
      $errors += $res;
    } elsif($res == -1 || $res == 1) {
      print " group '" . $group . "': cart range is overlapping with jingle cart range but is not a subset of it\n";
      $errors++;
    }

    if(!defined($type)) {
      print " group '" . $group . "': cart range is at least partly outside of all class ranges\n";
      $errors++;
    }
  }
  print "\n " . $errors . " errors found\n";

  return $errors;
}



sub check_dropboxes
{
  my ($ctx) = @_;

  my $errors = 0;
  print "dropboxes:\n";

  print "\n " . $errors . " errors found\n";

  return $errors;
}



my $errors = 0;
my ($ctx, $status, $errorstring) = RHRD::rddb::init();
if(defined $ctx) {
  for(;;) {
    my $ret = check_showids($ctx);
    if($ret < 0) {
      $errors = $ret;
      last;
    } else { $errors += $ret }

    print "\n";

    $ret = check_logs($ctx);
    if($ret < 0) {
      $errors = $ret;
      last;
    } else { $errors += $ret }

    print "\n";

    $ret = check_groups($ctx);
    if($ret < 0) {
      $errors = $ret;
      last;
    } else { $errors += $ret }

    print "\n";

    $ret = check_dropboxes($ctx);
    if($ret < 0) {
      $errors = $ret;
      last;
    } else { $errors += $ret }

    last;
  }
  RHRD::rddb::destroy($ctx);
} else {
  print STDERR "$errorstring\n";
  $errors = -1;
}

exit $errors;