Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 1 | # 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 | |
| 29 | print( "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/ |
| 35 | print( "Importing libraries." ) |
| 36 | library( ggplot2 ) |
| 37 | library( reshape2 ) |
| 38 | library( RPostgreSQL ) |
| 39 | |
| 40 | # Command line arguments are read. Args include the database credentials, test name, branch name, and the directory to output files. |
| 41 | print( "Reading commmand-line args." ) |
| 42 | args <- commandArgs( trailingOnly=TRUE ) |
| 43 | |
| 44 | # Check if sufficient args are provided. |
| 45 | if ( 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 | |
| 52 | outputFile <- paste( args[ 10 ], "SCPF_Front_Page" , sep="" ) |
| 53 | outputFile <- paste( outputFile, gsub( " ", "_", args[ 5 ] ), sep="_" ) |
| 54 | outputFile <- paste( outputFile, args[ 6 ], sep="_" ) |
| 55 | outputFile <- paste( outputFile, args[ 7 ], sep="_" ) |
| 56 | outputFile <- paste( outputFile, "dates", sep="-" ) |
| 57 | outputFile <- paste( outputFile, "_graph.jpg", sep="" ) |
| 58 | |
| 59 | # From RPostgreSQL |
| 60 | print( "Reading from databases." ) |
| 61 | con <- dbConnect( dbDriver( "PostgreSQL" ), dbname="onostest", host=args[ 1 ], port=strtoi( args[ 2 ] ), user=args[ 3 ],password=args[ 4 ] ) |
| 62 | |
| 63 | print( "Sending SQL command." ) |
| 64 | fileData <- dbGetQuery( con, args[ 8 ] ) |
| 65 | |
| 66 | # Title of graph based on command line args. |
| 67 | title <- args[ 5 ] |
| 68 | |
| 69 | # ********************************************************** |
| 70 | # STEP 2: Organize data. |
| 71 | # ********************************************************** |
| 72 | |
| 73 | print( "STEP 2: Organize data." ) |
| 74 | |
| 75 | # Create lists c() and organize data into their corresponding list. |
| 76 | print( "Sorting data into new data frame." ) |
| 77 | |
| 78 | if ( 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. |
| 86 | dataFrame <- melt( fileData ) |
| 87 | |
| 88 | dataFrame$date <- fileData$date |
| 89 | |
| 90 | colnames( dataFrame ) <- c( "Legend", "Values" ) |
| 91 | |
| 92 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 93 | dataFrame$Legend <- as.character( dataFrame$Legend ) |
| 94 | dataFrame$Legend <- factor( dataFrame$Legend, levels=unique( dataFrame$Legend ) ) |
| 95 | |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 96 | # Adding a temporary iterative list to the dataFrame so that there are no gaps in-between date numbers. |
| 97 | dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) ) |
| 98 | |
| 99 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 100 | |
| 101 | print( "Data Frame Results:" ) |
| 102 | print( dataFrame ) |
| 103 | |
| 104 | # ********************************************************** |
| 105 | # STEP 3: Generate graphs. |
| 106 | # ********************************************************** |
| 107 | |
| 108 | print( "STEP 3: Generate graphs." ) |
| 109 | |
| 110 | print( "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 Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 118 | theme_set( theme_grey( base_size = 20 ) ) # set the default text size of the graph. |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 119 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, y = Values, color = Legend ) ) |
| 120 | |
| 121 | print( "Formatting main plot." ) |
| 122 | |
| 123 | # Store plot configurations as 1 variable |
| 124 | fundamentalGraphData <- mainPlot + expand_limits( y = 0 ) |
| 125 | |
| 126 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, max( dataFrame$Values ) * 1.05, by = ceiling( max( dataFrame$Values ) / 10 ) ) ) |
| 127 | |
| 128 | xLabel <- xlab( "Date" ) |
| 129 | yLabel <- ylab( args[ 9 ] ) |
| 130 | fillLabel <- labs( fill="Type" ) |
| 131 | legendLabels <- scale_colour_discrete( labels = names( fileData ) ) |
| 132 | centerTitle <- theme( plot.title=element_text( hjust = 0.5 ) ) # To center the title text |
Jeremy Ronquillo | 2d2649d | 2017-09-14 12:53:06 -0700 | [diff] [blame] | 133 | theme <- theme( axis.text.x = element_blank(), axis.ticks.x = element_blank(), plot.title = element_text( size = 28, face='bold' ) ) |
Jeremy Ronquillo | 6df8781 | 2017-08-28 16:17:36 +0000 | [diff] [blame] | 134 | |
| 135 | fundamentalGraphData <- fundamentalGraphData + yScaleConfig + xLabel + yLabel + fillLabel + legendLabels + centerTitle + theme |
| 136 | print( "Generating line graph." ) |
| 137 | |
| 138 | lineGraphFormat <- geom_line() |
| 139 | pointFormat <- geom_point( size = 0.2 ) |
| 140 | title <- ggtitle( title ) |
| 141 | |
| 142 | result <- fundamentalGraphData + lineGraphFormat + pointFormat + title |
| 143 | |
| 144 | # Save graph to file |
| 145 | print( paste( "Saving result graph to", outputFile ) ) |
| 146 | ggsave( outputFile, width = 10, height = 6, dpi = 200 ) |
| 147 | print( paste( "Successfully wrote result graph out to", outputFile ) ) |