blib/lib/App/Netdisco/Util/Statistics.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 87 CC 9 SCAR 25.6
CC
9
Cov
0%
update_stats
20.0
pretty_version
20.0
postgres_version
12.0
CRAP 12.9
1 package App::Netdisco::Util::Statistics;
2  
3 use Dancer qw/:syntax :script/;
4 use Dancer::Plugin::DBIC 'schema';
5  
6 use Time::Piece; # for OO localtime
7  
8 use base 'Exporter';
9 our @EXPORT = ();
10 our @EXPORT_OK = qw/pretty_version postgres_version update_stats/;
11 our %EXPORT_TAGS = (all => \@EXPORT_OK);
12  
13 =head1 NAME
14  
15 App::Netdisco::Util::Statistics
16  
17 =head1 DESCRIPTION
18  
19 Update the Netdisco statistics.
20  
21 There are no default exports, however the C<:all> tag will export all
22 subroutines.
23  
24 =head1 EXPORT_OK
25  
26 =head2 update_stats()
27  
28 Update the Netdisco statistics, either new for today or updating today's
29 figures.
30  
31 =cut
32  
33 sub update_stats {
34 my $schema = schema(vars->{'tenant'});
35 eval { require SNMP::Info };
36 my $snmpinfo_ver = ($@ ? 'n/a' : $SNMP::Info::VERSION);
37 my $postgres_ver = postgres_version($schema->storage->dbh->{pg_server_version});
38  
39 # roll everything back if we're testing
40 my $txn_guard = $ENV{ND2_DB_ROLLBACK}
41 ? $schema->storage->txn_scope_guard : undef;
42  
43 # TODO: (when we have the capabilities table?)
44 # $stats{waps} = sql_scalar('device',['COUNT(*)'], {"model"=>"AIR%"});
45  
46 $schema->txn_do(sub {
47 $schema->resultset('Statistics')->update_or_create({
48 day => localtime->ymd,
49  
50 device_count =>
51 $schema->resultset('Device')->count_rs->as_query,
52 device_ip_count =>
53 $schema->resultset('DeviceIp')->count_rs->as_query,
54 device_link_count =>
55 ( $postgres_ver =~ m/^8\./ ? 0 :
56 $schema->resultset('Virtual::DeviceLinks')->search(undef, {
57 select => [ { coalesce => [ { sum => 'aggports' }, 0 ] } ],
58 as => ['totlinks'],
59 })->get_column('totlinks')->as_query ),
60 device_port_count =>
61 $schema->resultset('DevicePort')->count_rs->as_query,
62 device_port_up_count =>
63 $schema->resultset('DevicePort')->count_rs({up => 'up'})->as_query,
64 ip_table_count =>
65 $schema->resultset('NodeIp')->count_rs->as_query,
66 ip_active_count =>
67 $schema->resultset('NodeIp')->search({-bool => 'active'},
68 {columns => 'ip', distinct => 1})->count_rs->as_query,
69 node_table_count =>
70 $schema->resultset('Node')->count_rs->as_query,
71 node_active_count =>
72 $schema->resultset('Node')->search({-bool => 'active'},
73 {columns => 'mac', distinct => 1})->count_rs->as_query,
74 phone_count =>
75 $schema->resultset('DevicePortProperties')->
76 search({-bool => 'remote_is_phone'})->count_rs->as_query,
77 wap_count =>
78 $schema->resultset('DevicePortProperties')->
79 search({-bool => 'remote_is_wap'})->count_rs->as_query,
80  
81 netdisco_ver => pretty_version($App::Netdisco::VERSION, 3),
82 snmpinfo_ver => pretty_version($snmpinfo_ver, 3),
83 schema_ver => $schema->schema_version,
84 perl_ver => pretty_version($], 3),
85 python_ver => vars->{'python_ver'},
86 pg_ver => $postgres_ver,
87  
88 }, { key => 'primary' });
89 });
90 }
91  
92 =head2 pretty_version ( $versionstring , $seglen )
93  
94 Splits a string (only numbers and dots allowed) into a number of parts which
95 are seglen long, then removes all leading zeros from each part and returns
96 the parts joined by dots as one string.
97  
98 Returns the original versionstring if unallowed characters are found or seglen
99 is negative.
100  
101 Returns C<undef> if seglen is zero.
102  
103 =cut
104  
105 sub pretty_version {
106 my ($version, $seglen) = @_;
107 return unless $version and $seglen;
108 return $version unless $seglen > 0;
109 return $version if $version !~ m/^[0-9.]+$/;
110 $version =~ s/\.//g;
111 $version = (join '.', reverse map {scalar reverse}
112 unpack("(A${seglen})*", reverse $version));
113 $version =~ s/\.000/.0/g;
114 $version =~ s/\.0+([1-9]+)/.$1/g;
115 return $version;
116 }
117  
118 =head2 postgres_version ( $server_version_num )
119  
120 Formats PostgreSQL's C<server_version_num> for display: C<180006> as C<18.6>,
121 and C<90624> as C<9.6.24>.
122  
123 Returns C<undef> unless given a whole number.
124  
125 =cut
126  
127 # From PostgreSQL 10 the number is major * 10000 + minor, so the two-digit
128 # segments pretty_version() cuts would show 18.6 as 18.00.6.
129 sub postgres_version {
130 my $version = shift;
131 return unless defined $version and $version =~ m/^\d+$/;
132 return sprintf '%d.%d', int($version / 10000), $version % 10000
133 if $version >= 100000;
134 return sprintf '%d.%d.%d',
135 int($version / 10000), int($version / 100) % 100, $version % 100;
136 }
137  
138 true;