blob: 5671a387d1f00386c019e682e4c07bad4053fad3 [file] [log] [blame]
Jeremy Ronquillo6df87812017-08-28 16:17:36 +00001# 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 Ronquillob6268842017-10-03 13:02:58 -070021# please contact Jeremy Ronquillo: j_ronquillo@u.pacific.edu
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000022
Jeremy Ronquillob6268842017-10-03 13:02:58 -070023# This is the R script that generates the FUNC, HA, and various USECASE result graphs.
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000024
25# **********************************************************
26# STEP 1: Data management.
27# **********************************************************
28
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070029print( "**********************************************************" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000030print( "STEP 1: Data management." )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070031print( "**********************************************************" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000032
33# Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files.
34print( "Reading commmand-line args." )
35args <- commandArgs( trailingOnly=TRUE )
36
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070037# ----------------
38# Import Libraries
39# ----------------
40
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000041print( "Importing libraries." )
42library( ggplot2 )
43library( reshape2 )
44library( RPostgreSQL )
45
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070046# -------------------
47# Check CLI Arguments
48# -------------------
49
50print( "Verifying CLI args." )
51
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000052if ( is.na( args[ 8 ] ) ){
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070053
54 print( paste( "Usage: Rscript testCaseGraphGenerator.R",
55 "<database-host>",
56 "<database-port>",
57 "<database-user-id>",
58 "<database-password>",
59 "<test-name>", # part of the output filename
60 "<branch-name>", # for sql and output filename
61 "<#-builds-to-show>", # for sql and output filename
62 "<directory-to-save-graphs>",
63 sep=" " ) )
64
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -080065 quit( status = 1 ) # basically exit(), but in R
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000066}
67
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070068# -------------------------------
69# Create Title and Graph Filename
70# -------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000071
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070072print( "Creating title of graph." )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000073
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070074title <- paste( args[ 5 ],
75 " - ",
76 args[ 6 ],
77 " \n Results of Last ",
78 args[ 7 ],
79 " Builds",
80 sep="" )
81
82print( "Creating graph filename." )
83
84outputFile <- paste( args[ 8 ],
85 args[ 5 ],
86 "_",
87 args[ 6 ],
88 "_",
89 args[ 7 ],
90 "-builds_graph.jpg",
91 sep="" )
92
93# ------------------
94# SQL Initialization
95# ------------------
96
97print( "Initializing SQL" )
98
99con <- 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# ---------------------
107# Test Case SQL Command
108# ---------------------
109print( "Generating Test Case SQL command." )
110
111command <- paste( "SELECT * FROM executed_test_tests WHERE actual_test_name='",
112 args[ 5 ],
113 "' AND branch='",
114 args[ 6 ],
115 "' ORDER BY date DESC LIMIT ",
116 args[ 7 ],
117 sep="" )
118
119print( "Sending SQL command:" )
120print( command )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000121fileData <- dbGetQuery( con, command )
122
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000123
124# **********************************************************
125# STEP 2: Organize data.
126# **********************************************************
127
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700128print( "**********************************************************" )
129print( "STEP 2: Organize Data." )
130print( "**********************************************************" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000131
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700132# -------------------------------------------------------
133# Combining Passed, Failed, and Planned Data
134# -------------------------------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000135
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700136print( "Combining Passed, Failed, and Planned Data." )
137
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800138requiredColumns <- c( "num_failed", "num_passed", "num_planned" )
139
140tryCatch( categories <- c( fileData[ requiredColumns] ),
141 error = function( e ) {
142 print( "[ERROR] One or more expected columns are missing from the data. Please check that the data and SQL command are valid, then try again." )
143 print( "Required columns: " )
144 print( requiredColumns )
145 print( "Actual columns: " )
146 print( names( fileData ) )
147 print( "Error dump:" )
148 print( e )
149 quit( status = 1 )
150 }
151 )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700152
153# --------------------
154# Construct Data Frame
155# --------------------
156
157print( "Constructing data frame from combined data." )
158
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000159dataFrame <- melt( categories )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700160
161# Rename column names in dataFrame
162colnames( dataFrame ) <- c( "Tests",
163 "Status" )
164
165# Add build dates to the dataFrame
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000166dataFrame$build <- fileData$build
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000167
168# Format data frame so that the data is in the same order as it appeared in the file.
169dataFrame$Status <- as.character( dataFrame$Status )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700170dataFrame$Status <- factor( dataFrame$Status, levels = unique( dataFrame$Status ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000171
172# Add planned, passed, and failed results to the dataFrame (for the fill below the lines)
173dataFrame$num_planned <- fileData$num_planned
174dataFrame$num_passed <- fileData$num_passed
175dataFrame$num_failed <- fileData$num_failed
176
177# Adding a temporary reversed iterative list to the dataFrame so that there are no gaps in-between build numbers.
178dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) )
179
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700180# Omit any data that doesn't exist
181dataFrame <- na.omit( dataFrame )
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700182
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000183print( "Data Frame Results:" )
184print( dataFrame )
185
186# **********************************************************
187# STEP 3: Generate graphs.
188# **********************************************************
189
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700190print( "**********************************************************" )
191print( "STEP 3: Generate Graph." )
192print( "**********************************************************" )
193
194# -------------------
195# Main Plot Generated
196# -------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000197
198print( "Creating main plot." )
199# Create the primary plot here.
200# ggplot contains the following arguments:
201# - data: the data frame that the graph will be based off of
202# - aes: the asthetics of the graph which require:
203# - x: x-axis values (usually iterative, but it will become build # later)
204# - y: y-axis values (usually tests)
205# - color: the category of the colored lines (usually status of test)
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700206
207mainPlot <- ggplot( data = dataFrame, aes( x = iterative,
208 y = Tests,
209 color = Status ) )
210
211# -------------------
212# Main Plot Formatted
213# -------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000214
215print( "Formatting main plot." )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700216
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000217# geom_ribbon is used so that there is a colored fill below the lines. These values shouldn't be changed.
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700218failedColor <- geom_ribbon( aes( ymin = 0,
219 ymax = dataFrame$num_failed ),
220 fill = "red",
221 linetype = 0,
222 alpha = 0.07 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000223
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700224passedColor <- geom_ribbon( aes( ymin = 0,
225 ymax = dataFrame$num_passed ),
226 fill = "green",
227 linetype = 0,
228 alpha = 0.05 )
Jeremy Ronquilloe9063762017-10-17 15:36:09 -0700229
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700230plannedColor <- geom_ribbon( aes( ymin = 0,
231 ymax = dataFrame$num_planned ),
232 fill = "blue",
233 linetype = 0,
234 alpha = 0.01 )
235
236# Colors for the lines
237lineColors <- scale_color_manual( values=c( "#E80000", # red
238 "#00B208", # green
239 "#00A5FF") ) # blue
240
241# ------------------------------
242# Fundamental Variables Assigned
243# ------------------------------
244
245print( "Generating fundamental graph data." )
246
247theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph.
248
249xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative,
Jeremy Ronquillo7fe4cdb2017-11-15 11:26:24 -0800250 label = dataFrame$build )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700251yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$Tests ),
252 by = ceiling( max( dataFrame$Tests ) / 10 ) ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000253
254xLabel <- xlab( "Build Number" )
255yLabel <- ylab( "Test Cases" )
Jeremy Ronquilloe9063762017-10-17 15:36:09 -0700256
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700257imageWidth <- 15
258imageHeight <- 10
259imageDPI <- 200
260
261legendLabels <- scale_colour_discrete( labels = c( "Failed Cases",
262 "Passed Cases",
263 "Planned Cases" ) )
264
265# Set other graph configurations here.
266theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face ='bold' ),
267 axis.text.x = element_text( angle = 0, size = 14 ),
268 legend.position = "bottom",
269 legend.text = element_text( size = 22 ),
270 legend.title = element_blank(),
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800271 legend.key.size = unit( 1.5, 'lines' ),
272 plot.subtitle = element_text( size=16, hjust=1.0 ) )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700273
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800274subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" )
275
276title <- labs( title = title, subtitle = subtitle )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000277
278# Store plot configurations as 1 variable
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700279fundamentalGraphData <- mainPlot +
280 plannedColor +
281 passedColor +
282 failedColor +
283 xScaleConfig +
284 yScaleConfig +
285 xLabel +
286 yLabel +
287 lineColors +
288 legendLabels +
289 theme +
290 title
291
292# ----------------------------
293# Generating Line Graph Format
294# ----------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000295
296print( "Generating line graph." )
297
298lineGraphFormat <- geom_line( size = 1.1 )
299pointFormat <- geom_point( size = 3 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000300
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700301result <- fundamentalGraphData +
302 lineGraphFormat +
303 pointFormat
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000304
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700305# -----------------------
306# Exporting Graph to File
307# -----------------------
308
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000309print( paste( "Saving result graph to", outputFile ) )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700310
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800311tryCatch( ggsave( outputFile,
312 width = imageWidth,
313 height = imageHeight,
314 dpi = imageDPI ),
315 error = function( e ){
316 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
317 print( e )
318 quit( status = 1 )
319 }
320 )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700321
322print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) )
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800323quit( status = 0 )