blob: 0b4d4b58239436c0e81d1f7edddfb006d0c4ec6a [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
23# **********************************************************
24# STEP 1: Data management.
25# **********************************************************
26
27print( "**********************************************************" )
28print( "STEP 1: Data management." )
29print( "**********************************************************" )
30
31# Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files.
32print( "Reading commmand-line args." )
33args <- commandArgs( trailingOnly=TRUE )
34
35databaseHost <- 1
36databasePort <- 2
37databaseUserID <- 3
38databasePassword <- 4
39testSuiteName <- 5
40branchName <- 6
41testsToInclude <- 7
42buildToShow <- 8
43displayStatus <- 9
44scaleOfPercent <- 10
45saveDirectory <- 11
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 "<build-to-show>",
73 "<pass/fail/plan>",
74 "<percent-scale>",
75 "<directory-to-save-graphs>",
76 sep=" " ) )
77
78 quit( status = 1 ) # basically exit(), but in R
79}
80
81# ------------------
82# SQL Initialization
83# ------------------
84
85print( "Initializing SQL" )
86
87con <- dbConnect( dbDriver( "PostgreSQL" ),
88 dbname = "onostest",
89 host = args[ databaseHost ],
90 port = strtoi( args[ databasePort ] ),
91 user = args[ databaseUserID ],
92 password = args[ databasePassword ] )
93
94# ---------------------
95# Test Case SQL Command
96# ---------------------
97print( "Generating Test Case SQL command." )
98
99tests <- "'"
100for ( test in as.list( strsplit( args[ testsToInclude ], "," )[[1]] ) ){
101 tests <- paste( tests, test, "','", sep="" )
102}
103tests <- substr( tests, 0, nchar( tests ) - 2 )
104
105fileBuildToShow <- args[ buildToShow ]
106operator <- "= "
107if ( args[ buildToShow ] == "latest" ){
108 operator <- ">= "
109 args[ buildToShow ] <- "1000"
110}
111
112command <- paste( "SELECT * ",
113 "FROM executed_test_tests a ",
114 "WHERE ( SELECT COUNT( * ) FROM executed_test_tests b ",
115 "WHERE b.branch='",
116 args[ branchName ],
117 "' AND b.actual_test_name IN (",
118 tests,
119 ") AND a.actual_test_name = b.actual_test_name AND a.date <= b.date AND b.build ", operator,
120 args[ buildToShow ],
121 " ) = ",
122 1,
123 " AND a.branch='",
124 args[ branchName ],
125 "' AND a.actual_test_name IN (",
126 tests,
127 ") AND a.build ", operator,
128 args[ buildToShow ],
129 " ORDER BY a.actual_test_name DESC, a.date DESC",
130 sep="")
131
132print( "Sending SQL command:" )
133print( command )
134dbResult <- dbGetQuery( con, command )
135
136maxBuild <- max( dbResult[ 'build' ] )
137dbResult <- dbResult[ which( dbResult[,4]>=maxBuild ), ]
138
139# -------------------------------
140# Create Title and Graph Filename
141# -------------------------------
142
143print( "Creating title of graph." )
144
145titleDisplayStatus <- ""
146if ( args[ displayStatus ] == "fail" ){
147 titleDisplayStatus <- "Failed"
148} else if ( args[ displayStatus ] == "plan" ){
149 titleDisplayStatus <- "Executed"
150} else if ( args[ displayStatus ] == "pass" ){
151 titleDisplayStatus <- "Succeeded"
152} else {
153 print( paste( "[ERROR]: Invalid histogram display status: ", args[ displayStatus ], sep="" ) )
154 quit( status = 1 )
155}
156
157title <- paste( args[ testSuiteName ],
158 " Tests ",
159 titleDisplayStatus,
160 " - ",
161 args[ branchName ],
162 " \n Build #",
163 max( dbResult[ 'build' ] ),
164 sep="" )
165
166print( "Creating graph filename." )
167
168outputFile <- paste( args[ saveDirectory ],
169 args[ testSuiteName ],
170 "_",
171 args[ branchName ],
172 "_build-",
173 fileBuildToShow,
174 "_",
175 args[ scaleOfPercent ],
176 "-scaling",
177 "_",
178 args[ displayStatus ],
179 "_histogram.jpg",
180 sep="" )
181
182print( dbResult )
183
184# **********************************************************
185# STEP 2: Organize data.
186# **********************************************************
187
188print( "**********************************************************" )
189print( "STEP 2: Organize Data." )
190print( "**********************************************************" )
191
192t <- subset( dbResult, select=c( "actual_test_name", "num_passed", "num_failed", "num_planned" ) )
193t$passed_percent <- t$num_passed / t$num_planned * 100
194t$failed_percent <- t$num_failed / t$num_planned * 100
195t$planned_percent <- ( t$num_passed + t$num_failed ) / t$num_planned * 100
196
197# --------------------
198# Construct Data Frame
199# --------------------
200
201dataFrame <- aggregate( t$passed_percent, by=list( Category=t$actual_test_name ), FUN=sum )
202if ( args[ displayStatus ] == "fail" ){
203 dataFrame <- aggregate( t$failed_percent, by=list( Category=t$actual_test_name ), FUN=sum )
204} else if ( args[ displayStatus ] == "plan" ){
205 dataFrame <- aggregate( t$planned_percent, by=list( Category=t$actual_test_name ), FUN=sum )
206}
207
208colnames( dataFrame ) <- c( "Test", paste( titleDisplayStatus, "%", sep="" ) )
209
210print( "Data Frame Results:" )
211print( dataFrame )
212
213# **********************************************************
214# STEP 3: Generate graphs.
215# **********************************************************
216
217print( "**********************************************************" )
218print( "STEP 3: Generate Graph." )
219print( "**********************************************************" )
220
221# -------------------
222# Main Plot Generated
223# -------------------
224
225print( "Creating main plot." )
226# Create the primary plot here.
227# ggplot contains the following arguments:
228# - data: the data frame that the graph will be based off of
229# - aes: the asthetics of the graph which require:
230# - x: x-axis values (usually iterative, but it will become build # later)
231# - y: y-axis values (usually tests)
232# - color: the category of the colored lines (usually status of test)
233
234mainPlot <- ggplot( data = dataFrame, aes( dataFrame[ ,2 ] ) )
235
236# -------------------
237# Main Plot Formatted
238# -------------------
239
240print( "Formatting main plot." )
241
242# ------------------------------
243# Fundamental Variables Assigned
244# ------------------------------
245
246print( "Generating fundamental graph data." )
247
248theme_set( theme_grey( base_size = 26 ) ) # set the default text size of the graph.
249
250xScaleConfig <- scale_x_continuous( breaks = seq( 0, 100, by = 10 ) )
251yScaleConfig <- scale_y_continuous( breaks = seq( 0, nrow( dbResult ), by = 1 ), limits = c( 0, nrow( dbResult ) ) )
252
253xLabel <- xlab( paste( titleDisplayStatus, "%" ) )
254yLabel <- ylab( "Frequency" )
255
256imageWidth <- 15
257imageHeight <- 10
258imageDPI <- 200
259
260# Set other graph configurations here.
261theme <- theme( plot.title = element_text( hjust = 0.5, size = 32, face ='bold' ),
262 axis.text.x = element_text( angle = 0, size = 14 ),
263 legend.position = "bottom",
264 legend.text = element_text( size = 22 ),
265 legend.title = element_blank(),
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800266 legend.key.size = unit( 1.5, 'lines' ),
267 plot.subtitle = element_text( size=16, hjust=1.0 ) )
Jeremy Ronquillo908cb442017-12-07 08:58:09 -0800268
Jeremy Ronquillo94f99dd2018-01-05 11:11:27 -0800269subtitle <- paste( "Last Updated: ", format( Sys.time(), format = "%b %d, %Y at %I:%M %p %Z" ), sep="" )
270
271title <- labs( title = title, subtitle = subtitle )
Jeremy Ronquillo908cb442017-12-07 08:58:09 -0800272
273# Store plot configurations as 1 variable
274fundamentalGraphData <- mainPlot +
275 xScaleConfig +
276 yScaleConfig +
277 xLabel +
278 yLabel +
279 theme +
280 title
281
282# ----------------------------
283# Generating Line Graph Format
284# ----------------------------
285
286print( "Generating line graph." )
287
288barColor <- "#00B208"
289if ( args[ displayStatus ] == "fail" ){
290 barColor <- "#E80000"
291} else if ( args[ displayStatus ] == "plan" ){
292 barColor <- "#00A5FF"
293}
294
295histogramFormat <- geom_histogram( col = "#000000",
296 fill = barColor,
297 breaks = seq( 0, 100, by = strtoi( args[ scaleOfPercent ] ) ),
298 lwd = 0.5 )
299
300result <- fundamentalGraphData +
301 histogramFormat
302
303# -----------------------
304# Exporting Graph to File
305# -----------------------
306
307print( paste( "Saving result graph to", outputFile ) )
308
309tryCatch( ggsave( outputFile,
310 width = imageWidth,
311 height = imageHeight,
312 dpi = imageDPI ),
313 error = function( e ){
314 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
315 print( e )
316 quit( status = 1 )
317 }
318 )
319
320print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) )
321quit( status = 0 )