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 | |
| 29 | print( "STEP 1: Data management." ) |
| 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 | # Import libraries to be used for graphing and organizing data, respectively. |
| 36 | # Find out more about ggplot2: https://github.com/tidyverse/ggplot2 |
| 37 | # reshape2: https://github.com/hadley/reshape |
| 38 | # RPostgreSQL: https://code.google.com/archive/p/rpostgresql/ |
| 39 | print( "Importing libraries." ) |
| 40 | library( ggplot2 ) |
| 41 | library( reshape2 ) |
| 42 | library( RPostgreSQL ) |
| 43 | |
| 44 | # Check if sufficient args are provided. |
| 45 | if ( is.na( args[ 8 ] ) ){ |
| 46 | print( "Usage: Rscript testCaseGraphGenerator.R <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <#-builds-to-show> <directory-to-save-graphs>" ) |
| 47 | q() # basically exit(), but in R |
| 48 | } |
| 49 | |
| 50 | # Filenames for the output graph include the testname, branch, and the graph type. |
| 51 | outputFile <- paste( args[ 8 ], args[ 5 ], sep="" ) |
| 52 | outputFile <- paste( outputFile, args[ 6 ], sep="_" ) |
| 53 | outputFile <- paste( outputFile, args[ 7 ], sep="_" ) |
| 54 | outputFile <- paste( outputFile, "builds", sep="-" ) |
| 55 | outputFile <- paste( outputFile, "_graph.jpg", sep="" ) |
| 56 | |
| 57 | # From RPostgreSQL |
| 58 | print( "Reading from databases." ) |
| 59 | con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] ) |
| 60 | |
| 61 | print( "Creating SQL command." ) |
| 62 | # Creating SQL command based on command line args. |
| 63 | command <- paste( "SELECT * FROM executed_test_tests WHERE actual_test_name='", args[ 5 ], sep="" ) |
| 64 | command <- paste( command, "' AND branch='", sep="" ) |
| 65 | command <- paste( command, args[ 6 ], sep="" ) |
| 66 | command <- paste( command, "' ORDER BY date DESC LIMIT ", sep="" ) |
| 67 | command <- paste( command, args[ 7 ], sep="" ) |
| 68 | fileData <- dbGetQuery( con, command ) |
| 69 | |
| 70 | # Title of graph based on command line args. |
| 71 | title <- paste( args[ 5 ], args[ 6 ], sep=" - " ) |
| 72 | title <- paste( title, "Results of Last ", sep=" \n " ) |
| 73 | title <- paste( title, args[ 7 ], sep="" ) |
| 74 | title <- paste( title, " Builds", sep="" ) |
| 75 | |
| 76 | # ********************************************************** |
| 77 | # STEP 2: Organize data. |
| 78 | # ********************************************************** |
| 79 | |
| 80 | print( "STEP 2: Organize data." ) |
| 81 | |
| 82 | # Create lists c() and organize data into their corresponding list. |
| 83 | print( "Sorting data into new data frame." ) |
| 84 | categories <- c( fileData[ 'num_failed' ], fileData[ 'num_passed' ], fileData[ 'num_planned' ] ) |
| 85 | |
| 86 | # Parse lists into data frames. |
| 87 | # This is where reshape2 comes in. Avgs list is converted to data frame. |
| 88 | dataFrame <- melt( categories ) |
| 89 | dataFrame$build <- fileData$build |
| 90 | colnames( dataFrame ) <- c( "Tests", "Status", "Build" ) |
| 91 | |
| 92 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 93 | dataFrame$Status <- as.character( dataFrame$Status ) |
| 94 | dataFrame$Status <- factor( dataFrame$Status, levels=unique( dataFrame$Status ) ) |
| 95 | |
| 96 | # Add planned, passed, and failed results to the dataFrame (for the fill below the lines) |
| 97 | dataFrame$num_planned <- fileData$num_planned |
| 98 | dataFrame$num_passed <- fileData$num_passed |
| 99 | dataFrame$num_failed <- fileData$num_failed |
| 100 | |
| 101 | # Adding a temporary reversed iterative list to the dataFrame so that there are no gaps in-between build numbers. |
| 102 | dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) ) |
| 103 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 104 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 105 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 106 | print( "Data Frame Results:" ) |
| 107 | print( dataFrame ) |
| 108 | |
| 109 | # ********************************************************** |
| 110 | # STEP 3: Generate graphs. |
| 111 | # ********************************************************** |
| 112 | |
| 113 | print( "STEP 3: Generate graphs." ) |
| 114 | |
| 115 | print( "Creating main plot." ) |
| 116 | # Create the primary plot here. |
| 117 | # ggplot contains the following arguments: |
| 118 | # - data: the data frame that the graph will be based off of |
| 119 | # - aes: the asthetics of the graph which require: |
| 120 | # - x: x-axis values (usually iterative, but it will become build # later) |
| 121 | # - y: y-axis values (usually tests) |
| 122 | # - color: the category of the colored lines (usually status of test) |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 123 | theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph. |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 124 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, y = Tests, color = Status ) ) |
| 125 | |
| 126 | print( "Formatting main plot." ) |
| 127 | # geom_ribbon is used so that there is a colored fill below the lines. These values shouldn't be changed. |
| 128 | failedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_failed ), fill = "red", linetype = 0, alpha = 0.07 ) |
| 129 | passedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_passed ), fill = "green", linetype = 0, alpha = 0.05 ) |
| 130 | plannedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_planned ), fill = "blue", linetype = 0, alpha = 0.01 ) |
| 131 | |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 132 | colors <- scale_color_manual( values=c( "#E80000", "#00B208", "#00A5FF") ) |
| 133 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 134 | xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, label = dataFrame$Build ) |
| 135 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$Tests ), by = ceiling( max( dataFrame$Tests ) / 10 ) ) ) |
| 136 | |
| 137 | xLabel <- xlab( "Build Number" ) |
| 138 | yLabel <- ylab( "Test Cases" ) |
| 139 | fillLabel <- labs( fill="Type" ) |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 140 | legendLabels <- scale_colour_discrete( labels = c( "Failed Cases", "Passed Cases", "Planned Cases" ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 141 | centerTitle <- theme( plot.title=element_text( hjust = 0.5 ) ) # To center the title text |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 142 | theme <- theme( plot.title = element_text( size = 32, face='bold' ), axis.text.x = element_text( angle = 0, size = 14 ), legend.position="bottom", legend.text=element_text( size=18, face="bold" ), legend.title = element_blank() ) |
| 143 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 144 | |
| 145 | # Store plot configurations as 1 variable |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 146 | fundamentalGraphData <- mainPlot + plannedColor + passedColor + failedColor + xScaleConfig + yScaleConfig + xLabel + yLabel + fillLabel + colors + legendLabels + centerTitle + theme |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 147 | |
| 148 | print( "Generating line graph." ) |
| 149 | |
| 150 | lineGraphFormat <- geom_line( size = 1.1 ) |
| 151 | pointFormat <- geom_point( size = 3 ) |
| 152 | title <- ggtitle( title ) |
| 153 | |
| 154 | result <- fundamentalGraphData + lineGraphFormat + pointFormat + title |
| 155 | |
| 156 | # Save graph to file |
| 157 | print( paste( "Saving result graph to", outputFile ) ) |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 158 | ggsave( outputFile, width = 15, height = 10, dpi = 200 ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 159 | print( paste( "Successfully wrote result graph out to", outputFile ) ) |