blob: 395823dfc6fdbb36cee77e0530e4f307b57e508e [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
29print( "STEP 1: Data management." )
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
35# Import libraries to be used for graphing and organizing data, respectively.
36# Find out more about ggplot2: https://github.com/tidyverse/ggplot2
37# reshape2: https://github.com/hadley/reshape
38# RPostgreSQL: https://code.google.com/archive/p/rpostgresql/
39print( "Importing libraries." )
40library( ggplot2 )
41library( reshape2 )
42library( RPostgreSQL )
43
44# Check if sufficient args are provided.
45if ( is.na( args[ 8 ] ) ){
46 print( "Usage: Rscript testCaseGraphGenerator.R <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <#-builds-to-show> <directory-to-save-graphs>" )
47 q() # basically exit(), but in R
48}
49
50# Filenames for the output graph include the testname, branch, and the graph type.
51outputFile <- paste( args[ 8 ], args[ 5 ], sep="" )
52outputFile <- paste( outputFile, args[ 6 ], sep="_" )
53outputFile <- paste( outputFile, args[ 7 ], sep="_" )
54outputFile <- paste( outputFile, "builds", sep="-" )
55outputFile <- paste( outputFile, "_graph.jpg", sep="" )
56
57# From RPostgreSQL
58print( "Reading from databases." )
59con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] )
60
61print( "Creating SQL command." )
62# Creating SQL command based on command line args.
63command <- paste( "SELECT * FROM executed_test_tests WHERE actual_test_name='", args[ 5 ], sep="" )
64command <- paste( command, "' AND branch='", sep="" )
65command <- paste( command, args[ 6 ], sep="" )
66command <- paste( command, "' ORDER BY date DESC LIMIT ", sep="" )
67command <- paste( command, args[ 7 ], sep="" )
68fileData <- dbGetQuery( con, command )
69
70# Title of graph based on command line args.
71title <- paste( args[ 5 ], args[ 6 ], sep=" - " )
72title <- paste( title, "Results of Last ", sep=" \n " )
73title <- paste( title, args[ 7 ], sep="" )
74title <- paste( title, " Builds", sep="" )
75
76# **********************************************************
77# STEP 2: Organize data.
78# **********************************************************
79
80print( "STEP 2: Organize data." )
81
82# Create lists c() and organize data into their corresponding list.
83print( "Sorting data into new data frame." )
84categories <- c( fileData[ 'num_failed' ], fileData[ 'num_passed' ], fileData[ 'num_planned' ] )
85
86# Parse lists into data frames.
87# This is where reshape2 comes in. Avgs list is converted to data frame.
88dataFrame <- melt( categories )
89dataFrame$build <- fileData$build
90colnames( dataFrame ) <- c( "Tests", "Status", "Build" )
91
92# Format data frame so that the data is in the same order as it appeared in the file.
93dataFrame$Status <- as.character( dataFrame$Status )
94dataFrame$Status <- factor( dataFrame$Status, levels=unique( dataFrame$Status ) )
95
96# Add planned, passed, and failed results to the dataFrame (for the fill below the lines)
97dataFrame$num_planned <- fileData$num_planned
98dataFrame$num_passed <- fileData$num_passed
99dataFrame$num_failed <- fileData$num_failed
100
101# Adding a temporary reversed iterative list to the dataFrame so that there are no gaps in-between build numbers.
102dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) )
103
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700104dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist
105
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000106print( "Data Frame Results:" )
107print( dataFrame )
108
109# **********************************************************
110# STEP 3: Generate graphs.
111# **********************************************************
112
113print( "STEP 3: Generate graphs." )
114
115print( "Creating main plot." )
116# Create the primary plot here.
117# ggplot contains the following arguments:
118# - data: the data frame that the graph will be based off of
119# - aes: the asthetics of the graph which require:
120# - x: x-axis values (usually iterative, but it will become build # later)
121# - y: y-axis values (usually tests)
122# - color: the category of the colored lines (usually status of test)
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700123theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph.
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000124mainPlot <- ggplot( data = dataFrame, aes( x = iterative, y = Tests, color = Status ) )
125
126print( "Formatting main plot." )
127# geom_ribbon is used so that there is a colored fill below the lines. These values shouldn't be changed.
128failedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_failed ), fill = "red", linetype = 0, alpha = 0.07 )
129passedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_passed ), fill = "green", linetype = 0, alpha = 0.05 )
130plannedColor <- geom_ribbon( aes( ymin = 0, ymax = dataFrame$num_planned ), fill = "blue", linetype = 0, alpha = 0.01 )
131
132xScaleConfig <- scale_x_continuous( breaks = dataFrame$iterative, label = dataFrame$Build )
133yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$Tests ), by = ceiling( max( dataFrame$Tests ) / 10 ) ) )
134
135xLabel <- xlab( "Build Number" )
136yLabel <- ylab( "Test Cases" )
137fillLabel <- labs( fill="Type" )
138legendLabels <- scale_colour_discrete( labels = c( "Failed", "Passed", "Planned" ) )
139centerTitle <- theme( plot.title=element_text( hjust = 0.5 ) ) # To center the title text
Jeremy Ronquillo542433f2017-09-22 08:50:54 -0700140theme <- theme( plot.title = element_text( size = 28, face='bold' ), axis.text.x = element_text( angle = 45, size = 10 ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000141
142# Store plot configurations as 1 variable
143fundamentalGraphData <- mainPlot + plannedColor + passedColor + failedColor + xScaleConfig + yScaleConfig + xLabel + yLabel + fillLabel + legendLabels + centerTitle + theme
144
145print( "Generating line graph." )
146
147lineGraphFormat <- geom_line( size = 1.1 )
148pointFormat <- geom_point( size = 3 )
149title <- ggtitle( title )
150
151result <- fundamentalGraphData + lineGraphFormat + pointFormat + title
152
153# Save graph to file
154print( paste( "Saving result graph to", outputFile ) )
155ggsave( outputFile, width = 10, height = 6, dpi = 200 )
156print( paste( "Successfully wrote result graph out to", outputFile ) )