blib/lib/Smolder/Control/Graphs.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 185 CC 11 SCAR 31.2
CC
11
Cov
0%
image
72.0
start
12.0
_create_progress_gd
6.0
CRAP 22.6
1 package Smolder::Control::Graphs;
2 use base 'Smolder::Control';
3 use strict;
4 use warnings;
5 use Smolder::Conf;
6 use Smolder::DB::Project;
7 use Smolder::DB::SmokeReport;
8 use DateTime;
9 use DateTime::Format::Strptime;
10 use File::Spec::Functions qw(catdir catfile);
11 use GD::Graph::area;
12 use GD::Graph::bars3d;
13 use GD::Graph::lines3d;
14 use GD::Graph::linespoints;
15 use GD::Text;
16 use HTML::FillInForm;
17  
18 =head1 NAME
19  
20 Smolder::Control::Graphs
21  
22 =head1 DESCRIPTION
23  
24 Controller module for generating graph images.
25  
26 =cut
27  
28 # allowable graph types
29 my %TYPE_MAP = (
30 bar => 'bars3d',
31 line => 'lines3d',
32 area => 'area',
33 points => 'linespoints',
34 );
35  
36 # corresponding color and legend for each data type
37 my %FIELDS = (
38 total => [qw(lblue Total)],
39 pass => [qw(green Pass)],
40 fail => [qw(red Fail)],
41 todo => [qw(lorange TODO)],
42 skip => [qw(lyellow Skip)],
43 duration => [qw(lbrown Duration)],
44 );
45  
46 sub setup {
47 my $self = shift;
48 $self->start_mode('start');
49 $self->run_modes(
50 [
51 qw(
52 start
53 image
54 )
55 ]
56 );
57 }
58  
59 =head1 RUN MODES
60  
61 =head2 start
62  
63 Display the initial start form for a project's graph with some
64 reasonable defaults. Uses the F<Developer/Graphs/start.tmpl>
65 template.
66  
67 =cut
68  
69 sub start {
70 my ($self, $tt_params) = @_;
71 $tt_params ||= {};
72  
73 my $project = Smolder::DB::Project->retrieve($self->param('id'));
74 return $self->error_message('Project does not exist')
75 unless $project;
76  
77 # make sure ths developer is a member of this project
78 return $self->error_message('Unauthorized for this project')
79 unless $self->can_see_project($project);
80  
81 $tt_params->{project} = $project;
82  
83 # the defaults
84 my %fill_data = (
85 start => $project->graph_start_datetime->strftime(' %m/%d/%Y'),
86 stop => DateTime->today()->strftime('%m/%d/%Y'),
87 pass => 1,
88 fail => 1,
89 type => 'bar',
90 );
91  
92 return HTML::FillInForm->new()->fill(
93 scalarref => $self->tt_process($tt_params),
94 fdat => \%fill_data,
95 );
96 }
97  
98 =head2 image
99  
100 Creates and returns a graph image to the browser based on the parameters
101 chosen by the user.
102  
103 =cut
104  
105 sub image {
106 my $self = shift;
107 my $query = $self->query();
108  
109 my $project = Smolder::DB::Project->retrieve($self->param('id'));
110 return $self->error_message('Project does not exist')
111 unless $project;
112  
113 my ($start, $stop);
114 my $dt_format = DateTime::Format::Strptime->new(pattern => '%m/%d/%Y',);
115 if ($query->param('start')) {
116 $start = $dt_format->parse_datetime($query->param('start'));
117 } else {
118 $start = $project->graph_start_datetime;
119 }
120 if ($query->param('stop')) {
121 $stop = $dt_format->parse_datetime($query->param('stop'));
122 } else {
123 $stop = DateTime->today();
124 }
125 $self->log->debug("Graph starting $start and ending $stop");
126  
127 # which fields do we need to show?
128 my @fields;
129 if ($query->param('change')) {
130 foreach my $field (keys %FIELDS) {
131 push(@fields, $field) if ($query->param($field));
132 }
133 } else {
134  
135 # by default, show pass vs fail
136 @fields = qw(pass fail);
137 }
138  
139 my %search_params = (
140 start => $start,
141 stop => $stop,
142 );
143  
144 foreach my $extra_param qw(tag architecture platform) {
145 $search_params{$extra_param} = $query->param($extra_param)
146 if ($query->param($extra_param));
147 }
148  
149 my $data = $project->report_graph_data(
150 fields => \@fields,
151 %search_params,
152 );
153  
154 # send out our own headers
155 # XXX - fix to use CGI.pm no Apache to send no-cache image/png headers'
156 $self->header_type('none');
157  
158 # if we don't have any data, then just send the no_graph_data.png file
159 if (scalar @$data == 0) {
160 my $NO_DATA_FH;
161 my $file = catfile(Smolder::Conf->get('HtdocsDir'), 'images', 'no_graph_data.png');
162 open($NO_DATA_FH, '<', $file)
163 or die "Could not open '$file' for reading: $!";
164 local $/ = undef;
165 print <$NO_DATA_FH>;
166 close($NO_DATA_FH) or die "Could not close file '$file': $!";
167 } else {
168  
169 # else create the graph and send it
170 my @colors = map { $FIELDS{$_}->[0] } @fields;
171 my @legend = map { $FIELDS{$_}->[1] } @fields;
172  
173 my $title = "'"
174 . $project->name
175 . "' Smoke Progress ("
176 . $start->strftime('%m/%d/%Y') . ' - '
177 . $stop->strftime('%m/%d/%Y') . ')';
178 my $gd = $self->_create_progress_gd(
179 colors => \@colors,
180 data => $data,
181 legend => \@legend,
182 title => $title,
183 );
184 print $gd->png;
185 }
186 }
187  
188 sub _create_progress_gd {
189 my ($self, %args) = @_;
190 my $data = $args{data};
191 my $colors = $args{colors};
192 my $legend = $args{legend};
193 my $title = $args{title};
194  
195 # what type of graph are we?
196 my $type = $TYPE_MAP{$self->param('type')} || 'bars3d';
197  
198 # we just want to show the first, middle and last labels
199 # on the X axis
200 my $x_skip = int((scalar(@{$data->[0]}) - 1) / 2) + 1;
201  
202 # find the maximun value for the Y axis
203 my $y_max = 0;
204 for my $i (1 .. $#$data) {
205 foreach my $point (@{$data->[$i]}) {
206 $y_max = $point if ($point > $y_max);
207 }
208 }
209  
210 # now round up to the nearest 100
211 $y_max = ($y_max + 100) - ($y_max % 100);
212  
213 my $class = 'GD::Graph::' . $type;
214 my $graph = $class->new(600, 300);
215 $graph->set(
216 title => $title,
217 bgclr => 'white',
218 fgclr => 'gray',
219 textclr => 'dgray',
220 axislabelclr => 'dgray',
221 labelclr => 'dgray',
222 dclrs => $colors,
223 y_label => '# of Tests',
224 y_max_value => $y_max,
225 x_label_skip => $x_skip,
226 overwrite => 1,
227 legend_placement => 'RT',
228 legend_marker_width => 20,
229 legend_marker_height => 20,
230 legend_spacing => 8,
231 x_labels_vertical => 0,
232 ) or die "Could not set graph attributes! - " . $graph->error();
233 $graph->set_legend(@$legend);
234  
235 my $gd = $graph->plot($data)
236 or die "Could not plot graph into GD object! - " . $graph->error();
237 return $gd;
238 }
239  
240 1;