[ONOS-7175]: Refractor R Scripts that generate wiki result graphs in TestON.

- Code is "chunked"; blocks of code are sectioned into sub-steps.
- Many comments have been added and updated.
- Many console messages have been added and updated.

Change-Id: I86853b4a3917d807e634311b672ab6d6d57b1194
diff --git a/TestON/JenkinsFile/scripts/SCPFscalingMaxIntents.R b/TestON/JenkinsFile/scripts/SCPFscalingMaxIntents.R
index 0f09023..2ca0627 100644
--- a/TestON/JenkinsFile/scripts/SCPFscalingMaxIntents.R
+++ b/TestON/JenkinsFile/scripts/SCPFscalingMaxIntents.R
@@ -21,75 +21,127 @@
 # please contact Jeremy Ronquillo: j_ronquillo@u.pacific.edu
 
 # **********************************************************
-# STEP 1: File management.
+# STEP 1: Data management.
 # **********************************************************
 
-print( "STEP 1: File management." )
+print( "**********************************************************" )
+print( "STEP 1: Data management." )
+print( "**********************************************************" )
 
 print( "Reading commmand-line args." )
 args <- commandArgs( trailingOnly=TRUE )
 
-# Import libraries to be used for graphing and organizing data, respectively.
-# Find out more about ggplot2: https://github.com/tidyverse/ggplot2
-#                     reshape2: https://github.com/hadley/reshape
+# ----------------
+# Import Libraries
+# ----------------
+
 print( "Importing libraries." )
 library( ggplot2 )
 library( reshape2 )
 library( RPostgreSQL )    # For databases
 
-# Normal usage
-# Check if sufficient args are provided.
+# -------------------
+# Check CLI Arguments
+# -------------------
+
+print( "Verifying CLI args." )
+
 if ( is.na( args[ 8 ] ) ){
-    print( "Usage: Rscript SCPFInstalledIntentsFlows <has-flowObj> <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <directory-to-save-graphs>" )
+
+    print( paste( "Usage: Rscript SCPFInstalledIntentsFlows",
+                                  "<has-flowObj>",
+                                  "<database-host>",
+                                  "<database-port>",
+                                  "<database-user-id>",
+                                  "<database-password>",
+                                  "<test-name>",
+                                  "<branch-name>",
+                                  "<directory-to-save-graphs>",
+                                  sep=" " ) )
+
     q()  # basically exit(), but in R
 }
 
-# paste() is used to concatenate strings.
-outputFile <- paste( args[ 8 ], args[ 6 ], sep="" )
-if ( args[ 1 ] == "y" ){
-    outputFile <- paste( outputFile, "flowObj", sep="_" )
-}
-outputFile <- paste( outputFile, args[ 7 ], sep="_" )
-outputFile <- paste( outputFile, "_errGraph.jpg", sep="" )
+# -----------------
+# Create File Names
+# -----------------
 
-print( "Reading from databases." )
+print( "Creating filenames and title of graph." )
 
-con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 2 ], port=strtoi( args[ 3 ] ), user=args[ 4 ],password=args[ 5 ] )
-
-command  <- "SELECT * FROM max_intents_"
-if ( args[ 1 ] == "y" ){
-    command <- paste( command, "fobj_", sep="" )
-}
-command <- paste( command, "tests WHERE branch = '", sep = "" )
-command <- paste( command, args[ 7 ], sep="" )
-command <- paste( command, "' AND date IN ( SELECT MAX( date ) FROM max_intents_", sep="" )
-if ( args[ 1 ] == "y" ){
-    command <- paste( command, "fobj_", sep="" )
-}
-command <- paste( command, "tests WHERE branch = '", sep = "" )
-command <- paste( command, args[ 7 ], sep = "" )
-command <- paste( command, "' ) ", sep="" )
-
-print( paste( "Sending SQL command:", command ) )
-
-fileData <- dbGetQuery( con, command )
+fileFlowObjModifier <- ""
+sqlFlowObjModifier <- ""
+chartTitle <- "Number of Installed Intents & Flows"
 
 if ( args[ 1 ] == "y" ){
+    fileFlowObjModifier <- "_flowObj"
+    sqlFlowObjModifier <- "fobj_"
     chartTitle <- "Number of Installed Intents & Flows\n with Flow Objectives"
-} else {
-    chartTitle <- "Number of Installed Intents & Flows"
 }
 
+outputFile <- paste( args[ 8 ],
+                     args[ 6 ],
+                     fileFlowObjModifier,
+                     "_",
+                     args[ 7 ],
+                     "_errGraph.jpg",
+                     sep="" )
+
+# ------------------
+# SQL Initialization
+# ------------------
+
+print( "Initializing SQL" )
+
+con <- dbConnect( dbDriver( "PostgreSQL" ),
+                  dbname = "onostest",
+                  host = args[ 2 ],
+                  port = strtoi( args[ 3 ] ),
+                  user = args[ 4 ],
+                  password = args[ 5 ] )
+
+# -------------------------------
+# Scaling Max Intents SQL Command
+# -------------------------------
+
+print( "Scaling Max Intents SQL Command" )
+
+command <- paste( "SELECT * FROM max_intents_",
+                  sqlFlowObjModifier,
+                  "tests WHERE branch = '",
+                  args[ 7 ],
+                  "' AND date IN ( SELECT MAX( date ) FROM max_intents_",
+                  sqlFlowObjModifier,
+                  "tests WHERE branch = '",
+                  args[ 7 ],
+                  "' ) ",
+                  sep="" )
+
+print( "Sending SQL command:" )
+print( command )
+fileData <- dbGetQuery( con, command )
+
 # **********************************************************
 # STEP 2: Organize data.
 # **********************************************************
 
-fileDataNames <- names( fileData )
+print( "**********************************************************" )
+print( "STEP 2: Organize Data." )
+print( "**********************************************************" )
 
-avgs <- c()
+# ------------
+# Data Sorting
+# ------------
 
 print( "Sorting data." )
-avgs <- c( fileData[ 'max_intents_ovs' ], fileData[ 'max_flows_ovs' ] )
+
+avgs <- c( fileData[ 'max_intents_ovs' ],
+           fileData[ 'max_flows_ovs' ] )
+
+# --------------------
+# Construct Data Frame
+# --------------------
+
+print( "Constructing Data Frame" )
 
 dataFrame <- melt( avgs )
 dataFrame$scale <- fileData$scale
@@ -108,30 +160,90 @@
 # STEP 3: Generate graphs.
 # **********************************************************
 
+print( "**********************************************************" )
+print( "STEP 3: Generate Graph." )
+print( "**********************************************************" )
+
+# ------------------
+# Generate Main Plot
+# ------------------
+
+print( "Creating main plot." )
+mainPlot <- ggplot( data = dataFrame, aes( x = scale,
+                                           y = ms,
+                                           fill = type ) )
+
+# ------------------------------
+# Fundamental Variables Assigned
+# ------------------------------
+
 print( "Generating fundamental graph data." )
 
+barWidth <- 1.3
 theme_set( theme_grey( base_size = 22 ) )   # set the default text size of the graph.
-
-mainPlot <- ggplot( data = dataFrame, aes( x = scale, y = ms, fill = type ) )
-xScaleConfig <- scale_x_continuous( breaks=c( 1, 3, 5, 7, 9) )
+xScaleConfig <- scale_x_continuous( breaks=c( 1, 3, 5, 7, 9 ) )
 xLabel <- xlab( "Scale" )
 yLabel <- ylab( "Max Number of Intents/Flow Rules" )
 fillLabel <- labs( fill="Type" )
-theme <- theme( plot.title=element_text( hjust = 0.5, size = 32, face='bold' ), legend.position="bottom", legend.text=element_text( size=22 ), legend.title = element_blank(), legend.key.size = unit( 1.5, 'lines' ) )
-colors <- scale_fill_manual( values=c( "#F77670", "#619DFA" ) )
-wrapLegend <- guides( fill=guide_legend( nrow=1, byrow=TRUE ) )
-fundamentalGraphData <- mainPlot + xScaleConfig + xLabel + yLabel + fillLabel + theme + wrapLegend
+imageWidth <- 15
+imageHeight <- 10
+imageDPI <- 200
 
+theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face = 'bold' ),
+                legend.position = "bottom",
+                legend.text = element_text( size=22 ),
+                legend.title = element_blank(),
+                legend.key.size = unit( 1.5, 'lines' ) )
 
-print( "Generating bar graph bars." )
-width <- 1.3
-barGraphFormat <- geom_bar( stat="identity", position=position_dodge( ), width = width )
-values <- geom_text( aes( x=dataFrame$scale, y=dataFrame$ms + 0.015 * max( dataFrame$ms ), label = format( dataFrame$ms, digits=3, big.mark = ",", scientific = FALSE ) ), size = 5.2, fontface = "bold", position=position_dodge( width=1.25 ) )
+colors <- scale_fill_manual( values = c( "#F77670",
+                                         "#619DFA" ) )
+
+wrapLegend <- guides( fill = guide_legend( nrow = 1, byrow = TRUE ) )
 title <- ggtitle( chartTitle )
-result <- fundamentalGraphData + barGraphFormat + colors + title + values
 
+fundamentalGraphData <- mainPlot +
+                        xScaleConfig +
+                        xLabel +
+                        yLabel +
+                        fillLabel +
+                        theme +
+                        wrapLegend +
+                        title +
+                        colors
+
+# ---------------------------
+# Generating Bar Graph Format
+# ---------------------------
+
+print( "Generating bar graph." )
+
+barGraphFormat <- geom_bar( stat = "identity",
+                            position = position_dodge(),
+                            width = barWidth )
+
+values <- geom_text( aes( x = dataFrame$scale,
+                          y = dataFrame$ms + 0.015 * max( dataFrame$ms ),
+                          label = format( dataFrame$ms,
+                                          digits=3,
+                                          big.mark = ",",
+                                          scientific = FALSE ) ),
+                          size = 5.2,
+                          fontface = "bold",
+                          position = position_dodge( width = 1.25 ) )
+
+result <- fundamentalGraphData +
+          barGraphFormat +
+          values
+
+# -----------------------
+# Exporting Graph to File
+# -----------------------
 
 print( paste( "Saving bar chart to", outputFile ) )
-ggsave( outputFile, width = 15, height = 10, dpi = 200 )
 
-print( paste( "Successfully wrote bar chart out to", outputFile ) )
+ggsave( outputFile,
+        width = imageWidth,
+        height = imageHeight,
+        dpi = imageDPI )
+
+print( paste( "[SUCCESS] Successfully wrote bar chart out to", outputFile ) )