Jeremy Ronquillo | 908cb44 | 2017-12-07 08:58:09 -0800 | [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, |
| 21 | # please contact Jeremy Ronquillo: j_ronquillo@u.pacific.edu |
| 22 | |
| 23 | pipelineMinValue = 1000 |
| 24 | |
| 25 | # ********************************************************** |
| 26 | # STEP 1: Data management. |
| 27 | # ********************************************************** |
| 28 | |
| 29 | print( "**********************************************************" ) |
| 30 | print( "STEP 1: Data management." ) |
| 31 | print( "**********************************************************" ) |
| 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 | |
| 37 | databaseHost <- 1 |
| 38 | databasePort <- 2 |
| 39 | databaseUserID <- 3 |
| 40 | databasePassword <- 4 |
| 41 | testSuiteName <- 5 |
| 42 | branchName <- 6 |
| 43 | testsToInclude <- 7 |
| 44 | buildsToShow <- 8 |
| 45 | saveDirectory <- 9 |
| 46 | |
| 47 | # ---------------- |
| 48 | # Import Libraries |
| 49 | # ---------------- |
| 50 | |
| 51 | print( "Importing libraries." ) |
| 52 | library( ggplot2 ) |
| 53 | library( reshape2 ) |
| 54 | library( RPostgreSQL ) |
| 55 | |
| 56 | # ------------------- |
| 57 | # Check CLI Arguments |
| 58 | # ------------------- |
| 59 | |
| 60 | print( "Verifying CLI args." ) |
| 61 | |
| 62 | if ( is.na( args[ saveDirectory ] ) ){ |
| 63 | |
| 64 | print( paste( "Usage: Rscript testCategoryTrend.R", |
| 65 | "<database-host>", |
| 66 | "<database-port>", |
| 67 | "<database-user-id>", |
| 68 | "<database-password>", |
| 69 | "<test-suite-name>", |
| 70 | "<branch-name>", |
| 71 | "<tests-to-include-(as-one-string)>", |
| 72 | "<builds-to-show>", |
| 73 | "<directory-to-save-graphs>", |
| 74 | sep=" " ) ) |
| 75 | |
| 76 | quit( status = 1 ) # basically exit(), but in R |
| 77 | } |
| 78 | |
| 79 | # ------------------------------- |
| 80 | # Create Title and Graph Filename |
| 81 | # ------------------------------- |
| 82 | |
| 83 | print( "Creating title of graph." ) |
| 84 | |
| 85 | title <- paste( args[ testSuiteName ], |
| 86 | " Test Results Trend - ", |
| 87 | args[ branchName ], |
| 88 | " \n Results of Last ", |
| 89 | args[ buildsToShow ], |
| 90 | " Nightly Builds", |
| 91 | sep="" ) |
| 92 | |
| 93 | print( "Creating graph filename." ) |
| 94 | |
| 95 | outputFile <- paste( args[ saveDirectory ], |
| 96 | args[ testSuiteName ], |
| 97 | "_", |
| 98 | args[ branchName ], |
| 99 | "_overview.jpg", |
| 100 | sep="" ) |
| 101 | |
| 102 | # ------------------ |
| 103 | # SQL Initialization |
| 104 | # ------------------ |
| 105 | |
| 106 | print( "Initializing SQL" ) |
| 107 | |
| 108 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 109 | dbname = "onostest", |
| 110 | host = args[ databaseHost ], |
| 111 | port = strtoi( args[ databasePort ] ), |
| 112 | user = args[ databaseUserID ], |
| 113 | password = args[ databasePassword ] ) |
| 114 | |
| 115 | # --------------------- |
| 116 | # Test Case SQL Command |
| 117 | # --------------------- |
| 118 | print( "Generating Test Case SQL command." ) |
| 119 | |
| 120 | tests <- "'" |
| 121 | for ( test in as.list( strsplit( args[ testsToInclude ], "," )[[1]] ) ){ |
| 122 | tests <- paste( tests, test, "','", sep="" ) |
| 123 | } |
| 124 | tests <- substr( tests, 0, nchar( tests ) - 2 ) |
| 125 | |
| 126 | command <- paste( "SELECT * ", |
| 127 | "FROM executed_test_tests a ", |
| 128 | "WHERE ( SELECT COUNT( * ) FROM executed_test_tests b ", |
| 129 | "WHERE b.branch='", |
| 130 | args[ branchName ], |
| 131 | "' AND b.actual_test_name IN (", |
| 132 | tests, |
| 133 | ") AND a.actual_test_name = b.actual_test_name AND a.date <= b.date AND b.build >= ", |
| 134 | pipelineMinValue, |
| 135 | " ) <= ", |
| 136 | args[ buildsToShow ], |
| 137 | " AND a.branch='", |
| 138 | args[ branchName ], |
| 139 | "' AND a.actual_test_name IN (", |
| 140 | tests, |
| 141 | ") AND a.build >= ", |
| 142 | pipelineMinValue, |
| 143 | " ORDER BY a.actual_test_name DESC, a.date DESC", |
| 144 | sep="") |
| 145 | |
| 146 | print( "Sending SQL command:" ) |
| 147 | print( command ) |
| 148 | dbResult <- dbGetQuery( con, command ) |
| 149 | maxBuild <- max( dbResult[ 'build' ] ) - strtoi( args[ buildsToShow ] ) |
| 150 | dbResult <- dbResult[ which( dbResult[,4]>maxBuild ), ] |
| 151 | print( dbResult ) |
| 152 | |
| 153 | # ********************************************************** |
| 154 | # STEP 2: Organize data. |
| 155 | # ********************************************************** |
| 156 | |
| 157 | print( "**********************************************************" ) |
| 158 | print( "STEP 2: Organize Data." ) |
| 159 | print( "**********************************************************" ) |
| 160 | |
| 161 | t <- subset( dbResult, select=c( "actual_test_name", "build", "num_failed" ) ) |
| 162 | t$num_failed <- ceiling( t$num_failed / ( t$num_failed + 1 ) ) |
| 163 | t$num_planned <- 1 |
| 164 | |
| 165 | fileData <- aggregate( t$num_failed, by=list( Category=t$build ), FUN=sum ) |
| 166 | colnames( fileData ) <- c( "build", "num_failed" ) |
| 167 | |
| 168 | fileData$num_planned <- ( aggregate( t$num_planned, by=list( Category=t$build ), FUN=sum ) )$x |
| 169 | fileData$num_passed <- fileData$num_planned - fileData$num_failed |
| 170 | |
| 171 | print(fileData) |
| 172 | |
| 173 | # -------------------- |
| 174 | # Construct Data Frame |
| 175 | # -------------------- |
| 176 | # |
| 177 | |
| 178 | dataFrame <- melt( subset( fileData, select=c( "num_failed", "num_passed", "num_planned" ) ) ) |
| 179 | dataFrame$build <- fileData$build |
| 180 | colnames( dataFrame ) <- c( "status", "results", "build" ) |
| 181 | |
| 182 | dataFrame$num_failed <- fileData$num_failed |
| 183 | dataFrame$num_passed <- fileData$num_passed |
| 184 | dataFrame$num_planned <- fileData$num_planned |
| 185 | dataFrame$iterative <- seq( 1, nrow( fileData ), by = 1 ) |
| 186 | |
| 187 | print( "Data Frame Results:" ) |
| 188 | print( dataFrame ) |
| 189 | |
| 190 | # ********************************************************** |
| 191 | # STEP 3: Generate graphs. |
| 192 | # ********************************************************** |
| 193 | |
| 194 | print( "**********************************************************" ) |
| 195 | print( "STEP 3: Generate Graph." ) |
| 196 | print( "**********************************************************" ) |
| 197 | |
| 198 | # ------------------- |
| 199 | # Main Plot Generated |
| 200 | # ------------------- |
| 201 | |
| 202 | print( "Creating main plot." ) |
| 203 | # Create the primary plot here. |
| 204 | # ggplot contains the following arguments: |
| 205 | # - data: the data frame that the graph will be based off of |
| 206 | # - aes: the asthetics of the graph which require: |
| 207 | # - x: x-axis values (usually iterative, but it will become build # later) |
| 208 | # - y: y-axis values (usually tests) |
| 209 | # - color: the category of the colored lines (usually status of test) |
| 210 | |
| 211 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, |
| 212 | y = results, |
| 213 | color = status ) ) |
| 214 | |
| 215 | # ------------------- |
| 216 | # Main Plot Formatted |
| 217 | # ------------------- |
| 218 | |
| 219 | print( "Formatting main plot." ) |
| 220 | |
| 221 | # geom_ribbon is used so that there is a colored fill below the lines. These values shouldn't be changed. |
| 222 | failedColor <- geom_ribbon( aes( ymin = 0, |
| 223 | ymax = dataFrame$num_failed ), |
| 224 | fill = "#ff0000", |
| 225 | linetype = 0, |
| 226 | alpha = 0.07 ) |
| 227 | |
| 228 | passedColor <- geom_ribbon( aes( ymin = 0, |
| 229 | ymax = dataFrame$num_passed ), |
| 230 | fill = "#0083ff", |
| 231 | linetype = 0, |
| 232 | alpha = 0.05 ) |
| 233 | |
| 234 | plannedColor <- geom_ribbon( aes( ymin = 0, |
| 235 | ymax = dataFrame$num_planned ), |
| 236 | fill = "#000000", |
| 237 | linetype = 0, |
| 238 | alpha = 0.01 ) |
| 239 | |
| 240 | # Colors for the lines |
| 241 | lineColors <- scale_color_manual( values=c( "#ff0000", # fail |
| 242 | "#0083ff", # pass |
| 243 | "#000000"), |
| 244 | labels = c( "Containing Failures", |
| 245 | "No Failures", |
| 246 | "Total Built" ) ) # planned |
| 247 | |
| 248 | # ------------------------------ |
| 249 | # Fundamental Variables Assigned |
| 250 | # ------------------------------ |
| 251 | |
| 252 | print( "Generating fundamental graph data." ) |
| 253 | |
| 254 | theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph. |
| 255 | |
| 256 | xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, |
| 257 | label = dataFrame$build ) |
| 258 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$results ), |
| 259 | by = ceiling( max( dataFrame$results ) / 10 ) ) ) |
| 260 | |
| 261 | xLabel <- xlab( "Build Number" ) |
| 262 | yLabel <- ylab( "Tests" ) |
| 263 | |
| 264 | imageWidth <- 15 |
| 265 | imageHeight <- 10 |
| 266 | imageDPI <- 200 |
| 267 | |
| 268 | # Set other graph configurations here. |
| 269 | theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face ='bold' ), |
| 270 | axis.text.x = element_text( angle = 0, size = 14 ), |
| 271 | legend.position = "bottom", |
| 272 | legend.text = element_text( size = 22 ), |
| 273 | legend.title = element_blank(), |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 274 | legend.key.size = unit( 1.5, 'lines' ), |
| 275 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
Jeremy Ronquillo | 908cb44 | 2017-12-07 08:58:09 -0800 | [diff] [blame] | 276 | |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 277 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
| 278 | |
| 279 | title <- labs( title = title, subtitle = subtitle ) |
Jeremy Ronquillo | 908cb44 | 2017-12-07 08:58:09 -0800 | [diff] [blame] | 280 | |
| 281 | # Store plot configurations as 1 variable |
| 282 | fundamentalGraphData <- mainPlot + |
| 283 | plannedColor + |
| 284 | passedColor + |
| 285 | failedColor + |
| 286 | xScaleConfig + |
| 287 | yScaleConfig + |
| 288 | xLabel + |
| 289 | yLabel + |
| 290 | theme + |
| 291 | title + |
| 292 | lineColors |
| 293 | |
| 294 | # ---------------------------- |
| 295 | # Generating Line Graph Format |
| 296 | # ---------------------------- |
| 297 | |
| 298 | print( "Generating line graph." ) |
| 299 | |
| 300 | lineGraphFormat <- geom_line( size = 1.1 ) |
| 301 | pointFormat <- geom_point( size = 3 ) |
| 302 | |
| 303 | result <- fundamentalGraphData + |
| 304 | lineGraphFormat + |
| 305 | pointFormat |
| 306 | |
| 307 | # ----------------------- |
| 308 | # Exporting Graph to File |
| 309 | # ----------------------- |
| 310 | |
| 311 | print( paste( "Saving result graph to", outputFile ) ) |
| 312 | |
| 313 | tryCatch( ggsave( outputFile, |
| 314 | width = imageWidth, |
| 315 | height = imageHeight, |
| 316 | dpi = imageDPI ), |
| 317 | error = function( e ){ |
| 318 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 319 | print( e ) |
| 320 | quit( status = 1 ) |
| 321 | } |
| 322 | ) |
| 323 | |
| 324 | print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) ) |
| 325 | quit( status = 0 ) |