blob: 33664b0eb5524cbc08334a5d4857b496aa93989d [file] [log] [blame]
Jeremy Ronquillo908cb442017-12-07 08:58:09 -08001# 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
23pipelineMinValue = 1000
24
25# **********************************************************
26# STEP 1: Data management.
27# **********************************************************
28
29print( "**********************************************************" )
30print( "STEP 1: Data management." )
31print( "**********************************************************" )
32
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
37databaseHost <- 1
38databasePort <- 2
39databaseUserID <- 3
40databasePassword <- 4
41testSuiteName <- 5
42branchName <- 6
43testsToInclude <- 7
44buildsToShow <- 8
45saveDirectory <- 9
46
47# ----------------
48# Import Libraries
49# ----------------
50
51print( "Importing libraries." )
52library( ggplot2 )
53library( reshape2 )
54library( RPostgreSQL )
55
56# -------------------
57# Check CLI Arguments
58# -------------------
59
60print( "Verifying CLI args." )
61
62if ( 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 "<builds-to-show>",
73 "<directory-to-save-graphs>",
74 sep=" " ) )
75
76 quit( status = 1 ) # basically exit(), but in R
77}
78
79# -------------------------------
80# Create Title and Graph Filename
81# -------------------------------
82
83print( "Creating title of graph." )
84
85title <- paste( args[ testSuiteName ],
86 " Test Results Trend - ",
87 args[ branchName ],
88 " \n Results of Last ",
89 args[ buildsToShow ],
90 " Nightly Builds",
91 sep="" )
92
93print( "Creating graph filename." )
94
95outputFile <- paste( args[ saveDirectory ],
96 args[ testSuiteName ],
97 "_",
98 args[ branchName ],
99 "_overview.jpg",
100 sep="" )
101
102# ------------------
103# SQL Initialization
104# ------------------
105
106print( "Initializing SQL" )
107
108con <- dbConnect( dbDriver( "PostgreSQL" ),
109 dbname = "onostest",
110 host = args[ databaseHost ],
111 port = strtoi( args[ databasePort ] ),
112 user = args[ databaseUserID ],
113 password = args[ databasePassword ] )
114
115# ---------------------
116# Test Case SQL Command
117# ---------------------
118print( "Generating Test Case SQL command." )
119
120tests <- "'"
121for ( test in as.list( strsplit( args[ testsToInclude ], "," )[[1]] ) ){
122 tests <- paste( tests, test, "','", sep="" )
123}
124tests <- substr( tests, 0, nchar( tests ) - 2 )
125
126command <- paste( "SELECT * ",
127 "FROM executed_test_tests a ",
128 "WHERE ( SELECT COUNT( * ) FROM executed_test_tests b ",
129 "WHERE b.branch='",
130 args[ branchName ],
131 "' AND b.actual_test_name IN (",
132 tests,
133 ") AND a.actual_test_name = b.actual_test_name AND a.date <= b.date AND b.build >= ",
134 pipelineMinValue,
135 " ) <= ",
136 args[ buildsToShow ],
137 " AND a.branch='",
138 args[ branchName ],
139 "' AND a.actual_test_name IN (",
140 tests,
141 ") AND a.build >= ",
142 pipelineMinValue,
143 " ORDER BY a.actual_test_name DESC, a.date DESC",
144 sep="")
145
146print( "Sending SQL command:" )
147print( command )
148dbResult <- dbGetQuery( con, command )
149maxBuild <- max( dbResult[ 'build' ] ) - strtoi( args[ buildsToShow ] )
150dbResult <- dbResult[ which( dbResult[,4]>maxBuild ), ]
151print( dbResult )
152
153# **********************************************************
154# STEP 2: Organize data.
155# **********************************************************
156
157print( "**********************************************************" )
158print( "STEP 2: Organize Data." )
159print( "**********************************************************" )
160
161t <- subset( dbResult, select=c( "actual_test_name", "build", "num_failed" ) )
162t$num_failed <- ceiling( t$num_failed / ( t$num_failed + 1 ) )
163t$num_planned <- 1
164
165fileData <- aggregate( t$num_failed, by=list( Category=t$build ), FUN=sum )
166colnames( fileData ) <- c( "build", "num_failed" )
167
168fileData$num_planned <- ( aggregate( t$num_planned, by=list( Category=t$build ), FUN=sum ) )$x
169fileData$num_passed <- fileData$num_planned - fileData$num_failed
170
171print(fileData)
172
173# --------------------
174# Construct Data Frame
175# --------------------
176#
177
178dataFrame <- melt( subset( fileData, select=c( "num_failed", "num_passed", "num_planned" ) ) )
179dataFrame$build <- fileData$build
180colnames( dataFrame ) <- c( "status", "results", "build" )
181
182dataFrame$num_failed <- fileData$num_failed
183dataFrame$num_passed <- fileData$num_passed
184dataFrame$num_planned <- fileData$num_planned
185dataFrame$iterative <- seq( 1, nrow( fileData ), by = 1 )
186
187print( "Data Frame Results:" )
188print( dataFrame )
189
190# **********************************************************
191# STEP 3: Generate graphs.
192# **********************************************************
193
194print( "**********************************************************" )
195print( "STEP 3: Generate Graph." )
196print( "**********************************************************" )
197
198# -------------------
199# Main Plot Generated
200# -------------------
201
202print( "Creating main plot." )
203# Create the primary plot here.
204# ggplot contains the following arguments:
205# - data: the data frame that the graph will be based off of
206# - aes: the asthetics of the graph which require:
207# - x: x-axis values (usually iterative, but it will become build # later)
208# - y: y-axis values (usually tests)
209# - color: the category of the colored lines (usually status of test)
210
211mainPlot <- ggplot( data = dataFrame, aes( x = iterative,
212 y = results,
213 color = status ) )
214
215# -------------------
216# Main Plot Formatted
217# -------------------
218
219print( "Formatting main plot." )
220
221# geom_ribbon is used so that there is a colored fill below the lines. These values shouldn't be changed.
222failedColor <- geom_ribbon( aes( ymin = 0,
223 ymax = dataFrame$num_failed ),
224 fill = "#ff0000",
225 linetype = 0,
226 alpha = 0.07 )
227
228passedColor <- geom_ribbon( aes( ymin = 0,
229 ymax = dataFrame$num_passed ),
230 fill = "#0083ff",
231 linetype = 0,
232 alpha = 0.05 )
233
234plannedColor <- geom_ribbon( aes( ymin = 0,
235 ymax = dataFrame$num_planned ),
236 fill = "#000000",
237 linetype = 0,
238 alpha = 0.01 )
239
240# Colors for the lines
241lineColors <- scale_color_manual( values=c( "#ff0000", # fail
242 "#0083ff", # pass
243 "#000000"),
244 labels = c( "Containing Failures",
245 "No Failures",
246 "Total Built" ) ) # planned
247
248# ------------------------------
249# Fundamental Variables Assigned
250# ------------------------------
251
252print( "Generating fundamental graph data." )
253
254theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph.
255
256xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative,
257 label = dataFrame$build )
258yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$results ),
259 by = ceiling( max( dataFrame$results ) / 10 ) ) )
260
261xLabel <- xlab( "Build Number" )
262yLabel <- ylab( "Tests" )
263
264imageWidth <- 15
265imageHeight <- 10
266imageDPI <- 200
267
268# Set other graph configurations here.
269theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face ='bold' ),
270 axis.text.x = element_text( angle = 0, size = 14 ),
271 legend.position = "bottom",
272 legend.text = element_text( size = 22 ),
273 legend.title = element_blank(),
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800274 legend.key.size = unit( 1.5, 'lines' ),
275 plot.subtitle = element_text( size=16, hjust=1.0 ) )
Jeremy Ronquillo908cb442017-12-07 08:58:09 -0800276
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800277subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" )
278
279title <- labs( title = title, subtitle = subtitle )
Jeremy Ronquillo908cb442017-12-07 08:58:09 -0800280
281# Store plot configurations as 1 variable
282fundamentalGraphData <- mainPlot +
283 plannedColor +
284 passedColor +
285 failedColor +
286 xScaleConfig +
287 yScaleConfig +
288 xLabel +
289 yLabel +
290 theme +
291 title +
292 lineColors
293
294# ----------------------------
295# Generating Line Graph Format
296# ----------------------------
297
298print( "Generating line graph." )
299
300lineGraphFormat <- geom_line( size = 1.1 )
301pointFormat <- geom_point( size = 3 )
302
303result <- fundamentalGraphData +
304 lineGraphFormat +
305 pointFormat
306
307# -----------------------
308# Exporting Graph to File
309# -----------------------
310
311print( paste( "Saving result graph to", outputFile ) )
312
313tryCatch( ggsave( outputFile,
314 width = imageWidth,
315 height = imageHeight,
316 dpi = imageDPI ),
317 error = function( e ){
318 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
319 print( e )
320 quit( status = 1 )
321 }
322 )
323
324print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) )
325quit( status = 0 )