blob: c0211bc5640009d66865f5b2c2f9d4116248e79d [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(),
266 legend.key.size = unit( 1.5, 'lines' ) )
267
268title <- ggtitle( title )
269
270# Store plot configurations as 1 variable
271fundamentalGraphData <- mainPlot +
272 xScaleConfig +
273 yScaleConfig +
274 xLabel +
275 yLabel +
276 theme +
277 title
278
279# ----------------------------
280# Generating Line Graph Format
281# ----------------------------
282
283print( "Generating line graph." )
284
285barColor <- "#00B208"
286if ( args[ displayStatus ] == "fail" ){
287 barColor <- "#E80000"
288} else if ( args[ displayStatus ] == "plan" ){
289 barColor <- "#00A5FF"
290}
291
292histogramFormat <- geom_histogram( col = "#000000",
293 fill = barColor,
294 breaks = seq( 0, 100, by = strtoi( args[ scaleOfPercent ] ) ),
295 lwd = 0.5 )
296
297result <- fundamentalGraphData +
298 histogramFormat
299
300# -----------------------
301# Exporting Graph to File
302# -----------------------
303
304print( paste( "Saving result graph to", outputFile ) )
305
306tryCatch( ggsave( outputFile,
307 width = imageWidth,
308 height = imageHeight,
309 dpi = imageDPI ),
310 error = function( e ){
311 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
312 print( e )
313 quit( status = 1 )
314 }
315 )
316
317print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) )
318quit( status = 0 )