[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/SCPFswitchLat.R b/TestON/JenkinsFile/scripts/SCPFswitchLat.R
index 7bf0a44..97b8d44 100644
--- a/TestON/JenkinsFile/scripts/SCPFswitchLat.R
+++ b/TestON/JenkinsFile/scripts/SCPFswitchLat.R
@@ -21,48 +21,92 @@
 # 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
 
-# Check if sufficient args are provided.
+# -------------------
+# Check CLI Arguments
+# -------------------
+
+print( "Verifying CLI args." )
+
 if ( is.na( args[ 7 ] ) ){
-    print( "Usage: Rscript SCPFswitchLat <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <directory-to-save-graphs>" )
+
+    print( paste( "Usage: Rscript SCPFswitchLat",
+                            "<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.
-errBarOutputFileUp <- paste( args[ 7 ], "SCPFswitchLat_", sep = "" )
-errBarOutputFileUp <- paste( errBarOutputFileUp, args[ 6 ], sep = "" )
-errBarOutputFileUp <- paste( errBarOutputFileUp, "_UpErrBarWithStack.jpg", sep = "" )
+# -----------------
+# Create File Names
+# -----------------
 
-errBarOutputFileDown <- paste( args[ 7 ], "SCPFswitchLat_", sep = "" )
-errBarOutputFileDown <- paste( errBarOutputFileDown, args[ 6 ], sep = "" )
-errBarOutputFileDown <- paste( errBarOutputFileDown, "_DownErrBarWithStack.jpg", sep = "" )
+print( "Creating filenames and title of graph." )
 
-print( "Reading from databases." )
+errBarOutputFileUp <- paste( args[ 7 ],
+                             "SCPFswitchLat_",
+                             args[ 6 ],
+                             "_UpErrBarWithStack.jpg",
+                             sep="" )
 
-con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] )
+errBarOutputFileDown <- paste( args[ 7 ],
+                               "SCPFswitchLat_",
+                               args[ 6 ],
+                               "_DownErrBarWithStack.jpg",
+                               sep="" )
+# ------------------
+# SQL Initialization
+# ------------------
 
-command <- paste( "SELECT * FROM switch_latency_details WHERE branch = '", args[ 6 ], sep="" )
-command <- paste( command, "' AND date IN ( SELECT MAX( date ) FROM switch_latency_details WHERE branch='", sep = "")
-command <- paste( command, args[ 6 ], sep="" )
-command <- paste( command, "' )", sep="" )
+print( "Initializing SQL" )
 
-print( paste( "Sending SQL command:", command ) )
+con <- dbConnect( dbDriver( "PostgreSQL" ),
+                  dbname = "onostest",
+                  host = args[ 1 ],
+                  port = strtoi( args[ 2 ] ),
+                  user = args[ 3 ],
+                  password = args[ 4 ] )
+
+# --------------------------
+# Switch Latency SQL Command
+# --------------------------
+
+print( "Generating Switch Latency SQL Command" )
+
+command <- paste( "SELECT * FROM switch_latency_details WHERE branch = '",
+                  args[ 6 ],
+                  "' AND date IN ( SELECT MAX( date ) FROM switch_latency_details WHERE branch='",
+                  args[ 6 ],
+                  "' )",
+                  sep="" )
+
+print( "Sending SQL command:" )
+print( command )
 
 fileData <- dbGetQuery( con, command )
 
@@ -70,31 +114,83 @@
 # STEP 2: Organize data.
 # **********************************************************
 
-print( "Sorting data." )
+print( "**********************************************************" )
+print( "STEP 2: Organize Data." )
+print( "**********************************************************" )
 
-upAvgs <- c( fileData[ 'up_device_to_graph_avg' ], fileData[ 'role_reply_to_device_avg' ], fileData[ 'role_request_to_role_reply_avg' ], fileData[ 'feature_reply_to_role_request_avg' ], fileData[ 'tcp_to_feature_reply_avg' ] )
+# -------------------------------
+# Switch Up Averages Data Sorting
+# -------------------------------
+
+print( "Sorting data for Switch Up Averages." )
+
+upAvgs <- c( fileData[ 'up_device_to_graph_avg' ],
+             fileData[ 'role_reply_to_device_avg' ],
+             fileData[ 'role_request_to_role_reply_avg' ],
+             fileData[ 'feature_reply_to_role_request_avg' ],
+             fileData[ 'tcp_to_feature_reply_avg' ] )
+
+# ------------------------------
+# Switch Up Construct Data Frame
+# ------------------------------
+
+print( "Constructing Switch Up data frame." )
+
 upAvgsData <- melt( upAvgs )
 upAvgsData$scale <- fileData$scale
 upAvgsData$up_std <- fileData$up_std
+upAvgsData <- na.omit( upAvgsData )
 
-colnames( upAvgsData ) <- c( "ms", "type", "scale", "stds" )
+colnames( upAvgsData ) <- c( "ms",
+                             "type",
+                             "scale",
+                             "stds" )
+
 upAvgsData$type <- as.character( upAvgsData$type )
 upAvgsData$type <- factor( upAvgsData$type, levels=unique( upAvgsData$type ) )
 
-downAvgs <- c( fileData[ 'down_device_to_graph_avg' ], fileData[ 'ack_to_device_avg' ], fileData[ 'fin_ack_to_ack_avg' ] )
+sumOfUpAvgs <- fileData[ 'up_device_to_graph_avg' ] +
+               fileData[ 'role_reply_to_device_avg' ] +
+               fileData[ 'role_request_to_role_reply_avg' ] +
+               fileData[ 'feature_reply_to_role_request_avg' ] +
+               fileData[ 'tcp_to_feature_reply_avg' ]
+
+print( "Up Averages Results:" )
+print( upAvgsData )
+
+# ---------------------------------
+# Switch Down Averages Data Sorting
+# ---------------------------------
+
+print( "Sorting data for Switch Down Averages." )
+
+downAvgs <- c( fileData[ 'down_device_to_graph_avg' ],
+               fileData[ 'ack_to_device_avg' ],
+               fileData[ 'fin_ack_to_ack_avg' ] )
+
+# --------------------------------
+# Switch Down Construct Data Frame
+# --------------------------------
+
+print( "Constructing Switch Down data frame." )
+
 downAvgsData <- melt( downAvgs )
 downAvgsData$scale <- fileData$scale
 downAvgsData$down_std <- fileData$down_std
 
-colnames( downAvgsData ) <- c( "ms", "type", "scale", "stds" )
+colnames( downAvgsData ) <- c( "ms",
+                               "type",
+                               "scale",
+                               "stds" )
+
 downAvgsData$type <- as.character( downAvgsData$type )
 downAvgsData$type <- factor( downAvgsData$type, levels=unique( downAvgsData$type ) )
 
-upAvgsData <- na.omit( upAvgsData )   # Omit any data that doesn't exist
 downAvgsData <- na.omit( downAvgsData )
 
-print( "Up Averages Results:" )
-print( upAvgsData )
+sumOfDownAvgs <- fileData[ 'down_device_to_graph_avg' ] +
+                 fileData[ 'ack_to_device_avg' ] +
+                 fileData[ 'fin_ack_to_ack_avg' ]
 
 print( "Down Averages Results:" )
 print( downAvgsData )
@@ -103,58 +199,164 @@
 # STEP 3: Generate graphs.
 # **********************************************************
 
+print( "**********************************************************" )
+print( "STEP 3: Generate Graph." )
+print( "**********************************************************" )
 
-print( "Generating fundamental graph data (Switch Up Latency)." )
-width <- 1
+# ------------------------------------
+# Initialize Variables For Both Graphs
+# ------------------------------------
 
-theme_set( theme_grey( base_size = 22 ) )   # set the default text size of the graph.
+print( "Initializing variables used in both graphs." )
 
-mainPlot <- ggplot( data = upAvgsData, aes( x = scale, y = ms, fill = type, ymin = fileData[ 'up_end_to_end_avg' ], ymax = fileData[ 'up_end_to_end_avg' ] + stds ) )
-xScaleConfig <- scale_x_continuous( breaks=c( 1, 3, 5, 7, 9) )
+theme_set( theme_grey( base_size = 22 ) )   # set the default text size of the graphs
+xScaleConfig <- scale_x_continuous( breaks = c( 1, 3, 5, 7, 9 ) )
 xLabel <- xlab( "Scale" )
 yLabel <- ylab( "Latency (ms)" )
-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' ) )
+imageWidth <- 15
+imageHeight <- 10
+imageDPI <- 200
+errorBarColor <- rgb( 140, 140, 140, maxColorValue = 255 )
+barWidth <- 1
 
-fundamentalGraphData <- mainPlot + xScaleConfig + xLabel + yLabel + fillLabel + theme
+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' ) )
+
+# ----------------------------
+# Switch Up Generate Main Plot
+# ----------------------------
+
+print( "Creating main plot (Switch Up Latency)." )
+
+mainPlot <- ggplot( data = upAvgsData, aes( x = scale,
+                                            y = ms,
+                                            fill = type,
+                                            ymin = fileData[ 'up_end_to_end_avg' ],
+                                            ymax = fileData[ 'up_end_to_end_avg' ] + stds ) )
+
+# ----------------------------------------
+# Switch Up Fundamental Variables Assigned
+# ----------------------------------------
+
+print( "Generating fundamental graph data (Switch Up Latency)." )
+
+title <- ggtitle( "Switch Up Latency" )
+
+fundamentalGraphData <- mainPlot +
+                        xScaleConfig +
+                        xLabel +
+                        yLabel +
+                        theme +
+                        title
+
+# -------------------------------------
+# Switch Up Generating Bar Graph Format
+# -------------------------------------
 
 print( "Generating bar graph with error bars (Switch Up Latency)." )
-barGraphFormat <- geom_bar( stat="identity", width = width )
-errorBarFormat <- geom_errorbar( width = width, color=rgb( 140, 140, 140, maxColorValue=255 ) )
-sum <- fileData[ 'up_device_to_graph_avg' ] + fileData[ 'role_reply_to_device_avg' ] + fileData[ 'role_request_to_role_reply_avg' ] + fileData[ 'feature_reply_to_role_request_avg' ] + fileData[ 'tcp_to_feature_reply_avg' ]
-values <- geom_text( aes( x=upAvgsData$scale, y=sum + 0.04 * max( sum ), label = format( sum, digits=3, big.mark = ",", scientific = FALSE ) ), size = 7.0, fontface = "bold" )
-title <- ggtitle( "Switch Up Latency" )
-wrapLegend <- guides( fill=guide_legend( nrow=2, byrow=TRUE ) )
-result <- fundamentalGraphData + barGraphFormat + errorBarFormat + title + values + wrapLegend
 
+barGraphFormat <- geom_bar( stat = "identity", width = barWidth )
+errorBarFormat <- geom_errorbar( width = barWidth, color = errorBarColor )
+
+barGraphValues <- geom_text( aes( x = upAvgsData$scale,
+                                  y = sumOfUpAvgs + 0.04 * max( sumOfUpAvgs ),
+                                  label = format( sumOfUpAvgs,
+                                                  digits = 3,
+                                                  big.mark = ",",
+                                                  scientific = FALSE ) ),
+                                  size = 7.0,
+                                  fontface = "bold" )
+
+wrapLegend <- guides( fill = guide_legend( nrow = 2, byrow = TRUE ) )
+
+result <- fundamentalGraphData +
+          barGraphFormat +
+          errorBarFormat +
+          barGraphValues +
+          wrapLegend
+
+# ---------------------------------
+# Switch Up Exporting Graph to File
+# ---------------------------------
 
 print( paste( "Saving bar chart with error bars (Switch Up Latency) to", errBarOutputFileUp ) )
-ggsave( errBarOutputFileUp, width = 15, height = 10, dpi = 200 )
 
-print( paste( "Successfully wrote bar chart with error bars (Switch Up Latency) out to", errBarOutputFileUp ) )
+ggsave( errBarOutputFileUp,
+        width = imageWidth,
+        height = imageHeight,
+        dpi = imageDPI )
 
-# Generate switch down latency graph
+print( paste( "[SUCCESS] Successfully wrote bar chart with error bars (Switch Up Latency) out to", errBarOutputFileUp ) )
+
+# ------------------------------
+# Switch Down Generate Main Plot
+# ------------------------------
+
+print( "Creating main plot (Switch Down Latency)." )
+
+mainPlot <- ggplot( data = downAvgsData, aes( x = scale,
+                                              y = ms,
+                                              fill = type,
+                                              ymin = fileData[ 'down_end_to_end_avg' ],
+                                              ymax = fileData[ 'down_end_to_end_avg' ] + stds ) )
+
+# ------------------------------------------
+# Switch Down Fundamental Variables Assigned
+# ------------------------------------------
 
 print( "Generating fundamental graph data (Switch Down Latency)." )
 
-mainPlot <- ggplot( data = downAvgsData, aes( x = scale, y = ms, fill = type, ymin = fileData[ 'down_end_to_end_avg' ], ymax = fileData[ 'down_end_to_end_avg' ] + stds ) )
-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", "#18BA48" ) )
-
-fundamentalGraphData <- mainPlot + xScaleConfig + xLabel + yLabel + fillLabel + theme
-
-print( "Generating bar graph with error bars (Switch Down Latency)." )
-barGraphFormat <- geom_bar( stat="identity", width = width )
-errorBarFormat <- geom_errorbar( width = width, color=rgb( 140, 140, 140, maxColorValue=255 ) )
+colors <- scale_fill_manual( values=c( "#F77670",       # Red
+                                       "#619DFA",       # Blue
+                                       "#18BA48" ) )    # Green
 
 title <- ggtitle( "Switch Down Latency" )
-sum <- fileData[ 'down_device_to_graph_avg' ] + fileData[ 'ack_to_device_avg' ] + fileData[ 'fin_ack_to_ack_avg' ]
-values <- geom_text( aes( x=downAvgsData$scale, y=sum + 0.04 * max( sum ), label = format( sum, digits=3, big.mark = ",", scientific = FALSE ) ), size = 7.0, fontface = "bold" )
-wrapLegend <- guides( fill=guide_legend( nrow=1, byrow=TRUE ) )
-result <- fundamentalGraphData + barGraphFormat + colors + errorBarFormat + title + values + wrapLegend
+
+fundamentalGraphData <- mainPlot +
+                        xScaleConfig +
+                        xLabel +
+                        yLabel +
+                        theme +
+                        title
+
+# ---------------------------------------
+# Switch Down Generating Bar Graph Format
+# ---------------------------------------
+
+print( "Generating bar graph with error bars (Switch Down Latency)." )
+barGraphFormat <- geom_bar( stat = "identity", width = barWidth )
+errorBarFormat <- geom_errorbar( width = barWidth, color = errorBarColor )
+
+barGraphValues <- geom_text( aes( x = downAvgsData$scale,
+                                  y = sumOfDownAvgs + 0.04 * max( sumOfDownAvgs ),
+                                  label = format( sumOfDownAvgs,
+                                                  digits = 3,
+                                                  big.mark = ",",
+                                                  scientific = FALSE ) ),
+                                  size = 7.0,
+                                  fontface = "bold" )
+
+wrapLegend <- guides( fill = guide_legend( nrow = 1, byrow = TRUE ) )
+
+result <- fundamentalGraphData +
+          barGraphFormat +
+          colors +
+          errorBarFormat +
+          barGraphValues +
+          wrapLegend
+
+# -----------------------------------
+# Switch Down Exporting Graph to File
+# -----------------------------------
 
 print( paste( "Saving bar chart with error bars (Switch Down Latency) to", errBarOutputFileDown ) )
-ggsave( errBarOutputFileDown, width = 15, height = 10, dpi = 200 )
 
+ggsave( errBarOutputFileDown,
+        width = imageWidth,
+        height = imageHeight,
+        dpi = imageDPI )
 
-print( paste( "Successfully wrote bar chart with error bars (Switch Down Latency) out to", errBarOutputFileDown ) )
+print( paste( "[SUCCESS] Successfully wrote bar chart with error bars (Switch Down Latency) out to", errBarOutputFileDown ) )