Changeset 527 for trunk/cgi-bin/IPDB.pm


Ignore:
Timestamp:
10/23/12 13:32:47 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Clean up and move SQL for showRBlock to IPDB.pm
Tweak listFree() since the segments in showMaster and showRBlock it
replaced weren't quite as identical as I thought
Convert template to use odd and squeeze a bit more HTML out of
main.cgi and IPDB.pm

See #34.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/IPDB.pm

    r525 r527  
    2828        &addMaster
    2929        &listSummary &listMaster &listRBlock &listFree
     30        &getRoutedCity
    3031        &allocateBlock &deleteBlock &getBlockData
    3132        &getNodeList
     
    4041                &addMaster
    4142                &listSummary &listMaster &listRBlock &listFree
     43                &getRoutedCity
    4244                &allocateBlock &deleteBlock &getBlockData
    4345                &getNodeList
     
    373375
    374376
    375 # &listRBlock
    376 
    377 
    378 ## IPDB::listFree()
     377## IPDB::listRBlock()
    379378# Gets a list of free blocks in the requested parent/master in both CIDR and range notation
    380379# Takes a parent/master and an optional flag to look at routed or unrouted blocks, depending
    381380# on whether the master is a direct master or a routed block
    382381# Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
     382sub listRBlock {
     383  my $dbh = shift;
     384  my $routed = shift;
     385
     386  # Snag the allocations for this block
     387  my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description".
     388        " FROM allocations WHERE cidr <<= ? ORDER BY cidr");
     389  $sth->execute($routed);
     390
     391  # hack hack hack
     392  # set up to flag swip=y records if they don't actually have supporting data in the customers table
     393  my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
     394
     395  my @blocklist;
     396  while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) {
     397    $custsth->execute($custid);
     398    my ($ncust) = $custsth->fetchrow_array();
     399    my %row = (
     400        block => $cidr,
     401        city => $city,
     402        type => $disp_alloctypes{$type},
     403        custid => $custid,
     404        swip => ($swip eq 'y' ? 'Yes' : 'No'),
     405        partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
     406        desc => $desc
     407        );
     408    $row{subblock} = ($type =~ /^.r$/);         # hmf.  wonder why these won't work in the hash declaration...
     409    $row{listpool} = ($type =~ /^.[pd]$/);
     410    push (@blocklist, \%row);
     411  }
     412  return \@blocklist;
     413} # end listRBlock()
     414
     415
     416## IPDB::listFree()
     417# Gets a list of free blocks in the requested parent/master in both CIDR and range notation
     418# Takes a parent/master and an optional "routed or unrouted" flag that defaults to unrouted.
     419# Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
     420# Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes
    383421sub listFree {
    384422  my $dbh = shift;
    385423  my $master = shift;
    386   my $routed = shift || 'y';
     424  my $routed = shift || 'n';
    387425
    388426  # do it this way so we can waste a little less time iterating
    389   my $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE cidr <<= ? AND routed = ? ORDER BY cidr");
    390   $sth->execute($master, $routed);
     427  my $sth = $dbh->prepare("SELECT cidr,routed FROM freeblocks WHERE cidr <<= ? AND ".
     428        ($routed eq 'n' ? '' : 'NOT')." routed = 'n' ORDER BY cidr");
     429  $sth->execute($master);
    391430  my @flist;
    392   while (my ($cidr) = $sth->fetchrow_array()) {
     431  while (my ($cidr,$rtype) = $sth->fetchrow_array()) {
    393432    $cidr = new NetAddr::IP $cidr;
    394     my %row = (fblock => "$cidr", frange => $cidr->range);
     433    my %row = (
     434        fblock => "$cidr",
     435        frange => $cidr->range,
     436        );
     437    if ($routed eq 'y') {
     438      $row{subblock} = ($rtype ne 'y' && $rtype ne 'n');
     439      $row{fbtype} = $rtype;
     440    }
    395441    push @flist, \%row;
    396442  }
    397443  return \@flist;
    398 }
     444} # end listFree()
     445
     446
     447## IPDB::getRoutedCity()
     448# Get the city for a routed block.
     449sub getRoutedCity {
     450  my $dbh = shift;
     451  my $block = shift;
     452
     453  my ($rcity) = $dbh->selectrow_array("SELECT city FROM routed WHERE cidr = ?", undef, ($block) );
     454  return $rcity;
     455} # end getRoutedCity()
    399456
    400457
Note: See TracChangeset for help on using the changeset viewer.