implement (initial) device port listing

This commit is contained in:
Oliver Gorwits
2012-01-11 22:12:30 +00:00
parent d704a04760
commit 4e34274ac9
6 changed files with 172 additions and 10 deletions

View File

@@ -64,7 +64,17 @@ __PACKAGE__->set_primary_key("port", "ip");
# Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:lcbweb0loNwHoWUuxTN/hA
__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip' );
__PACKAGE__->belongs_to( device => 'Netdisco::DB::Result::Device', 'ip',
{
'+select' => [
\"replace(age(timestamp 'epoch' + uptime / 100 * interval '1 second', timestamp '1970-01-01 00:00:00-00')::text, 'mon', 'month')",
\"to_char(last_discover, 'YYYY-MM-DD HH24:MI')",
\"to_char(last_macsuck, 'YYYY-MM-DD HH24:MI')",
\"to_char(last_arpnip, 'YYYY-MM-DD HH24:MI')",
],
'+as' => [qw/ uptime last_discover last_macsuck last_arpnip /],
},
);
__PACKAGE__->has_many( port_vlans_tagged => 'Netdisco::DB::Result::DevicePortVlan',
sub {
my $args = shift;
@@ -91,4 +101,25 @@ sub native_vlan {
return eval { $row->native_port_vlan->vlan || undef };
};
sub is_free {
my ($row, $num, $unit) = @_;
return unless $num =~ m/^\d+$/
and $unit =~ m/(?:days|weeks|months|years)/;
return 0 unless
($row->up_admin and $row->up_admin eq 'up')
and ($row->up and $row->up eq 'down');
my $quan = {
days => (60 * 60 * 24),
weeks => (60 * 60 * 24 * 7),
months => (60 * 60 * 24 * 31),
years => (60 * 60 * 24 * 365),
};
my $total = $num * $quan->{$unit};
my $diff_sec = $row->lastchange / 100;
return ($diff_sec >= $total ? 1 : 0);
}
1;

View File

@@ -4,6 +4,24 @@ use base 'DBIx::Class::ResultSet';
use strict;
use warnings FATAL => 'all';
sub by_ip {
my ($set, $ip) = @_;
return $set unless $ip;
return $set->search(
{
'me.ip' => $ip,
},
{
'+select' => [
\"to_char(last_discover - (uptime - lastchange) / 100 * interval '1 second', 'YYYY-MM-DD HH24:MI:SS')",
],
'+as' => [qw/ lastchange_stamp /],
join => 'device',
}
);
}
sub by_mac {
my ($set, $mac) = @_;
return $set unless $mac;