Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 1 | # Copyright 2017 Open Networking Foundation (ONF) |
| 2 | # |
| 3 | # Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 4 | # the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 5 | # or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 6 | # |
| 7 | # TestON is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation, either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # TestON is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | # If you have any questions, or if you don't understand R, |
Jeremy Ronquillo | b626884 | 2017-10-03 13:02:58 -0700 | [diff] [blame] | 21 | # please contact Jeremy Ronquillo: j_ronquillo@u.pacific.edu |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 22 | |
| 23 | # This is the R script that generates the SCPF front page graphs. |
| 24 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 25 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 26 | # ********************************************************** |
| 27 | # STEP 1: Data management. |
| 28 | # ********************************************************** |
| 29 | |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 30 | database_host = 1 |
| 31 | database_port = 2 |
| 32 | database_u_id = 3 |
| 33 | database_pw = 4 |
| 34 | graph_title = 5 |
| 35 | branch_name = 6 |
| 36 | num_dates = 7 |
| 37 | sql_commands = 8 |
| 38 | y_axis = 9 |
| 39 | old_flow = 10 |
| 40 | save_directory = 11 |
| 41 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 42 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 43 | print( "STEP 1: Data management." ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 44 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 45 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 46 | # ---------------- |
| 47 | # Import Libraries |
| 48 | # ---------------- |
| 49 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 50 | print( "Importing libraries." ) |
| 51 | library( ggplot2 ) |
| 52 | library( reshape2 ) |
| 53 | library( RPostgreSQL ) |
| 54 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 55 | # ------------------- |
| 56 | # Check CLI Arguments |
| 57 | # ------------------- |
| 58 | |
| 59 | print( "Verifying CLI args." ) |
| 60 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 61 | # Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files. |
| 62 | print( "Reading commmand-line args." ) |
| 63 | args <- commandArgs( trailingOnly=TRUE ) |
| 64 | |
| 65 | # Check if sufficient args are provided. |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 66 | if ( is.na( args[ save_directory ] ) ){ |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 67 | |
| 68 | print( paste( "Usage: Rscript testresultgraph.R", |
| 69 | "<database-host>", |
| 70 | "<database-port>", |
| 71 | "<database-user-id>", |
| 72 | "<database-password>", |
| 73 | "<graph-title>", # part of the output filename as well |
| 74 | "<branch-name>", # part of the output filename |
| 75 | "<#-dates>", # part of the output filename |
| 76 | "<SQL-command>", |
| 77 | "<y-axis-title>", # y-axis may be different among other SCPF graphs (ie: batch size, latency, etc. ) |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 78 | "<using-old-flow>", |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 79 | "<directory-to-save-graph>", |
| 80 | sep = " " ) ) |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 81 | quit( status = 1 ) # basically exit(), but in R |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 84 | # ------------------------------- |
| 85 | # Create Title and Graph Filename |
| 86 | # ------------------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 87 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 88 | print( "Creating title of graph" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 89 | |
| 90 | # Title of graph based on command line args. |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 91 | |
| 92 | title <- args[ graph_title ] |
Devin Lim | 1bba762 | 2017-11-14 16:31:41 -0800 | [diff] [blame] | 93 | title <- paste( title, if( args[ old_flow ] == "y" ) "\nWith Eventually Consistent Flow Rule Store" else "" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 94 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 95 | print( "Creating graph filename." ) |
| 96 | |
| 97 | # Filenames for the output graph include the testname, branch, and the graph type. |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 98 | outputFile <- paste( args[ save_directory ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 99 | "SCPF_Front_Page_", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 100 | gsub( " ", "_", args[ graph_title ] ), |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 101 | "_", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 102 | args[ branch_name ], |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 103 | "_", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 104 | args[ num_dates ], |
| 105 | "-dates", |
| 106 | if( args[ old_flow ] == "y" ) "_OldFlow" else "", |
| 107 | "_graph.jpg", |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 108 | sep="" ) |
| 109 | |
| 110 | # ------------------ |
| 111 | # SQL Initialization |
| 112 | # ------------------ |
| 113 | |
| 114 | print( "Initializing SQL" ) |
| 115 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 116 | dbname = "onostest", |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 117 | host = args[ database_host ], |
| 118 | port = strtoi( args[ database_port ] ), |
| 119 | user = args[ database_u_id ], |
| 120 | password = args[ database_pw ] ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 121 | |
| 122 | print( "Sending SQL command:" ) |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 123 | print( args[ sql_commands ] ) |
Jeremy Ronquillo | 9d867f4 | 2017-11-28 14:31:18 -0800 | [diff] [blame] | 124 | |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 125 | fileData <- dbGetQuery( con, args[ sql_commands ] ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 126 | |
Jeremy Ronquillo | 9d867f4 | 2017-11-28 14:31:18 -0800 | [diff] [blame] | 127 | # Check if data has been received |
| 128 | if ( nrow( fileData ) == 0 ){ |
| 129 | print( "[ERROR]: No data received from the databases. Please double check this by manually running the SQL command." ) |
| 130 | quit( status = 1 ) |
| 131 | } |
| 132 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 133 | # ********************************************************** |
| 134 | # STEP 2: Organize data. |
| 135 | # ********************************************************** |
| 136 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 137 | print( "**********************************************************" ) |
| 138 | print( "STEP 2: Organize Data." ) |
| 139 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 140 | |
| 141 | # Create lists c() and organize data into their corresponding list. |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 142 | print( "Combine data retrieved from databases into a list." ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 143 | |
Jeremy Ronquillo | 20a7966 | 2017-12-20 10:58:59 -0800 | [diff] [blame^] | 144 | buildNums <- fileData$build |
| 145 | fileData$build <- c() |
| 146 | print( fileData ) |
| 147 | |
Jeremy Ronquillo | 9d867f4 | 2017-11-28 14:31:18 -0800 | [diff] [blame] | 148 | if ( ncol( fileData ) > 1 ){ |
| 149 | for ( i in 2:ncol( fileData ) ){ |
| 150 | fileData[ i ] <- fileData[ i - 1 ] + fileData[ i ] |
| 151 | } |
| 152 | } |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 153 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 154 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 155 | # -------------------- |
| 156 | # Construct Data Frame |
| 157 | # -------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 158 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 159 | print( "Constructing data frame from combined data." ) |
| 160 | |
| 161 | dataFrame <- melt( fileData ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 162 | dataFrame$date <- fileData$date |
| 163 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 164 | colnames( dataFrame ) <- c( "Legend", |
| 165 | "Values" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 166 | |
| 167 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 168 | dataFrame$Legend <- as.character( dataFrame$Legend ) |
| 169 | dataFrame$Legend <- factor( dataFrame$Legend, levels=unique( dataFrame$Legend ) ) |
Jeremy Ronquillo | 20a7966 | 2017-12-20 10:58:59 -0800 | [diff] [blame^] | 170 | dataFrame$build <- buildNums |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 171 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 172 | # Adding a temporary iterative list to the dataFrame so that there are no gaps in-between date numbers. |
| 173 | dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) ) |
| 174 | |
| 175 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 176 | |
| 177 | print( "Data Frame Results:" ) |
| 178 | print( dataFrame ) |
| 179 | |
| 180 | # ********************************************************** |
| 181 | # STEP 3: Generate graphs. |
| 182 | # ********************************************************** |
| 183 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 184 | print( "**********************************************************" ) |
| 185 | print( "STEP 3: Generate Graph." ) |
| 186 | print( "**********************************************************" ) |
| 187 | |
| 188 | # ------------------- |
| 189 | # Main Plot Generated |
| 190 | # ------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 191 | |
| 192 | print( "Creating main plot." ) |
| 193 | # Create the primary plot here. |
| 194 | # ggplot contains the following arguments: |
| 195 | # - data: the data frame that the graph will be based off of |
| 196 | # - aes: the asthetics of the graph which require: |
| 197 | # - x: x-axis values (usually iterative, but it will become date # later) |
| 198 | # - y: y-axis values (usually tests) |
| 199 | # - color: the category of the colored lines (usually legend of test) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 200 | |
| 201 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, |
| 202 | y = Values, |
| 203 | color = Legend ) ) |
| 204 | |
| 205 | # ------------------- |
| 206 | # Main Plot Formatted |
| 207 | # ------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 208 | |
| 209 | print( "Formatting main plot." ) |
| 210 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 211 | limitExpansion <- expand_limits( y = 0 ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 212 | |
Jeremy Ronquillo | 20a7966 | 2017-12-20 10:58:59 -0800 | [diff] [blame^] | 213 | tickLength <- 3 |
| 214 | breaks <- seq( max( dataFrame$iterative ) %% tickLength, max( dataFrame$iterative ), by = tickLength ) |
| 215 | breaks <- breaks[ which( breaks != 0 ) ] |
| 216 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 217 | maxYDisplay <- max( dataFrame$Values ) * 1.05 |
| 218 | yBreaks <- ceiling( max( dataFrame$Values ) / 10 ) |
| 219 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, maxYDisplay, by = yBreaks ) ) |
Jeremy Ronquillo | 20a7966 | 2017-12-20 10:58:59 -0800 | [diff] [blame^] | 220 | xScaleConfig <- scale_x_continuous( breaks = breaks, label = rev( dataFrame$build )[ breaks ] ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 221 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 222 | # ------------------------------ |
| 223 | # Fundamental Variables Assigned |
| 224 | # ------------------------------ |
| 225 | |
| 226 | print( "Generating fundamental graph data." ) |
| 227 | |
| 228 | theme_set( theme_grey( base_size = 22 ) ) # set the default text size of the graph. |
| 229 | xLabel <- xlab( "Build" ) |
Devin Lim | 0e96716 | 2017-11-03 15:59:53 -0700 | [diff] [blame] | 230 | yLabel <- ylab( args[ y_axis ] ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 231 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 232 | imageWidth <- 15 |
| 233 | imageHeight <- 10 |
| 234 | imageDPI <- 200 |
| 235 | |
| 236 | # Set other graph configurations here. |
Jeremy Ronquillo | 20a7966 | 2017-12-20 10:58:59 -0800 | [diff] [blame^] | 237 | theme <- theme( axis.text.x = element_text( angle = 0, size = 13 ), |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 238 | plot.title = element_text( size = 32, face='bold', hjust = 0.5 ), |
| 239 | legend.position = "bottom", |
| 240 | legend.text = element_text( size=22 ), |
| 241 | legend.title = element_blank(), |
| 242 | legend.key.size = unit( 1.5, 'lines' ), |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 243 | legend.direction = 'horizontal', |
| 244 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
| 245 | |
| 246 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
| 247 | |
| 248 | title <- labs( title = title, subtitle = subtitle ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 249 | |
| 250 | # Colors used for the lines. |
| 251 | # Note: graphs that have X lines will use the first X colors in this list. |
| 252 | colors <- scale_color_manual( values=c( "#111111", # black |
| 253 | "#008CFF", # blue |
| 254 | "#FF3700", # red |
| 255 | "#00E043", # green |
| 256 | "#EEB600", # yellow |
| 257 | "#E500FF") ) # purple (not used) |
| 258 | |
| 259 | wrapLegend <- guides( color = guide_legend( nrow = 2, byrow = TRUE ) ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 260 | |
| 261 | fundamentalGraphData <- mainPlot + |
| 262 | limitExpansion + |
Jeremy Ronquillo | 20a7966 | 2017-12-20 10:58:59 -0800 | [diff] [blame^] | 263 | xScaleConfig + |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 264 | yScaleConfig + |
| 265 | xLabel + |
| 266 | yLabel + |
| 267 | theme + |
| 268 | colors + |
| 269 | wrapLegend + |
| 270 | title |
| 271 | |
| 272 | # ---------------------------- |
| 273 | # Generating Line Graph Format |
| 274 | # ---------------------------- |
| 275 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 276 | print( "Generating line graph." ) |
| 277 | |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 278 | lineGraphFormat <- geom_line( size = 0.75 ) |
| 279 | pointFormat <- geom_point( size = 1.75 ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 280 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 281 | result <- fundamentalGraphData + |
| 282 | lineGraphFormat + |
| 283 | pointFormat |
Jeremy Ronquillo | 0e27b91 | 2017-10-21 14:34:24 -0700 | [diff] [blame] | 284 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 285 | # ----------------------- |
| 286 | # Exporting Graph to File |
| 287 | # ----------------------- |
| 288 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 289 | print( paste( "Saving result graph to", outputFile ) ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 290 | |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 291 | tryCatch( ggsave( outputFile, |
| 292 | width = imageWidth, |
| 293 | height = imageHeight, |
| 294 | dpi = imageDPI ), |
| 295 | error = function( e ){ |
| 296 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 297 | print( e ) |
| 298 | quit( status = 1 ) |
| 299 | } |
| 300 | ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 301 | |
| 302 | print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) ) |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 303 | quit( status = 0 ) |