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 | # ********************************************************** |
| 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 | displayStatus <- 9 |
| 44 | scaleOfPercent <- 10 |
| 45 | saveDirectory <- 11 |
| 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 | "<build-to-show>", |
| 73 | "<pass/fail/plan>", |
| 74 | "<percent-scale>", |
| 75 | "<directory-to-save-graphs>", |
| 76 | sep=" " ) ) |
| 77 | |
| 78 | quit( status = 1 ) # basically exit(), but in R |
| 79 | } |
| 80 | |
| 81 | # ------------------ |
| 82 | # SQL Initialization |
| 83 | # ------------------ |
| 84 | |
| 85 | print( "Initializing SQL" ) |
| 86 | |
| 87 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 88 | dbname = "onostest", |
| 89 | host = args[ databaseHost ], |
| 90 | port = strtoi( args[ databasePort ] ), |
| 91 | user = args[ databaseUserID ], |
| 92 | password = args[ databasePassword ] ) |
| 93 | |
| 94 | # --------------------- |
| 95 | # Test Case SQL Command |
| 96 | # --------------------- |
| 97 | print( "Generating Test Case SQL command." ) |
| 98 | |
| 99 | tests <- "'" |
| 100 | for ( test in as.list( strsplit( args[ testsToInclude ], "," )[[1]] ) ){ |
| 101 | tests <- paste( tests, test, "','", sep="" ) |
| 102 | } |
| 103 | tests <- substr( tests, 0, nchar( tests ) - 2 ) |
| 104 | |
| 105 | fileBuildToShow <- args[ buildToShow ] |
| 106 | operator <- "= " |
| 107 | if ( args[ buildToShow ] == "latest" ){ |
| 108 | operator <- ">= " |
| 109 | args[ buildToShow ] <- "1000" |
| 110 | } |
| 111 | |
| 112 | command <- paste( "SELECT * ", |
| 113 | "FROM executed_test_tests a ", |
| 114 | "WHERE ( SELECT COUNT( * ) FROM executed_test_tests b ", |
| 115 | "WHERE b.branch='", |
| 116 | args[ branchName ], |
| 117 | "' AND b.actual_test_name IN (", |
| 118 | tests, |
| 119 | ") AND a.actual_test_name = b.actual_test_name AND a.date <= b.date AND b.build ", operator, |
| 120 | args[ buildToShow ], |
| 121 | " ) = ", |
| 122 | 1, |
| 123 | " AND a.branch='", |
| 124 | args[ branchName ], |
| 125 | "' AND a.actual_test_name IN (", |
| 126 | tests, |
| 127 | ") AND a.build ", operator, |
| 128 | args[ buildToShow ], |
| 129 | " ORDER BY a.actual_test_name DESC, a.date DESC", |
| 130 | sep="") |
| 131 | |
| 132 | print( "Sending SQL command:" ) |
| 133 | print( command ) |
| 134 | dbResult <- dbGetQuery( con, command ) |
| 135 | |
| 136 | maxBuild <- max( dbResult[ 'build' ] ) |
| 137 | dbResult <- dbResult[ which( dbResult[,4]>=maxBuild ), ] |
| 138 | |
| 139 | # ------------------------------- |
| 140 | # Create Title and Graph Filename |
| 141 | # ------------------------------- |
| 142 | |
| 143 | print( "Creating title of graph." ) |
| 144 | |
| 145 | titleDisplayStatus <- "" |
| 146 | if ( args[ displayStatus ] == "fail" ){ |
| 147 | titleDisplayStatus <- "Failed" |
| 148 | } else if ( args[ displayStatus ] == "plan" ){ |
| 149 | titleDisplayStatus <- "Executed" |
| 150 | } else if ( args[ displayStatus ] == "pass" ){ |
| 151 | titleDisplayStatus <- "Succeeded" |
| 152 | } else { |
| 153 | print( paste( "[ERROR]: Invalid histogram display status: ", args[ displayStatus ], sep="" ) ) |
| 154 | quit( status = 1 ) |
| 155 | } |
| 156 | |
| 157 | title <- paste( args[ testSuiteName ], |
| 158 | " Tests ", |
| 159 | titleDisplayStatus, |
| 160 | " - ", |
| 161 | args[ branchName ], |
| 162 | " \n Build #", |
| 163 | max( dbResult[ 'build' ] ), |
| 164 | sep="" ) |
| 165 | |
| 166 | print( "Creating graph filename." ) |
| 167 | |
| 168 | outputFile <- paste( args[ saveDirectory ], |
| 169 | args[ testSuiteName ], |
| 170 | "_", |
| 171 | args[ branchName ], |
| 172 | "_build-", |
| 173 | fileBuildToShow, |
| 174 | "_", |
| 175 | args[ scaleOfPercent ], |
| 176 | "-scaling", |
| 177 | "_", |
| 178 | args[ displayStatus ], |
| 179 | "_histogram.jpg", |
| 180 | sep="" ) |
| 181 | |
| 182 | print( dbResult ) |
| 183 | |
| 184 | # ********************************************************** |
| 185 | # STEP 2: Organize data. |
| 186 | # ********************************************************** |
| 187 | |
| 188 | print( "**********************************************************" ) |
| 189 | print( "STEP 2: Organize Data." ) |
| 190 | print( "**********************************************************" ) |
| 191 | |
| 192 | t <- subset( dbResult, select=c( "actual_test_name", "num_passed", "num_failed", "num_planned" ) ) |
| 193 | t$passed_percent <- t$num_passed / t$num_planned * 100 |
| 194 | t$failed_percent <- t$num_failed / t$num_planned * 100 |
| 195 | t$planned_percent <- ( t$num_passed + t$num_failed ) / t$num_planned * 100 |
| 196 | |
| 197 | # -------------------- |
| 198 | # Construct Data Frame |
| 199 | # -------------------- |
| 200 | |
| 201 | dataFrame <- aggregate( t$passed_percent, by=list( Category=t$actual_test_name ), FUN=sum ) |
| 202 | if ( args[ displayStatus ] == "fail" ){ |
| 203 | dataFrame <- aggregate( t$failed_percent, by=list( Category=t$actual_test_name ), FUN=sum ) |
| 204 | } else if ( args[ displayStatus ] == "plan" ){ |
| 205 | dataFrame <- aggregate( t$planned_percent, by=list( Category=t$actual_test_name ), FUN=sum ) |
| 206 | } |
| 207 | |
| 208 | colnames( dataFrame ) <- c( "Test", paste( titleDisplayStatus, "%", sep="" ) ) |
| 209 | |
| 210 | print( "Data Frame Results:" ) |
| 211 | print( dataFrame ) |
| 212 | |
| 213 | # ********************************************************** |
| 214 | # STEP 3: Generate graphs. |
| 215 | # ********************************************************** |
| 216 | |
| 217 | print( "**********************************************************" ) |
| 218 | print( "STEP 3: Generate Graph." ) |
| 219 | print( "**********************************************************" ) |
| 220 | |
| 221 | # ------------------- |
| 222 | # Main Plot Generated |
| 223 | # ------------------- |
| 224 | |
| 225 | print( "Creating main plot." ) |
| 226 | # Create the primary plot here. |
| 227 | # ggplot contains the following arguments: |
| 228 | # - data: the data frame that the graph will be based off of |
| 229 | # - aes: the asthetics of the graph which require: |
| 230 | # - x: x-axis values (usually iterative, but it will become build # later) |
| 231 | # - y: y-axis values (usually tests) |
| 232 | # - color: the category of the colored lines (usually status of test) |
| 233 | |
| 234 | mainPlot <- ggplot( data = dataFrame, aes( dataFrame[ ,2 ] ) ) |
| 235 | |
| 236 | # ------------------- |
| 237 | # Main Plot Formatted |
| 238 | # ------------------- |
| 239 | |
| 240 | print( "Formatting main plot." ) |
| 241 | |
| 242 | # ------------------------------ |
| 243 | # Fundamental Variables Assigned |
| 244 | # ------------------------------ |
| 245 | |
| 246 | print( "Generating fundamental graph data." ) |
| 247 | |
| 248 | theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph. |
| 249 | |
| 250 | xScaleConfig <- scale_x_continuous( breaks = seq( 0, 100, by = 10 ) ) |
| 251 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, nrow( dbResult ), by = 1 ), limits = c( 0, nrow( dbResult ) ) ) |
| 252 | |
| 253 | xLabel <- xlab( paste( titleDisplayStatus, "%" ) ) |
| 254 | yLabel <- ylab( "Frequency" ) |
| 255 | |
| 256 | imageWidth <- 15 |
| 257 | imageHeight <- 10 |
| 258 | imageDPI <- 200 |
| 259 | |
| 260 | # Set other graph configurations here. |
| 261 | theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face ='bold' ), |
| 262 | axis.text.x = element_text( angle = 0, size = 14 ), |
| 263 | legend.position = "bottom", |
| 264 | legend.text = element_text( size = 22 ), |
| 265 | legend.title = element_blank(), |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 266 | legend.key.size = unit( 1.5, 'lines' ), |
| 267 | plot.subtitle = element_text( size=16, hjust=1.0 ) ) |
Jeremy Ronquillo | 908cb44 | 2017-12-07 08:58:09 -0800 | [diff] [blame] | 268 | |
Jeremy Ronquillo | 94f99dd | 2018-01-05 11:11:27 -0800 | [diff] [blame] | 269 | subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" ) |
| 270 | |
| 271 | title <- labs( title = title, subtitle = subtitle ) |
Jeremy Ronquillo | 908cb44 | 2017-12-07 08:58:09 -0800 | [diff] [blame] | 272 | |
| 273 | # Store plot configurations as 1 variable |
| 274 | fundamentalGraphData <- mainPlot + |
| 275 | xScaleConfig + |
| 276 | yScaleConfig + |
| 277 | xLabel + |
| 278 | yLabel + |
| 279 | theme + |
| 280 | title |
| 281 | |
| 282 | # ---------------------------- |
| 283 | # Generating Line Graph Format |
| 284 | # ---------------------------- |
| 285 | |
| 286 | print( "Generating line graph." ) |
| 287 | |
| 288 | barColor <- "#00B208" |
| 289 | if ( args[ displayStatus ] == "fail" ){ |
| 290 | barColor <- "#E80000" |
| 291 | } else if ( args[ displayStatus ] == "plan" ){ |
| 292 | barColor <- "#00A5FF" |
| 293 | } |
| 294 | |
| 295 | histogramFormat <- geom_histogram( col = "#000000", |
| 296 | fill = barColor, |
| 297 | breaks = seq( 0, 100, by = strtoi( args[ scaleOfPercent ] ) ), |
| 298 | lwd = 0.5 ) |
| 299 | |
| 300 | result <- fundamentalGraphData + |
| 301 | histogramFormat |
| 302 | |
| 303 | # ----------------------- |
| 304 | # Exporting Graph to File |
| 305 | # ----------------------- |
| 306 | |
| 307 | print( paste( "Saving result graph to", outputFile ) ) |
| 308 | |
| 309 | tryCatch( ggsave( outputFile, |
| 310 | width = imageWidth, |
| 311 | height = imageHeight, |
| 312 | dpi = imageDPI ), |
| 313 | error = function( e ){ |
| 314 | print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" ) |
| 315 | print( e ) |
| 316 | quit( status = 1 ) |
| 317 | } |
| 318 | ) |
| 319 | |
| 320 | print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) ) |
| 321 | quit( status = 0 ) |