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, |
| 21 | # please contact Jeremy Ronquillo: jeremyr@opennetworking.org |
| 22 | |
| 23 | # This is the R script that generates the FUNC and HA result graphs. |
| 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 | |
| 104 | print( "Data Frame Results:" ) |
| 105 | print( dataFrame ) |
| 106 | |
| 107 | # ********************************************************** |
| 108 | # STEP 3: Generate graphs. |
| 109 | # ********************************************************** |
| 110 | |
| 111 | print( "STEP 3: Generate graphs." ) |
| 112 | |
| 113 | print( "Creating main plot." ) |
| 114 | # Create the primary plot here. |
| 115 | # ggplot contains the following arguments: |
| 116 | # - data: the data frame that the graph will be based off of |
| 117 | # - aes: the asthetics of the graph which require: |
| 118 | # - x: x-axis values (usually iterative, but it will become build # later) |
| 119 | # - y: y-axis values (usually tests) |
| 120 | # - color: the category of the colored lines (usually status of test) |
| 121 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, y = Tests, color = Status ) ) |
| 122 | |
| 123 | print( "Formatting main plot." ) |
| 124 | # geom_ribbon is used so that there is a colored fill below the lines. These values shouldn't be changed. |
| 125 | failedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_failed ), fill = "red", linetype = 0, alpha = 0.07 ) |
| 126 | passedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_passed ), fill = "green", linetype = 0, alpha = 0.05 ) |
| 127 | plannedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_planned ), fill = "blue", linetype = 0, alpha = 0.01 ) |
| 128 | |
| 129 | xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, label = dataFrame$Build ) |
| 130 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$Tests ), by = ceiling( max( dataFrame$Tests ) / 10 ) ) ) |
| 131 | |
| 132 | xLabel <- xlab( "Build Number" ) |
| 133 | yLabel <- ylab( "Test Cases" ) |
| 134 | fillLabel <- labs( fill="Type" ) |
| 135 | legendLabels <- scale_colour_discrete( labels = c( "Failed", "Passed", "Planned" ) ) |
| 136 | centerTitle <- theme( plot.title=element_text( hjust = 0.5 ) ) # To center the title text |
| 137 | theme <- theme( plot.title = element_text( size = 18, face='bold' ) ) |
| 138 | |
| 139 | # Store plot configurations as 1 variable |
| 140 | fundamentalGraphData <- mainPlot + plannedColor + passedColor + failedColor + xScaleConfig + yScaleConfig + xLabel + yLabel + fillLabel + legendLabels + centerTitle + theme |
| 141 | |
| 142 | print( "Generating line graph." ) |
| 143 | |
| 144 | lineGraphFormat <- geom_line( size = 1.1 ) |
| 145 | pointFormat <- geom_point( size = 3 ) |
| 146 | title <- ggtitle( title ) |
| 147 | |
| 148 | result <- fundamentalGraphData + lineGraphFormat + pointFormat + title |
| 149 | |
| 150 | # Save graph to file |
| 151 | print( paste( "Saving result graph to", outputFile ) ) |
| 152 | ggsave( outputFile, width = 10, height = 6, dpi = 200 ) |
| 153 | print( paste( "Successfully wrote result graph out to", outputFile ) ) |