From e10c9d729f9c7c9bf342e0053dc53988d4e26ff2 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Fri, 9 Oct 2015 00:42:20 +0200
Subject: most important group sanity cheks are done


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index f60b78f..5bbc645 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -31,6 +31,7 @@ use DBI;
 use constant {
   DB_VERSION => 245,
   RD_CONFIG_FILE => '/etc/rd.conf',
+  RD_INVALID_CART => 0,
   RD_MIN_CART => 1,
   RD_MAX_CART => 999999,
   RD_MIN_CUT => 1,
@@ -957,7 +958,7 @@ sub create_show_group
     return (undef, $status, $errorstring);
   }
 
-  ($cnt, $status, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, 'Y', 'Y', 'Y');
+  ($cnt, $status, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, 'Y', 'N', 'N');
   unless(defined $cnt) {
     return (undef, $status, $errorstring);
   }
diff --git a/utils/rhrd-group b/utils/rhrd-group
index 0f82c33..4f59c07 100755
--- a/utils/rhrd-group
+++ b/utils/rhrd-group
@@ -36,8 +36,8 @@ sub print_usage
                "       rd-group (check|remove|get-members|get-carts|get-reports) <groupname>\n" .
                "       rd-group add <groupname> [ <description> ]\n" .
                "       rd-group (add-member|remove-member|is-member) <groupname> <user>\n" .
-               "       rd-group set-carts <groupname> [ <low> [ <high> [ <type> [ <enforce range> ]]]\n" .
-               "       rd-group set-reports <groupname> [ <nownext> [ <traffic> [ <music> ]]]\n";
+               "       rd-group set-carts <groupname> <low> <high> [ <type> [ <enforce range> ]]\n" .
+               "       rd-group set-reports <groupname> <nownext> <traffic> <music>\n";
 }
 
 sub list
@@ -163,6 +163,12 @@ sub set_carts
 {
   my ($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_;
 
+  $low_cart = RHRD::rddb::RD_INVALID_CART unless($low_cart >= RHRD::rddb::RD_MIN_CART && $low_cart <= RHRD::rddb::RD_MAX_CART);
+  $high_cart = RHRD::rddb::RD_INVALID_CART unless($high_cart >= RHRD::rddb::RD_MIN_CART && $high_cart <= RHRD::rddb::RD_MAX_CART);
+  $cart_type = 1 unless($cart_type == 1 || $cart_type == 2);
+  $enforce_cart_range = 'Y' if ($enforce_cart_range eq 'Y' || $enforce_cart_range eq 'y' || $enforce_cart_range eq '1');
+  $enforce_cart_range = 'N' unless ($enforce_cart_range eq 'Y');
+
   my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_cart_range($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
@@ -189,6 +195,13 @@ sub set_reports
 {
   my ($ctx, $groupname, $now_next, $traffic, $music) = @_;
 
+  $now_next = 'Y' if ($now_next eq 'Y' || $now_next eq 'y' || $now_next eq '1');
+  $now_next = 'N' unless ($now_next eq 'Y');
+  $traffic = 'Y' if ($traffic eq 'Y' || $traffic eq 'y' || $traffic eq '1');
+  $traffic = 'N' unless ($traffic eq 'Y');
+  $music = 'Y' if ($music eq 'Y' || $music eq 'y' || $music eq '1');
+  $music = 'N' unless ($music eq 'Y');
+
   my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, $now_next, $traffic, $music);
   unless(defined $cnt) {
     print STDERR "$errorstring\n";
@@ -284,7 +297,7 @@ if(defined $ctx) {
     }
   }
   elsif($cmd eq "set-carts") {
-    if($num_args < 2 || $num_args > 6) {
+    if($num_args < 4 || $num_args > 6) {
       print_usage();
       $ret = 1;
     } else {
@@ -300,7 +313,7 @@ if(defined $ctx) {
     }
   }
   elsif($cmd eq "set-reports") {
-    if($num_args < 2 || $num_args > 5) {
+    if($num_args != 5) {
       print_usage();
       $ret = 1;
     } else {
diff --git a/utils/rhrd-sanity-check b/utils/rhrd-sanity-check
index a64a5b7..9dc5c43 100755
--- a/utils/rhrd-sanity-check
+++ b/utils/rhrd-sanity-check
@@ -43,22 +43,152 @@ if($num_args > 0) {
 my $ret = 0;
 
 
+
+# -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";
+  }
+
+  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 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 . "': 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 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 . "': 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);
   if(!$shows_low_cart) {
     print "$shows_high_cart: $shows_chunk_size\n";
-    return 1;
+    return -1;
+  }
+
+  my ($musicpools_low_cart, $musicpools_high_cart, $musicpools_chunk_size) = RHRD::rddb::get_musicpools_cart_range($ctx);
+  if(!$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);
+  if(!$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;
+    return -1;
   }
   for my $group (@groups) {
     next if($group eq $ctx->{'config'}{'specialgroups'}{'system'});
@@ -67,43 +197,99 @@ sub check_groups
     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;
+      return -1;
     }
     if(scalar @users == 0) {
-      print " group: '" . $group . "' has no members\n";
+      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++;
     }
 
-    my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $group);
-    if(!defined $carts[0] && defined $carts[1]) {
-      print STDERR "$carts[2]\n";
-      return 1;
+    $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(scalar @carts == 0) {
-      print " group: '" . $group . "' no carts of range are used\n";
+
+    if(!defined($type)) {
+      print " group '" . $group . "': cart range is at least partly outside of all class ranges\n";
+      $errors++;
     }
   }
-  return 0;
+  print "\n " . $errors . " errors found\n";
+
+  return $errors;
 }
 
+
 sub check_logs
 {
   my ($ctx) = @_;
 
+  my $errors = 0,
   print "logs:\n";
-  print "  check not yet implemtned!!\n";
+  print "  ... checks not yet implemtned!!\n";
+
+  print "\n " . $errors . " errors found\n";
+
+  return $errors;
 }
 
+
 sub check_dropboxes
 {
   my ($ctx) = @_;
 
+  my $errors = 0;
   print "dropboxes:\n";
-  print "  check not yet implemtned!!\n";
+  print "  ... checks not yet implemtned!!\n";
+
+  print "\n " . $errors . " errors found\n";
+
+  return $errors;
 }
 
+
 my ($ctx, $status, $errorstring) = RHRD::rddb::init();
 if(defined $ctx) {
   check_groups($ctx);
-- 
cgit v0.10.2