Jeremy Ronquillo | dae1104 | 2018-02-21 09:21:44 -0800 | [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: j_ronquillo@u.pacific.edu |
| 22 | |
| 23 | # This is the R script that generates the SCPF front page graphs. |
| 24 | |
| 25 | |
| 26 | # ********************************************************** |
| 27 | # STEP 1: Data management. |
| 28 | # ********************************************************** |
| 29 | |
| 30 | # Args 1 through 6 reside in fundamentalGraphData.R |
| 31 | num_dates = 7 |
| 32 | sql_commands = 8 |
| 33 | y_axis = 9 |
| 34 | old_flow = 10 |
| 35 | save_directory = 11 |
| 36 | |
| 37 | print( "**********************************************************" ) |
| 38 | print( "STEP 1: Data management." ) |
| 39 | print( "**********************************************************" ) |
| 40 | |
| 41 | # ---------------- |
| 42 | # Import Libraries |
| 43 | # ---------------- |
| 44 | |
| 45 | print( "Importing libraries." ) |
| 46 | library( ggplot2 ) |
| 47 | library( reshape2 ) |
| 48 | library( RPostgreSQL ) |
Devin Lim | 324806b | 2018-05-11 15:36:52 -0700 | [diff] [blame] | 49 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/saveGraph.R" ) |
| 50 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/fundamentalGraphData.R" ) |
| 51 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/initSQL.R" ) |
| 52 | source( "~/OnosSystemTest/TestON/JenkinsFile/wikiGraphRScripts/dependencies/cliArgs.R" ) |
Jeremy Ronquillo | dae1104 | 2018-02-21 09:21:44 -0800 | [diff] [blame] | 53 | |
| 54 | # ------------------- |
| 55 | # Check CLI Arguments |
| 56 | # ------------------- |
| 57 | |
| 58 | print( "Verifying CLI args." ) |
| 59 | |
| 60 | args <- commandArgs( trailingOnly=TRUE ) |
| 61 | |
| 62 | # Check if sufficient args are provided. |
| 63 | if ( length( args ) != save_directory ){ |
| 64 | specialArgs <- c( "#-dates", |
| 65 | "SQL-command", |
| 66 | "y-axis-title", |
| 67 | "using-old-flow" ) |
| 68 | usage( "trendSCPF.R", specialArgs ) |
| 69 | quit( status = 1 ) |
| 70 | } |
| 71 | |
| 72 | # ------------------------------- |
| 73 | # Create Title and Graph Filename |
| 74 | # ------------------------------- |
| 75 | |
| 76 | print( "Creating title of graph" ) |
| 77 | |
| 78 | # Title of graph based on command line args. |
| 79 | |
| 80 | title <- args[ graph_title ] |
| 81 | title <- paste( title, if( args[ old_flow ] == "y" ) "\nWith Eventually Consistent Flow Rule Store" else "" ) |
| 82 | |
| 83 | print( "Creating graph filename." ) |
| 84 | |
| 85 | # Filenames for the output graph include the testname, branch, and the graph type. |
| 86 | outputFile <- paste( args[ save_directory ], |
| 87 | "SCPF_Front_Page_", |
| 88 | gsub( " ", "_", args[ graph_title ] ), |
| 89 | "_", |
| 90 | args[ branch_name ], |
| 91 | "_", |
| 92 | args[ num_dates ], |
| 93 | "-dates", |
| 94 | if( args[ old_flow ] == "y" ) "_OldFlow" else "", |
| 95 | "_graph.jpg", |
| 96 | sep="" ) |
| 97 | |
| 98 | # ------------------ |
| 99 | # SQL Initialization |
| 100 | # ------------------ |
| 101 | |
| 102 | print( "Initializing SQL" ) |
| 103 | con <- initSQL( args[ database_host ], args[ database_port ], args[ database_u_id ], args[ database_pw ] ) |
| 104 | |
| 105 | fileData <- retrieveData( con, args[ sql_commands ] ) |
| 106 | |
| 107 | # ********************************************************** |
| 108 | # STEP 2: Organize data. |
| 109 | # ********************************************************** |
| 110 | |
| 111 | print( "**********************************************************" ) |
| 112 | print( "STEP 2: Organize Data." ) |
| 113 | print( "**********************************************************" ) |
| 114 | |
| 115 | # Create lists c() and organize data into their corresponding list. |
| 116 | print( "Combine data retrieved from databases into a list." ) |
| 117 | |
| 118 | buildNums <- fileData$build |
| 119 | fileData$build <- c() |
| 120 | print( fileData ) |
| 121 | |
| 122 | if ( ncol( fileData ) > 1 ){ |
| 123 | for ( i in 2:ncol( fileData ) ){ |
| 124 | fileData[ i ] <- fileData[ i - 1 ] + fileData[ i ] |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | # -------------------- |
| 130 | # Construct Data Frame |
| 131 | # -------------------- |
| 132 | |
| 133 | print( "Constructing data frame from combined data." ) |
| 134 | |
| 135 | dataFrame <- melt( fileData ) |
| 136 | dataFrame$date <- fileData$date |
| 137 | |
| 138 | colnames( dataFrame ) <- c( "Legend", |
| 139 | "Values" ) |
| 140 | |
| 141 | # Format data frame so that the data is in the same order as it appeared in the file. |
| 142 | dataFrame$Legend <- as.character( dataFrame$Legend ) |
| 143 | dataFrame$Legend <- factor( dataFrame$Legend, levels=unique( dataFrame$Legend ) ) |
| 144 | dataFrame$build <- buildNums |
| 145 | |
| 146 | # Adding a temporary iterative list to the dataFrame so that there are no gaps in-between date numbers. |
| 147 | dataFrame$iterative <- rev( seq( 1, nrow( fileData ), by = 1 ) ) |
| 148 | |
| 149 | dataFrame <- na.omit( dataFrame ) # Omit any data that doesn't exist |
| 150 | |
| 151 | print( "Data Frame Results:" ) |
| 152 | print( dataFrame ) |
| 153 | |
| 154 | # ********************************************************** |
| 155 | # STEP 3: Generate graphs. |
| 156 | # ********************************************************** |
| 157 | |
| 158 | print( "**********************************************************" ) |
| 159 | print( "STEP 3: Generate Graph." ) |
| 160 | print( "**********************************************************" ) |
| 161 | |
| 162 | # ------------------- |
| 163 | # Main Plot Generated |
| 164 | # ------------------- |
| 165 | |
| 166 | print( "Creating main plot." ) |
| 167 | # Create the primary plot here. |
| 168 | # ggplot contains the following arguments: |
| 169 | # - data: the data frame that the graph will be based off of |
| 170 | # - aes: the asthetics of the graph which require: |
| 171 | # - x: x-axis values (usually iterative, but it will become date # later) |
| 172 | # - y: y-axis values (usually tests) |
| 173 | # - color: the category of the colored lines (usually legend of test) |
| 174 | |
| 175 | mainPlot <- ggplot( data = dataFrame, aes( x = iterative, |
| 176 | y = Values, |
| 177 | color = Legend ) ) |
| 178 | |
| 179 | # ------------------- |
| 180 | # Main Plot Formatted |
| 181 | # ------------------- |
| 182 | |
| 183 | print( "Formatting main plot." ) |
| 184 | |
| 185 | limitExpansion <- expand_limits( y = 0 ) |
| 186 | |
| 187 | tickLength <- 3 |
| 188 | breaks <- seq( max( dataFrame$iterative ) %% tickLength, max( dataFrame$iterative ), by = tickLength ) |
| 189 | breaks <- breaks[ which( breaks != 0 ) ] |
| 190 | |
| 191 | maxYDisplay <- max( dataFrame$Values ) * 1.05 |
| 192 | yBreaks <- ceiling( max( dataFrame$Values ) / 10 ) |
| 193 | yScaleConfig <- scale_y_continuous( breaks = seq( 0, maxYDisplay, by = yBreaks ) ) |
| 194 | xScaleConfig <- scale_x_continuous( breaks = breaks, label = rev( dataFrame$build )[ breaks ] ) |
| 195 | |
| 196 | # ------------------------------ |
| 197 | # Fundamental Variables Assigned |
| 198 | # ------------------------------ |
| 199 | |
| 200 | print( "Generating fundamental graph data." ) |
| 201 | |
| 202 | defaultTextSize() |
| 203 | xLabel <- xlab( "Build" ) |
| 204 | yLabel <- ylab( args[ y_axis ] ) |
| 205 | |
| 206 | # Set other graph configurations here. |
| 207 | theme <- graphTheme() |
| 208 | |
| 209 | title <- labs( title = title, subtitle = lastUpdatedLabel() ) |
| 210 | |
| 211 | # Colors used for the lines. |
| 212 | # Note: graphs that have X lines will use the first X colors in this list. |
| 213 | colors <- scale_color_manual( values=c( webColor( "black" ), # black |
| 214 | webColor( "blue" ), # blue |
| 215 | webColor( "red" ), # red |
| 216 | webColor( "green" ), # green |
| 217 | webColor( "yellow" ), # yellow |
| 218 | webColor( "purple" ) ) ) # purple (not used) |
| 219 | |
| 220 | wrapLegend <- wrapLegend() |
| 221 | |
| 222 | fundamentalGraphData <- mainPlot + |
| 223 | limitExpansion + |
| 224 | xScaleConfig + |
| 225 | yScaleConfig + |
| 226 | xLabel + |
| 227 | yLabel + |
| 228 | theme + |
| 229 | colors + |
| 230 | wrapLegend + |
| 231 | title |
| 232 | |
| 233 | # ---------------------------- |
| 234 | # Generating Line Graph Format |
| 235 | # ---------------------------- |
| 236 | |
| 237 | print( "Generating line graph." ) |
| 238 | |
| 239 | lineGraphFormat <- geom_line( size = 0.75 ) |
| 240 | pointFormat <- geom_point( size = 1.75 ) |
| 241 | |
| 242 | result <- fundamentalGraphData + |
| 243 | lineGraphFormat + |
| 244 | pointFormat |
| 245 | |
| 246 | # ----------------------- |
| 247 | # Exporting Graph to File |
| 248 | # ----------------------- |
| 249 | |
| 250 | saveGraph( outputFile ) # from saveGraph.R |
| 251 | quit( status = 0 ) |