blob: d063d0ab7729f28e7432e9fd197524e532de94d8 [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
23# This is the R script that generates the SCPF front page graphs.
24
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070025
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000026# **********************************************************
27# STEP 1: Data management.
28# **********************************************************
29
Devin Lim0e967162017-11-03 15:59:53 -070030database_host = 1
31database_port = 2
32database_u_id = 3
33database_pw = 4
34graph_title = 5
35branch_name = 6
36num_dates = 7
37sql_commands = 8
38y_axis = 9
39old_flow = 10
40save_directory = 11
41
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070042print( "**********************************************************" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000043print( "STEP 1: Data management." )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070044print( "**********************************************************" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000045
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070046# ----------------
47# Import Libraries
48# ----------------
49
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000050print( "Importing libraries." )
51library( ggplot2 )
52library( reshape2 )
53library( RPostgreSQL )
54
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070055# -------------------
56# Check CLI Arguments
57# -------------------
58
59print( "Verifying CLI args." )
60
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000061# Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files.
62print( "Reading commmand-line args." )
63args <- commandArgs( trailingOnly=TRUE )
64
65# Check if sufficient args are provided.
Devin Lim0e967162017-11-03 15:59:53 -070066if ( is.na( args[ save_directory ] ) ){
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070067
68 print( paste( "Usage: Rscript testresultgraph.R",
69 "<database-host>",
70 "<database-port>",
71 "<database-user-id>",
72 "<database-password>",
73 "<graph-title>", # part of the output filename as well
74 "<branch-name>", # part of the output filename
75 "<#-dates>", # part of the output filename
76 "<SQL-command>",
77 "<y-axis-title>", # y-axis may be different among other SCPF graphs (ie: batch size, latency, etc. )
Devin Lim0e967162017-11-03 15:59:53 -070078 "<using-old-flow>",
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070079 "<directory-to-save-graph>",
80 sep = " " ) )
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -080081 quit( status = 1 ) # basically exit(), but in R
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000082}
83
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070084# -------------------------------
85# Create Title and Graph Filename
86# -------------------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000087
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070088print( "Creating title of graph" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000089
90# Title of graph based on command line args.
Devin Lim0e967162017-11-03 15:59:53 -070091
92title <- args[ graph_title ]
Devin Lim1bba7622017-11-14 16:31:41 -080093title <- paste( title, if( args[ old_flow ] == "y" ) "\nWith Eventually Consistent Flow Rule Store" else "" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +000094
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070095print( "Creating graph filename." )
96
97# Filenames for the output graph include the testname, branch, and the graph type.
Devin Lim0e967162017-11-03 15:59:53 -070098outputFile <- paste( args[ save_directory ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -070099 "SCPF_Front_Page_",
Devin Lim0e967162017-11-03 15:59:53 -0700100 gsub( " ", "_", args[ graph_title ] ),
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700101 "_",
Devin Lim0e967162017-11-03 15:59:53 -0700102 args[ branch_name ],
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700103 "_",
Devin Lim0e967162017-11-03 15:59:53 -0700104 args[ num_dates ],
105 "-dates",
106 if( args[ old_flow ] == "y" ) "_OldFlow" else "",
107 "_graph.jpg",
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700108 sep="" )
109
110# ------------------
111# SQL Initialization
112# ------------------
113
114print( "Initializing SQL" )
115con <- dbConnect( dbDriver( "PostgreSQL" ),
116 dbname = "onostest",
Devin Lim0e967162017-11-03 15:59:53 -0700117 host = args[ database_host ],
118 port = strtoi( args[ database_port ] ),
119 user = args[ database_u_id ],
120 password = args[ database_pw ] )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700121
122print( "Sending SQL command:" )
Devin Lim0e967162017-11-03 15:59:53 -0700123print( args[ sql_commands ] )
Jeremy Ronquillo9d867f42017-11-28 14:31:18 -0800124
Devin Lim0e967162017-11-03 15:59:53 -0700125fileData <- dbGetQuery( con, args[ sql_commands ] )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700126
Jeremy Ronquillo9d867f42017-11-28 14:31:18 -0800127# Check if data has been received
128if ( nrow( fileData ) == 0 ){
129 print( "[ERROR]: No data received from the databases. Please double check this by manually running the SQL command." )
130 quit( status = 1 )
131}
132
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000133# **********************************************************
134# STEP 2: Organize data.
135# **********************************************************
136
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700137print( "**********************************************************" )
138print( "STEP 2: Organize Data." )
139print( "**********************************************************" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000140
141# Create lists c() and organize data into their corresponding list.
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700142print( "Combine data retrieved from databases into a list." )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000143
Jeremy Ronquillo9d867f42017-11-28 14:31:18 -0800144if ( ncol( fileData ) > 1 ){
145 for ( i in 2:ncol( fileData ) ){
146 fileData[ i ] <- fileData[ i - 1 ] + fileData[ i ]
147 }
148}
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800149
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000150
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700151# --------------------
152# Construct Data Frame
153# --------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000154
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700155print( "Constructing data frame from combined data." )
156
157dataFrame <- melt( fileData )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000158dataFrame$date <- fileData$date
159
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700160colnames( dataFrame ) <- c( "Legend",
161 "Values" )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000162
163# Format data frame so that the data is in the same order as it appeared in the file.
164dataFrame$Legend <- as.character( dataFrame$Legend )
165dataFrame$Legend <- factor( dataFrame$Legend, levels=unique( dataFrame$Legend ) )
166
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700167# Adding a temporary iterative list to the dataFrame so that there are no gaps in-between date numbers.
168dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) )
169
170dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000171
172print( "Data Frame Results:" )
173print( dataFrame )
174
175# **********************************************************
176# STEP 3: Generate graphs.
177# **********************************************************
178
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700179print( "**********************************************************" )
180print( "STEP 3: Generate Graph." )
181print( "**********************************************************" )
182
183# -------------------
184# Main Plot Generated
185# -------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000186
187print( "Creating main plot." )
188# Create the primary plot here.
189# ggplot contains the following arguments:
190# - data: the data frame that the graph will be based off of
191# - aes: the asthetics of the graph which require:
192# - x: x-axis values (usually iterative, but it will become date # later)
193# - y: y-axis values (usually tests)
194# - color: the category of the colored lines (usually legend of test)
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700195
196mainPlot <- ggplot( data = dataFrame, aes( x = iterative,
197 y = Values,
198 color = Legend ) )
199
200# -------------------
201# Main Plot Formatted
202# -------------------
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000203
204print( "Formatting main plot." )
205
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700206limitExpansion <- expand_limits( y = 0 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000207
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700208maxYDisplay <- max( dataFrame$Values ) * 1.05
209yBreaks <- ceiling( max( dataFrame$Values ) / 10 )
210yScaleConfig <- scale_y_continuous( breaks = seq( 0, maxYDisplay, by = yBreaks ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000211
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700212# ------------------------------
213# Fundamental Variables Assigned
214# ------------------------------
215
216print( "Generating fundamental graph data." )
217
218theme_set( theme_grey( base_size = 22 ) ) # set the default text size of the graph.
219xLabel <- xlab( "Build" )
Devin Lim0e967162017-11-03 15:59:53 -0700220yLabel <- ylab( args[ y_axis ] )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000221
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700222imageWidth <- 15
223imageHeight <- 10
224imageDPI <- 200
225
226# Set other graph configurations here.
227theme <- theme( axis.text.x = element_blank(),
228 axis.ticks.x = element_blank(),
229 plot.title = element_text( size = 32, face='bold', hjust = 0.5 ),
230 legend.position = "bottom",
231 legend.text = element_text( size=22 ),
232 legend.title = element_blank(),
233 legend.key.size = unit( 1.5, 'lines' ),
234 legend.direction = 'horizontal' )
235
236# Colors used for the lines.
237# Note: graphs that have X lines will use the first X colors in this list.
238colors <- scale_color_manual( values=c( "#111111", # black
239 "#008CFF", # blue
240 "#FF3700", # red
241 "#00E043", # green
242 "#EEB600", # yellow
243 "#E500FF") ) # purple (not used)
244
245wrapLegend <- guides( color = guide_legend( nrow = 2, byrow = TRUE ) )
246title <- ggtitle( title )
247
248fundamentalGraphData <- mainPlot +
249 limitExpansion +
250 yScaleConfig +
251 xLabel +
252 yLabel +
253 theme +
254 colors +
255 wrapLegend +
256 title
257
258# ----------------------------
259# Generating Line Graph Format
260# ----------------------------
261
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000262print( "Generating line graph." )
263
Jeremy Ronquilloe9063762017-10-17 15:36:09 -0700264lineGraphFormat <- geom_line( size = 0.75 )
265pointFormat <- geom_point( size = 1.75 )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000266
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700267result <- fundamentalGraphData +
268 lineGraphFormat +
269 pointFormat
Jeremy Ronquillo0e27b912017-10-21 14:34:24 -0700270
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700271# -----------------------
272# Exporting Graph to File
273# -----------------------
274
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000275print( paste( "Saving result graph to", outputFile ) )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700276
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800277tryCatch( ggsave( outputFile,
278 width = imageWidth,
279 height = imageHeight,
280 dpi = imageDPI ),
281 error = function( e ){
282 print( "[ERROR] There was a problem saving the graph due to a graph formatting exception. Error dump:" )
283 print( e )
284 quit( status = 1 )
285 }
286 )
Jeremy Ronquillo7673f802017-10-30 09:42:44 -0700287
288print( paste( "[SUCCESS] Successfully wrote result graph out to", outputFile ) )
Jeremy Ronquillo9ea85d02017-11-27 10:21:03 -0800289quit( status = 0 )