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 | |
Jeremy Ronquillo | b626884 | 2017-10-03 13:02:58 -0700 | [diff] [blame] | 23 | # This is the R script that generates the FUNC, HA, and various USECASE result graphs. |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 24 | |
| 25 | # ********************************************************** |
| 26 | # STEP 1: Data management. |
| 27 | # ********************************************************** |
| 28 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 29 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 30 | print( "STEP 1: Data management." ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 31 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 32 | |
| 33 | # Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files. |
| 34 | print( "Reading commmand-line args." ) |
| 35 | args <- commandArgs( trailingOnly=TRUE ) |
| 36 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 37 | # ---------------- |
| 38 | # Import Libraries |
| 39 | # ---------------- |
| 40 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 41 | print( "Importing libraries." ) |
| 42 | library( ggplot2 ) |
| 43 | library( reshape2 ) |
| 44 | library( RPostgreSQL ) |
| 45 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 46 | # ------------------- |
| 47 | # Check CLI Arguments |
| 48 | # ------------------- |
| 49 | |
| 50 | print( "Verifying CLI args." ) |
| 51 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 52 | if ( is.na( args[ 8 ] ) ){ |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 53 | |
| 54 | print( paste( "Usage: Rscript testCaseGraphGenerator.R", |
| 55 | "<database-host>", |
| 56 | "<database-port>", |
| 57 | "<database-user-id>", |
| 58 | "<database-password>", |
| 59 | "<test-name>", # part of the output filename |
| 60 | "<branch-name>", # for sql and output filename |
| 61 | "<#-builds-to-show>", # for sql and output filename |
| 62 | "<directory-to-save-graphs>", |
| 63 | sep=" " ) ) |
| 64 | |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 65 | quit( status = 1 ) # basically exit(), but in R |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 68 | # ------------------------------- |
| 69 | # Create Title and Graph Filename |
| 70 | # ------------------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 71 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 72 | print( "Creating title of graph." ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 73 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 74 | title <- paste( args[ 5 ], |
| 75 | " - ", |
| 76 | args[ 6 ], |
| 77 | " \n Results of Last ", |
| 78 | args[ 7 ], |
| 79 | " Builds", |
| 80 | sep="" ) |
| 81 | |
| 82 | print( "Creating graph filename." ) |
| 83 | |
| 84 | outputFile <- paste( args[ 8 ], |
| 85 | args[ 5 ], |
| 86 | "_", |
| 87 | args[ 6 ], |
| 88 | "_", |
| 89 | args[ 7 ], |
| 90 | "-builds_graph.jpg", |
| 91 | sep="" ) |
| 92 | |
| 93 | # ------------------ |
| 94 | # SQL Initialization |
| 95 | # ------------------ |
| 96 | |
| 97 | print( "Initializing SQL" ) |
| 98 | |
| 99 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 100 | dbname = "onostest", |
| 101 | host = args[ 1 ], |
| 102 | port = strtoi( args[ 2 ] ), |
| 103 | user = args[ 3 ], |
| 104 | password = args[ 4 ] ) |
| 105 | |
| 106 | # --------------------- |
| 107 | # Test Case SQL Command |
| 108 | # --------------------- |
| 109 | print( "Generating Test Case SQL command." ) |
| 110 | |
| 111 | command <- paste( "SELECT * FROM executed_test_tests WHERE actual_test_name='", |
| 112 | args[ 5 ], |
| 113 | "' AND branch='", |
| 114 | args[ 6 ], |
| 115 | "' ORDER BY date DESC LIMIT ", |
| 116 | args[ 7 ], |
| 117 | sep="" ) |
| 118 | |
| 119 | print( "Sending SQL command:" ) |
| 120 | print( command ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 121 | fileData <- dbGetQuery( con, command ) |
| 122 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 123 | |
| 124 | # ********************************************************** |
| 125 | # STEP 2: Organize data. |
| 126 | # ********************************************************** |
| 127 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 128 | print( "**********************************************************" ) |
| 129 | print( "STEP 2: Organize Data." ) |
| 130 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 131 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 132 | # ------------------------------------------------------- |
| 133 | # Combining Passed, Failed, and Planned Data |
| 134 | # ------------------------------------------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 135 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 136 | print( "Combining Passed, Failed, and Planned Data." ) |
| 137 | |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 138 | requiredColumns <- c( "num_failed", "num_passed", "num_planned" ) |
| 139 | |
| 140 | tryCatch( categories <- c( fileData[ requiredColumns] ), |
| 141 | error = function( e ) { |
| 142 | print( "[ERROR] One or more expected columns are missing from the data. Please check that the data and SQL command are valid, then try again." ) |
| 143 | print( "Required columns: " ) |
| 144 | print( requiredColumns ) |
| 145 | print( "Actual columns: " ) |
| 146 | print( names( fileData ) ) |
| 147 | print( "Error dump:" ) |
| 148 | print( e ) |
| 149 | quit( status = 1 ) |
| 150 | } |
| 151 | ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 152 | |
| 153 | # -------------------- |
| 154 | # Construct Data Frame |
| 155 | # -------------------- |
| 156 | |
| 157 | print( "Constructing data frame from combined data." ) |
| 158 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 159 | dataFrame <- melt( categories ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 160 | |
| 161 | # Rename column names in dataFrame |
| 162 | colnames( dataFrame ) <- c( "Tests", |
| 163 | "Status" ) |
| 164 | |
| 165 | # Add build dates to the dataFrame |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 166 | dataFrame$build <- fileData$build |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 167 | |
| 168 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 169 | dataFrame$Status <- as.character( dataFrame$Status ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 170 | dataFrame$Status <- factor( dataFrame$Status, levels = unique( dataFrame$Status ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 171 | |
| 172 | # Add planned, passed, and failed results to the dataFrame (for the fill below the lines) |
| 173 | dataFrame$num_planned <- fileData$num_planned |
| 174 | dataFrame$num_passed <- fileData$num_passed |
| 175 | dataFrame$num_failed <- fileData$num_failed |
| 176 | |
| 177 | # Adding a temporary reversed iterative list to the dataFrame so that there are no gaps in-between build numbers. |
| 178 | dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) ) |
| 179 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 180 | # Omit any data that doesn't exist |
| 181 | dataFrame <- na.omit( dataFrame ) |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 182 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 183 | print( "Data Frame Results:" ) |
| 184 | print( dataFrame ) |
| 185 | |
| 186 | # ********************************************************** |
| 187 | # STEP 3: Generate graphs. |
| 188 | # ********************************************************** |
| 189 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 190 | print( "**********************************************************" ) |
| 191 | print( "STEP 3: Generate Graph." ) |
| 192 | print( "**********************************************************" ) |
| 193 | |
| 194 | # ------------------- |
| 195 | # Main Plot Generated |
| 196 | # ------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 197 | |
| 198 | print( "Creating main plot." ) |
| 199 | # Create the primary plot here. |
| 200 | # ggplot contains the following arguments: |
| 201 | # - data: the data frame that the graph will be based off of |
| 202 | # - aes: the asthetics of the graph which require: |
| 203 | # - x: x-axis values (usually iterative, but it will become build # later) |
| 204 | # - y: y-axis values (usually tests) |
| 205 | # - color: the category of the colored lines (usually status of test) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 206 | |
| 207 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, |
| 208 | y = Tests, |
| 209 | color = Status ) ) |
| 210 | |
| 211 | # ------------------- |
| 212 | # Main Plot Formatted |
| 213 | # ------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 214 | |
| 215 | print( "Formatting main plot." ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 216 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 217 | # geom_ribbon is used so that there is a colored fill below the lines. These values shouldn't be changed. |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 218 | failedColor <- geom_ribbon( aes( ymin = 0, |
| 219 | ymax = dataFrame$num_failed ), |
| 220 | fill = "red", |
| 221 | linetype = 0, |
| 222 | alpha = 0.07 ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 223 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 224 | passedColor <- geom_ribbon( aes( ymin = 0, |
| 225 | ymax = dataFrame$num_passed ), |
| 226 | fill = "green", |
| 227 | linetype = 0, |
| 228 | alpha = 0.05 ) |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 229 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 230 | plannedColor <- geom_ribbon( aes( ymin = 0, |
| 231 | ymax = dataFrame$num_planned ), |
| 232 | fill = "blue", |
| 233 | linetype = 0, |
| 234 | alpha = 0.01 ) |
| 235 | |
| 236 | # Colors for the lines |
| 237 | lineColors <- scale_color_manual( values=c( "#E80000", # red |
| 238 | "#00B208", # green |
| 239 | "#00A5FF") ) # blue |
| 240 | |
| 241 | # ------------------------------ |
| 242 | # Fundamental Variables Assigned |
| 243 | # ------------------------------ |
| 244 | |
| 245 | print( "Generating fundamental graph data." ) |
| 246 | |
| 247 | theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph. |
| 248 | |
| 249 | xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, |
Jeremy Ronquillo | 7fe4cdb | 2017-11-15 11:26:24 -0800 | [diff] [blame] | 250 | label = dataFrame$build ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 251 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$Tests ), |
| 252 | by = ceiling( max( dataFrame$Tests ) / 10 ) ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 253 | |
| 254 | xLabel <- xlab( "Build Number" ) |
| 255 | yLabel <- ylab( "Test Cases" ) |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 256 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 257 | imageWidth <- 15 |
| 258 | imageHeight <- 10 |
| 259 | imageDPI <- 200 |
| 260 | |
| 261 | legendLabels <- scale_colour_discrete( labels = c( "Failed Cases", |
| 262 | "Passed Cases", |
| 263 | "Planned Cases" ) ) |
| 264 | |
| 265 | # Set other graph configurations here. |
| 266 | theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face ='bold' ), |
| 267 | axis.text.x = element_text( angle = 0, size = 14 ), |
| 268 | legend.position = "bottom", |
| 269 | legend.text = element_text( size = 22 ), |
| 270 | legend.title = element_blank(), |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 271 | legend.key.size = unit( 1.5, 'lines' ), |
| 272 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 273 | |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 274 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
| 275 | |
| 276 | title <- labs( title = title, subtitle = subtitle ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 277 | |
| 278 | # Store plot configurations as 1 variable |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 279 | fundamentalGraphData <- mainPlot + |
| 280 | plannedColor + |
| 281 | passedColor + |
| 282 | failedColor + |
| 283 | xScaleConfig + |
| 284 | yScaleConfig + |
| 285 | xLabel + |
| 286 | yLabel + |
| 287 | lineColors + |
| 288 | legendLabels + |
| 289 | theme + |
| 290 | title |
| 291 | |
| 292 | # ---------------------------- |
| 293 | # Generating Line Graph Format |
| 294 | # ---------------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 295 | |
| 296 | print( "Generating line graph." ) |
| 297 | |
| 298 | lineGraphFormat <- geom_line( size = 1.1 ) |
| 299 | pointFormat <- geom_point( size = 3 ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 300 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 301 | result <- fundamentalGraphData + |
| 302 | lineGraphFormat + |
| 303 | pointFormat |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 304 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 305 | # ----------------------- |
| 306 | # Exporting Graph to File |
| 307 | # ----------------------- |
| 308 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 309 | print( paste( "Saving result graph to", outputFile ) ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 310 | |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 311 | tryCatch( ggsave( outputFile, |
| 312 | width = imageWidth, |
| 313 | height = imageHeight, |
| 314 | dpi = imageDPI ), |
| 315 | error = function( e ){ |
| 316 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 317 | print( e ) |
| 318 | quit( status = 1 ) |
| 319 | } |
| 320 | ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 321 | |
| 322 | print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) ) |
Jeremy Ronquillo | 9ea85d0 | 2017-11-27 10:21:03 -0800 | [diff] [blame] | 323 | quit( status = 0 ) |