blib/lib/MaxMind/DB/Writer/Tree/Processor/VisualizeTree.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 99 CC 4 SCAR 12.3
CC
4
Cov
0%
process_node_record
6.0
process_data_record
6.0
_data_record_representation
6.0
CRAP 3.4
1 package MaxMind::DB::Writer::Tree::Processor::VisualizeTree;
2  
3 use strict;
4 use warnings;
5 use namespace::autoclean;
6  
7 our $VERSION = '0.300004';
8  
9 use Data::Dumper::Concise;
10 use Digest::MD5 qw( md5_hex );
11 use GraphViz2;
12 use Net::Works::Network;
13  
14 use Moose;
15  
16 has ip_version => (
17 is => 'ro',
18 isa => 'Int',
19 required => 1,
20 );
21  
22 has show_full_data_record => (
23 is => 'ro',
24 isa => 'Bool',
25 default => 0,
26 );
27  
28 has graph => (
29 is => 'ro',
30 isa => 'GraphViz2',
31 init_arg => undef,
32 default => sub { GraphViz2->new( global => { directed => 1 } ) },
33 );
34  
35 has _labels => (
36 is => 'ro',
37 init_arg => undef,
38 default => sub { {} },
39 );
40  
41 ## no critic (Subroutines::ProhibitManyArgs)
42 sub process_node_record {
43 my $self = shift;
44 my $node_num = shift;
45 my $dir = shift;
46 my $node_ip_num = shift;
47 my $node_prefix_length = shift;
48 my $record_ip_num = shift;
49 my $record_prefix_length = shift;
50 my $record_node_num = shift;
51  
52 $self->graph()->add_edge(
53 from => $self->_label_for_node(
54 $node_num, $node_ip_num, $node_prefix_length
55 ),
56 to => $self->_label_for_node(
57 $record_node_num, $record_ip_num, $record_prefix_length
58 ),
59 label => ( $dir ? 'RIGHT' : 'LEFT' ),
60 );
61  
62 return 1;
63 }
64  
65 sub process_empty_record {
66 return;
67 }
68  
69 sub process_data_record {
70 my $self = shift;
71 my $node_num = shift;
72 my $dir = shift;
73 my $node_ip_num = shift;
74 my $node_prefix_length = shift;
75 my $record_ip_num = shift;
76 my $record_prefix_length = shift;
77 my $value = shift;
78  
79 $self->graph()->add_edge(
80 from => $self->_label_for_node(
81 $node_num, $node_ip_num, $node_prefix_length
82 ),
83 to => $self->_network( $record_ip_num, $record_prefix_length ) . ' = '
84 . $self->_data_record_representation($value),
85 label => ( $dir ? 'RIGHT' : 'LEFT' ),
86 );
87  
88 return 1;
89 }
90  
91 sub _label_for_node {
92 my $self = shift;
93 my $node_num = shift;
94 my $ip_num = shift;
95 my $prefix_length = shift;
96  
97 my $network = $self->_network( $ip_num, $prefix_length );
98  
99 return $self->_labels()->{$node_num} //= sprintf(
100 'Node %d - %s (%s - %s)',
101 $node_num,
102 $network->as_string,
103 $network->first()->as_string,
104 $network->last()->as_string,
105 );
106 }
107  
108 sub _data_record_representation {
109 my $self = shift;
110 my $value = shift;
111  
112 if ( $self->show_full_data_record() ) {
113 return quotemeta( Dumper($value) );
114 }
115 else {
116 return md5_hex( Dumper($value) );
117 }
118 }
119  
120 sub _network {
121 my $self = shift;
122 my $ip_num = shift;
123 my $prefix_length = shift;
124  
125 return Net::Works::Network->new_from_integer(
126 integer => $ip_num,
127 prefix_length => $prefix_length,
128 version => $self->ip_version(),
129 );
130 }
131  
132 __PACKAGE__->meta()->make_immutable();
133  
134 1;