Changeset 663


Ignore:
Timestamp:
12/30/14 16:09:20 (9 years ago)
Author:
Kris Deugau
Message:

/trunk

Clean up some minor snafu-ness in listSubs()
Commit new subs listContainers() and listAllocations() for alternate

browse listings

Bring getPoolSelect() up to date with current database structure

File:
1 edited

Legend:

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

    r662 r663  
    2929        &initIPDBGlobals &connectDB &finish &checkDBSanity
    3030        &addMaster &touchMaster
    31         &listSummary &listSubs &listFree &listPool
     31        &listSummary &listSubs &listContainers &listAllocations &listFree &listPool
    3232        &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
    3333        &ipParent &subParent &blockParent &getRoutedCity
     
    4444                &initIPDBGlobals &connectDB &finish &checkDBSanity
    4545                &addMaster &touchMaster
    46                 &listSummary &listSubs &listFree &listPool
     46                &listSummary &listSubs &listContainers &listAllocations &listFree &listPool
    4747                &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
    4848                &ipParent &subParent &blockParent &getRoutedCity
     
    506506
    507507  # snag some more details
    508   my $substh = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm' AND master_id = ? AND NOT cidr = ? ");
     508  my $substh = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? AND ".
     509        "AND type ~ '[mc]\$' AND master_id = ? AND NOT cidr = ? ");
    509510  my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
    510511        "AND NOT type='rm' AND NOT type='mm' AND master_id = ?");
     
    550551
    551552
     553## IPDB::listContainers()
     554# List all container-type allocations in a given parent
     555# Takes a database handle and a hash:
     556#  - parent is the ID of the parent block
     557# Returns an arrayref to a list of hashrefs with the CIDR block, location, type,
     558# description, block ID, and counts for the nmber uf suballocations (all types),
     559# free blocks, and the CIDR size of the largest free block
     560sub listContainers {
     561  my $dbh = shift;
     562  my %args = @_;
     563
     564  # Just In Case
     565  $args{vrf} = '' if !$args{vrf};
     566
     567  # Snag the allocations for this block
     568  my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,id,master_id".
     569        " FROM allocations WHERE parent_id = ? AND type ~ '[mc]\$' ORDER BY cidr");
     570  $sth->execute($args{parent});
     571
     572  my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
     573        "AND NOT type='rm' AND NOT type='mm' AND master_id = ?");
     574  my $freesth = $dbh->prepare("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?");
     575  my $lfreesth = $dbh->prepare("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
     576        " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1");
     577
     578  my @blocklist;
     579  while (my ($cidr,$city,$type,$custid,$swip,$desc,$id,$mid) = $sth->fetchrow_array()) {
     580    $alsth->execute($cidr, $mid);
     581    my ($alloc) = $alsth->fetchrow_array();
     582    $freesth->execute($cidr, $mid);
     583    my ($free) = $freesth->fetchrow_array();
     584    $lfreesth->execute($cidr, $mid);
     585    my ($lfree) = $lfreesth->fetchrow_array();
     586    $lfree = "/$lfree" if $lfree;
     587    $lfree = '<NONE>' if !$lfree;
     588    my %row = (
     589        block => $cidr,
     590        suballocs => $alloc,
     591        subfree => $free,
     592        lfree => $lfree,
     593        city => $city,
     594        type => $disp_alloctypes{$type},
     595        desc => $desc,
     596        id => $id,
     597        );
     598    push (@blocklist, \%row);
     599  }
     600  return \@blocklist;
     601} # end listContainers()
     602
     603
     604## IPDB::listAllocations()
     605# List all end-use allocations in a given parent
     606# Takes a database handle and a hash:
     607#  - parent is the ID of the parent block
     608# Returns an arrayref to a list of hashrefs with the CIDR block, location, type,
     609# custID, SWIP flag, description, block ID, and master ID
     610sub listAllocations {
     611  my $dbh = shift;
     612  my %args = @_;
     613
     614  # Snag the allocations for this block
     615  my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,id,master_id".
     616        " FROM allocations WHERE parent_id = ? AND type !~ '[mc]\$' ORDER BY cidr");
     617  $sth->execute($args{parent});
     618
     619  # hack hack hack
     620  # set up to flag swip=y records if they don't actually have supporting data in the customers table
     621  my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
     622
     623  my @blocklist;
     624  while (my ($cidr,$city,$type,$custid,$swip,$desc,$id,$mid) = $sth->fetchrow_array()) {
     625    $custsth->execute($custid);
     626    my ($ncust) = $custsth->fetchrow_array();
     627    my %row = (
     628        block => $cidr,
     629        city => $city,
     630        type => $disp_alloctypes{$type},
     631        custid => $custid,
     632        swip => ($swip eq 'y' ? 'Yes' : 'No'),
     633        partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
     634        desc => $desc,
     635        id => $id,
     636        );
     637#    $row{subblock} = ($type =~ /^.r$/);         # hmf.  wonder why these won't work in the hash declaration...
     638    $row{listpool} = ($type =~ /^.[pd]$/);
     639    push (@blocklist, \%row);
     640  }
     641  return \@blocklist;
     642} # end listAllocations()
     643
     644
    552645## IPDB::listFree()
    553646# Gets a list of free blocks in the requested parent/master and VRF instance in both CIDR and range notation
     
    670763  $ptype .= '_';
    671764
    672   my $plist = $dbh->selectall_arrayref(
    673         "SELECT count(*) AS poolfree,p.pool AS poolblock, a.city AS poolcit, a.rdepth AS poolrdepth ".
    674         "FROM poolips p ".
    675         "JOIN allocations a ON p.pool=a.cidr ".
    676         "WHERE p.available='y' AND a.city = ? AND p.type LIKE ? ".
    677         "GROUP BY p.pool,a.city,a.rdepth",
     765  my $plist = $dbh->selectall_arrayref( q(
     766        SELECT count(*) AS poolfree,p.pool AS poolblock, a.city AS poolcit
     767        FROM poolips p
     768        JOIN allocations a ON p.parent_id=a.id
     769        WHERE p.available='y' AND a.city = ? AND p.type LIKE ?
     770        GROUP BY p.pool,a.city
     771        ),
    678772        { Slice => {} }, ($pcity, $ptype) );
    679773  return $plist;
Note: See TracChangeset for help on using the changeset viewer.