implement ports list filtering
This commit is contained in:
		| @@ -64,7 +64,7 @@ sub by_name { | ||||
| # should match edge ports only | ||||
| sub by_vlan { | ||||
|     my ($set, $vlan) = @_; | ||||
|     return $set unless $vlan and $vlan =~ m/^\d+$/;; | ||||
|     return $set unless $vlan and $vlan =~ m/^\d+$/; | ||||
|  | ||||
|     return $set->search( | ||||
|       { | ||||
| @@ -78,5 +78,21 @@ sub by_vlan { | ||||
|     ); | ||||
| } | ||||
|  | ||||
| sub by_port { | ||||
|     my ($set, $port) = @_; | ||||
|     return $set unless $port; | ||||
|  | ||||
|     return $set->search( | ||||
|       { | ||||
|         'me.port' => $port, | ||||
|       }, | ||||
|       { | ||||
|         order_by => [qw/ me.ip me.port /], | ||||
|         columns => [qw/ ip port descr name vlan device.dns /], | ||||
|         join => 'device', | ||||
|       }, | ||||
|     ); | ||||
| } | ||||
|  | ||||
| 1; | ||||
|  | ||||
|   | ||||
| @@ -94,9 +94,32 @@ ajax '/ajax/content/device/ports' => sub { | ||||
|     return unless $ip; | ||||
|  | ||||
|     my $set = schema('netdisco')->resultset('DevicePort')->by_ip($ip); | ||||
|  | ||||
|     # refine by ports if requested | ||||
|     my $q = param('q'); | ||||
|     if ($q) { | ||||
|         if ($q =~ m/^\d+$/) { | ||||
|             $set = $set->by_vlan($q); | ||||
|         } | ||||
|         else { | ||||
|             my $c = schema('netdisco')->resultset('DevicePort')->by_ip($ip)->by_port($q); | ||||
|             if ($c->count) { | ||||
|                 $set = $set->by_port($q); | ||||
|             } | ||||
|             else { | ||||
|                 $set = $set->by_name($q); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     return unless $set->count; | ||||
|  | ||||
|     my $results = [ sort { &netdisco::sort_port($a->port, $b->port) } $set->all ]; | ||||
|     # sort, and filter by free ports | ||||
|     # the filter could be in the template but here allows a 'no records' msg | ||||
|     my $results = [ sort { &netdisco::sort_port($a->port, $b->port) } | ||||
|                     grep { not param('free') | ||||
|                            or $_->is_free(param('age_num'), param('age_unit')) } $set->all ]; | ||||
|  | ||||
|     return unless scalar @$results; | ||||
|  | ||||
|     content_type('text/html'); | ||||
|     template 'ajax/device/ports.tt', { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user