Compact.pm

filter: Statementstmt 88.9% 121 / 136 Branchbran 68.7% 22 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 94.1% 16 / 17 Podpod 40.0% 2 / 5 totaltotal 84.5% 169 / 200 CC 25 SCAR 18.9
CC
25
Cov
85%
to_ds
20.0
to_svg
10.0
layout
6.0
CRAP 6.6
↑ Summary Compact/Event.pm »
1 package SVG::Timeline::Compact;
2  
3 # ABSTRACT: A Moose based SVG Timeline drawing class.
4  
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use SVG::Timeline::Compact::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DateTime::Format::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POSIX ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16
17  
18  
19 1 has _events =>(is=>'ro',isa =>'ArrayRef[SVG::Timeline::Compact::Event]',traits=>['Array'],handles=>{ev_push=>'push',ev_all=>'elements'});
20 1 has _min=>(is=>'rw',isa =>'DateTime');
21 1 has _max=>(is=>'rw',isa =>'DateTime');
22 1 ▶has _resolution=>(is=>'rw',isa=>'DateTime::Duration',default=>sub { DateTime::Duration->new(days=>1); });
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 has min_width=>(is=>'ro',isa=>'Int',default=>1);
24 1 has min_height=>(is=>'ro',isa=>'Int',default=>20); # Each Bar is 20 pixels high
25 1 has units=>(is=>'ro',isa=>'Int',default=>800);
26 1 1;
27  
28  
29 11 ▶sub add_event {
Subroutine add_event: called CC=5 SCAR=16.1
Pod documented
30 11 my ($self,%hsh)=@_;
31 11 ▶ if (not defined $self->_min or DateTime->compare($self->_min,$hsh{start})>0 ) {$self->_min($hsh{start});}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->_min or "DateTime"->compare($self->_min, $hsh{"start"}) > 0) 2 9 11
Condition100%3/3
not defined $self->_min or "DateTime"->compare($self->_min, $hsh{"start"}) > 0
l!l&&r!l&&!r
119
MC/DC100%2/2
not defined $self->_min or "DateTime"->compare($self->_min, $hsh{"start"}) > 0
not defined $self->_min "DateTime"->compare($self->_min, $hsh{"start"}) > 0
Truth table100%3/3
not(A) or B
A= not defined $self->_minB= "DateTime"->compare($self->_min, $hsh{"start"}) > 0
ABresult
1X1
011
000
32 11 ▶ if (not defined $self->_max or DateTime->compare($self->_max,$hsh{end})<0 ) {$self->_max($hsh{end});}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->_max or "DateTime"->compare($self->_max, $hsh{"end"}) < 0) 1 10 11
Condition66%2/3
not defined $self->_max or "DateTime"->compare($self->_max, $hsh{"end"}) < 0
l!l&&r!l&&!r
1010
MC/DC50%1/2
not defined $self->_max or "DateTime"->compare($self->_max, $hsh{"end"}) < 0
not defined $self->_max "DateTime"->compare($self->_max, $hsh{"end"}) < 0
Truth table66%2/3
not(A) or B
A= not defined $self->_maxB= "DateTime"->compare($self->_max, $hsh{"end"}) < 0
ABresult
1X1
011
000
33 11 $hsh{min_width}=$self->min_width;
34 11 my $ev=SVG::Timeline::Compact::Event->new( %hsh);
35 11 $self->ev_push($ev);
36 }
37  
38 1 ▶sub layout {
Subroutine layout: called CC=6 SCAR=17.9
Pod undocumented
39 1 my $self=shift;
40 1 my @slot;
41 1 $self->_set_resolution();
42 1 foreach my $event (sort _byStart $self->ev_all){
43 11 my $found=0;
44 11 $event->resolution($self->_resolution);
45 11 $event->origin($self->_min);
46 11 for my $slot(0..$#slot){
47 32 my $evslot=$slot[$slot];
48 32 ▶ my $head=pop @{$evslot};if ($head){push @{$evslot}, $head;}
Branch: 1 branch
BranchTrueFalseTotal
if ($head) 32 0 32
49 32 ▶ if ($head->x0+$head->width <$event->x0){
Branch: 1 branch
BranchTrueFalseTotal
if ($head->x0 + $head->width < $event->x0) 4 28 32
50 4 push @{$evslot},$event;
51 4 $found=1;
52 4 last;
53  
54 }
55 }
56  
57 11 ▶ if ($found==0){
Branch: 1 branch
BranchTrueFalseTotal
if ($found == 0) 7 4 11
58 7 my $newslot=[$event];
59 7 unshift @slot,$newslot;
60 }
61 }
62 1 return @slot;
63 }
64  
65 0 ▶sub to_ds {
Subroutine to_ds: not called CC=4 SCAR=30.0
Pod undocumented
66 0 my $self=shift;
67 0 my @slot=$self->layout;
68 0 my @ds;
69 0 my ($y0,$y1,$x0,$maxX)=(0,1.5,0,0);
70 0 my $fmt="yyyy-MM-dd hh:mm";
71 0 foreach my $slot (@slot){
72 0 foreach my $ev (@{$slot}){
73 0 push @ds,{idchangeset=>$ev->id,count=>$ev->name,name=>$ev->tooltip,created=>$ev->start->format_cldr($fmt),
74 x=>$ev->x0,y=>$y0,width=>$ev->width,height=>$y1, status=>$ev->color};
75 0 ▶ $maxX=$ev->x0+$ev->width if ($maxX<$ev->x0+$ev->width);
Branch: 1 branch
BranchTrueFalseTotal
if $maxX < $ev->x0 + $ev->width 0 0 0
76 }
77 0 $y0+=1.5*$y1;
78 }
79 0 return {resolution=>$self->_resolution,start=>$self->_min->format_cldr($fmt),end=>$self->_max->format_cldr($fmt),maxX=>$maxX,maxY=>$y0,ds=>\@ds}
80 }
81  
82  
83 1 ▶sub to_svg{
Subroutine to_svg: called CC=10 SCAR=23.1
Pod documented
84 1 my $self=shift;
85 1 my @slot=$self->layout;
86 1 my $svg=SVG->new();
87 1 my $d = DateTime::Format::Duration->new( pattern => '%Y years, %m months, %e days, '. '%H hours, %M minutes, %S seconds');
88 1 my $bbox=$svg->group(id=>"bbox");
89 1 my $bars=$svg->group(id=>"bars");
90 1 my $def=$svg->defs(id=>"arrow","stroke-linecap"=>"round","stroke-width"=>"1");
91 1 $def->line(x1=>"-8",y1=>"-4",x2=>"1",y2=>"0");
92 1 $def->line(x1=>"1",y1=>"0",x2=>"-8",y2=>"4");
93 1 my ($y0,$y1,$x0,$maxX)=(0,$self->min_height,0,0);
94 1 foreach my $slot (@slot){
95 #p @slot;
96 7 foreach my $ev (@{$slot}){
97 #p $ev;
98 11 $x0=$ev->x0;
99 11 my $width=$ev->width;
100 11 my $color=$ev->color;
101 11 my $rect=$bars->rect(x=>$x0."px",y=>$y0."px",width=>$width."px",height=>$y1."px", fill=>$color,stroke=>"#000");
102 11 $bars->text(x=>$x0+$width/2,y=>($y0-2+$self->min_height),"text-anchor"=>"middle")->cdata($ev->name);
103 11 $rect->title->cdata($ev->tooltip.", Start: ". $ev->start.", End ". $ev->end);
104 11 ▶ $maxX=$x0+$width if ($maxX<$x0+$width);
Branch: 1 branch
BranchTrueFalseTotal
if $maxX < $x0 + $width 1 10 11
105 }
106 7 $y0+=$y1*1.5;
107  
108 }
109 #$y0+=$y1*1.5;
110 1 ▶ $maxX=$self->units if($maxX<$self->units);
Branch: 1 branch
BranchTrueFalseTotal
if $maxX < $self->units 1 0 1
111 1 my $border=$bbox->rect(x=>0,"fill-opacity"=>"0.1",y=>0,width=>$maxX."px",height=>$y0."px");
112 1 my $x=0 ;
113 1 my$dim=$bbox->group(id=>"dim");
114 1 $dim->line(x1=>0,y1=>($y0+$self->min_height),x2=>800,y2=>($y0+$self->min_height));
115 1 $dim->use("xlink:href"=>"#arrow",x=>0,y=>($y0+$self->min_height));
116 1 $dim->use("xlink:href"=>"#arrow",x=>800,y=>($y0+$self->min_height));
117 #$dim->text(x=>$maxX/2,y=>($y0+$self->min_height),"text-anchor"=>"middle")->cdata("<-------------- ". $self->_resolution. " --------------->");
118 #<use stroke="#000000" xlink:href="#ah" transform="translate(354.4 119.4)rotate(90)"/>
119 1 $bbox->text(x=>$maxX+10,y=>$self->min_height)->cdata("Min:\t".$self->_min->format_cldr("yyyy/MM/dd h:m"));
120 1 $bbox->text(x=>$maxX+10,y=>2*$self->min_height)->cdata("Max:\t".$self->_max->format_cldr("yyyy/MM/dd h:m"));
121  
122 1 my $unit;
123 1 ▶ $unit= $self->_resolution->in_units('months')." Months" if ($self->_resolution->in_units('months')>0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_resolution->in_units("months") > 0 0 1 1
124 1 ▶ $unit= $self->_resolution->in_units('days')." Days" if ($self->_resolution->in_units('days')>0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_resolution->in_units("days") > 0 0 1 1
125 1 ▶ $unit= $self->_resolution->in_units('minutes')." Minutes" if ($self->_resolution->in_units('minutes')>0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_resolution->in_units("minutes") > 0 1 0 1
126 1 $bbox->text(x=>$maxX+10,y=>3*$self->min_height)->cdata("Scale:\t ".$unit);
127 1 ▶ while($x<=$maxX){
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $maxX) 81 1 82
128 81 ▶ if($x%100 == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 100 == 0) { } 9 72 81
129 9 $bbox->line(x1=>$x,x2=>$x,y1=>$y0,y2=>0 ,style=>"stroke:#000;stroke-width:1px" );
130 9 $dim->text(x=>$x,y=>($y0+$self->min_height),"text-anchor"=>"middle")->cdata("$x");
131 }else{
132 72 $bbox->line(x1=>$x,x2=>$x,y1=>$y0,y2=>0 ,style=>"stroke:#fff;stroke-width:1px" );
133 }
134 81 $x=$x+10;
135  
136 }
137 1 return $svg->xmlify;
138 }
139 1 ▶sub _set_resolution{
Subroutine _set_resolution: called CC=1 SCAR=0.0
140 #We desire that atleast 50% of the graph is utilized. and we do not overflow.
141 #minutes up to 800 minutes
142 1 my $self=shift;
143 1 my $scale=get_scale($self->_min,$self->_max);
144 1 $self->_resolution($scale);
145 }
146 1 ▶sub get_scale{
Subroutine get_scale: called CC=4 SCAR=15.2
Pod undocumented
147 1 my ($min,$max)=@_;
148 1 my $duration=$max-$min;
149 1 my $days=$duration->in_units('months')*30 +$duration->in_units('days');
150 1 my $hours=$days*24+$duration->in_units('hours');
151 1 my $minutes=$hours*60+$duration->in_units('minutes');
152 1 my $scale;
153 #p $minutes;
154 1 ▶ if ($duration->in_units('months')>300){
Branch: 3 branches
BranchTrueFalseTotal
if ($duration->in_units('months') > 300) { } 0 1 1
elsif ($days > 300) { } 0 1 1
elsif ($hours > 300) { } 0 1 1
155 0 $scale=DateTime::Duration->new(months=>POSIX::ceil($duration->in_units('months')/800));
156 }elsif ($days>300){
157 0 $scale=DateTime::Duration->new(days=>POSIX::ceil($days/800));
158 }elsif ($hours>300){
159 0 $scale=DateTime::Duration->new(hours=>POSIX::ceil($hours/800));
160 }else{
161 1 $scale=DateTime::Duration->new(minutes=>POSIX::ceil($minutes/800));
162 }
163 1 return $scale;
164 }
165 26 ▶sub _byStart {
Subroutine _byStart: called CC=1 SCAR=0.0
166 26 return DateTime->compare($a->start,$b->start);
167 }
168  
169 __END__
↑ Summary Compact/Event.pm »