blob: 4b1ead6b2015c5d10f6c0798b2f01ab7084fec78 [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,
21# please contact Jeremy Ronquillo: jeremyr@opennetworking.org
22
23# This is the R script that generates the SCPF front page graphs.
24
25# **********************************************************
26# STEP 1: Data management.
27# **********************************************************
28
29print( "STEP 1: Data management." )
30
31# Import libraries to be used for graphing and organizing data, respectively.
32# Find out more about ggplot2: https://github.com/tidyverse/ggplot2
33# reshape2: https://github.com/hadley/reshape
34# RPostgreSQL: https://code.google.com/archive/p/rpostgresql/
35print( "Importing libraries." )
36library( ggplot2 )
37library( reshape2 )
38library( RPostgreSQL )
39
40# Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files.
41print( "Reading commmand-line args." )
42args <- commandArgs( trailingOnly=TRUE )
43
44# Check if sufficient args are provided.
45if ( is.na( args[ 10 ] ) ){
46 print( "Usage: Rscript testresultgraph.R <database-host> <database-port> <database-user-id> <database-password> <test-name> <branch-name> <#-dates> <SQL-command> <y-axis> <directory-to-save-graph>" )
47 q() # basically exit(), but in R
48}
49
50# Filenames for the output graph include the testname, branch, and the graph type.
51
52outputFile <- paste( args[ 10 ], "SCPF_Front_Page" , sep="" )
53outputFile <- paste( outputFile, gsub( " ", "_", args[ 5 ] ), sep="_" )
54outputFile <- paste( outputFile, args[ 6 ], sep="_" )
55outputFile <- paste( outputFile, args[ 7 ], sep="_" )
56outputFile <- paste( outputFile, "dates", sep="-" )
57outputFile <- paste( outputFile, "_graph.jpg", sep="" )
58
59# From RPostgreSQL
60print( "Reading from databases." )
61con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] )
62
63print( "Sending SQL command." )
64fileData <- dbGetQuery( con, args[ 8 ] )
65
66# Title of graph based on command line args.
67title <- args[ 5 ]
68
69# **********************************************************
70# STEP 2: Organize data.
71# **********************************************************
72
73print( "STEP 2: Organize data." )
74
75# Create lists c() and organize data into their corresponding list.
76print( "Sorting data into new data frame." )
77
78if ( ncol( fileData ) > 1 ){
79 for ( i in 2:ncol( fileData ) ){
80 fileData[ i ] <- fileData[ i - 1 ] + fileData[ i ]
81 }
82}
83
84# Parse lists into data frames.
85# This is where reshape2 comes in. Avgs list is converted to data frame.
86dataFrame <- melt( fileData )
87
88dataFrame$date <- fileData$date
89
90colnames( dataFrame ) <- c( "Legend", "Values" )
91
92# Format data frame so that the data is in the same order as it appeared in the file.
93dataFrame$Legend <- as.character( dataFrame$Legend )
94dataFrame$Legend <- factor( dataFrame$Legend, levels=unique( dataFrame$Legend ) )
95
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -070096# Adding a temporary iterative list to the dataFrame so that there are no gaps in-between date numbers.
97dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) )
98
99dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000100
101print( "Data Frame Results:" )
102print( dataFrame )
103
104# **********************************************************
105# STEP 3: Generate graphs.
106# **********************************************************
107
108print( "STEP 3: Generate graphs." )
109
110print( "Creating main plot." )
111# Create the primary plot here.
112# ggplot contains the following arguments:
113# - data: the data frame that the graph will be based off of
114# - aes: the asthetics of the graph which require:
115# - x: x-axis values (usually iterative, but it will become date # later)
116# - y: y-axis values (usually tests)
117# - color: the category of the colored lines (usually legend of test)
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700118theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph.
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000119mainPlot <- ggplot( data = dataFrame, aes( x = iterative, y = Values, color = Legend ) )
120
121print( "Formatting main plot." )
122
123# Store plot configurations as 1 variable
124fundamentalGraphData <- mainPlot + expand_limits( y = 0 )
125
126yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$Values ) * 1.05, by = ceiling( max( dataFrame$Values ) / 10 ) ) )
127
128xLabel <- xlab( "Date" )
129yLabel <- ylab( args[ 9 ] )
130fillLabel <- labs( fill="Type" )
131legendLabels <- scale_colour_discrete( labels = names( fileData ) )
132centerTitle <- theme( plot.title=element_text( hjust = 0.5 ) ) # To center the title text
Jeremy Ronquillo2d2649d2017-09-14 12:53:06 -0700133theme <- theme( axis.text.x = element_blank(), axis.ticks.x = element_blank(), plot.title = element_text( size = 28, face='bold' ) )
Jeremy Ronquillo6df87812017-08-28 16:17:36 +0000134
135fundamentalGraphData <- fundamentalGraphData + yScaleConfig + xLabel + yLabel + fillLabel + legendLabels + centerTitle + theme
136print( "Generating line graph." )
137
138lineGraphFormat <- geom_line()
139pointFormat <- geom_point( size = 0.2 )
140title <- ggtitle( title )
141
142result <- fundamentalGraphData + lineGraphFormat + pointFormat + title
143
144# Save graph to file
145print( paste( "Saving result graph to", outputFile ) )
146ggsave( outputFile, width = 10, height = 6, dpi = 200 )
147print( paste( "Successfully wrote result graph out to", outputFile ) )