R/boxplots.R

#' Plot boxplots of the names variable
#'
#' \code{\link{boxPlot}} is the base plotting function that is called by the
#' wrapper for each KPI.
#'
#' @param variable Variable to plot as the boxplot
#' @param ylabel Optional ylabel (just the label text)
#' @param extra Additional plot layers (fully specified)
#' @return Returns standard fast-analysis structured list
#' @name boxPlot
#' @family skeletonPlots
boxPlot <- function(variable, ylabel = NA, extra = NA) {

    if (!is.na(ylabel)) {
        ylabel <- sprintf(" +\n    ylab('%s')", ylabel)
    } else {
        ylabel <- ''
    }

    if (!is.na(extra)) {
        extra <- sprintf(' + \n    %s', extra)
        extra <- paste0(extra, collapse = '')
    } else {
        extra <- ''
    }

    plot <- sprintf("
plt <- ggplot(data, aes(x = carrier,
                        y = %s,
                        fill = carrier)) +
    geom_boxplot(notch = TRUE) +
    scale_fill_root() +
    theme_root() +
    xlab('') %s%s
print(plt)", variable, ylabel, extra)

    return(plot)

}

#' @rdname boxPlot
#' @export
downloadTaskBoxPlot <- function() {
    save <- 'download_task_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### Download Task Box Plot",
                  boxPlot("dsd_effective_download_test_speed/1000",
                          "Downlink Throughput (Mbps)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)
}

#' @rdname boxPlot
#' @export
downloadAccessBoxPlot <- function() {

    save <- 'download_access_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### Download Access Box Plot",
                  boxPlot("dsd_access_speed_median/1000",
                          "Downlink Access Speed (s)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)

}


#' @rdname boxPlot
#' @export
downloadTTFBBoxPlot <- function() {

    save <- 'download_ttfb_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### Download TTFB Plot",
                  boxPlot("dsd_time_to_first_byte_median/1000",
                          "Downlink TTFB (s)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)

}

#' @rdname boxPlot
#' @export
uploadTaskBoxPlot <- function() {
    save <- 'upload_task_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### Upload Task Box Plot",
                  boxPlot("dsu_effective_upload_test_speed/1000",
                          "Uplink Throughput (Mbps)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)
}

#' @rdname boxPlot
#' @export
uploadAccessBoxPlot <- function() {

    save <- 'upload_access_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### Upload Access Box Plot",
                  boxPlot("dsu_access_speed_median/1000",
                          "Uplink Access Speed (s)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)

}

#' @rdname boxPlot
#' @export
ldrTaskBoxPlot <- function() {
    save <- 'ldr_task_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### LDR Task Box Plot",
                  boxPlot("ldr_task_speed_median/1000",
                          "LDR Task Time (s)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)
}

#' @rdname boxPlot
#' @export
ldrAccessBoxPlot <- function() {

    save <- 'ldr_access_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### LDR Access Box Plot",
                  boxPlot("ldr_access_speed_median/1000",
                          "LDR Access Time (s)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)

}

#' @rdname boxPlot
#' @export
emailTaskBoxPlot <- function() {

    save <- 'email_task_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### E-Mail Task Box Plot",
                  boxPlot("email_total_task_time/1000",
                          "E-Mail Task Time (s)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)

}

#' @rdname boxPlot
#' @export
emailAccessBoxPlot <- function() {

    save <- 'email_access_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### E-Mail Access Box Plot",
                  boxPlot("email_total_response_time/1000",
                          "E-Mail Response Time (s)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)

}



#' @rdname boxPlot
#' @export
ltePowerBoxPlot <- function() {
    save <- 'lte_rsrp_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### LTE RSRP Box Plot\n",
                  "df <- data[!is.na(data$avg_lte_rsrp) & !is.na(data$avg_lte_rssnr),]",
                  boxPlot("avg_lte_rsrp", "LTE RSRP (dBm)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)
}

#' @rdname boxPlot
#' @export
lteNoiseBoxPlot <- function() {
    save <- 'lte_rssnr_boxplot'
    data <- pullTestSummary()
    plot <- paste("\n#### LTE RSSNR Box Plot",
                  "df <- data[!is.na(data$avg_lte_rsrp) & !is.na(data$avg_lte_rssnr),]",
                  boxPlot("avg_lte_rssnr", "LTE RSSNR (dBm)"),
                  saveLine(save),
                  sep = "")
    output <- list(data = data,
                   plot = plot,
                   save = save)
    return(output)
}

#' @rdname boxPlot
#' @export
dataspeedBoxPlot <- function() {
    save <- "dataspeed-boxplot"
    data <- pullDataspeed()
    process <- "
## Aggregate dataspeeds over sockets
speed <- aggregate(dataspeed$bytes_transferred,
                   by = list('carrier' = dataspeed$carrier,
                             'direction' = dataspeed$direction,
                             'device_id' = dataspeed$device_id,
                             'interval_sequence' = dataspeed$interval_sequence,
                             'test_id' = dataspeed$test_id,
                             'device_time' = dataspeed$test_device_time),
                   function(x){sum(x, na.rm = TRUE)})
"

    plot <- paste("
#### Dataspeed Interval Box Plot
plt <- ggplot(speed,
              aes(x = as.factor(interval_sequence),
                  y = x/(10^6),
                  fill = carrier,
                  color = as.factor(device_id))) +
    geom_boxplot() +
    facet_grid(direction ~ carrier) +
    theme_root() +
    scale_fill_root() +
    scale_color_grey(start = 0, end = 0, guide = 'none') +
    xlab('Index Interval') +
    ylab('Bytes Transferred (Mb)')
print(plt)",
                  saveLine(save),
                  sep = "")

    output <- list(data = data,
                   process = process,
                   plot = plot,
                   save = save)
    return(output)


}
mlhutchins/fast-analytics documentation built on May 23, 2019, 2:10 a.m.