This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
/
generate-consolidated-charts.pl
executable file
·340 lines (300 loc) · 10.3 KB
/
generate-consolidated-charts.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/perl
use Getopt::Std;
use File::Basename;
use XML::DOM;
use POSIX;
use Switch;
use Getopt::Long;
use strict;
my $usage =<< "END";
Usage : perl generate_charts_consolidated.pl --distro <distro_name>
--profile <profile>
--start <start date>
[--root <dest dir>]
--help
END
my ($distro_name, $profile, $start_date, $root_dir, $help);
my $result = GetOptions("distro:s"=>\$distro_name,
"profile:s"=> \$profile,
"start:s"=>\$start_date,
"root:s"=>\$root_dir,
"help!"=>\$help);
if (!$result || $help) {
print $usage;
exit;
}
if (!defined $distro_name || !defined($profile)) {
print $usage;
exit;
}
if (!defined $start_date) {
$start_date = "0";
}
if (!defined $root_dir) {
$root_dir = "/var/www/html/testresults";
}
my @dates = ();
my %test_suite_names = ();
my ($xml_dir, $chart_dir);
$root_dir .= "/$distro_name/$profile";
if ( !(-e $root_dir) || !(-d $root_dir)) {
print "Invalid distro name\n";
exit;
}
$xml_dir = $root_dir."/xml/";
$chart_dir = $root_dir."/charts/";
#$chart_dir = "/var/www/html/new_charts/$distro_name/$profile";
my @filenames = get_sorted_filenames($xml_dir, $start_date,$profile);
my %pass_fail_stats = ();
my %perc_stats = ();
my %total_stats = ();
foreach my $file (@filenames) {
print "Processing file : $file...\n";
get_stats ($file, \%pass_fail_stats, \%perc_stats, \%total_stats, \@dates, \%test_suite_names);
}
my $output_file = "$root_dir/testresults.dat";
write_data_to_file(\%pass_fail_stats, \@dates, \%test_suite_names, $output_file, 1 );
generate_charts($output_file, $chart_dir, "individual" );
$output_file = "$root_dir/testresults_perc.dat";
write_data_to_file(\%perc_stats, \@dates, \%test_suite_names, $output_file, 1 );
generate_charts($output_file, $chart_dir, "consolidated" );
$output_file = "$root_dir/total.dat";
write_data_to_file(\%total_stats, $output_file, 0, 0, 0 );
generate_charts($output_file, $chart_dir, "total" );
sub get_sorted_filenames
{
my ($dir, $start_date, $profile) = (@_);
opendir(DIR,$dir);
my $files_count = 0;
my @files = readdir(DIR);
my @sorted_files = ();
my $file_count = 0;
foreach my $file(@files) {
if ($file =~/^testresults\-$profile\-(\d{8}).xml$/) {
#if ($file =~/^testresults\-(\d{8}).xml$/) {
my $file_date = $1;
# Since dates are of teh YYYYMMDD format, we can
# safely compare them as numbers
if ($file_date >= $start_date) {
$sorted_files[$file_count] = $dir . $file;
$file_count++;
}
}
}
@sorted_files = sort(@sorted_files);
return @sorted_files;
}
sub get_stats {
my ($filename, $pass_fail_hash, $perc_hash, $total_hash, $dates, $keys) = (@_);
my $xmlParser = new XML::DOM::Parser();
my $doc = $xmlParser->parsefile($filename);
my @testsuites = $doc->getElementsByTagName("testsuite");
my $date = get_date_from_filename($filename);
push (@$dates, $date);
my $num_passes = 0;
my $num_failures = 0;
foreach my $testsuite (@testsuites) {
my $testsuite_name = $testsuite->getAttribute("name");
$keys->{$testsuite_name}= 1;
my $testsuite_failures = $testsuite->getAttribute("fail");
$num_failures += $testsuite_failures;
$pass_fail_hash->{$testsuite_name}{$date}{"fail"} = $testsuite_failures;
my $testsuite_passes = $testsuite->getAttribute("pass");
$num_passes += $testsuite_passes;
$pass_fail_hash->{$testsuite_name}{$date}{"pass"} = $testsuite_passes;
my $total = $testsuite_passes + $testsuite_failures ;
if ($total == 0) {
next;
}
$perc_hash->{$testsuite_name}{$date}{"pass"} = $testsuite_passes/$total * 100;
$perc_hash->{$testsuite_name}{$date}{"fail"} = $testsuite_failures/$total * 100;
$total_hash->{$date}{"fail"} = $num_failures;
$total_hash->{$date}{"pass"} = $num_passes;
$total_hash->{$date}{"fail_perc"} = $num_failures / ($num_passes + $num_failures) * 100;
$total_hash->{$date}{"pass_perc"} = $num_passes / ($num_passes + $num_failures) * 100;
}
}
sub get_date_from_filename {
my $fullPath = shift;
my $filename = basename($fullPath);
my $date = "";
if ($filename =~ /(\d{8})\.xml$/) {
$date = $1;
} else {
print "Invalid file name : $filename";
}
return $date;
}
sub write_data_to_file {
my ($hash, $dates, $keys, $file, $flag) = (@_);
if ( $flag == 1 )
{
open (INFILE, "<$file"); #|| die ("Cannot open file:$file for reading!! \n");
my @lines = <INFILE>;
close(INFILE);
open (OUTFILE, ">$file") || die ("Cant open file:$file for writing!! \n");
my @existing_testsuites_list = split(/\s/,$lines[0]);
if ($existing_testsuites_list[0] eq "#") {
undef $existing_testsuites_list[0];
}
for my $test_suite (@existing_testsuites_list) {
$keys->{$test_suite} = 0;
}
my $num_existing_elements = $#existing_testsuites_list;
for my $key (keys %$keys) {
if ($keys->{$key} == 1) {
push (@existing_testsuites_list, $key);
}
}
my $num_new_elements = $#existing_testsuites_list;
my $append_string= "";
for (my $i = 0; $i < $num_new_elements- $num_existing_elements; $i++) {
$append_string .= ";;";
}
print OUTFILE "# ";
foreach my $key (@existing_testsuites_list) {
if ($key eq "") {
next;
}
print OUTFILE "$key ";
}
print OUTFILE "\n";
for(my $line=1; $line<=$#lines;$line++){
chomp ($lines[$line]);
print OUTFILE "$lines[$line]$append_string\n";
}
foreach my $date ( sort @$dates) {
print OUTFILE "$date;";
foreach my $key (@existing_testsuites_list) {
if ($key eq "") {
next;
}
if (defined ($hash->{$key}{$date}{"pass"})) {
print OUTFILE $hash->{$key}{$date}{"pass"}.";";
} else {
print OUTFILE ";";
}
if (defined ($hash->{$key}{$date}{"fail"})) {
print OUTFILE $hash->{$key}{$date}{"fail"}.";";
} else {
print OUTFILE ";";
}
}
print OUTFILE "\n";
}
close OUTFILE;
}
if ( $flag == 0 )
{
my $file_exists = 0;
if ( -e $output_file) {
$file_exists = 1;
}
open(OUTPUT, ">>$output_file") || die ("Cannot open file for reading");
if (!$file_exists) {
print OUTPUT "# Passes Fail Pass% Fail%\n";
}
foreach my $date (sort keys %$hash) {
print OUTPUT "$date;";
print OUTPUT $hash->{$date}{"pass"} .";";
print OUTPUT $hash->{$date}{"fail"} .";";
print OUTPUT $hash->{$date}{"pass_perc"} .";";
print OUTPUT $hash->{$date}{"fail_perc"} .";";
print OUTPUT "\n";
}
close(OUTPUT);
}
}
sub generate_charts {
my ($filename, $root_dir, $type) = (@_);
open (INFILE, "<$filename") || die("Cannot open file :$filename for reading");
my $line = "";
my @fields = ();
print "$root_dir";
if (defined ($line = <INFILE>)) {
if (substr($line,0,1) != '#') {
print "Invalid file format\n";
close(INFILE);
return;
} else {
@fields = split(/\s/, substr($line,1));
}
}
close(INFILE);
if ($type eq "individual") {
for (my $i = 1; $i<=$#fields; $i++) {
my $outfile = "$root_dir/$fields[$i]_pass.png";
my $title = "Progress Charts for $fields[$i]";
my $ylabel = "Number Of Passes";
my $plot_cmd = "plot \"$filename\" using 1:2*$i title \"$title\" with linespoints lt 2 lw 1 pt 5 ps 1;";
plot_graph($outfile, $title, $ylabel, $plot_cmd);
$ylabel = "Number Of Failures";
$outfile = "$root_dir/$fields[$i]_fail.png";
$plot_cmd = "plot \"$filename\" using 1:2*$i+1 title \"$title\" with linespoints lt 1 lw 1 pt 5 ps 1;";
plot_graph($outfile, $title, $ylabel, $plot_cmd);
}
} elsif ($type eq "consolidated") {
for (my $i = 1; $i<=$#fields; $i++) {
my $outfile = "$root_dir/$fields[$i]_percent.png";
my $title = "Progress Charts for $fields[$i]";
my $ylabel = "Percentage (%)";
my $plot_cmd = "plot \"$filename\" using 1:2*$i title \"$title - Pass %\" with linespoints lt 2 lw 1 pt 5 ps 1, \"$filename\" using 1:2*$i+1 title \"$title - Fail %\" with linespoints lt 1 lw 1 pt 5 ps 1;";
plot_graph($outfile, $title, $ylabel, $plot_cmd);
}
} elsif ($type eq "total") {
my $outfile = "";
my $title = "";
my $ylabel = "";
my $linetype = "";
for (my $i = 2; $i <= 3; $i++) {
if ($i == 2) {
$outfile = "$root_dir/Total_pass.png";
$title = "Total number of Passes";
$ylabel = "Passes";
$linetype = 2;
} else {
$outfile = "$root_dir/Total_fail.png";
$title = "Total number of Failures";
$ylabel = "Failures";
$linetype = 1;
}
my $plot_cmd = "plot \"$filename\" using 1:$i title \"$title\" with linespoints lt $linetype lw 1 pt 5 ps 1;";
plot_graph($outfile, $title, $ylabel, $plot_cmd);
}
$outfile = "$root_dir/Total_percent.png";
$title = "Total";
$ylabel = "Percentage (%)";
$linetype = 1;
my $plot_cmd = "plot \"$filename\" using 1:4 title \"$title - Pass %\" with linespoints lt 2 lw 1 pt 5 ps 1, \"$filename\" using 1:5 title \"$title - Fail %\" with linespoints lt 1 lw 1 pt 5 ps 1;";
plot_graph($outfile, $title, $ylabel, $plot_cmd);
}
close INFILE;
}
sub plot_graph {
my ($output_file, $title, $ylabel, $plot_cmd) = (@_);
my $GNUPlot = '/usr/bin/gnuplot';
open ( GNUPLOT, "|$GNUPlot");
my $cmds = "";
#print "$plot_cmd\n";
$cmds =<< "gnuplot_Commands";
set terminal png;
set output "$output_file";
set time;
set title "$title";
set datafile separator ";"
set xlabel "Dates" 7,-3;
set ylabel "$ylabel"
set xdata time;
set timefmt "%Y%m%d";
set format x "%Y%m%d";
set key box lw 0.5;
set style line;
set size ratio .50;
set autoscale;
set size ratio 0.55;
$plot_cmd
gnuplot_Commands
print $cmds."\n";
print GNUPLOT $cmds;
close(GNUPLOT);
}