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 | |
| 23 | # This is the R script that generates the SCPF front page graphs. |
| 24 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 25 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 26 | # ********************************************************** |
| 27 | # STEP 1: Data management. |
| 28 | # ********************************************************** |
| 29 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 30 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 31 | print( "STEP 1: Data management." ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 32 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 33 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 34 | # ---------------- |
| 35 | # Import Libraries |
| 36 | # ---------------- |
| 37 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 38 | print( "Importing libraries." ) |
| 39 | library( ggplot2 ) |
| 40 | library( reshape2 ) |
| 41 | library( RPostgreSQL ) |
| 42 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 43 | # ------------------- |
| 44 | # Check CLI Arguments |
| 45 | # ------------------- |
| 46 | |
| 47 | print( "Verifying CLI args." ) |
| 48 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 49 | # Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files. |
| 50 | print( "Reading commmand-line args." ) |
| 51 | args <- commandArgs( trailingOnly=TRUE ) |
| 52 | |
| 53 | # Check if sufficient args are provided. |
| 54 | if ( is.na( args[ 10 ] ) ){ |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 55 | |
| 56 | print( paste( "Usage: Rscript testresultgraph.R", |
| 57 | "<database-host>", |
| 58 | "<database-port>", |
| 59 | "<database-user-id>", |
| 60 | "<database-password>", |
| 61 | "<graph-title>", # part of the output filename as well |
| 62 | "<branch-name>", # part of the output filename |
| 63 | "<#-dates>", # part of the output filename |
| 64 | "<SQL-command>", |
| 65 | "<y-axis-title>", # y-axis may be different among other SCPF graphs (ie: batch size, latency, etc. ) |
| 66 | "<directory-to-save-graph>", |
| 67 | sep = " " ) ) |
| 68 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 69 | q() # basically exit(), but in R |
| 70 | } |
| 71 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 72 | # ------------------------------- |
| 73 | # Create Title and Graph Filename |
| 74 | # ------------------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 75 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 76 | print( "Creating title of graph" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 77 | |
| 78 | # Title of graph based on command line args. |
| 79 | title <- args[ 5 ] |
| 80 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 81 | print( "Creating graph filename." ) |
| 82 | |
| 83 | # Filenames for the output graph include the testname, branch, and the graph type. |
| 84 | outputFile <- paste( args[ 10 ], |
| 85 | "SCPF_Front_Page_", |
| 86 | gsub( " ", "_", args[ 5 ] ), |
| 87 | "_", |
| 88 | args[ 6 ], |
| 89 | "_", |
| 90 | args[ 7 ], |
| 91 | "-dates_graph.jpg", |
| 92 | sep="" ) |
| 93 | |
| 94 | # ------------------ |
| 95 | # SQL Initialization |
| 96 | # ------------------ |
| 97 | |
| 98 | print( "Initializing SQL" ) |
| 99 | con <- dbConnect( dbDriver( "PostgreSQL" ), |
| 100 | dbname = "onostest", |
| 101 | host = args[ 1 ], |
| 102 | port = strtoi( args[ 2 ] ), |
| 103 | user = args[ 3 ], |
| 104 | password = args[ 4 ] ) |
| 105 | |
| 106 | print( "Sending SQL command:" ) |
| 107 | print( args[ 8 ] ) |
| 108 | fileData <- dbGetQuery( con, args[ 8 ] ) |
| 109 | |
| 110 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 111 | # ********************************************************** |
| 112 | # STEP 2: Organize data. |
| 113 | # ********************************************************** |
| 114 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 115 | print( "**********************************************************" ) |
| 116 | print( "STEP 2: Organize Data." ) |
| 117 | print( "**********************************************************" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 118 | |
| 119 | # Create lists c() and organize data into their corresponding list. |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 120 | print( "Combine data retrieved from databases into a list." ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 121 | |
| 122 | if ( ncol( fileData ) > 1 ){ |
| 123 | for ( i in 2:ncol( fileData ) ){ |
| 124 | fileData[ i ] <- fileData[ i - 1 ] + fileData[ i ] |
| 125 | } |
| 126 | } |
| 127 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 128 | # -------------------- |
| 129 | # Construct Data Frame |
| 130 | # -------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 131 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 132 | print( "Constructing data frame from combined data." ) |
| 133 | |
| 134 | dataFrame <- melt( fileData ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 135 | dataFrame$date <- fileData$date |
| 136 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 137 | colnames( dataFrame ) <- c( "Legend", |
| 138 | "Values" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 139 | |
| 140 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 141 | dataFrame$Legend <- as.character( dataFrame$Legend ) |
| 142 | dataFrame$Legend <- factor( dataFrame$Legend, levels=unique( dataFrame$Legend ) ) |
| 143 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 144 | # Adding a temporary iterative list to the dataFrame so that there are no gaps in-between date numbers. |
| 145 | dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) ) |
| 146 | |
| 147 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 148 | |
| 149 | print( "Data Frame Results:" ) |
| 150 | print( dataFrame ) |
| 151 | |
| 152 | # ********************************************************** |
| 153 | # STEP 3: Generate graphs. |
| 154 | # ********************************************************** |
| 155 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 156 | print( "**********************************************************" ) |
| 157 | print( "STEP 3: Generate Graph." ) |
| 158 | print( "**********************************************************" ) |
| 159 | |
| 160 | # ------------------- |
| 161 | # Main Plot Generated |
| 162 | # ------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 163 | |
| 164 | print( "Creating main plot." ) |
| 165 | # Create the primary plot here. |
| 166 | # ggplot contains the following arguments: |
| 167 | # - data: the data frame that the graph will be based off of |
| 168 | # - aes: the asthetics of the graph which require: |
| 169 | # - x: x-axis values (usually iterative, but it will become date # later) |
| 170 | # - y: y-axis values (usually tests) |
| 171 | # - color: the category of the colored lines (usually legend of test) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 172 | |
| 173 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, |
| 174 | y = Values, |
| 175 | color = Legend ) ) |
| 176 | |
| 177 | # ------------------- |
| 178 | # Main Plot Formatted |
| 179 | # ------------------- |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 180 | |
| 181 | print( "Formatting main plot." ) |
| 182 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 183 | limitExpansion <- expand_limits( y = 0 ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 184 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 185 | maxYDisplay <- max( dataFrame$Values ) * 1.05 |
| 186 | yBreaks <- ceiling( max( dataFrame$Values ) / 10 ) |
| 187 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, maxYDisplay, by = yBreaks ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 188 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 189 | # ------------------------------ |
| 190 | # Fundamental Variables Assigned |
| 191 | # ------------------------------ |
| 192 | |
| 193 | print( "Generating fundamental graph data." ) |
| 194 | |
| 195 | theme_set( theme_grey( base_size = 22 ) ) # set the default text size of the graph. |
| 196 | xLabel <- xlab( "Build" ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 197 | yLabel <- ylab( args[ 9 ] ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 198 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 199 | imageWidth <- 15 |
| 200 | imageHeight <- 10 |
| 201 | imageDPI <- 200 |
| 202 | |
| 203 | # Set other graph configurations here. |
| 204 | theme <- theme( axis.text.x = element_blank(), |
| 205 | axis.ticks.x = element_blank(), |
| 206 | plot.title = element_text( size = 32, face='bold', hjust = 0.5 ), |
| 207 | legend.position = "bottom", |
| 208 | legend.text = element_text( size=22 ), |
| 209 | legend.title = element_blank(), |
| 210 | legend.key.size = unit( 1.5, 'lines' ), |
| 211 | legend.direction = 'horizontal' ) |
| 212 | |
| 213 | # Colors used for the lines. |
| 214 | # Note: graphs that have X lines will use the first X colors in this list. |
| 215 | colors <- scale_color_manual( values=c( "#111111", # black |
| 216 | "#008CFF", # blue |
| 217 | "#FF3700", # red |
| 218 | "#00E043", # green |
| 219 | "#EEB600", # yellow |
| 220 | "#E500FF") ) # purple (not used) |
| 221 | |
| 222 | wrapLegend <- guides( color = guide_legend( nrow = 2, byrow = TRUE ) ) |
| 223 | title <- ggtitle( title ) |
| 224 | |
| 225 | fundamentalGraphData <- mainPlot + |
| 226 | limitExpansion + |
| 227 | yScaleConfig + |
| 228 | xLabel + |
| 229 | yLabel + |
| 230 | theme + |
| 231 | colors + |
| 232 | wrapLegend + |
| 233 | title |
| 234 | |
| 235 | # ---------------------------- |
| 236 | # Generating Line Graph Format |
| 237 | # ---------------------------- |
| 238 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 239 | print( "Generating line graph." ) |
| 240 | |
Jeremy Ronquillo | e906376 | 2017-10-17 15:36:09 -0700 | [diff] [blame] | 241 | lineGraphFormat <- geom_line( size = 0.75 ) |
| 242 | pointFormat <- geom_point( size = 1.75 ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 243 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 244 | result <- fundamentalGraphData + |
| 245 | lineGraphFormat + |
| 246 | pointFormat |
Jeremy Ronquillo | 0e27b91 | 2017-10-21 14:34:24 -0700 | [diff] [blame] | 247 | |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 248 | # ----------------------- |
| 249 | # Exporting Graph to File |
| 250 | # ----------------------- |
| 251 | |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 252 | print( paste( "Saving result graph to", outputFile ) ) |
Jeremy Ronquillo | 7673f80 | 2017-10-30 09:42:44 -0700 | [diff] [blame] | 253 | |
| 254 | ggsave( outputFile, |
| 255 | width = imageWidth, |
| 256 | height = imageHeight, |
| 257 | dpi = imageDPI ) |
| 258 | |
| 259 | print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) ) |