Skip to content

add json format output #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 90 additions & 12 deletions code2pg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use Getopt::Long qw(:config no_ignore_case bundling);
use File::Find::Rule;
use List::MoreUtils;
use Config::General;
use JSON;
# use File::Slurp;

################################################################################
Expand Down Expand Up @@ -103,7 +104,7 @@ my $ORAFCE = ""; # will the migration use orafce ?
my $OUTPUT_CSV = ""; # the output listing (csv format)
my $OUTPUT_DIRECTORY = ""; # the output directory
my $OUTPUT_FILE = ""; # the output report (html)
my $OUTPUT_FORMAT = "html"; # report's format (txt, html or minimal)
my $OUTPUT_FORMAT = "html"; # report's format (txt, html, json or minimal)
my $PASSWORD = ""; # password for svn access
my $QUIET = ""; # silence the script
my $RDBMS = ""; # the source rdbms instructions
Expand All @@ -122,6 +123,7 @@ my $file_config = ""; # name of the configuration file
my $file_output = "estimation.html"; # our report file name
my $file_output_handle;
my $instruction_dataset = "["; # the javascript dataset for datatables
my $inst_dict_dataset = ();
my $quiet_exec = 0; # don't output to stdout
my ( $day, $month, $year ) = (localtime)[ 3, 4, 5 ];
my $today = sprintf( "%02d/%02d/%04d", $day, $month + 1, $year + 1900 );
Expand Down Expand Up @@ -3215,7 +3217,7 @@ sub options_handling {
}

if ( ( $OUTPUT_FORMAT ne "txt" ) and ( $OUTPUT_FORMAT ne "html" ) and
( $OUTPUT_FORMAT ne "minimal" ) ) {
( $OUTPUT_FORMAT ne "json" ) and ( $OUTPUT_FORMAT ne "minimal" ) ) {
print STDERR "Unrecognized output format!\n";
exit(2);
}
Expand Down Expand Up @@ -3637,6 +3639,44 @@ $tbl_instructions
return 1;
}

################################################################################
# printing the json output
sub json_output {
my %niv_ = %{ +shift };
my $n_lvl1 = shift; # number of level1 instructions
my $n_lvl2 = shift; # number of level2 instructions
my $n_lvl3 = shift; # number of level3 instructions
my $n_lvl4 = shift; # number of level4 instructions
my $nb_fct_tot = shift;
my $estimated_load = shift; # total load in minutes
my $estimated_load_md = shift; # total load in man-days
my $ext_to_analyze = shift; # the type of extensions to analyze
my $nb_fic = shift;
my $total_nb_lines = shift; # number of analyzed lines of code

my %outdata = (
defects => {
"1"=>$n_lvl1,
"2"=>$n_lvl2,
"3"=>$n_lvl3,
"4"=>$n_lvl4,
},
n_fct_tot => $nb_fct_tot,
estimates => {
minutes => $estimated_load,
mandays => $estimated_load_md,
},
nb_files => $nb_fic,
nb_lines => $total_nb_lines,
details => $inst_dict_dataset,
ext_to_analyze => $ext_to_analyze
);

my $jsontext = encode_json({%outdata});
print $file_output_handle $jsontext;

}

################################################################################
# printing the html output
sub html_output {
Expand Down Expand Up @@ -4251,10 +4291,10 @@ sub main {
if ($OUTPUT_FORMAT eq "html") {
my $mymatch = quotemeta ${^MATCH};

# we add a comment column in the report and format it properly
my $comments = '';
# we add a comment column in the report and format it properly
my $comments = '';
if ( defined $rdbms_functions_list_{$level}{$element}{'sourcedocumentation'}) {
$comments .= "<a href=\"$rdbms_functions_list_{$level}{$element}{'sourcedocumentation'}\" target=\"_blank\">Source documentation</a><br/>";
$comments .= "<a href=\"$rdbms_functions_list_{$level}{$element}{'sourcedocumentation'}\" target=\"_blank\">Source documentation</a><br/>";
}
if ( defined $rdbms_functions_list_{$level}{$element}{'pgdocumentation'}) {
$comments .= "<a href=\"$rdbms_functions_list_{$level}{$element}{'pgdocumentation'}\" target=\"_blank\">PostgreSQL documentation</a><br/>";
Expand All @@ -4263,14 +4303,41 @@ sub main {
$comments .= "PostgreSQL instruction: $rdbms_functions_list_{$level}{$element}{'pginstruction'} <br/>";
}
if ( defined $rdbms_functions_list_{$level}{$element}{'comments'}) {
my $newcomments = $rdbms_functions_list_{$level}{$element}{'comments'} =~ s/\n/<br\/>/gr;
$newcomments =~ s/'/&apos;/g;
$newcomments =~ s/"/&quot;/g;
$comments .= "Comments: $newcomments <br/>";
my $newcomments = $rdbms_functions_list_{$level}{$element}{'comments'} =~ s/\n/<br\/>/gr;
$newcomments =~ s/'/&apos;/g;
$newcomments =~ s/"/&quot;/g;
$comments .= "Comments: $newcomments <br/>";
}
my $file_link = '<a href="file:///' . $file .'">' . $file .'</a>';
my $file_link = '<a href="file:///' . $file .'">' . $file .'</a>';
$instruction_dataset .= "\n\t['$level', '$file_link', '$line_number', '$element', '$mymatch', '$comments' ],";
}
if ( $OUTPUT_FORMAT eq "json" ) {
my $mymatch = quotemeta ${^MATCH};

# we add a comment column in the report and format it properly
my %comments = ();
if ( defined $rdbms_functions_list_{$level}{$element}{'sourcedocumentation'}) {
$comments{'souce_doc'} = $rdbms_functions_list_{$level}{$element}{'sourcedocumentation'};
}
if ( defined $rdbms_functions_list_{$level}{$element}{'pgdocumentation'}) {
$comments{'pgdoc'} = $rdbms_functions_list_{$level}{$element}{'pgdocumentation'};
}
if ( defined $rdbms_functions_list_{$level}{$element}{'pginstruction'}) {
$comments{"pginstruction"} = $rdbms_functions_list_{$level}{$element}{'pginstruction'};
}
if ( defined $rdbms_functions_list_{$level}{$element}{'comments'}) {
$comments{"comments"} = $rdbms_functions_list_{$level}{$element}{'comments'};
}
my $row_data = {
level => $level,
filename => $file,
lineno => $line_number,
element => $element,
matching => $mymatch,
comments => {%comments}
};
push(@{$inst_dict_dataset}, $row_data);
}

# use edit-file ?
if ($TAGFILE and ($directory_type eq "file") ) {
Expand All @@ -4288,7 +4355,7 @@ sub main {
. $LANGUAGE_COMMENTS_END{$chosen_language} . "/'";
# print "SED COMMAND = `sed -i $sed_command $file`\n" if ($DEBUG);
my $sed_res = `sed -i $sed_command "$file"` ;

$modified_file = 1;
}
}
Expand All @@ -4297,7 +4364,7 @@ sub main {

# Sed is adding ^M on the files I tried. We remove them.
if ( $modified_file == 1 ) {
my $sed_res = `sed -i 's///g' "$file"`;
my $sed_res = `sed -i 's/\r//g' "$file"`;
}
}
print "\rDone ! \n" if (!$quiet_exec);
Expand Down Expand Up @@ -4349,6 +4416,17 @@ sub main {
$nb_analyzed_lines
);
}
elsif ( $OUTPUT_FORMAT eq "json" ) {
json_output(
\%matching_lvl_, $count_lvl_{1},
$count_lvl_{2}, $count_lvl_{3},
$count_lvl_{4}, $new_nb_fct_total,
$estimated_load, $estimated_load_mandays,
$extensions_to_analyze, $nb_files,
$nb_analyzed_lines
);
}

else { # $OUTPUT_FORMAT will be "minimal"
if ($OUTPUT_FILE eq "") {
printf ("Estimation: %.2f man-days\n", $estimated_load_mandays );
Expand Down
2 changes: 1 addition & 1 deletion doc/code2pg.pod
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Can be used multiple times, for eg: ./code2pg -e ext1 -e ext2 ...
=item B<-f> , B<--format> string

The report will be generated according to the specified format. It could be one
of these values: txt, html or minimal. Defaults to html.
of these values: txt, html, json or minimal. Defaults to html.

The txt output is markdown compatible.

Expand Down