Jeremy Ronquillo | 478a8c1 | 2018-01-08 13:59:47 -0800 | [diff] [blame] | 1 | # Copyright 2018 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 | # ********************************************************** |
| 24 | # STEP 1: Data management. |
| 25 | # ********************************************************** |
| 26 | |
| 27 | print( "**********************************************************" ) |
| 28 | print( "STEP 1: Data management." ) |
| 29 | print( "**********************************************************" ) |
| 30 | |
| 31 | # Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files. |
| 32 | print( "Reading commmand-line args." ) |
| 33 | args <- commandArgs( trailingOnly=TRUE ) |
| 34 | |
| 35 | databaseHost <- 1 |
| 36 | databasePort <- 2 |
| 37 | databaseUserID <- 3 |
| 38 | databasePassword <- 4 |
| 39 | testSuiteName <- 5 |
| 40 | branchName <- 6 |
| 41 | testsToInclude <- 7 |
| 42 | buildToShow <- 8 |
| 43 | isDisplayingPlan <- 9 |
| 44 | saveDirectory <- 10 |
| 45 | |
| 46 | # ---------------- |
| 47 | # Import Libraries |
| 48 | # ---------------- |
| 49 | |
| 50 | print( "Importing libraries." ) |
| 51 | library( ggplot2 ) |
| 52 | library( reshape2 ) |
| 53 | library( RPostgreSQL ) |
| 54 | |
| 55 | # ------------------- |
| 56 | # Check CLI Arguments |
| 57 | # ------------------- |
| 58 | |
| 59 | print( "Verifying CLI args." ) |
| 60 | |
| 61 | if ( is.na( args[ saveDirectory ] ) ){ |
| 62 | |
| 63 | print( paste( "Usage: Rscript testCategoryPiePassFail.R", |
| 64 | "<database-host>", |
| 65 | "<database-port>", |
| 66 | "<database-user-id>", |
| 67 | "<database-password>", |
| 68 | "<test-suite-name>", |
| 69 | "<branch-name>", |
| 70 | "<tests-to-include-(as-one-string)>", |
| 71 | "<build-to-show>", |
| 72 | "<is-displaying-plan>", |
| 73 | "<directory-to-save-graphs>", |
| 74 | sep=" " ) ) |
| 75 | |
| 76 | quit( status = 1 ) # basically exit(), but in R |
| 77 | } |
| 78 | |
| 79 | # ------------------ |
| 80 | # SQL Initialization |
| 81 | # ------------------ |
| 82 | |
| 83 | print( "Initializing SQL" ) |
| 84 | |
| 85 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 86 | dbname = "onostest", |
| 87 | host = args[ databaseHost ], |
| 88 | port = strtoi( args[ databasePort ] ), |
| 89 | user = args[ databaseUserID ], |
| 90 | password = args[ databasePassword ] ) |
| 91 | |
| 92 | # --------------------- |
| 93 | # Test Case SQL Command |
| 94 | # --------------------- |
| 95 | |
| 96 | print( "Generating Test Case SQL command." ) |
| 97 | |
| 98 | tests <- "'" |
| 99 | for ( test in as.list( strsplit( args[ testsToInclude ], "," )[[1]] ) ){ |
| 100 | tests <- paste( tests, test, "','", sep="" ) |
| 101 | } |
| 102 | tests <- substr( tests, 0, nchar( tests ) - 2 ) |
| 103 | |
| 104 | fileBuildToShow <- args[ buildToShow ] |
| 105 | operator <- "= " |
| 106 | buildTitle <- "" |
| 107 | if ( args[ buildToShow ] == "latest" ){ |
| 108 | buildTitle <- "\nLatest Test Results" |
| 109 | operator <- ">= " |
| 110 | args[ buildToShow ] <- "1000" |
| 111 | } else { |
| 112 | buildTitle <- paste( " \n Build #", args[ buildToShow ], sep="" ) |
| 113 | } |
| 114 | |
| 115 | command <- paste( "SELECT * ", |
| 116 | "FROM executed_test_tests a ", |
| 117 | "WHERE ( SELECT COUNT( * ) FROM executed_test_tests b ", |
| 118 | "WHERE b.branch='", |
| 119 | args[ branchName ], |
| 120 | "' AND b.actual_test_name IN (", |
| 121 | tests, |
| 122 | ") AND a.actual_test_name = b.actual_test_name AND a.date <= b.date AND b.build ", operator, |
| 123 | args[ buildToShow ], |
| 124 | " ) = ", |
| 125 | 1, |
| 126 | " AND a.branch='", |
| 127 | args[ branchName ], |
| 128 | "' AND a.actual_test_name IN (", |
| 129 | tests, |
| 130 | ") AND a.build ", operator, |
| 131 | args[ buildToShow ], |
| 132 | " ORDER BY a.actual_test_name DESC, a.date DESC", |
| 133 | sep="") |
| 134 | |
| 135 | print( "Sending SQL command:" ) |
| 136 | print( command ) |
| 137 | |
| 138 | dbResult <- dbGetQuery( con, command ) |
| 139 | |
| 140 | print( "dbResult:" ) |
| 141 | print( dbResult ) |
| 142 | |
| 143 | # ------------------------------- |
| 144 | # Create Title and Graph Filename |
| 145 | # ------------------------------- |
| 146 | |
| 147 | print( "Creating title of graph." ) |
| 148 | |
| 149 | typeOfPieTitle <- "Executed Results" |
| 150 | typeOfPieFile <- "_passfail" |
| 151 | isPlannedPie <- FALSE |
| 152 | if ( args[ isDisplayingPlan ] == "y" ){ |
| 153 | typeOfPieTitle <- "Test Execution" |
| 154 | typeOfPieFile <- "_executed" |
| 155 | isPlannedPie <- TRUE |
| 156 | } |
| 157 | |
| 158 | title <- paste( args[ testSuiteName ], |
| 159 | " Tests: Summary of ", |
| 160 | typeOfPieTitle, |
| 161 | "", |
| 162 | " - ", |
| 163 | args[ branchName ], |
| 164 | buildTitle, |
| 165 | sep="" ) |
| 166 | |
| 167 | print( "Creating graph filename." ) |
| 168 | |
| 169 | outputFile <- paste( args[ saveDirectory ], |
| 170 | args[ testSuiteName ], |
| 171 | "_", |
| 172 | args[ branchName ], |
| 173 | "_build-", |
| 174 | fileBuildToShow, |
| 175 | typeOfPieFile, |
| 176 | "_pieChart.jpg", |
| 177 | sep="" ) |
| 178 | |
| 179 | # ********************************************************** |
| 180 | # STEP 2: Organize data. |
| 181 | # ********************************************************** |
| 182 | |
| 183 | print( "**********************************************************" ) |
| 184 | print( "STEP 2: Organize Data." ) |
| 185 | print( "**********************************************************" ) |
| 186 | |
| 187 | t <- subset( dbResult, select=c( "actual_test_name", "num_passed", "num_failed", "num_planned" ) ) |
| 188 | |
| 189 | executedTests <- sum( t$num_passed ) + sum( t$num_failed ) |
| 190 | |
| 191 | # -------------------- |
| 192 | # Construct Data Frame |
| 193 | # -------------------- |
| 194 | |
| 195 | print( "Constructing Data Frame." ) |
| 196 | |
| 197 | if ( isPlannedPie ){ |
| 198 | |
| 199 | nonExecutedTests <- sum( t$num_planned ) - executedTests |
| 200 | totalTests <- sum( t$num_planned ) |
| 201 | |
| 202 | executedPercent <- round( executedTests / totalTests * 100, digits = 2 ) |
| 203 | nonExecutedPercent <- 100 - executedPercent |
| 204 | |
| 205 | dfData <- c( nonExecutedPercent, executedPercent ) |
| 206 | |
| 207 | labels <- c( "Executed Test Cases", "Skipped Test Cases" ) |
| 208 | |
| 209 | dataFrame <- data.frame( |
| 210 | rawData <- dfData, |
| 211 | displayedData <- c( paste( nonExecutedPercent, "%\n", nonExecutedTests, " / ", totalTests, " Tests", sep="" ), paste( executedPercent, "%\n", executedTests, " / ", totalTests," Tests", sep="" ) ), |
| 212 | names <- factor( rev( labels ), levels = labels ) ) |
| 213 | } else { |
| 214 | |
| 215 | sumPassed <- sum( t$num_passed ) |
| 216 | sumFailed <- sum( t$num_failed ) |
| 217 | sumExecuted <- sumPassed + sumFailed |
| 218 | |
| 219 | percentPassed <- sumPassed / sumExecuted |
| 220 | percentFailed <- sumFailed / sumExecuted |
| 221 | |
| 222 | dfData <- c( percentFailed, percentPassed ) |
| 223 | labels <- c( "Failed Test Cases", "Passed Test Cases" ) |
| 224 | |
| 225 | dataFrame <- data.frame( |
| 226 | rawData <- dfData, |
| 227 | displayedData <- c( paste( round( percentFailed * 100, 2 ), "%\n", sumFailed, " / ", sumExecuted, " Tests", sep="" ), paste( round( percentPassed * 100, 2 ), "%\n", sumPassed, " / ", sumExecuted, " Tests", sep="" ) ), |
| 228 | names <- factor( labels, levels = rev( labels ) ) ) |
| 229 | } |
| 230 | |
| 231 | print( "Data Frame Results:" ) |
| 232 | print( dataFrame ) |
| 233 | |
| 234 | # ********************************************************** |
| 235 | # STEP 3: Generate graphs. |
| 236 | # ********************************************************** |
| 237 | |
| 238 | print( "**********************************************************" ) |
| 239 | print( "STEP 3: Generate Graph." ) |
| 240 | print( "**********************************************************" ) |
| 241 | |
| 242 | # ------------------- |
| 243 | # Main Plot Generated |
| 244 | # ------------------- |
| 245 | |
| 246 | print( "Creating main plot." ) |
| 247 | # Create the primary plot here. |
| 248 | # ggplot contains the following arguments: |
| 249 | # - data: the data frame that the graph will be based off of |
| 250 | # - aes: the asthetics of the graph which require: |
| 251 | # - x: x-axis values (usually iterative, but it will become build # later) |
| 252 | # - y: y-axis values (usually tests) |
| 253 | # - color: the category of the colored lines (usually status of test) |
| 254 | |
| 255 | mainPlot <- ggplot( data = dataFrame, |
| 256 | aes( x = "", y=rawData, fill = names ) ) |
| 257 | |
| 258 | # ------------------- |
| 259 | # Main Plot Formatted |
| 260 | # ------------------- |
| 261 | |
| 262 | print( "Formatting main plot." ) |
| 263 | |
| 264 | # ------------------------------ |
| 265 | # Fundamental Variables Assigned |
| 266 | # ------------------------------ |
| 267 | |
| 268 | print( "Generating fundamental graph data." ) |
| 269 | |
| 270 | theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph. |
| 271 | |
| 272 | imageWidth <- 12 |
| 273 | imageHeight <- 10 |
| 274 | imageDPI <- 200 |
| 275 | |
| 276 | # Set other graph configurations here. |
| 277 | theme <- theme( plot.title = element_text( hjust = 0.5, size = 30, face ='bold' ), |
| 278 | axis.text.x = element_blank(), |
| 279 | axis.title.x = element_blank(), |
| 280 | axis.title.y = element_blank(), |
| 281 | axis.ticks = element_blank(), |
| 282 | panel.border = element_blank(), |
| 283 | panel.grid=element_blank(), |
| 284 | legend.position = "bottom", |
| 285 | legend.text = element_text( size = 22 ), |
| 286 | legend.title = element_blank(), |
| 287 | legend.key.size = unit( 1.5, 'lines' ), |
| 288 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
| 289 | |
| 290 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
| 291 | |
| 292 | title <- labs( title = title, subtitle = subtitle ) |
| 293 | |
| 294 | # Store plot configurations as 1 variable |
| 295 | fundamentalGraphData <- mainPlot + |
| 296 | theme + |
| 297 | title |
| 298 | |
| 299 | # ---------------------------- |
| 300 | # Generating Line Graph Format |
| 301 | # ---------------------------- |
| 302 | |
| 303 | print( "Generating line graph." ) |
| 304 | |
| 305 | if ( isPlannedPie ){ |
| 306 | executedColor <- "#00A5FF" # Blue |
| 307 | nonExecutedColor <- "#CCCCCC" # Gray |
| 308 | pieColors <- scale_fill_manual( values = c( executedColor, nonExecutedColor ) ) |
| 309 | } else { |
| 310 | passColor <- "#16B645" # Green |
| 311 | failColor <- "#E02020" # Red |
| 312 | pieColors <- scale_fill_manual( values = c( passColor, failColor ) ) |
| 313 | } |
| 314 | |
| 315 | pieFormat <- geom_bar( width = 1, stat = "identity" ) |
| 316 | pieLabels <- geom_text( aes( y = rawData / length( rawData ) + c( 0, cumsum( rawData )[ -length( rawData ) ] ) ), |
| 317 | label = dataFrame$displayedData, |
| 318 | size = 7, fontface = "bold" ) |
| 319 | |
| 320 | |
| 321 | result <- fundamentalGraphData + |
| 322 | pieFormat + coord_polar( "y" ) + pieLabels + pieColors |
| 323 | # ----------------------- |
| 324 | # Exporting Graph to File |
| 325 | # ----------------------- |
| 326 | |
| 327 | print( paste( "Saving result graph to", outputFile ) ) |
| 328 | |
| 329 | tryCatch( ggsave( outputFile, |
| 330 | width = imageWidth, |
| 331 | height = imageHeight, |
| 332 | dpi = imageDPI ), |
| 333 | error = function( e ){ |
| 334 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 335 | print( e ) |
| 336 | quit( status = 1 ) |
| 337 | } |
| 338 | ) |
| 339 | |
| 340 | print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) ) |
| 341 | quit( status = 0 ) |