Wildcard support on Node name/MAC Search

This commit is contained in:
Oliver Gorwits
2014-03-02 20:36:55 +00:00
parent 80ffbd5963
commit e7b030758b
3 changed files with 87 additions and 65 deletions

View File

@@ -2,7 +2,8 @@
[ENHANCEMENTS] [ENHANCEMENTS]
* Partial search on Device Port MAC address * Wildcard support on Device Port MAC Search
* Wildcard support on Node name/MAC Search
2.024003 - 2014-02-27 2.024003 - 2014-02-27

View File

@@ -9,6 +9,7 @@ use NetAddr::IP::Lite ':lower';
use Net::MAC (); use Net::MAC ();
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
use App::Netdisco::Util::Web 'sql_match';
register_search_tab({ tag => 'node', label => 'Node' }); register_search_tab({ tag => 'node', label => 'Node' });
@@ -42,21 +43,51 @@ ajax '/ajax/content/search/node' => require_login sub {
} }
} }
if (! $mac->get_error) { my ($likeval, $likeclause) = sql_match($node, not param('partial'));
my $using_wildcards = (($likeval ne $node) ? 1 : 0);
my @where_mac =
($using_wildcards ? \['me.mac::text ILIKE ?', $likeval]
: ($mac->get_error ? \'0=1' : ('me.mac' => $mac->as_IEEE)) );
my $sightings = schema('netdisco')->resultset('Node') my $sightings = schema('netdisco')->resultset('Node')
->search_by_mac({mac => $mac->as_IEEE, @active, @times}); ->search({-and => [@where_mac, @active, @times]}, {
order_by => {'-desc' => 'time_last'},
'+columns' => [
'device.dns',
{ time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
{ time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
],
join => 'device',
});
my $ips = schema('netdisco')->resultset('NodeIp') my $ips = schema('netdisco')->resultset('NodeIp')
->search_by_mac({mac => $mac->as_IEEE, @active, @times}); ->search({-and => [@where_mac, @active, @times]}, {
order_by => {'-desc' => 'time_last'},
'+columns' => [
'oui.company',
{ time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
{ time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
],
join => 'oui'
});
my $netbios = schema('netdisco')->resultset('NodeNbt') my $netbios = schema('netdisco')->resultset('NodeNbt')
->search_by_mac({mac => $mac->as_IEEE, @active, @times}); ->search({-and => [@where_mac, @active, @times]}, {
order_by => {'-desc' => 'time_last'},
'+columns' => [
'oui.company',
{ time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" },
{ time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" },
],
join => 'oui'
});
my $ports = schema('netdisco')->resultset('DevicePort') my $ports = schema('netdisco')->resultset('DevicePort')
->search({mac => $mac->as_IEEE}); ->search({ -and => [@where_mac] });
my $wireless = schema('netdisco')->resultset('NodeWireless')->search( my $wireless = schema('netdisco')->resultset('NodeWireless')->search(
{ mac => $mac->as_IEEE }, { -and => [@where_mac] },
{ order_by => { '-desc' => 'time_last' }, { order_by => { '-desc' => 'time_last' },
'+columns' => [ '+columns' => [
{ {
@@ -65,12 +96,10 @@ ajax '/ajax/content/search/node' => require_login sub {
} }
); );
return unless $sightings->has_rows if ($sightings->has_rows or $ips->has_rows
or $ips->has_rows or $ports->has_rows or $netbios->has_rows) {
or $ports->has_rows
or $netbios->has_rows;
template 'ajax/search/node_by_mac.tt', { return template 'ajax/search/node_by_mac.tt', {
ips => $ips, ips => $ips,
sightings => $sightings, sightings => $sightings,
ports => $ports, ports => $ports,
@@ -78,15 +107,10 @@ ajax '/ajax/content/search/node' => require_login sub {
netbios => $netbios, netbios => $netbios,
}, { layout => undef }; }, { layout => undef };
} }
else {
my $set;
my $name = $node;
if (param('partial')) {
$name = "\%$name\%" if $name !~ m/%/;
}
$set = schema('netdisco')->resultset('NodeNbt') my $name = (param('partial') ? $likeval : $node);
my $set = schema('netdisco')->resultset('NodeNbt')
->search_by_name({nbname => $name, @active, @times}); ->search_by_name({nbname => $name, @active, @times});
unless ( $set->has_rows ) { unless ( $set->has_rows ) {
@@ -96,13 +120,11 @@ ajax '/ajax/content/search/node' => require_login sub {
->search_by_ip({ip => $ip, @active, @times}); ->search_by_ip({ip => $ip, @active, @times});
} }
else { else {
if ($name !~ m/%/ and setting('domain_suffix')) { $likeval .= setting('domain_suffix')
$name .= setting('domain_suffix') if index($node, setting('domain_suffix')) == -1;
if index($name, setting('domain_suffix')) == -1;
}
$set = schema('netdisco')->resultset('NodeIp') $set = schema('netdisco')->resultset('NodeIp')
->search_by_dns({dns => $name, @active, @times}); ->search_by_dns({dns => $likeval, @active, @times});
# if the user selects Vendor search opt, then # if the user selects Vendor search opt, then
# we'll try the OUI company name as a fallback # we'll try the OUI company name as a fallback
@@ -111,7 +133,7 @@ ajax '/ajax/content/search/node' => require_login sub {
$set = schema('netdisco')->resultset('NodeIp') $set = schema('netdisco')->resultset('NodeIp')
->with_times ->with_times
->search( ->search(
{'oui.company' => { -ilike => "\%$node\%"}, @times}, {'oui.company' => { -ilike => ''.sql_match($node)}, @times},
{'prefetch' => 'oui'}, {'prefetch' => 'oui'},
); );
} }
@@ -125,7 +147,6 @@ ajax '/ajax/content/search/node' => require_login sub {
macs => $set, macs => $set,
archive_filter => {@active}, archive_filter => {@active},
}, { layout => undef }; }, { layout => undef };
}
}; };
true; true;

View File

@@ -35,7 +35,7 @@
name="partial"[% ' checked="checked"' IF params.partial %]/> name="partial"[% ' checked="checked"' IF params.partial %]/>
</label> </label>
<label class="nd_checkboxlabel" for="node_partial"> <label class="nd_checkboxlabel" for="node_partial">
<span class="nd_searchcheckbox uneditable-input">Partial Name</span> <span class="nd_searchcheckbox uneditable-input">Partial Match</span>
</label> </label>
</div> </div>
<div class="clearfix"> <div class="clearfix">