implement smart search tab selection, and Node search

This commit is contained in:
Oliver Gorwits
2012-01-08 01:10:37 +00:00
parent 54d6422894
commit fc672f0524
10 changed files with 206 additions and 16 deletions

View File

@@ -48,6 +48,11 @@ __PACKAGE__->set_primary_key("mac", "switch", "port");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:sGGyKEfUkoIFVtmj1wnH7A
__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device',
{ 'foreign.ip' => 'self.switch' } );
__PACKAGE__->belongs_to( device_port => 'Netdisco::DB::Result::DevicePort',
{ 'foreign.ip' => 'self.switch', 'foreign.port' => 'self.port' } );
__PACKAGE__->has_many( ips => 'Netdisco::DB::Result::NodeIp',
{ 'foreign.mac' => 'self.mac' } );
# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;

View File

@@ -39,6 +39,49 @@ __PACKAGE__->set_primary_key("mac", "ip");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:9+CuvuVWH88WxAf6IBij8g
__PACKAGE__->has_many( nodeips => 'Netdisco::DB::Result::NodeIp',
{ 'foreign.mac' => 'self.mac' } );
__PACKAGE__->has_many( nodes => 'Netdisco::DB::Result::Node',
{ 'foreign.mac' => 'self.mac' } );
sub tidy_nodeips {
my ($row, $archive) = @_;
return $row->nodeips(
{
ip => { '!=' => $row->ip },
($archive ? () : (active => 1)),
},
{
order_by => {'-desc' => 'time_last'},
columns => [qw/ mac ip dns active /],
'+select' => [
\"to_char(time_first, 'YYYY-MM-DD HH24:MI')",
\"to_char(time_last, 'YYYY-MM-DD HH24:MI')",
],
'+as' => [qw/ time_first time_last /],
},
);
}
sub tidy_nodes {
my ($row, $archive) = @_;
return $row->nodes(
{
($archive ? () : (active => 1)),
},
{
order_by => {'-desc' => 'time_last'},
columns => [qw/ mac switch port oui active device.dns /],
'+select' => [
\"to_char(time_first, 'YYYY-MM-DD HH24:MI')",
\"to_char(time_last, 'YYYY-MM-DD HH24:MI')",
],
'+as' => [qw/ time_first time_last /],
join => 'device',
},
);
}
# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;

View File

@@ -11,10 +11,10 @@ sub carrying_vlan {
'port_vlans.vlan' => $vlan,
},
{
join => [qw/ port_vlans vlans /],
prefetch => 'vlans',
order_by => [qw/ me.dns me.ip /],
columns => [qw/ me.ip me.dns me.model me.os me.vendor /],
join => 'port_vlans',
prefetch => 'vlans',
},
);
}

View File

@@ -0,0 +1,25 @@
package Netdisco::DB::ResultSet::NodeIp;
use base 'DBIx::Class::ResultSet';
sub by_ip {
my ($set, $ip, $archive) = @_;
return $set unless $ip;
return $set->search(
{
ip => $ip,
($archive ? () : (active => 1)),
},
{
order_by => {'-desc' => 'time_last'},
columns => [qw/ mac ip dns active /],
'+select' => [
\"to_char(time_first, 'YYYY-MM-DD HH24:MI')",
\"to_char(time_last, 'YYYY-MM-DD HH24:MI')",
],
'+as' => [qw/ time_first time_last /],
},
);
}
1;

View File

@@ -22,6 +22,21 @@ ajax '/ajax/content/search/:thing' => sub {
return '<p>Hello '. param('thing') .'.</p>';
};
# nodes matching the param as an IP or DNS hostname or MAC
ajax '/ajax/content/search/node' => sub {
my $node = param('q');
return unless $node;
my $set = schema('netdisco')->resultset('NodeIp')
->by_ip($node, param('archived'));
return unless $set->count;
content_type('text/html');
template 'content/node.tt', {
results => $set,
}, { layout => undef };
};
# devices carrying vlan xxx
ajax '/ajax/content/search/vlan' => sub {
my $vlan = param('q');
@@ -48,6 +63,12 @@ ajax '/ajax/content/search/vlan' => sub {
};
get '/search' => sub {
# set up default search options for each type
if (not param('tab') or param('tab') ne 'node') {
params->{'stamps'} = 'checked';
params->{'vendor'} = 'checked';
}
my $q = param('q');
if ($q and not param('tab')) {
# pick most likely tab for initial results
@@ -55,7 +76,35 @@ get '/search' => sub {
params->{'tab'} = 'vlan';
}
else {
params->{'tab'} = 'device';
my $s = schema('netdisco');
if ($q =~ m/^[a-f0-9.:]+$/i) {
if ($s->resultset('Device')->find($q)) {
params->{'tab'} = 'device';
}
else {
# this will match for MAC addresses
# and partial IPs (subnets?)
params->{'tab'} = 'node';
}
}
else {
if ($s->resultset('Device')->search({
dns => { '-ilike' => "\%$q\%" },
})->count) {
params->{'tab'} = 'device';
}
elsif ($s->resultset('NodeIp')->search({
dns => { '-ilike' => "\%$q\%" },
})->count) {
params->{'tab'} = 'node';
}
elsif ($s->resultset('DevicePort')->search({
name => { '-ilike' => "\%$q\%" },
})->count) {
params->{'tab'} = 'port';
}
}
params->{'tab'} ||= 'device';
}
}
elsif (not $q) {
@@ -63,12 +112,6 @@ get '/search' => sub {
return;
}
# set up default search options for each type
if (param('tab') and param('tab') ne 'node') {
params->{'stamps'} = 'checked';
params->{'vendor'} = 'checked';
}
# list of tabs
var('tabs' => [
{ id => 'device', label => 'Device' },