[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/SCPFflowTp1g.R b/TestON/JenkinsFile/scripts/SCPFflowTp1g.R
index 26584ad..9c79ac8 100644
--- a/TestON/JenkinsFile/scripts/SCPFflowTp1g.R
+++ b/TestON/JenkinsFile/scripts/SCPFflowTp1g.R
@@ -21,89 +21,158 @@
 # 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( "**********************************************************" )
 
 # Command line arguments are read.
 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[ 9 ] ) ){
-    print( "Usage: Rscript SCPFflowTp1g.R <has-flow-obj> <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <has-neighbors> <directory-to-save-graphs>" )
+
+    print( paste( "Usage: Rscript SCPFflowTp1g.R",
+                                  "<has-flow-obj>",
+                                  "<database-host>",
+                                  "<database-port>",
+                                  "<database-user-id>",
+                                  "<database-password>",
+                                  "<test-name>",
+                                  "<branch-name>",
+                                  "<has-neighbors>",
+                                  "<directory-to-save-graphs>",
+                                  sep=" " ) )
+
     q()  # basically exit(), but in R
 }
 
-# paste() is used to concatenate strings.
-errBarOutputFile <- paste( args[ 9 ], args[ 6 ], sep="" )
-errBarOutputFile <- paste( errBarOutputFile, args[ 7 ], sep="_" )
-if ( args[ 8 ] == 'y' ){
-    errBarOutputFile <- paste( errBarOutputFile, "all-neighbors", sep="_" )
-} else {
-    errBarOutputFile <- paste( errBarOutputFile, "no-neighbors", sep="_" )
-}
-if ( args[ 1 ] == 'y' ){
-    errBarOutputFile <- paste( errBarOutputFile, "flowObj", sep="_")
-}
-errBarOutputFile <- paste( errBarOutputFile, "_graph.jpg", sep="" )
+# -----------------
+# Create File Names
+# -----------------
 
-print( "Reading from databases." )
-con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 2 ], port=strtoi( args[ 3 ] ), user=args[ 4 ],password=args[ 5 ] )
+print( "Creating filenames and title of graph." )
 
+chartTitle <- "Flow Throughput Test"
+fileNeighborsModifier <- "no"
 commandNeighborModifier <- ""
-flowObjModifier <- ""
+fileFlowObjModifier <- ""
+sqlFlowObjModifier <- ""
 if ( args[ 1 ] == 'y' ){
-    flowObjModifier <- "_fobj"
+    fileFlowObjModifier <- "_flowObj"
+    sqlFlowObjModifier <- "_fobj"
+    chartTitle <- paste( chartTitle, " with Flow Objectives", sep="" )
 }
+
+chartTitle <- paste( chartTitle, "\nNeighbors =", sep="" )
+
 if ( args[ 8 ] == 'y' ){
+    fileNeighborsModifier <- "all"
     commandNeighborModifier <- "scale=1 OR NOT "
+    chartTitle <- paste( chartTitle, "Cluster Size - 1" )
+} else {
+    chartTitle <- paste( chartTitle, "0" )
 }
 
-command <- paste( "SELECT scale, avg( avg ), avg( std ) FROM flow_tp", flowObjModifier, sep="" )
-command <- paste( command, "_tests WHERE (", sep="" )
-command <- paste( command, commandNeighborModifier, sep="" )
-command <- paste( command, "neighbors = 0 ) AND branch = '", sep="" )
-command <- paste( command, args[ 7 ], sep="" )
-command <- paste( command, "' AND date IN ( SELECT max( date ) FROM flow_tp", sep="" )
-command <- paste( command, flowObjModifier, sep="" )
-command <- paste( command, "_tests WHERE branch='", sep="" )
-command <- paste( command, args[ 7 ], sep="" )
-command <- paste( command,  "' ) GROUP BY scale ORDER BY scale", sep="" )
+errBarOutputFile <- paste( args[ 9 ],
+                           args[ 6 ],
+                           "_",
+                           args[ 7 ],
+                           "_",
+                           fileNeighborsModifier,
+                           "-neighbors",
+                           fileFlowObjModifier,
+                           "_graph.jpg",
+                           sep="" )
+# ------------------
+# SQL Initialization
+# ------------------
 
-print( paste( "Sending SQL command:", command ) )
+print( "Initializing SQL" )
+
+con <- dbConnect( dbDriver( "PostgreSQL" ),
+                  dbname = "onostest",
+                  host = args[ 2 ],
+                  port = strtoi( args[ 3 ] ),
+                  user = args[ 4 ],
+                  password = args[ 5 ] )
+
+# ---------------------------
+# Flow Throughput SQL Command
+# ---------------------------
+
+print( "Generating Flow Throughput SQL command." )
+
+command <- paste( "SELECT scale, avg( avg ), avg( std ) FROM flow_tp",
+                  sqlFlowObjModifier,
+                  "_tests WHERE (",
+                  commandNeighborModifier,
+                  "neighbors = 0 ) AND branch = '",
+                  args[ 7 ],
+                  "' AND date IN ( SELECT max( date ) FROM flow_tp",
+                  sqlFlowObjModifier,
+                  "_tests WHERE branch='",
+                  args[ 7 ],
+                  "' ) GROUP BY scale ORDER BY scale",
+                  sep="" )
+
+print( "Sending SQL command:" )
+print( command )
 
 fileData <- dbGetQuery( con, command )
 
-title <- paste( args[ 6 ], args[ 7 ], sep="_" )
-
 # **********************************************************
 # STEP 2: Organize data.
 # **********************************************************
 
-print( "STEP 2: Organize data." )
+print( "**********************************************************" )
+print( "STEP 2: Organize Data." )
+print( "**********************************************************" )
 
-# Create lists c() and organize data into their corresponding list.
-print( "Sorting data." )
-colnames( fileData ) <- c( "scale", "avg", "std" )
+# ------------
+# Data Sorting
+# ------------
+
+print( "Sorting data for Flow Throughput." )
+
+colnames( fileData ) <- c( "scale",
+                           "avg",
+                           "std" )
+
 avgs <- c( fileData[ 'avg' ] )
 
-# Parse lists into data frames.
+
+# ----------------------------
+# Flow TP Construct Data Frame
+# ----------------------------
+
+print( "Constructing Flow TP data frame." )
+
 dataFrame <- melt( avgs )              # This is where reshape2 comes in. Avgs list is converted to data frame
-dataFrame$scale <- fileData$scale          # Add node scaling to the data frame.
+dataFrame$scale <- fileData$scale      # Add node scaling to the data frame.
 dataFrame$std <- fileData$std
 
-colnames( dataFrame ) <- c( "throughput", "type", "scale", "std" )
+colnames( dataFrame ) <- c( "throughput",
+                            "type",
+                            "scale",
+                            "std" )
 
 dataFrame <- na.omit( dataFrame )   # Omit any data that doesn't exist
 
@@ -114,19 +183,15 @@
 # STEP 3: Generate graphs.
 # **********************************************************
 
-print( "STEP 3: Generate graphs." )
+print( "**********************************************************" )
+print( "STEP 3: Generate Graph." )
+print( "**********************************************************" )
 
-# 1. Graph fundamental data is generated first.
-#    These are variables that apply to all of the graphs being generated, regardless of type.
-#
-# 2. Type specific graph data is generated.
-#     Data specific for the error bar and stacked bar graphs are generated.
-#
-# 3. Generate and save the graphs.
-#      Graphs are saved to the filename above, in the directory provided in command line args
+# ------------------
+# Generate Main Plot
+# ------------------
 
-print( "Generating fundamental graph data." )
-
+print( "Generating main plot." )
 # Create the primary plot here.
 # ggplot contains the following arguments:
 #     - data: the data frame that the graph will be based off of
@@ -135,33 +200,47 @@
 #        - y: y-axis values (usually time in milliseconds)
 #        - fill: the category of the colored side-by-side bars (usually type)
 
-theme_set( theme_grey( base_size = 22 ) )   # set the default text size of the graph.
+mainPlot <- ggplot( data = dataFrame, aes( x = scale,
+                                           y = throughput,
+                                           ymin = throughput,
+                                           ymax = throughput + std,
+                                           fill = type ) )
+# ------------------------------
+# Fundamental Variables Assigned
+# ------------------------------
 
-mainPlot <- ggplot( data = dataFrame, aes( x = scale, y = throughput, ymin = throughput, ymax = throughput + std, fill = type ) )
+print( "Generating fundamental graph data." )
 
 # Formatting the plot
+theme_set( theme_grey( base_size = 22 ) )   # set the default text size of the graph.
 width <- 0.7  # Width of the bars.
-xScaleConfig <- scale_x_continuous( breaks = dataFrame$scale, label = dataFrame$scale )
+xScaleConfig <- scale_x_continuous( breaks = dataFrame$scale,
+                                    label = dataFrame$scale )
 xLabel <- xlab( "Scale" )
 yLabel <- ylab( "Throughput (,000 Flows/sec)" )
 fillLabel <- labs( fill="Type" )
-chartTitle <- "Flow Throughput Test"
-if ( args[ 1 ] == 'y' ){
-    chartTitle <- paste( chartTitle, " with Flow Objectives", sep="" )
-}
-chartTitle <- paste( chartTitle, "\nNeighbors =", sep="" )
-if ( args[ 8 ] == 'y' ){
-    chartTitle <- paste( chartTitle, "Cluster Size - 1" )
-} else {
-    chartTitle <- paste( chartTitle, "0" )
-}
+imageWidth <- 15
+imageHeight <- 10
+imageDPI <- 200
+errorBarColor <- rgb( 140, 140, 140, maxColorValue=255 )
 
-theme <- theme( plot.title=element_text( hjust = 0.5, size = 32, face='bold' ) )
-
+theme <- theme( plot.title = element_text( hjust = 0.5,
+                                           size = 32,
+                                           face = 'bold' ) )
+title <- ggtitle( chartTitle )
 
 # Store plot configurations as 1 variable
-fundamentalGraphData <- mainPlot + xScaleConfig + xLabel + yLabel + fillLabel + theme
+fundamentalGraphData <- mainPlot +
+                        xScaleConfig +
+                        xLabel +
+                        yLabel +
+                        fillLabel +
+                        theme +
+                        title
 
+# ---------------------------
+# Generating Bar Graph Format
+# ---------------------------
 
 # Create the stacked bar graph with error bars.
 # geom_bar contains:
@@ -169,13 +248,37 @@
 #    - width: the width of the bar types (declared above)
 # geom_errorbar contains similar arguments as geom_bar.
 print( "Generating bar graph with error bars." )
-barGraphFormat <- geom_bar( stat = "identity", width = width, fill="#FFAA3C" )
-errorBarFormat <- geom_errorbar( width = width, position=position_dodge(), color=rgb( 140,140,140, maxColorValue=255 ) )
-values <- geom_text( aes( x=dataFrame$scale, y=dataFrame$throughput + 0.03 * max( dataFrame$throughput ), label = format( dataFrame$throughput, digits=3, big.mark = ",", scientific = FALSE ) ), size = 7.0, fontface = "bold" )
-title <- ggtitle( paste( chartTitle, "" ) )
-result <- fundamentalGraphData + barGraphFormat + errorBarFormat + title + values
+barGraphFormat <- geom_bar( stat = "identity",
+                            width = width,
+                            fill = "#FFAA3C" )
 
-# Save graph to file
+errorBarFormat <- geom_errorbar( width = width,
+                                 position = position_dodge(),
+                                 color = errorBarColor )
+
+values <- geom_text( aes( x = dataFrame$scale,
+                          y = dataFrame$throughput + 0.03 * max( dataFrame$throughput ),
+                          label = format( dataFrame$throughput,
+                                          digits=3,
+                                          big.mark = ",",
+                                          scientific = FALSE ) ),
+                          size = 7.0,
+                          fontface = "bold" )
+
+result <- fundamentalGraphData +
+          barGraphFormat +
+          errorBarFormat +
+          values
+
+# -----------------------
+# Exporting Graph to File
+# -----------------------
+
 print( paste( "Saving bar chart with error bars to", errBarOutputFile ) )
-ggsave( errBarOutputFile, width = 15, height = 10, dpi = 200 )
-print( paste( "Successfully wrote bar chart with error bars out to", errBarOutputFile ) )
+
+ggsave( errBarOutputFile,
+        width = imageWidth,
+        height = imageHeight,
+        dpi = imageDPI )
+
+print( paste( "[SUCCESS] Successfully wrote bar chart with error bars out to", errBarOutputFile ) )