blib/lib/App/Netdisco/DB/Result/NodeNbt.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran n/a Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 60 CC 1 SCAR 6.9
CC
1
Cov
0%
node_sightings
2.0
time_first_stamp
2.0
time_last_stamp
2.0
CRAP 2.0
1 use utf8;
2 package App::Netdisco::DB::Result::NodeNbt;
3  
4  
5 use strict;
6 use warnings;
7  
8 use NetAddr::MAC;
9  
10 use base 'App::Netdisco::DB::Result';
11 __PACKAGE__->table("node_nbt");
12 __PACKAGE__->add_columns(
13 "mac",
14 { data_type => "macaddr", is_nullable => 0 },
15 "ip",
16 { data_type => "inet", is_nullable => 1 },
17 "nbname",
18 { data_type => "text", is_nullable => 1 },
19 "domain",
20 { data_type => "text", is_nullable => 1 },
21 "server",
22 { data_type => "boolean", is_nullable => 1 },
23 "nbuser",
24 { data_type => "text", is_nullable => 1 },
25 "active",
26 { data_type => "boolean", is_nullable => 1 },
27 "time_first",
28 {
29 data_type => "timestamp",
30 default_value => \"LOCALTIMESTAMP",
31 is_nullable => 1,
32 original => { default_value => \"LOCALTIMESTAMP" },
33 },
34 "time_last",
35 {
36 data_type => "timestamp",
37 default_value => \"LOCALTIMESTAMP",
38 is_nullable => 1,
39 original => { default_value => \"LOCALTIMESTAMP" },
40 },
41 );
42 __PACKAGE__->set_primary_key("mac");
43  
44  
45  
46 =head1 RELATIONSHIPS
47  
48 =head2 oui
49  
50 DEPRECATED: USE MANUFACTURER INSTEAD
51  
52 Returns the C<oui> table entry matching this Node. You can then join on this
53 relation and retrieve the Company name from the related table.
54  
55 The JOIN is of type LEFT, in case the OUI table has not been populated.
56  
57 =cut
58  
59 __PACKAGE__->belongs_to( oui => 'App::Netdisco::DB::Result::Oui',
60 sub {
61 my $args = shift;
62 return {
63 "$args->{foreign_alias}.oui" =>
64 { '=' => \"substring(cast($args->{self_alias}.mac as varchar) for 8)" }
65 };
66 },
67 { join_type => 'LEFT' }
68 );
69  
70 =head2 manufacturer
71  
72 Returns the C<manufacturer> table entry matching this Node. You can then join on this
73 relation and retrieve the Company name from the related table.
74  
75 The JOIN is of type LEFT, in case the Manufacturer table has not been populated.
76  
77 =cut
78  
79 __PACKAGE__->belongs_to( manufacturer => 'App::Netdisco::DB::Result::Manufacturer',
80 sub {
81 my $args = shift;
82 return {
83 "$args->{foreign_alias}.range" => { '@>' =>
84 \qq{('x' || lpad( translate( $args->{self_alias}.mac ::text, ':', ''), 16, '0')) ::bit(64) ::bigint} },
85 };
86 },
87 { join_type => 'LEFT' }
88 );
89  
90 =head2 nodes
91  
92 Returns the set of C<node> entries associated with this IP. That is, all the
93 MAC addresses recorded which have ever hosted this IP Address.
94  
95 Remember you can pass a filter to this method to find only active or inactive
96 nodes, but do take into account that both the C<node> and C<node_nbt> tables
97 include independent C<active> fields.
98  
99 See also the C<node_sightings> helper routine, below.
100  
101 =cut
102  
103 __PACKAGE__->has_many( nodes => 'App::Netdisco::DB::Result::Node',
104 { 'foreign.mac' => 'self.mac' } );
105  
106  
107 =head2 nodeips
108  
109 Returns the set of C<node_ip> entries associated with this NetBIOS entry.
110 That is, the IP addresses which the same MAC address at the time of discovery.
111  
112 Note that the Active status of the returned IP entries will all be the same
113 as the current NetBIOS entry.
114  
115 =cut
116  
117 __PACKAGE__->has_many( nodeips => 'App::Netdisco::DB::Result::NodeIp',
118 { 'foreign.mac' => 'self.mac', 'foreign.active' => 'self.active' } );
119  
120  
121 my $search_attr = {
122 order_by => {'-desc' => 'time_last'},
123 '+columns' => {
124 time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')",
125 time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')",
126 },
127 };
128  
129 =head2 node_sightings( \%cond, \%attrs? )
130  
131 Returns the set of C<node> entries associated with this IP. That is, all the
132 MAC addresses recorded which have ever hosted this IP Address.
133  
134 Remember you can pass a filter to this method to find only active or inactive
135 nodes, but do take into account that both the C<node> and C<node_ip> tables
136 include independent C<active> fields.
137  
138 =over 4
139  
140 =item *
141  
142 Results are ordered by time last seen.
143  
144 =item *
145  
146 Additional columns C<time_first_stamp> and C<time_last_stamp> provide
147 preformatted timestamps of the C<time_first> and C<time_last> fields.
148  
149 =item *
150  
151 A JOIN is performed on the Device table and the Device DNS column prefetched.
152  
153 =back
154  
155 =cut
156  
157 sub node_sightings {
158 my ($row, $cond, $attrs) = @_;
159  
160 return $row
161 ->nodes({}, {
162 '+columns' => [qw/ device.dns /],
163 join => 'device',
164 })
165 ->search_rs({}, $search_attr)
166 ->search($cond, $attrs);
167 }
168  
169 =head1 ADDITIONAL COLUMNS
170  
171 =head2 time_first_stamp
172  
173 Formatted version of the C<time_first> field, accurate to the minute.
174  
175 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
176 between the date stamp and time stamp. That is:
177  
178 2012-02-06 12:49
179  
180 =cut
181  
182 sub time_first_stamp { return (shift)->get_column('time_first_stamp') }
183  
184 =head2 time_last_stamp
185  
186 Formatted version of the C<time_last> field, accurate to the minute.
187  
188 The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T>
189 between the date stamp and time stamp. That is:
190  
191 2012-02-06 12:49
192  
193 =cut
194  
195 sub time_last_stamp { return (shift)->get_column('time_last_stamp') }
196  
197 =head2 net_mac
198  
199 Returns the C<mac> column instantiated into a L<NetAddr::MAC> object.
200  
201 =cut
202  
203 sub net_mac { return NetAddr::MAC->new(mac => ((shift)->mac || '')) }
204  
205 1;